Skip to content

Getting Nginx + PHP5 + PHP-FPM + CodeIgniter to work on Debian Squeeze

After hearing so many good things about Nginx, I decided to give it a try to scale one of our CodeIgniter app that got quite a lot of traffic lately.

Nginx (pronounced “engine X”) is a Russian lightweight Httpd server. Think Apache, but much smaller & faster. Nginx is used by companies like NetflixHuluPinterestCloudFlareAirbnbWordPress.comGitHubSoundCloudZyngaEventbriteZappos,Media TempleHerokuRightScaleEngine Yard to name a few.

This week end I finally decided to install it on one of our Debian 6.0 Squeeze server. While there’s plenty of documentation on the net about it, I found out that many articles were actually outdated and misled me to waste a few hours.

First of all, get PHP5 and nginx from Debian default repository package. (Unlike I saw on many posts, do NOT use dotdeb packages for nginx as it will just mess up your install. I strongly advise you to use Debian official packages only.)

Then install PHP and a few other dependencies

apt-get install php5 php5-fpm php-pear php5-common php5-mcrypt php5-mysql php5-cli php5-gd php-gd php-curl php-apc php5-apc

Notice that we installed PHP-APC, which works great for speeding up your PHP scripts executions. It works by caching the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request. I strongly recommend it, especially for webservices layers with heavy usage.
Now let’s install nginx and start it

apt-get install nginx

/etc/init.d/nginx start

Then you can do some fine tuning to the /etc/php5/fpm/php5-fpm.conf  to fit your server setup. There’s a great post about it here.

Now create the folder where you will have your CodeIgniter app and upload your CodeIgniter app in it.

mkdir /var/www/

chown -R www-data:www-data /var/www/

Now we will need to configure Nginx to link our CodeIgniter app to our domain. Use pico or your favorite editor to change your /etc/nginx/conf.d/default.conf file with the following content :

server {
listen 80;

root /var/www/;
index index.php;

# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;

location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;

location ~* \.php$ {
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;

## prevent php version info
fastcgi_hide_header X-Powered-By;

Notice that in this file, we have a line “include fastcgi_params” which, as it says, will include the file fastcgi_params which should be under /etc/nginx/. In this file, you should add the following bloc of parameters to avoid PHP to choke and get 503 errors from nginx. You will find all the explanations of these variables here.

Also, it would be great to enable gzip compression on several types of content, which can greatly reduce the consumed bandwidth and also the connection time required by each HTTP transaction on your web server. Add the following configuration bloc in you /etc/nginx/nginx.conf

You can find more info about this topic here.

After this, make sure that your codeIgniter config.php contains the following information:

Tip : Some PHP applications require a larger amount of memory, don’t forget to tweak your PHP config file (memory_limit for example). You can check the main PHP variables here.

Let’s restart nginx and PHP-FPM at the same time with the following command line :

for s in nginx php5-fpm; do service $s restart; done

Note: Nginx can be restarted alone via the command line

Voila, you can now fire your favorite browser and check, which should display your CodeIgniter app.

Try it for yourself but Nginx is a great webserver that could allow you to save a lot of money if tuned properly. Also check this great post benchmarking the memory usage of Apache and Nginx with an increasing load.



Share your thoughts