June 1, 2012

Beginner

How To Install Rails, Apache, and MySQL on Ubuntu with Passenger

Intro


Installing Ruby on Rails and MySQL on an apache virtual private server is the first step toward getting Ruby applications live and online. Three useful installers make the task of building this server easier than ever before.

Setup

This tutorial requires you to have a droplet or server up and running. Additionally, the rails ready script needs to be performed by a user with sudo privileges. If you don't have a user like that on your server, you can check out how to do that in steps 3 and 4 of this tutorial.

Step One—Install Rails Ready


Once you are logged in on your virtual server with your user with root privileges, type in the command to install Rails Ready:
wget --no-check-certificate https://raw.github.com/joshfng/railsready/master/railsready.sh && bash railsready.sh

Rails Ready can be installed either from the source or with RVM, the Ruby Version Manager. I would recommend using RVM—it's an easy installation and will later let you to switch between multiple versions of Ruby if needed.

Overall the installation does take a while—be prepared to wait.

However, once Rails Ready finishes the process, your VPS will be fully equipped with Ruby, Gems, and Rails. Then, following the instructions on screen, "logout and back in to access Ruby"

Step Two—Install Apache with Phusion Passenger


Once RVM is set up, you can use it to install rails:
rvm install 1.9.3

And set RVM to use Ruby 1.9.3 by default:
rvm use --default 1.9.3

Then install the passenger gem:
gem install passenger

As a useful bonus, RailsReady comes packaged with Phusion Passenger, which we can use to then automatically install and configure Apache on our server.

Use this command to start the apache installation:
passenger-install-apache2-module

Step Three—Update the Apache configuration


Passenger will display this text after Apache installs:
The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /home/username/.rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/ext/apache2/mod_passenger.so
   PassengerRoot /home/username/.rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12
   PassengerRuby /home/username/.rvm/wrappers/ruby-1.9.3-p194/ruby

To finish the process, open up the Apache config and paste the three required lines into the file:
 sudo nano /etc/apache2/apache2.conf

Save and Exit.

Step Four—Install MySQL


Before we conclude the installation, we should add one more useful program to our virtual server.

MySQL is a powerful database management system used for organizing and retrieving data.

To install MySQL, open terminal and type in these commands:
sudo aptitude update
sudo aptitude install mysql-server

During the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell with this command:
UPDATE mysql.user SET Password = PASSWORD('password') WHERE User = 'root';

Congratulations! WIth the help of three useful installers, we now have Ruby on Rails, Apache, and MySQL on our Ubuntu server!

See More

Once you have installed Ruby on Rails on your server, you can proceed to Create a SSL Certificate for your site or Install an FTP server



By Etel Sverdlov

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

15 Comments

Write Tutorial
  • Gravatar amnesia7 over 1 year

    Is there any chance of a tutorial on setting up Rails+nginx+Postgres+ElasticSearch? Thanks

  • Gravatar Moisey over 1 year

    We're going to look into that. Probably split it into two separate tutorials, one on Rails + Postgres with nginx and the other on elastic search.

  • Gravatar amnesia7 over 1 year

    Sounds like a plan. This link (http://asquera.de/opensource/2012/11/25/elasticsearch-pre-flight-checklist/) might help with the elasticsearch. Thanks

  • Gravatar Clifton Labrum about 1 year

    I'm getting an error on Step 2 above: `sudo: passenger-install-apache2-module: command not found` Anyone else run into this?

  • Gravatar Andrew Havens about 1 year

    Clifton: don't use sudo. This tutorial uses RVM and installs passenger as a gem within RVM. In general, RVM is nice because it makes it easy to install different versions of Ruby, but it does some funky stuff with environment variables. When you type sudo, your environment changes and RVM gets lost. If you really need to use sudo, type rvmsudo instead.

  • Gravatar Andrew Havens about 1 year

    ...also make sure you log out and log back in after installing RVM so that your bash environment will reload with the necessary environment variables.

  • Gravatar Clifton Labrum about 1 year

    Thanks, Andrew. I should have noted that I've tried it without sudo with the same result. Am I supposed to use `passenger-install-apache2-module` in conjunction with an rvm command somehow?

  • Gravatar Clifton Labrum about 1 year

    It looks like you need to install Ruby and Passenger prior to installing the Apache 2 module. I have emailed DO about this, and they are going to update this article. Thanks, everyone!

  • Gravatar jeffmsmithdev about 1 year

    Might I suggest adding a section of the Apache virtual directory set up

  • Gravatar ehs.ghan about 1 year

    I get the following error when I try to restart apache: apache2: Syntax error on line 265 of /etc/apache2/apache2.conf: Cannot load /home/*****/.rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/ext/apache2/mod_passenger.so into server: /home/*****/.rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/ext/apache2/mod_passenger.so: cannot open shared object file: No such file or directory Action 'configtest' failed. The Apache error log may have more information. ...fail!

  • Gravatar registrations about 1 year

    A few things re this tutorial: Instead of dumping the following into the apache.conf LoadModule passenger_module /some/path/here/mod_passenger.so PassengerRoot /some/path/here/passenger-3.0.12 PassengerRuby /some/path/here/ It's considered to be best practise to instead create two files: /etc/apache2/mods-available/passenger.load and /etc/apache2/mods-available/passenger.conf passenger.load #obviously swap this path out with correct path as per the passenger installer LoadModule passenger_module /some/path/here/mod_passenger.so passenger.conf #obviously swap these paths out with correct paths as per the passenger installer PassengerRoot /some/path/here/passenger-3.0.12 PassengerRuby /some/path/here/ Then run sudo a2enmod passenger The same process can be applied when creating your vhosts; instead of creating the vhost inside apache2.conf /etc/apache2/sites-available/somesitehere.com sudo a2ensite somesitehere.com ------------------------------- Additionally, rbenv is considered by many to be superior to rvm. Quick install of rbenv would go as follows on Ubuntu. curl -L https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash #copy the script output into the top of ~/.bashrc rbenv bootstrap-ubuntu-12-04 rbenv install 1.9.3-p385 rbenv global 1.9.3-p385 ruby -v gem install bundler --no-ri --no-rdoc rbenv rehash bundle -v

  • Gravatar mpapis 9 months

    Could you change `rvm install 1.9.3` to `rvm use --install ruby` - it is essential to `use` ruby as it configures environment and of course using latest ruby is encouraged as it's compatible with older versions (checked with rails)

  • Gravatar er.tejaspatel88 8 months

    I set up Apache and Passenger but my rails app display directory structure rather than rendering actual rails app.Can anyone please shed some light how it works?

  • Gravatar er.tejaspatel88 8 months

    My virtual Host file looks like DocumentRoot /var/www/testapp.varniinfotech.com/public ServerName testapp.varniinfotech.com ServerAlias www.testapp.varniinfotech.com AllowOverride all Options -MultiViews

  • Gravatar Kamal Nasser 8 months

    @er.tejaspatel88: Did you follow Step 3? You should restart apache after editing the config files:

    sudo service apache2 restart

Leave a Comment

Create an account or login:
Ajax-loader