Practical Web Programming

Saturday, January 09, 2016

Deploying Rails Application for the First Time

I always forget these steps when deploying a Rails app for the first time. So I'm finally writing this in as a guide for the future.

This assumes that you already have a running Ubuntu server with Apache and Passenger installed (see Basic Security Installs for Ubuntu and How to Install Passenger + Apache in Ubuntu).
  1. Clone the git repo.
    git clone
    cd app_name
    bundle install
    If you get a "bundle not yet installed" error, run the following comment:
    gem install bundle
  2. Go to /var/www/ type and this command to create a symbolic link.
    sudo ln -s /home/kabalweg/app_name/ ./app_name
  3. Go to /etc/apache2/sites-available and create a virtualhost file, app_name.conf, or copy and existing one and edit the appropriate values in that file and save.
    <VirtualHost *:80>
        Redirect permanent /
    <VirtualHost *:80>
      # Set the environment to production
      RailsEnv production
      <IfModule mod_passenger.c>
          # Set to on when debugging errors
          PassengerFriendlyErrorPages off
          #PassengerRoot /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini
          #PassengerDefaultRuby /home/kabalweg/.rvm/gems/ruby-2.2.1@rails4.2/wrappers/ruby
          PassengerMaxPoolSize 2
          PassengerPoolIdleTime 0
          PassengerMaxRequests 1000
        DocumentRoot /var/www/app_name/public
        <Directory /var/www/app_name/public>
          AllowOverride all
          Options -MultiViews
          #Require all granted
          Order deny,allow
          Allow from all
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
  4. Disable the default virtualhost and enable the new virtualhost file and activate it.
    sudo a2dissite 000-default.conf
    sudo a2ensite app_name.conf
    sudo apachectl -k graceful
  5. Generate secret key and put the resulting key in the secret.yml file.
    rake secret RAILS_ENV=production
    Note: It's not a good and safe practice to put production config values in the repo. A good way is to ignore config files (*.yml) using git so it don't get save in the repo, then just manually create this files, with the correct values, in the production server.
  6. Pre-compile assets.
    rake assets:precompile RAILS_ENV=production
  7. Restart application by typing below in your application's root directory.
    touch tmp/restart.txt
    Create this folder (tmp) if you don't have this.


Recent Post