Typo with Lighttpd and MySQL on Ubuntu

Lighttpd is a secure, fast, compliant, and very flexible web-server that has been optimized for high-performance environments. “Ruby on Rails” is a robust web development language/framework combination which has become quite famouse lately. And Typo is an application written with RoR, a lean engine that makes blogging easy.

This tutorial introduces how to deploy a Typo blog with lighttpd as web server and MySQL as database on a Ubuntu (Feisty) Linux system.

Install Ruby, RubyGems and Rails

RubyGems is an application behaves like apt-get, which installs ruby apps from a repository called RubyForge. Though there are some ruby application contained in Ubuntu’s repositories, it’s preferred to let RubyGems handle all the ruby packages, because gems packages seem to be more up-to-date.

Ruby and RubyGems

sudo apt-get install ruby ruby1.8-dev libzlib-ruby rdoc irb rubygems

To test if ruby is successfully installed, you can use interactive Ruby interpreter to evaluate ruby expression:

$ irb
rb(main):001:0> 3+5
=> 8
irb(main):002:0> 4*20
=> 80

Then use RubyGems to update the system (include itself)

$ sudo gem update --system

Note: The reason to install package “ruby1.8-dev” is to avoid errors in later steps which may occur during compiling.

Rails

Now you can use RubyGems to install Rails and any gems you want.

$ sudo gem install rails -y

The “-y” parameter is used to install dependent packages by default, to avoid being ask “Y/N” every time.

Lighttpd

Lighttpd is simple and fast, we use it instead of Apache, which may be more powerful but at the same time more complicated and slower.

Ubuntu’s repository also contains pre-compiled packages. So we install it together with FastCGI library for Ruby

$ sudo apt-get install lighttpd lighttpd-doc libfcgi-ruby1.8

The configuration files of Lighttpd is organized in typical debian style. Those of available modules are put into /etc/lighttpd/conf-availabl/. By using command lighty-enable-mod or lighty-disable-mod, symlinks of relavant .conf files are created or deleted in directory /etc/lighttpd/conf-enabled/, respectively. General configuration are written inside /etc/lighttpd/lighttpd.conf.

So we enable the fastcgi module by

$ sudo lighty-enalbe-mod fastcgi

If you look into the enabled .conf file /etc/lighttpd/conf-enabled/10-fastcgi.conf, you will find it automatically add the fastcgi support for php. So if you would like keep it(i.e. don’t comment them out), you must install package php5-cgi by apt-get and add the line:

cgi.fix_pathinfo = 1

to you php.ini file (/etc/php5/cgi/php.ini), otherwise there will be errors when lighttpd starts.

MySQL

$ sudo apt-get install mysql-server libmysql-ruby1.8 mysql-admin

Since the database and the application accessing it are on the same machine, so we don’t need to set bind address. But we need to set root password as follow:

$ mysql -u root

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpassword');

You can use MySQL Admin to visually configurate the database. Another solution is to install phpMyAdmin.

Install and Deploy Typo

Install Typo is simple, just do:

sudo gem install typo -y

All the dependency will be installed including a web server Mongrel and SQLite interface for Ruby. (Though we don’t need them later on). You may need to install the package “build-essential” and “libsqlite3-dev” in order to successfully build these packages.

Then we deploy Typo into a directory. I’d like to put it into a directory I made dedicated for RoR apps (/var/rails/), while you could put anywhere you’d like to put, e.g. ~/typo.

$ sudo typo install /var/rails/typo

A lot of files will be copied to that directory and finally Typo will be automactically started. Actually at this point, you’ve already had a running Typo based on the Mongrel server. You can use the link prompted at the end of the command output (something like http://localhost:4741) to test if Typo works, and then stop Typo by

$ sudo typo stop /var/rails/typo

Then we need to configure Typo’s variables in order not to use its default web server Morgrel and database SQLite.

$ sudo typo config /var/rails/typo web-server=external database=mysql

Next step is to create a database and a user for Typo. (here I name both of them “typo”) Following are the steps by command lines to finish this job, you can also use MySQL Admin.

$ mysql -h localhost -u root -p
Enter password: *******
mysql> CREATE DATABASE typo;
   mysql> GRANT ALL ON typo.* TO typo@localhost IDENTIFIED BY 'typopassword';

Now the file /var/rails/typo/config/database.yml should be created from database.yml.example (or be modified if already exists), it should look like this:

login: &login
    adapter: mysql
     host: localhost
        username: typo
         password: typopassword
         database: typo

development:
     <<: *login

test:
      database: typo_tests
       <<: *login

production:
  <<: *login

So the database is ready, we simple load the sql file into it by:

$ sudo rake db:migrate RAILS_ENV=production

it also can be done by using mysql command:

$ sudo mysql typo -h localhost -u typo -p < /var/rails/typo/db/schema.mysql.sql

Server Intergration

Before doing anything, we’d better make sure the file system permission:

$ sudo chown www-data:www-data /var/rails/typo/public -R

And then we write a piece of configuration file for running Typo with fastcgi. Following the convention of Debian/Ubuntu, I put this file into /etc/lighttpd/conf-available/ and later use lighty-enable-mod to switch it on.

The content of this file (/etc/lighttpd/conf-available/10-typo.conf) is:

$HTTP["host"] == "typo" {
  server.document-root = "/var/rails/typo/public/"
  server.error-handler-404 = "/dispatch.fcgi"
  server.indexfiles = ("dispatch.fcgi")
  server.errorlog = "/var/rails/typo/log/lighttpd_error.log"
  url.rewrite = ("^/$" => "index.html", "^([^.]+)$" => "$1.html")
  fastcgi.server = (".fcgi" =>
      ("localhost" =>
          (
          "socket" => "/tmp/lighttpd-fcgi-typo.socket",
          "bin-path" => "/var/rails/typo/public/dispatch.fcgi",
          "bin-environment" => ( "RAILS_ENV" => "production" ),
          "max-load-per-proc" => 25,
             "min-procs" => 1,
              "max-procs" => 4,
              "idle-timeout" => 60
                          )
      )
  )
}

As you can see in the file, I give Typo a host name of its own, and set several conditional rules based on this. If your deploy your Typo somewhere else, don’t forget to change those paths above accordingly.

The last step is to add “typo” to my host file (/etc/hosts) and restart the web server by

$ sudo /etc/init.d/lighttpd force-reload

And now you simply enter “typo” in your Firefox and then pray, in 1 or 2 seconds, the singup page of Typo will appear, hopefully.

Note: Firefox tends to cache http request for optimization. But this may bring problem when you test your sever locally, since the frequently changed settings may not be refreshed and recogonized by Firefox. In such a case, it’s neccessary to switch the “network.http.use-cache” to false in the about:config page.

Reference

  1. Ruby on Rails on Debian, Debian Administration
  2. Installing Typo: MySQL, Apache, lighttpd and FastCGI, Dev411 – The Code Wiki
  3. Setting up PHP with Lighttpd, Lighttpd Wiki
  4. Typoinstall, Typo Wiki
Advertisements

About this entry