In this article I’m going to share some techniques of configuring typical Ruby/RoR server. I’ll skip all steps on installing OS, so lets just assume that we have Ubuntu 8.04 LTS Server 64bit installed out of the box with SSH account.
Plan
- Setting up server
- Installing ruby/rails
- Installing necessary gems
- Setting up application
- Moving data
1. Setting up server
- Users set up
- Setting up SSH
- Setting up public keys
- Set up time/timezone
1.1 Users set up
First I need to create a user for deploying and accessing server, lets call it ‘deploy’ user.sudo adduser --home /home/deploy deployIt’s a good practice to select complicated password for you accounts, here is a great password generator I use.
Then add the following line deploy ALL=(ALL) ALL after root ALL=(ALL) ALL to suoders list.
sudo nano /etc/suodersRemove all unneeded users. A list of all users you can find at /etc/passwd. User can be deleted by:
$ sudo deluser usernameSet up user password policies. User policies are changed via chage command. Here is an example:
sudo chage -l deploy sudo change deploy
For a detailed description of commands refer to the following manual on user management.
1.2 Setting up SSH
All ssh setting are found at /etc/ssh/sshd_config. Basically what I need to change are port, protocol, root access, public key auth.#/etc/ssh/sshd_config ... Port <b>567</b> ... Protocol <b>2</b> ... PermitRootLogin <b>no</b> ... PubkeyAuthentication <b>yes</b> ...Once you made any changes to ssh, you need to reboot ssh deamon:
$ /etc/init.d/ssh restart $ exit Connection to xx.xx.xx.xx closed. $ ssh deploy@xx.xx.xx.xx -p 567
1.3 Setting up public keys
Every time I deploy or access my server, I need to enter long unmemorable password. To avoid that I set up public keys. This is done simply, first you need to generate public key on your site by:home$ ssh-keygen home$ cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAqLHM9juBojORQIrIcvo0.......And then append your ssh-rsa key to authorized_keys (Create ~/.ssh/authorized_keys if file doesn’t exists).
cat id_rsa.pub > ~/.ssh/authorized_keys
Here is a manual for generating ssh keys.
1.3 Setting up time/timezone
There are actually several ways how to set up time on your server. First one is to set up timezone, which is described here
The other way, which I prefer more, is to change system time:$ hwclock --set --date="YYYY-MM-DD HH:MM:SS" $ hwclock -s
That’s it with server configuration.
2. Installing ruby/rails
- Installing development libraries
- Installing RubyEE
- Install mysql, sphinx, postfix, etc
sudo apt-get update sudo apt-get install libssl-dev -y sudo apt-get install build-essential -yand some others
sudo apt-get -y install git-core -y sudo apt-get -y install curl wget -y sudo apt-get install libcurl-ocaml-dev -y sudo apt-get install mysql-server -y sudo apt-get install libmysqlclient15-dev -y sudo apt-get install libsqlite3-dev make -y sudo apt-get install libjpeg-progs sudo apt-get install libmagick9-dev sudo apt-get install imagemagickSetting up sphinx:
wget http://www.sphinxsearch.com/downloads/sphinx-0.9.9.tar.gz gzip -d sphinx-0.9.9.tar.gz & tar -xvf sphinx-0.9.9.tar cd sphinx-0.9.9 ./configure make sudo make installOnce this is done, go and download rubyEE, which comes together with nginx or apache, whatever you need. And run the following command:
mkdir ~/sources
cd ~/sources
wget http://rubyforge.org/frs/download.php/68720/ruby-enterprise_1.8.7-2010.01_amd64.deb
sudo dpkg -i ruby-enterprise_1.8.7-2010.01_amd64.deb
sudo /usr/local/bin/passenger-install-apache2-module
To get mails sent, I also need a postfix installed. This is simple as
sudo apt-get install postfix
This would install ruby,rails and nginx.
3. Installing gems
Here’s just a small list of gems, required by regular rails project.
sudo gem sources --add http://gems.github.com
sudo gem install backup \
archive-tar-minitar \
aws-s3 \
chronic \
curb \
fastthread \
javan-whenever \
mime-types \
mysql \
net-scp \
net-sftp \
net-ssh \
rack \
ruby-hmac \
rubyzip
sudo gem install rmagick -v 2.10.0
sudo gem install rails -v 2.3.5
sudo gem install javan-whenever
sudo gem install daemons
4. Setting up application
First I need to create a folder where my application would be stored. Usually I create it in /opt/applications.
$ mkdir -p /opt/applications/app_name $ cd /opt/applications/app_name/shared
To deploy source code you need to add your public key to github (in case of deploying from github). For more details on public keys and github access this manual.
Before first deploy I prefer setting up MySql and shared folder.
It is a good practice to create a separate MySQL user for you application. This is done simple by:
$ mysql --user=root database mysql> CREATE USER 'deploy'@'localhost' IDENTIFIED BY 'some_pass'; mysql> GRANT ALL PRIVILEGES ON *.* TO 'deploy'@'localhost';
Setting up shared folder. Usually there are log, pids and system folder. With all basic configuration files.
$ cd /opt/application/app_name/shared $ mkdir log $ mkdir pids $ mkdir system $ mkdir sphinx $ tocuh database.yml <- production databases username and password
Once you have MySQL installed, it’s time to think about nginx configuration. Here is a typical nginx configuration for my applications (/opt/nginx/conf/nginx.onf).
Another useful thing for nginx is a init.d deamon file.
Add nginx to startup on reboot:
$ sudo crontab -e @reboot /etc/init.d/nginx start
Next, I should set up postfix.
And MySQL config with some optimisations.
So, once, we are done, it’s time for deploy script.
And now on you local machine just type:$ cap production deploy
4. Moving data
First create a mysql dump:$ mysqldump --complete-insert --skip-extended-insert --user=deploy --password="xxxx" app_name > backup.sql
And then rsync all data from old server to the new one.
$ rsync -az folder/ -e 'ssh -p 987' deploy@domain.com:/opt/applications/app_name/shared/ $ mysql -u deploy -p app_name < backup.sql
And we are actually done. Now just restart server and application is ready for running.
$ sudo /etc/init.d/nginx restart