blog home

How to set up your server for Rails and Passenger - Part II

December 31st 2008

comments

This is part II of III. If you need to catch up, Part I is here

Apache

Lets start by getting Apache installed. Apache is what's going to actually serve webpages to our visitors. This will be considerable quicker than the SSH stuff.

Login to your server,
ssh deploy@ipaddress,
and run
sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert
all as one line

sudo says to the server "do this as a Super User" so it'll ask you for your Super User password.

You'll see a bunch of stuff happening that we don't need to worry about. You might however worry about the last line apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName. All this means is that we haven't set the name of our Apache server.
If you want to, do sudo nano /etc/apache2/apache2.conf you'll find a very long Apache configuration file with lots of options and things to break (I recommend some google searching for more info) but adding
ServerName aCatchyServerName
to the very last line is pretty innocuous and will get rid of that "error" message every time we restart Apache. Save and exit then
sudo apache2ctl graceful
No errors, right?

apache2ctl is a way of starting, stopping or restarting the Apache server. apache2ctl graceful is a restart that doesn't kill open connections.

Now, to test that Apache is working and can serve webpages, open your favourite browser (or IE if that's all you have) and go to your IP address (http://123.45.67.890) and fingers crossed we'll see Apache's "It works!" screen.

Just how frigging easy was that, eh?!

Ruby etc

Installing Ruby, Rails and all that is a little tedious so forgive me and I'll try to make this a simple as possible.

SSH into your server if you're not there already. Lets install Ruby, sqlite3 (MySQL to follow later) and a bunch of other things you'll need littering your clean server. (Again, all this code goes on one line.)

sudo aptitude install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby sqlite3 libsqlite3-ruby1.8

This might take a wee while but there's nothing to do but sit and watch it do it's stuff. Once it's done we'll create some symlinks so that we can just refer to the location of Ruby as /usr/bin/ruby not /usr/bin/ruby1.8 run

sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby then
sudo ln -s /usr/bin/ri1.8 /usr/bin/ri then
sudo ln -s /usr/bin/rdoc1.8 /usr/bin/rdoc then
sudo ln -s /usr/bin/irb1.8 /usr/bin/irb

And then let's ask what version of Ruby we have installed (reason being, if it's OK we'll get a version number, if it's messed up it'll tell us that ruby is an unknown command). ruby -v should return something like
ruby 1.8.6 (2007-06-07 patchlevel 36) [x86_64-linux].

Rails

In order to install Rails (and a whole load of other Rails related stuff later on), we need to install RubyGems. This bit is a little fiddly. First, go to http://rubyforge.org/projects/rubygems/ and copy the link for the latest version (under "Latest File Releases" click download then copy the "rubygems-1.3.1.tgz" link where 1.3.1 is the latest version). As of today it's 1.3.1 so that's what I'll be using.

Take a deep breath and (still on your server) run
mkdir ~/sources && cd ~/sources then
wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz to download it to our sources folder,
tar xzvf rubygems-1.3.1.tgz && cd rubygems1.3.1 to unpack it and go to that unpacked folder,
sudo ruby setup.rb to install it and
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem to set a symlink for it just like we did with Ruby and then test that it works with our patented version test gem -v.

And finally, to Rails itself sudo gem install rails will to the trick or sudo gem install rails -v 2.1.1 if you want a specific older version of Rails installed. While we're here feel free to install whatever gems you know you'll need just like you do on your development machine sudo gem install gemName.

MySQL

Although sqlite3 is now the default database in Rails many people like using MySQL for the production db so I'll go through how to install that.

On your server run sudo aptitude install mysql-server mysql-client libmysqlclient15-dev libmysql-ruby1.8 -y. During installation it will ask you for a password. Read carefully. It's asking you to setup a password for the MySQL root user. You'll need this in just a mo...

Lets setup a MySQL user for our Rails application to use.
mysql -u root -p and when prompted enter that password I warned you that you would need. We're now inside the MySQL app.
CREATE USER 'deploy'@'localhost' IDENTIFIED BY 'deploy_password'; will create a mysql user called deploy and mysql will let deploy mess around with databases if it's a local connection and if he comes with the password of 'deploy_password'. These are the credentials that will need to go in your config/database.yml file for your production db. And you'll also need to give your app the socket of MySQL which for me was /var/run/mysqld/mysqld.sock.

Anytime you mess around with users in MySQL run FLUSH PRIVILEGES; when you're done.

In the future, once we have some Rails apps installed we'll come back here to create our dbs. But just incase I forget, once you've logged in to MySQL as root do something like CREATE DATABASE testapp_production; where testapp_production is the name of the production db set in config/database.yml. Then run GRANT ALL PRIVILEGES ON `testapp_production` . * TO 'deploy'@'localhost'; to give our mysql user "deploy" privileges to access and update this database.

And don't forget to type exit to exit out of MySQL when you're done.

Good work boys and girls! We're nearly there, I promise.

Come along now, this way to Part III please.

blog comments powered by Disqus