Install nginx, php5 with php5-fpm on ubuntu

Here is described how to install nginx with php support on Ubuntu (Kubuntu) 10.10 but it can be used for any other Linux distribution.

If you already have installed Apache web server on your system make sure it is not running:

$ sudo /etc/init.d/apache2 status
Apache2 is running (pid 4757).

$ sudo /etc/init.d/apache2 stop
* Stopping web server apache2                                                                                                                                                                 … waiting

$ sudo /etc/init.d/apache2 status
Apache2 is NOT running.

Next step is to install nginx server:

$ sudo apt-get install nginx

and php fpm:

$ sudo apt-get install php5-fpm

Now you need to create a new virtual host with name example.loc or whatever your domain is

cd  /etc/nginx/sites-available/

sudo cp default example.loc

And now edit this file with editor you like (for example with nano):

sudo nano example.loc

server {

listen   80; ## listen for ipv4
#listen   [::]:80 default ipv6only=on; ## listen for ipv6

server_name  example.loc;

access_log  /var/log/nginx/localhost.access.log;

location / {
root   /var/www;
index  index.php index.html index.htm;

#location /doc {
#root   /usr/share;
#autoindex on;
#deny all;

#location /images {
#root   /usr/share;
#autoindex on;

#error_page  404  /404.html;

# redirect server error pages to the static page /50x.html
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /var/www/nginx-default;

# proxy the PHP scripts to Apache listening on
#location ~ \.php$ {

# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
include fastcgi_params;

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
location ~ /\.ht {
deny  all;

cd /etc/nginx/sites-enabled/

sudo ln -s /etc/nginx/sites-available/example.loc example.loc

You can check if links are ok with :

$ ls -la
total 8
drwxr-xr-x 2 root root 4096 2011-02-21 11:09 .
drwxr-xr-x 5 root root 4096 2011-02-21 10:56 ..
lrwxrwxrwx 1 root root   34 2011-02-21 10:56 default -> /etc/nginx/sites-available/default
lrwxrwxrwx 1 root root   38 2011-02-21 11:09 example.loc -> /etc/nginx/sites-available/example.loc

Now add this domain in hosts file:

sudo nano /etc/hosts

Add this line (if this ip address already exists just append ‘example.loc’ at end of the line )

….       example.loc


You can check if conf syntax is OK with:

$ sudo nginx -t
the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful

Restart nginx and php fpm:

$ sudo /etc/init.d/nginx restart
Restarting nginx: nginx.
$ sudo /etc/init.d/php5-fpm restart
* Stopping PHP5 FPM…                                                                                                                                                                [ OK ]
* Starting PHP5 FPM…                                                                                                                                                                       Feb 21 11:17:02.990639 [WARNING] [pool www] pm.start_servers is not set. It’s been set to 20.
[ OK ]

Now you can test it with loading domain url in browser: http://example.loc
You can switch between Apache and nginx with:

$ sudo /etc/init.d/nginx stop

Stopping nginx: nginx.

$ sudo /etc/init.d/apache2 start
* Starting web server apache2

Rewrite rules used with Zend framework can be used with nginx like this:

.htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ – [NC,L]
RewriteRule ^.*$ index.php [NC,L]

For nginx make this changes:

sudo nano /etc/nginx/sites-available/example.loc


location / {
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
root   /var/www;
index  index.php index.html index.htm;


And you can force which server to load on system load with:

sudo sysv-rc-conf

Used resources: