Improving performance in Rails with Apache2
The standard Apache2 installation shipping with most Linux distributions uses MPM prefork. While this is a good model, for instance, when using apache modules to run PHP, it does not perform that well when running Rails using FastCGI. Instead, we can use MPM Worker for a better performance.
So, what's the difference between prefork and worker?
Quoting from the Apache MPM Prefork page:
MPM Prefork implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3.
And for the Apache MPM Worker says:
MPM Worker implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with less system resources than a process-based server.
So, you have decided you want to switch to Apache2 MPM Worker. One of the problems you may find when doing an apt-get install apache2-mpm-worker is that dpkg will automatically delete the PHP module and other packages associated to it. The solution is to let dpkg remove PHP and install it by ourselves.
We will have to install some build-related packages if they were not installed before:
persefone:~# apt-get install build-essential flex libxml2-dev
Installing apache2-mpm worker:
persefone:~# apt-get install apache2-mpm-worker (will remove mpm-prefork, and libapache2-mod-php and related packages)
We need to install libmysqlclient development headers to compile PHP with mysql support:
persefone:~# apt-get install libmysqlclient15-dev
We also need apxs2, so we will install the apache2 development headers for mpm worker:
persefone:~# apt-get install apache2-threaded-dev
We can download the PHP source code from http://www.php.net/downloads.php and compile it as usual:
persefone:~# ./configure --with-apxs2=/usr/bin/apxs2 --with-mysql --enable-maintainer-zts --prefix=/usr
persefone:~# make
persefone:~# make install
There is no need to make a symbolic link at /etc/apache2/mods-enabled/ for php5 now, as it is automatically enabled.
So, what's the difference between prefork and worker?
Quoting from the Apache MPM Prefork page:
MPM Prefork implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3.
And for the Apache MPM Worker says:
MPM Worker implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with less system resources than a process-based server.
So, you have decided you want to switch to Apache2 MPM Worker. One of the problems you may find when doing an apt-get install apache2-mpm-worker is that dpkg will automatically delete the PHP module and other packages associated to it. The solution is to let dpkg remove PHP and install it by ourselves.
We will have to install some build-related packages if they were not installed before:
persefone:~# apt-get install build-essential flex libxml2-dev
Installing apache2-mpm worker:
persefone:~# apt-get install apache2-mpm-worker (will remove mpm-prefork, and libapache2-mod-php and related packages)
We need to install libmysqlclient development headers to compile PHP with mysql support:
persefone:~# apt-get install libmysqlclient15-dev
We also need apxs2, so we will install the apache2 development headers for mpm worker:
persefone:~# apt-get install apache2-threaded-dev
We can download the PHP source code from http://www.php.net/downloads.php and compile it as usual:
persefone:~# ./configure --with-apxs2=/usr/bin/apxs2 --with-mysql --enable-maintainer-zts --prefix=/usr
persefone:~# make
persefone:~# make install
There is no need to make a symbolic link at /etc/apache2/mods-enabled/ for php5 now, as it is automatically enabled.

