Screencast - Deploying - 3
This screencast covers using the Passenger module for the Apache webserver (the Passenger module is also known as “mod_rails”); and we’ll also look at MySQL.
MySQL first.
To install MySQL, you need to do three things: (1) Install MySQL; (2) create a user and database for your application; and (3) change config/database.yml so that it can get to your MySQL database.
In the instructions that follow, will will simply re-run the migrations to get the database set up. Migration the data from Sqlite3 to MySQL is beyond the scope of this screencast.
So . . . install MySQL (when the install script has asked for a password for the MySQL “root” login, we have simply pressed return; if you want more security, provide a clever password):
sudo apt-get install libmysql-ruby mysql-server libmysqlclient15-dev sudo gem install mysql
Create a user and database for your application:
mysql -u root create database metricsmine_production; grant all on metricsmine_production.* to 'mm_user'@'localhost' identified by 'mm_pass';
And, finally, we need to update config/database.yml. Notice that we are only updating the “production” settings — which will look like this:
production: adapter: mysql database: metricsmine_production username: mm_user password: mm_pass
Now run migrations with
RAILS_ENV=production rake db:migrate
We can now restart Mongrel with
sudo script/server -p 80 -e production
and browse into our server. Now we’re using MySQL. Notice that we use sudo to start the server. This is because we have selected port 80, which is a privileged port. Try doing it without “sudo” to see the error we would get otherwise.
On to getting Passenger going.
First let’s stop Mongrel; if you started it the way I describe above, just press control c.
Install Apache2:
sudo apt-get install apache2 apache2-prefork-dev
Browse into your server, and you should see “It works!” Let’s stop the server for the next step (though it probably isn’t necessary to do so) with
sudo /etc/init.d/apache2 stop
Now for Passenger:
sudo gem install passenger sudo passenger-install-apache2-module
Now we must make two tweaks to the Apache configuration. First we must add some lines at the end of /etc/apache2/apache2.conf. When I do this, I will start with “sudo bash” so that I have a root shell which will make it easier to navigate around and run the editor (vi).
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6 PassengerRuby /usr/bin/ruby1.8
Then we must set up a virtual host configuration for our server. New versions of Apache keep site configurations in the directory /etc/apache2/sites-available, and then for sites you actually want to enable, you create a symbolic link from /etc/apache2/sites-enabled to the right file in /etc/apache2/sites-available. To do this, we will change directory to /etc/apache2/sites-available. We’ll rename the old default configuration to default-orig (so we can revert it if necessary). Then we’ll go over to /etc/apache2/sites-enabled, and remove 000-default.
cd /etc/apache2/sites-available mv default default-orig cd ../sites-enabled rm 000-default
Now we will create a new /etc/apache2/sites-available/default file describing our application. That file will look like this:
<virtualhost *:80> ServerAdmin jgn@localhost ServerName www.metricsmine.com DocumentRoot /home/jgn/metricsmine02/public PassengerPoolIdleTime 1800 </virtualhost>
Notice the value 1800 for PassengerPoolIdleTime. Normally Passenger will go to sleep after some number of seconds of “idleness” for your app. We have increased this number so that the app will typically stay available in memory. With a smaller number, we would see Rails getting unloaded, and, consequently, users on occasion would have to wait for Rails to start up (we saw this numerous times in class).
Finally, we need to create the symbolic link from /etc/apache2/sites-available/default to /etc/apache2/sites-enabled/default:
ln -s /etc/apache2/sites-available/default /etc/apache2/sites-enabled/default
Now restart Apache2 with /etc/init.d/apache2 start. Voila!
Deploying, Part 3, Screencast 1 of 2
Deploying, Part 3, Screencast 2 of 2
blog comments powered by Disqus
Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment