Ubuntu 8.04 Rails Server Using Passenger - Part 2 26

Posted by Ron Valente Tue, 13 May 2008 17:46:00 GMT

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
sudo /etc/init.d/gpm start

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-1.0.5/ext/apache2/mod_passenger.so
RailsSpawnServer /usr/lib/ruby/gems/1.8/passenger-1.0.5/bin/passenger-spawn-server
RailsRuby /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.

<VirtualHost *:80>
    ServerName test.demo.rails
    DocumentRoot /var/rails/test/public
</VirtualHost>

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

Trackbacks

Use the following link to trackback from your own site:
http://www.sysadminschronicles.com/trackbacks?article_id=ubuntu-8-04-rails-server-using-passenger-part-2&day=13&month=05&year=2008

  1. Story added fsdaily.com
    This story has been submitted to fsdaily.com! If you think this story should be read by the free software community, come vote it up and discuss it here: http://www.fsdaily.com/HighEnd/GNU_Linux_Rails_Server_Using_Passenger_part_2
Comments

Leave a comment

  1. Mario 32 minutes later:

    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 17 minutes later:

    Thanks for the suggestion, I added a link to part 1 in the introduction.

    -Ron

  3. Samuel Prado about 20 hours later:

    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 about 20 hours later:

    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 about 21 hours later:

    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 about 21 hours later:

    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 4 days later:

    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 5 days later:

    @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 5 days later:

    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 9 days later:

    Excellent series by the way.

    A memcache how-to would be most lovely.

  11. ronan 14 days later:

    thanks a lot for these tutorials! great work.

  12. David Winter 16 days later:

    Great howto - thanks!

    Really looking forward to part 3, especially Capistrano + git.

  13. Nilesh Trivedi 19 days later:

    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 21 days later:

    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 21 days later:

    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 21 days later:

    the file should be named “passenger.load” sorry for that

  17. Mathieu Martin 22 days later:

    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 22 days later:

    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 26 days later:

    @Mathieu Martin

    capistrano mod_rails multistage script

    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 about 1 month later:

    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 about 1 month later:

    phpmyadmin setup

  22. Fusilly about 1 month later:

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

  23. khelll about 1 month later:

    am getting :

    Forbidden

    You don’t have permission to access / on this server.

  24. Ken about 1 month later:

    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 about 1 month later:

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

  26. Martin about 1 month later:

    Any chance part 3 of this guide will be ready soon?

Comments