Quick Guide to Nginx

Introduction

Like Lighttpd, Nginx is a faster and leaner alternative to Apache. It can be used as either a regular web server or a reverse proxy. This is a short HOWTO on installing Nginx with PHP5 in FastCGI mode on Ubuntu (10.04).

Setup

From package (Ubuntu)

Install Nginx and check that it works OK before moving on to adding PHP5 to the mix:

  1. apt-get install nginx
  2. /etc/init.d/nginx start
  3. netstat -lpn
  4. http://nginx-srv/
  5. /etc/init.d/nginx stop
  6. update-rc.d nginx defaults
  7. chown -R www-data.www-data /var/www?

To remove Nginx: apt-get --purge remove nginx ; rm -Rf /var/www/nginx-default/

Note that if you perform multiple install/remove cycles, you might need to clean the database:

dpkg --purge $(COLUMNS=200 dpkg -l | grep "^rc" | tr -s ' ' | cut -d ' ' -f 2)

From source

  1. cd /usr/src
  2. wget -c http://sysoev.ru/nginx/nginx-0.7.67.tar.gz
  3. tar xzvf ./nginx-0.7.67.tar.gz
  4. cd nginx-0.7.67
  5. ./configure
  6. make
  7. make install

More information:

Adding PHP5

There are several ways to run PHP5 with Nginx:

We'll use PHP-FPM to run PHP5.

Through packages

  1. apt-get install php5-cli php5-common php5-suhosin php5-mysql
  2. apt-get install python-software-properties
  3. add-apt-repository ppa:brianmercer/php
  4. apt-get update
  5. apt-get install php5-fpm php5-cgi
  6. /etc/init.d/php5-fpm start
  7. "netstat -nat" and "ps aux", and check that a group of php5-fpm processes are running as www-data

Tips:

Configuring Nginx + PHP5

By default, the followings files in /etc/nginx/ don't need to be modified: nginx.conf, fastcgi_params. The only file you need to edit for a standard install is /etc/nginx/sites-available/default. Here's an example:

server {
    listen 80 default;
    server_name  localhost;
    access_log  /var/log/nginx/localhost.access.log;
    root   /var/www/nginx-default;
 
    location / {
        index index.php index.html;
    }
 
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
      access_log        off;
      expires           30d;
    }
 
    location ~ \.php$ {
        include fastcgi_params;
 
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass   backend;
        fastcgi_index  index.php;
 
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 
        fastcgi_intercept_errors        on;
        fastcgi_ignore_client_abort     off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }
 
## Disable viewing .htaccess & .htpassword
    location ~ /\.ht {
        deny  all;
    }
}
 
upstream backend {
        server 127.0.0.1:9000;
}

Next, add the usual PHP script to check that it works:

  1. cd /var/www/nginx-default/; mv index.html index.html.orig
  2. vi index.php: <?php phpinfo(); ?>

Restart Nginx: /etc/init.d/nginx restart

Aim your browser to the server and check that it works as expected.

Check for errors in /var/log/nginx/error.log.

php.ini

If you make any change to php.ini, remember to /etc/init.d/php5-fpm restart

Does FPM use both INI files below?

# ll /etc/php5/fpm/

conf.d -> ../conf.d/

php5-fpm.conf

php.ini

Should I include this file to use PDO in PHP5 scripts?

# ll /etc/php5/conf.d/

pdo.ini

suhosin.ini

Q&A

Why does lack of trailing slash redirects to localhost?

http://192.168.0.3/punbb -> http://localhost/punbb/

http://192.168.0.3/punbb/ OK

How to change the port on which Nginx is listening?

Updated /etc/nginx/sites-available/default, restarted both PHP5-FPM and Nginx: redirected to 80 with 301 error

Resources

To read

Third party modules

NginxFullExample

Wordpress

DONE http://till.klampaeckel.de/blog/archives/30-PHP-performance-III-Running-nginx.html

http://www.sitepoint.com/blogs/2010/05/04/php-with-nginx-is-about-to-become-a-lot-easier/

http://blog.hbis.fr/2009/03/18/nginx-fastcgi-php/