Skip to content

Instantly share code, notes, and snippets.

@truffaut
Last active August 29, 2015 14:01
Show Gist options
  • Save truffaut/bc5e07c4e15abbfa01ff to your computer and use it in GitHub Desktop.
Save truffaut/bc5e07c4e15abbfa01ff to your computer and use it in GitHub Desktop.
## An alternate guide: https://gorails.com/deploy/ubuntu/14.04
So, you've built a cool Rails-based application, now you want to share it with others. You can go the Heroku route, they
have a free tier, its quick and painless, no mucking around in configuration files, just `git push` and you're done. But, what happens when you need more control? What happens when you build an application that uses a lot of functionality that only comes from paid add-ons on Heroku, but for a site that makes little-to-no revenue? In those situations, you can use a Virtual Private Server, or VPS for short.
#Why VPS?
VPS's are cheap, are relatively easy to setup (with some Linux know-how), and are much easier than having to deal directly with physical server hardware.
# Sign up for DigitalOcean
* Go here: https://www.digitalocean.com/
* Sign up for an account. Log in.
* Click the big, green "Create" button
* Enter a hostname on the first line (e.g. michaelornellas.com)
* Select the server size you want to provision, if you are only going to have a small amount of traffic (friends, family, fellow students, career day people only would be fine), then the smallest server size should be sufficient. You can always change this later.
* Select a region for your server, either NY or SF should be good.
* Select a Linux distribution to install. If you aren't familiar with Linux, I'd recommend going with Ubuntu 14.04 x32 since you'll find the most documentation for Ubuntu-based distributions and 14.04 is the latest Long Term Support release
* If everything is filled out correctly, you should now be able to scroll to the bottom of the page and click the big "Create Droplet" button.
* Wait for your droplet to be created (your root password will be emailed to you).
# Our Plan
* So right now, we have a fresh slate:
* Our machine is clean: it only has the packages that were installed by default. Meaning: no Ruby, no server software, no postgresql, no firewall, no anything.
* Our machine only has one user: root, and we're directly logging into it, and worse, from a password that was emailed to us.
* Our plan of action from here:
* Disable root access. We are going to make another normal, non-root user and disable direct root user access for security reasons.
* Install packages for the software we need (ruby, nginx, passenger, postgresql, monit)
* Configure the software to work the way we want it to.
* Install our application, its dependencies and complete the setup process.
* Bask in the glory of our creations
# Locking the doors
First off, we need to get things set up. Assuming you're using Ubuntu 14.04, follow the instructions in [this tutorial](https://www.digitalocean.com/community/articles/initial-server-setup-with-ubuntu-14-04) to get your server setup initially. I usually follow all of the advice here (even the optional steps) at the bare minimum for every server I set up.
If you change the ssh port like this guide recommends, just make sure it is a number that you can recall, otherwise you'll have a hard time when you come back to change something in a few months and can't get access again.
Make sure you pick a strong enough password for your accounts as well, but I shouldn't have to tell you this at this point (...I'm watching you, don't you dare do it).
Once we're all done disabling the root user and have logged back in with our new username, we can get down to the "fun" stuff!
# Install RVM and Ruby
1. Install RVM and Ruby 2.0.0-p451
1. https://www.digitalocean.com/community/articles/how-to-install-ruby-on-rails-on-ubuntu-14-04-using-rvm - manually install ruby 2.0.0-p451 and pick that as your default after installing RVM
2. run `rvm requirements`
2. run `gem install bundler`
3. install postgresql
1. https://www.digitalocean.com/community/articles/how-to-install-and-use-postgresql-on-ubuntu-14-04 - Just follow the part up to creating a new user where you should follow my instructions instead
2. When you are supposed to create a user, use the command `createuser -P --interactive`
3. Your user should have a password, shouldn't be a superuser, should be able to create databases and shouldnt be able to create other roles.
4. run the commands: `sudo apt-get install libpq-dev`
4. `sudo apt-get install git libcurl4-openssl-dev nodejs npm`
5. `gem install passenger`
6. `export rvmsudo_secure_path=1`
7. `rvmsudo passenger-install-nginx-module`
8. `wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh`
9. `sudo mv init-deb.sh /etc/init.d/nginx`
10. `sudo chmod +x /etc/init.d/nginx`
11. `sudo /usr/sbin/update-rc.d -f nginx defaults`
You should now be able to start, stop and restart nginx using the commands:
```
sudo service nginx start
sudo service nginx stop
sudo service nginx restart
```
You can ensure Nginx is running by checking to see if it is an active process by running the command: `ps aux | grep nginx`
# Pointing Nginx at your application
For the last step, we need to make it so Nginx can actually serve up your application. Run `sudo nano /opt/nginx/conf/nginx.conf` and then remove the `location /` lines inside the server block that has `listen 80` at the top:
```
location / {
root html;
index index.html index.htm;
}
```
We'll be replacing that `location /` block with the following:
```
passenger_enabled on;
root /home/my_sweet_user_name/my_awesome_rails_app/public;
```
This root clause will point to the public folder of your Rails application.
# DANGERS
I can't in good faith put out a beginner-oriented guide to without a good warning about the inherent dangers of administering your own servers.
**Administering your own server as a beginner is like trying to pilot a really leaky ship when you a.) can't realize the boat is leaking, b.) don't yet know how to figure out where the leaky spots are, and c.) don't know how to patch the holes if/when you find them.**
It's ok, you're still learning. It's ok for prototyping, it's ok for demoing to employers, clients, friends, or family, but running a server is not a suitable replacement for actually having a real-life system admin with experience and a love for security mailing-lists watching your back if you have a real product with actual paying customers and can afford to do that. In the worst case, there's always the chance that you'll wake up one morning and have to delete everything and start over.
# (Optional) Setting up a firewall
* easy mode
* https://www.digitalocean.com/community/articles/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server
* hard mode
* https://www.digitalocean.com/community/articles/how-the-iptables-firewall-works
* https://www.digitalocean.com/community/articles/how-to-set-up-a-firewall-using-iptables-on-ubuntu-14-04
# (Optional) Setting up a domain name
If you have a domain name you have bought, you can use it with your DigitalOcean server. You'll want to change your nameservers on the site where you bought your domain to use the digitalocean nameservers. You can find out what these are from the DNS section of DigitalOcean's website. Follow this guide for more details: https://www.digitalocean.com/community/articles/how-to-set-up-a-host-name-with-digitalocean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment