Topic: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

In this tutorial, I am going to cover installing a Ubuntu web server utilizing Apache2, Passenger, and using MySQL as the RDBMS.

As with other tutorials, anything in code blocks, type in the command.

Type these things in the shell

So let's get started.

I am starting out with Ubuntu Server 8.04 LTS (Hardy Heron). With it's Ubuntu Server or "Desktop", it doesn't matter. If you're using Desktop, just use the Terminal for the entire process.

First thing you want to do to make sure APT is current, type

sudo apt-get update

Type in your password. Once you are returned to the cursor prompt, we will install Ruby, RubyGems, ri, rdoc, and irb.

sudo apt-get install ruby ri rdoc irb -y

Once that completes, type

wget [url]http://rubyforge.org/frs/download.php/55066/rubygems-1.3.2.tgz[/url]

That will take a short moment to download. Once finished, type

tar -xvf rubygems-1.3.2.tgz

Now lets navigate to the new extracted folder.

cd rubygems-1.3.2

Now lets setup RubyGems

sudo ruby ./setup.rb

Lets create a symbolic link, so the "gem" command works

sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

Now we want to install Apache2, the MySQL client, and all necessary Apache MPM modules. Type

sudo apt-get install apache2.2-common apache2-mpm-prefork apache2-prefork-dev -y

Now let's install MySQL and all the necessary libraries and modules for MySQL connectivity to happen.

sudo apt-get install mysql-client libmysql-ruby libmysqlclient15-dev

And let's install the remaining libraries needed to make everything work.

sudo apt-get install ruby1.8-dev build-essential libapr1-dev -y

Now let's start to install some gems, starting with Rails. As of this tutorial, the most current version is 2.3.2.

sudo gem install rails

After that's finished, let's install the MySQL gem.

sudo gem install mysql

Now, I am fairly positive I didn't miss any libraries for the MySQL gem to work, so if you are getting the "failed to build native extensions" error, let me know.

Now lets install Passenger Phusion

sudo gem install passenger

And now let's install Passenger to Apache.

sudo passenger-install-apache2-module

Follow the text based wizard. All you should have to do is hit Enter, but if we missed anything, it will tell you what, and give you the commands.

Once the prerequisites are installed and Passenger is installed properly, it gives you 3 lines to add to your /etc/apache2/apache2.conf. I usually add these lines at the bottom of the file, but some people have a preference where they want the lines. Just copy the 3 lines from the shell and open nano or vi, and paste those lines.

I'd put them here, but as they upgrade Passenger over the years, the versions and formatting of the lines change, so this could just confuse you if they switch it up again.

Make sure to save the apache2.conf file, and then restart apache.

sudo /etc/init.d/apache2 restart

Now let's create a Rails app. I'll just call mine myapp.

rails myapp -d mysql

You see the output that your structure is created. Now let's cd to our app, in my case,

cd myapp

Now let's create a controller called hello, with an index action

script/generate controller hello index

Let's make sure that worked by itself by firing up Mongrel

script/server

And let's open a browser window and point to http://<IP>:3000/hello

Where <IP> is the IP Address of that server/box.

You should see something like "#Hello..". If so, that's great. Hit Ctrl+C to kill Mongrel.

Now, let's create a VirtualHost for Apache.

sudo nano /etc/apache2/sites-available/myapp

And once in there, type

<VirtualHost :*80>
   ServerName myapp
   DocumentRoot /home/<username>/myapp/public
</VirtualHost>

Where <username> is your linux username.

Now, let's disable the default site, and enable the myapp site.

sudo a2dissite default
sudo a2ensite myapp

Now before we reload Apache, let's make a change in our environment.rb

nano config/environment.rb

And let's put Rails in production mode for this app, so it can work with Passenger.
Add the line

ENV['RAILS_ENV'] ||= 'production'

If you see the line is already there, just uncomment it by removing the # in front of it.

Alternatively, you can set passenger in development mode.

Now, let's reload apache.

sudo /etc/init.d/apache2 reload

Now lets see if it worked. Browse to http://<IP>/hello. You should see the same thing as you did with Mongrel, but now it's being served by Apache.

And that's it! Just delete the myapp (or whatever you called it) if you want, and create more rails apps, and add VirtualHosts for each site you want Apache to serve. If you do have more than one site, make sure to use NameVirtualHost *:80 above the <VirtualHost *:80> line on the first site, but leave the others like above.

Hope this help anyone looking to setup their Ubuntu box as a Rails Apache Web server using external MySQL databases.

Last edited by jmbrink26 (2010-01-31 18:55:40)

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

You don't need to put an app into production mode to work with passenger. You can set the rails_env in your virtual host for your application like so:
Apache:

rails_env production

nginx:
rails_env production;

Of course it doesn't have to be production. On my MacBook it is obviously development, on my server it is either staging or production depending which I am on at the time.

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Right.

The Passenger docs say you can set Passenger into development mode too if you want. But I look in that section, and it doesn't tell you how per se....

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

So would the code to use passenger with the development mode look like this:
[code ruby]
<VirtualHost :*80>
  ServerName myapp
  rails_env development
  DocumentRoot /home/<username>/myapp/public
</VirtualHost>
[/code]

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Very long tutorial with Ubuntu, thanks...

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

ewproctor wrote:

So would the code to use passenger with the development mode look like this:

<VirtualHost :*80>
  ServerName myapp
  rails_env development
  DocumentRoot /home/<username>/myapp/public
</VirtualHost>

Right. Actually now, it's RailsEnv I guess. I think they changed it in the last while.

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

sacsp wrote:

Very long tutorial with Ubuntu, thanks...

LOL no problem. Had to make sure I covered all bases, as it can be a bit tricky getting it all working the first time. After you've done it a couple times it's cake. big_smile

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

jmbrink26 wrote:
ewproctor wrote:

So would the code to use passenger with the development mode look like this:

<VirtualHost :*80>
  ServerName myapp
  rails_env development
  DocumentRoot /home/<username>/myapp/public
</VirtualHost>

Right. Actually now, it's RailsEnv I guess. I think they changed it in the last while.

Yes it is for Apache, sorry my rails_env is because I use nginx.

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Oh, right. I forgot they wrote that function differently.

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

pls tell me how to install on windows

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Phusion Passenger won't run on Windows. It's unix software.

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Yeah the closest thing you can remotely get to get it working that seamlessly and fast with Apache on Windows is InstantRails. Some people like it, I never cared for it though (then again, when do I hardly ever use Windows these days? tongue )...

Last edited by jmbrink26 (2009-11-27 04:02:34)

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Hey JM, I didnt follow your tutorial for this, but used a bit for reference. Mostly used the mod_rails series on slicehost's website. Anyways, I can launch the demo rails app fine (I called it testapp) and view at port 3000. However, it doesnt seem to work when running through apache.

http://agmprojects.com/ If you click on the link to view the application environment, it responds with an error. If you go to /hello, then it has the error page as well that "something went wrong". Any ideas on what to try to fix this? I have installed passenger, copy and pasted the output to the apache2.conf; created the vhost and such as well.

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Check the log file for your environment. So either log/development.log or log/production.log. What does it say when the HTTP 500 template is triggered?

Also, paste your configuration in /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf that you have the enable mod added to for passenger.

Also, paste your VirtualHost configuration.

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Yeah I had a look at the development log file. Basically it had an sqlite3 error on the http 500 code, that was when I launched it and didnt setup the database.yml properly. Other than that I have connection statements from when I launched it in mongrel. In terms of the apache logs, the error.log file states that passenger loaded fine and that. Here's some of the output.

[Fri Dec 18 04:56:56 2009] [notice] Apache/2.2.11 (Ubuntu) Phusion_Passenger/2.2.7 configured -- resuming normal operations
18 04:56:56 2009] [notice] Apache/2.2.11 (Ubuntu) Phusion_Passenger/2.2.7 configured -- resuming normal operations
[Fri Dec 18 17:13:30 2009] [error] [client 74.86.75.194] client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /w00tw00t.at.ISC.SANS.DFind:)

Here's my file in sites-available.

<VirtualHost *:80>
  ServerName agmprojects.com
  ServerAlias www.agmprojects.com
  DocumentRoot /home/aaron/public_html/testapp/public
  RailsEnv production
  <Directory /home/aaron/public_html/testapp/public>
    AllowOverride all
    Options -MultiViews
  </Directory>
</VirtualHost>

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

I do have iptables setup as listed here: http://articles.slicehost.com/2008/4/25 … up-page-1, however I have also allowed port 2222 for ssh, and port 3000 for when I run a mongrel instance.

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

any ideas guys? Still having trouble with this, no luck from the slicehost forums either.

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Hey. SH forums explained how the "Something went wrong" is usually a production mode error. I switched to development in both the environment.rb file and the virtual host file. It seems to work now. Does that shed any light why it wouldnt work in production?

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

Nevermind, blonde moment. No production database :\

Re: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu

So you got it going it looks like?

Sorry, I totally forgot about this thread tongue