Ubuntu 8.04 Rails Server Using Passenger - Part 2

05/13/2008

Introduction

Here is the, the long awaited second part of my extensive guide on setting up a rails server running on Ubuntu 8.04. Everything should be going smoothly so far and you should be at the point where we need to setup Apache and link everything together. This guide will be quite verbose and much longer than the first one. This is due mostly to all the configuration that will be required. That being said I will contemplate making a third part of this guide that will cover the version control and capistrano recipes. Your thoughts are greatly apreciated, esecially if you want me to cover any other features or topics after you finish reading this guide.

As pointed out by a reader, some people may not have read part 1 yet. Click here to read Part 1 of this guide.

Enabling GPM

Note: You are going to want to have GPM enabled for this if you are just using Ubuntu Server. GPM will allow you to copy and paste output from the terminal using your mouse.


sudo apt-get install gpm

Mod_Rails

To Start the installation of passenger after it has been installed via the gem run the following command:


sudo passenger-install-apache2-module

This will commence the user-friendly installer created by the Phusion team. My hats off to Phusion for making something so incredible sexy.

Run the following command in a new terminal (Alt-F2):


sudo vim /etc/apache2/apache2.conf

Scroll down the page the bottom of the configuration file and right about the "# Include of directories ignores editors" add the following from the output of the passenger install screen. Copy YOUR output NOT mine.


LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/passenger-2.0.3
PassengerRuby /usr/bin/ruby1.8

NameVirtualHost *:80

Now that you are done with the passenger install we need to take our rails app and create a virtual host for this app. To do that run the following command.


sudo vim /etc/apache2/sites-available/test

Once the file is open for editing add the following but change the parts specific to your application.

Note: The ServerName MUST be resolvable and resolve to the host that the application is running on.



    ServerName test.rails
    DocumentRoot /var/rails/test/public

Now that the virtual host is created and we dont need the default page anymore we can enable the test app vhost and disable the default vhost.


sudo a2dissite default
sudo a2ensite test

Creating Test Application

First things first we want to make the directory that all the rails apps we will be using will be stored.


sudo mkdir -p /var/rails
cd /var/rails

Now we want to create the rails skeleton and hand the permissions over to the apache web server.


sudo rails test
sudo chown -R www-data:www-data test

Once this is complete we are now ready to start the customization of the application and setting up the databases.

Setting Up MySQL

We want to login to the mysql database using the root account and the password we set earlier.


sudo mysql -p
--Enter Password--

There are many parts of MySQL which I do not like, like their user management. It is a poor way to manage access to the database. That being said it still is one of the more popular databases and it has a smaller memory footprint than PostgreSQL.

Creating the Databases


mysql> CREATE DATABASE test_development;
mysql> CREATE DATABASE test_test;
mysql> CREATE DATABASE test;

Creating a User and Setting the Password for the test database

The code listed below will add a user called "testapp" with the password "testpass", I recommend that you make your passwords extremely complex. For example a 16 character password would work wonderfully because it is stored in the database.yml so you dont need to memorize it.


mysql> GRANT all privileges ON test_development.* to testapp@"localhost" IDENTIFIED by 'testpass';
mysql> GRANT all privileges ON test_test.* to testapp@"localhost" IDENTIFIED by 'testpass';
mysql> GRANT all privileges ON test.* to testapp@"localhost" IDENTIFIED by 'testpass';

This command below will flush the privileges so that the access levels we just added will take effect.


mysql> FLUSH PRIVILEGES;

Configuring Rails App To Connect To the Database

Now that the database is configured properly we need to tell the rails app we created about the database. This is all done in the database.yml file.


cd /var/rails/test/config
sudo vim database.yml

Now that we are editing the database.yml file, you want to make you file look as follows...


login: &login
  adapter: mysql
  username: testapp
  password: testpass
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *login
  database: test_development

test:
  <<: *login
  database: test_test

production:
  <<: *login
  database: test

Last Minute Tricks...

Passenger does not like the .htaccess file to be in the public directory of the rails app. To remove this file just run the following command.


sudo rm /var/rails/test/public/.htaccess

Testing the Test Application

Now we have setup passenger to point at our rails app and we have to just restart the webserver.


sudo /etc/init.d/apache2 restart

Edit the /etc/hosts file on your client computer if you do not have DNS setup and add the following line.


SERVER_IPADDRESS test.demo.rails

Now open a web browser and type in the URL specified in the hosts file.

You should see the default "Welcome to Rails" screen

Status Checklist

Should Be Completed

  • Installed All Packages/Gems
  • Installed Passenger
  • Setup Apache 2.2.8 to use Passenger
  • Created Test Rails App
  • Created Databases
  • Configure Test Rails App To Use MySQL Database
  • Test Rails App is Functioning

Left To Do

  • SSH Goodies
  • Reader Suggestions

SSH Goodies

This section pertains to ssh security and best practices. SSH is a very important service that is run on any server and must be configured properly in order to be secure. By the end of this section you should have a "more secure" server that can securely be accessed via your computer using RSA encrypted SSH keys instead of passwords.

Creating Keys on your Client

This is a very simple process, all you need to do is run one command.


ssh-keygen

Now that this is running you will want to accept the default location to save your keys and type a password when prompted. This will password protect your keys for added security. You output should look as follows.


Generating public/private rsa key pair.
Enter file in which to save the key (/home/rvalente/.ssh/id_rsa): 
Created directory '/home/rvalente/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/rvalente/.ssh/id_rsa.
Your public key has been saved in /home/rvalente/.ssh/id_rsa.pub.
The key fingerprint is:
60:8e:b1:4c:cd:df:4d:82:b2:c4:05:97:0b:1b:56:fd rvalente@ubuntu

Now all you need to do is copy this key from your client machine to you server as follows. Make sure you use the account on your server instead of the account I list below.


scp .ssh/id_rsa.pub SERVER_IPADDRESS:.ssh/authorized_keys

Once that process completes then turn off password authentication on your ssh server. You do that but opening your /etc/ssh/sshd_config and changing this line.


#PasswordAuthentication yes
PasswordAuthentication no

That is it, now your server will use keys for authentication only.

Last but Not Least

I will be writing a part three of this guide for the capistrano/git version control and automation of a rails server. Since there are not many complete guides and they stop before the actual system administration occurs. Before I do this part three I want reader input for anything that you would like to see on top of the capistrano recipes and version control topics. Any input is greatly appreciated.

Thanks for reading!

-Ron

There are 34 comments to this article:

  1. Mario says:

    05/06/2008

    wow, super cool man. Very clear, and Very helpful. Just a suggestion... you Might want to add a link to part 1 so people can easily jump through. I came upon your blog starting on part 2... thanks!
  2. Ron Valente says:

    05/06/2008

    Thanks for the suggestion, I added a link to part 1 in the introduction. -Ron
  3. Samuel Prado says:

    05/06/2008

    Man, this guide is awesome! I'm using it in my Ubuntu VM recently created and all goes well!! Thanks! And I'm anxiously waiting for the part 3 ;)
  4. mlambie says:

    05/06/2008

    Nice tutorial - I think it was the .htaccess file that broke my install. One thing to note though, you've got a typo in the RailsSpawnServer line with "passegner-1.0.5" though that might be an error with the installer spitting out the wrong code to copy-and-paste.
  5. Ron Valente says:

    05/06/2008

    Glad to see everyone is having success with this tutorial. I will be writing up part 3 after I get some more suggestions on topics to include. Right now the list consists of Version Control, Capistrano, and RMagick. Any more ideas are welcomed. -Ron
  6. Willem says:

    05/06/2008

    Ron Thanks for this excellent post! It is much appreciated. I am keenly looking forward to your next post about setting up capistrano/Git and the automation of a rails deployment. This is exactly what I need now and your help comes just at the right time. Thanks! Willem
  7. Raul says:

    05/06/2008

    Some interesting topics -for me, of course- would be how to: - automate backups and restore from them. - monitor services - make performance benchmarks - setup memcache
  8. Ronald Valente says:

    05/06/2008

    @Raul Automated Backups will be covered in a different post altogether, just due to the size and complexity of a good backup solution. Monitor services - I was planning on using god (a great ruby gem) for monitoring of the web app. I have a large amount of experience with Nagios but I do not think that a monitoring solution that large is worth it. Performance Benchmarks - being done currently. We are benchmarking Apache + Passenger, Nginx + Thin, WEBRick, and LiteSpeed. Setting Up Memcache - I will definitely look into this! Thanks for your input! -Ron
  9. Foo says:

    05/06/2008

    Great tutorial. I had problems with passenger complaining that I needed to install the prefork version of apache. After doing: sudo apt-get install apache2-mpm-prefork apache2-prefork-dev I was able to run 'sudo passenger-install-apache2-module' without any other warnings/errors. I would be interested in seeing how to add multiple rails applications with apache and passenger. Foo
  10. Randy says:

    05/06/2008

    Excellent series by the way. A memcache how-to would be most lovely.
  11. ronan says:

    05/06/2008

    thanks a lot for these tutorials! great work.
  12. David Winter says:

    05/06/2008

    Great howto - thanks! Really looking forward to part 3, especially Capistrano + git.
  13. Nilesh Trivedi says:

    05/06/2008

    You said that Passenger does not like public/.htaccess. But currently I need something like the following: Serve static public/main.html for http://www.mydomain.com and http://mydomain.com Route the request to rails app as usual for any subdomains (http://subdomain.mydomain.com) . The subdomains have been set up as mirrors of the main domain and the application but the index of the main page needs to be faster and hence a static page. Help will be appreciated. The app is hosted on dreamhost
  14. Tini says:

    05/06/2008

    I'm strongly interested in how to set up multiple rails application using passenger and of course a tutorial about how to set up Capistrano.
  15. Stefan Haubold says:

    05/06/2008

    Instead of Adding the Passenger configuration to apache2.conf, i would add a file in /etc/apache2/mods-available for example "passenger" and paste the config lines in it. you can activate passenger with a2enmod passenger This setup is complety update safe. And you will not get any conflicts when apache2.conf changes with the next update.
  16. Stefan Haubold says:

    05/06/2008

    the file should be named "passenger.load" sorry for that
  17. Mathieu Martin says:

    05/06/2008

    Two truly great tutorials, Ronald. Thanks a lot. I can't wait to read part 3 :-) I'd like to offer a suggestion that ties in well with the Git+Capistrano ninja combo. How about creating a Cap file that can deploy 2 different branches, named 'production' and 'staging' for -- you guessed it -- a production environment and a staging environment :-) To keep things simple, of course, we can put em both on the same machine.
  18. Ronald Valente says:

    05/06/2008

    I apologize about my tardiness with Part 3. I just started a new job in North Carolina and I am pretty busy at this moment. I will have part 3 done as soon as I can. Thank you for understanding. -Ron
  19. ahab says:

    05/06/2008

    @Mathieu Martin [capistrano mod_rails multistage script](http://www.ahabman.com/blog/2008/05/mod_rails-set-rails_env-variable-to-qa-staging-or-production/) I came up with one way to do the have capistrano deploy to multiple stages (qa, staging and production). It uses cap-ext, and a short ruby task to swap out the rails_env var.
  20. icebreaker says:

    05/06/2008

    Thanks for the nice writeup. Got the rails app up and running on an Ubuntu 8.04 server under VMVare on Vista+learnt a lot on the way.
  21. shawn says:

    05/06/2008

    phpmyadmin setup
  22. Fusilly says:

    05/06/2008

    Great tutorial thanks! If you already have a virtual host you can use RailsBaseURI in your apache configuration.
  23. khelll says:

    05/06/2008

    am getting : Forbidden You don't have permission to access / on this server.
  24. Ken says:

    05/06/2008

    Nice. The tip on Passenger is worth it's weight in gold, giving me the needed ease of deployment to my home server. Very many thanks.
  25. Michael says:

    05/06/2008

    Thank you so much for this guide, it was extraordinarily helpful! I am anxiously awaiting Part 3.
  26. Martin says:

    05/06/2008

    Any chance part 3 of this guide will be ready soon?
  27. Adam says:

    05/06/2008

    Great Tutorials. I have a problem, when i run the test app, i get the expected rails screen. However, when I click the "About your application's envirionment" link, I get a box that says "The page you were looking for does not exist. Any suggestions?
  28. Ron Valente says:

    05/06/2008

    Hello all! I am terrible sorry yet again for the blogging delay. Both Ben and Myself have been quite busy working in our respective jobs the past 3 months. I have finally gotten the chance to sit down and do some much needed catching up. That being said part 3 should be online shortly. Best Regards, Ron Valente
  29. Ramiro Varandas Jr says:

    09/19/2008

    Hi Ron,

    I liked a lot of your tutorial and because of that I created a Bash Shell Script file to install all the steps of part 1 and 2 of the tutorial automatic.

    As I couldn’t find your contact e-mail, please, if you’re interested in posting the file for people to download, send me an e-mail and I’ll reply with the file.

    Regards,

    Ramiro

  30. jDeppen says:

    12/14/2008

    I’m having a hard time connecting to my database using Navicat. Any ideas?

  31. Ron Valente says:

    12/14/2008

    @jDeppen Are there any errors? What OS are you running on?

  32. Chuck Vose says:

    01/27/2009

    I just wanted to take a moment to thank you for the excellent tutorial. Got me up and running very quickly with no problems whatsoever. I really appreciate the mysql instructions even though you use Postgres.

    This guide is awesome even without part 3, it’s everything that I needed.

  33. Eduardo says:

    05/09/2009

    Thank you! Excellent work, everything running fine here.

  34. free mario games says:

    05/16/2009

    Thanks for the tutorial, I’m using it on my site and it has cut down memory usage incredibly ;)

Write your comment: