15.2. Basic Installation Web Server

Please log in as root on the web server.

Buildsystem

First, we install a few packages we are going to need.
root@debian:~# apt-get -y install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion python
[...]
root@debian:~# 

Node.js

To make the most of the asset pipeline, we install Node.js. Please go to the homepage of Node.js (http://nodejs.org/), search for the current stable release and adapt the commands listed here accordingly.
root@debian:~# cd /usr/src
root@debian:/usr/src# wget http://nodejs.org/dist/v0.8.1/node-v0.8.1.tar.gz
[...]
root@debian:/usr/src# tar xzf node-v0.8.1.tar.gz 
root@debian:/usr/src# cd node-v0.8.1
root@debian:/usr/src/node-v0.8.1# ./configure 
[...]
root@debian:/usr/src/node-v0.8.1# make
[...]
root@debian:/usr/src/node-v0.8.1# make install
[...]
root@debian:/usr/src/node-v0.8.1# cd
[...]
root@debian:~# 

nginx

Nginx will be our web server to the outside world. It acts as proxy and load balancer. As the Debian distribution includes a rather old Nginx version, we install the packages distributed by Nginx directly as well.
root@debian:~# curl http://nginx.org/keys/nginx_signing.key | apt-key add -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1561  100  1561    0     0   3887      0 --:--:-- --:--:-- --:--:--  8819
OK
root@debian:~# echo 'deb http://nginx.org/packages/debian/ squeeze nginx' >> /etc/apt/sources.list
root@debian:~# echo 'deb-src http://nginx.org/packages/debian/ squeeze nginx' >> /etc/apt/sources.list
root@debian:~# apt-get update
[...]
root@debian:~# apt-get -y install nginx
[...]
root@debian:~# 

Creating User Deployer

Our Rails project is going to run within a Ruby and Rails installed via RVM in the user space. So we create a new user with the name deployer:
root@debian:~# adduser deployer
Lege Benutzer »deployer« an ...
Lege neue Gruppe »deployer« (1002) an ...
Lege neuen Benutzer »deployer« (1002) mit Gruppe »deployer« an ...
Erstelle Home-Verzeichnis »/home/deployer« ...
Kopiere Dateien aus »/etc/skel« ...
Geben Sie ein neues UNIX-Passwort ein: 
Geben Sie das neue UNIX-Passwort erneut ein: 
passwd: Passwort erfolgreich geändert
Benutzerinformationen für deployer werden geändert.
Geben Sie einen neuen Wert an oder drücken Sie ENTER für den Standardwert
 Vollständiger Name []: Deployer
 Raumnummer []: 
 Telefon geschäftlich []: 
 Telefon privat []: 
 Sonstiges []: 
Sind die Informationen korrekt? [J/n] J
root@debian:~#

Create Directory Structure

We now create a directory /var/www and set the user rights for it:
root@debian:~# mkdir -p /var/www
root@debian:~# usermod -a -G www-data nginx
root@debian:~# usermod -a -G www-data deployer
root@debian:~# chgrp -R www-data /var/www
root@debian:~# chmod -R 775 /var/www
root@debian:~#

Setting up Rails Environment for User Deployer

Next, the user deployer:
root@debian:~# su - deployer
deployer@debian:~$ 
As user deployer, please carry out the steps described in Section 1.5, “Debian 6.0 (Squeeze) - Installation of Ruby on Rails 3.2 with RVM ” for installing Ruby 1.9.3 and Rails 3.2 via rvm. Here is the single line cut-and-paste short version:
deployer@debian:~$ curl -L https://get.rvm.io | bash -s stable && source ~/.rvm/scripts/'rvm' && rvm install 1.9.3 && gem install rails
[...]
deployer@debian:~$
To be able to start Unicorn with the RVM environment from within an Init.d script, we now need to generate a corresponding wrapper:
deployer@debian:~$ gem install unicorn
[...]
deployer@debian:~$ rvm wrapper 1.9.3 bootup unicorn
deployer@debian:~$ exit
root@debian:~$

Database

Usually, you want to use a "large" database in a production system, such as PostgreSQL or MySQL. So here is how to install a MySQL database on this system and what you need to adapt in the Rails project.

MySQL Installation

Next, we install the database MySQL. You will be asked for a database password. Please remember this password. Later, root can use it to log in to the database.
root@debian:~# apt-get -y install mysql-server libmysql-ruby libmysqlclient-dev
[...]
root@debian:~# 

Creating Database with Rights

In the MySQL database, we need to create the database blog with access rights for the user deployer:
deployer@debian:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.1.63-0+squeeze1 (Debian)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE blog;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT USAGE ON *.* TO deployer@localhost IDENTIFIED BY 'IhrLieblingsPasswort';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON blog.* TO deployer@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye
deployer@debian:~$

Installing Memcache

If you are working with a cache server (highly recommended), you of course have to install the appropriate software. For memcached (http://memcached.org/) you would enter this:
root@debian:~# apt-get -y install memcached
[...]
root@debian:~#

Updates about this book will be published on my Twitter feed.