Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Japestrale/8fb48e21c98791385fe8 to your computer and use it in GitHub Desktop.
Save Japestrale/8fb48e21c98791385fe8 to your computer and use it in GitHub Desktop.

#How To Deploy a Rails App with Passenger, Apache and RVM on Ubuntu 14.04

##Getting Started

We're assuming you've got an basic Ubuntu installation set up.

If you've just set up a Digital Ocean droplet, I suggest you make sure you create a non root account with which to use.

I'd suggest following this guide https://www.digitalocean.com/community/tutorials/how-to-add-and-delete-users-on-an-ubuntu-14-04-vps

You'll want to make sure you grant this user Sudo privileges.

Then you'll want to exit, and sign back in as this account. The reason is that it's never really a good idea to install things as the root account because it can cause permissions issues.

##1. Make sure we're running the latest updates

Update and upgrade OS to use latest software.

$ sudo apt-get update
$ sudo apt-get upgrade

Cleanup old packages and linux images to free up space.

$ sudo apt-get autoremove

##2. Install Apache2

I've borrowed bits from this guide on how to set up a basic LAMP stack https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu

Really all we're interested in is setting up Apache2 and MySQL

$ sudo apt-get install apache2

##3. Install MySQL

$ sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

Once you have installed MySQL, we should activate it with this command:

$ sudo mysql_install_db

Finish up by running the MySQL set up script:

$ sudo /usr/bin/mysql_secure_installation

##5. Install RVM, and Ruby

We're now following this guide http://railsapps.github.io/installrubyonrails-ubuntu.html

Install cURL (providing a library and command-line tool for transferring data using various protocols)

$ sudo apt-get install curl

Download RVM and install

$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
$ \curl -sSL https://get.rvm.io | bash -s stable --ruby --rails

Run this to allow us to use rvm commands in the terminal without having to reload the terminal

$ source ~/.rvm/scripts/rvm

See what version of ruby we're currently running

$ rvm list

List what gemsets we have

$ rvm gemset list

We're going to use the global gemset and update/add some common gems in there. Gems you install to the @global gemset for a given ruby are available to all other gemsets you create in association with that ruby.

NOTE: A little bit about where the default and global gemsets differ. If you don't use a gemset at all, you get the gems in the 'default' set. If you use a specific gemset (say @testing), it will inherit gems from that ruby's @global. The 'global' gemset is to allow you to share gems to all your gemsets. The word 'default' quite literally says it all, eg. the gemset used without selecting one for a specific installed ruby.

$ rvm gemset use global

Stop any documentation being installed

$ echo "gem: --no-document" >> ~/.gemrc

Update default gems

$ gem update

Install some gems we're going to need for every project

$ gem install rails

##6. Install Rails

Install nodejs first

$ sudo apt-get install nodejs

##6. Install Passenger

We're now install passenger as a gem into the global gemset and then configuring it. There are quite a few things to go through, but follow its instructions and you'll be fine. It may take a while to install. It will also ask you to add some lines to the apache config file when it's done.

$ gem install passenger
$ passenger-install-apache2-module

The lines it will ask you to add to the config file will look something like this:

LoadModule passenger_module /home/tluce/.rvm/gems/ruby-2.2.1@global/gems/passenger-5.0.21/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /home/tluce/.rvm/gems/ruby-2.2.1@global/gems/passenger-5.0.21
  PassengerRuby /home/tluce/.rvm/gems/ruby-2.2.1@global/wrappers/ruby
</IfModule>

##7. Getting your files onto the box

We're assuming you're going to want to use git here. We'll need to install it

$ sudo apt-get install git

Clone your repo to a folder on the box. All files should go within a folder in the /var/www directory. We have to create the directory then change its owner to be your linux user.

$ sudo mkdir /var/www/{folder-name}
$ sudo chown {linux-username}:{linux-username} /var/www/{folder-name}
$ git clone https://github.com/{github-account-name}/{repo-name}.git /var/www/{folder-name}

Make sure you are on the right git branch

##8. Setting up the apache virtual host

Copy the default vhost file and then configure it.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/{filename}.conf
$ sudo nano /etc/apache2/sites-available/{filename}.conf

For example:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/{folder-name}/public
    <Directory /var/www/{folder-name}/public>
        Allow from all
        Options -MultiViews
        # Uncomment this if you're on Apache >= 2.4:
        #Require all granted
    </Directory>
</VirtualHost>

We now have to enable the site

$ sudo a2ensite {filename}.conf
$ sudo service apache2 reload

##9. Setting up our rails app

$ cd /var/www/{folder-name}
$ bundle install

Bundler may fail to install a gem or two, but should provide info on how to fix any problems.

E.g. if mysql2 fails, it may be necessary to run:

$ sudo apt-get install libmysqlclient-dev

And then run bundle install again.

Set up the secret key. Use rake secret to generate it and then I prefer to add it to the secrets.yml file rather than set it as an environment variable. You can use the nano editor to do this. Replace <%= ENV["SECRET_KEY_BASE"] %> with the key generated by rake secret. Use CTRL+X and then press Y in order to save.

$ rake secret
$ sudo nano config/secrets.yml

Setup our database if we have one, and precompile the assets

$ RAILS_ENV=production rake db:setup
$ RAILS_ENV=production rake assets:precompile

Then restart Apache.

$ sudo service apache2 restart

The application has now been deployed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment