For this tutorial I am using the Ubuntu 8.10 linux version. We given very easy steps for installing php and ngnix and configuration details.

How to install Nginx and PHP on Ubuntu

Install Nginx
# sudo apt-get install nginx

If apache server is installed on your machine then please stop the apache server first.

#sudo /etc/init.d/nginx start

#sudo update-rc.d nginx defaults

Install PHP5

#sudo apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Open the PHP.ini file

#sudo vim /etc/php5/cgi/php.ini

Insert following line at the end of php.ini file
cgi.fix_pathinfo = 1

Install the lighttpd server

#sudo apt-get install lighttpd

You will see an error message saying that lighttpd couldn’t start because port 80 is already in use.

For removing the error message use following command

#sudo update-rc.d -f lighttpd remove

Start the FastCGI daemon service

#sudo /usr/bin/spawn-fcgi -a -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/

If you want run following command whenever you boot the system. Open following file.

#sudo vim /etc/rc.local

Copy paste following following lines in file.

/usr/bin/spawn-fcgi -a -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/

Configuration Nginx

#sudo vim /etc/nginx/nginx.conf
Just copy paste the following code in end of httpd block. Dont paste outside of http block.

server {
listen 80;
server_name _;

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

location / {
root /var/www/nginx-default;
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/nginx-default$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;

How to install Nginx and PHP on Ubuntu

How to install Nginx and PHP on Ubuntu

Just restart the Nginx server.

#sudo /etc/init.d/nginx restart

Written by Purab Kharat

I Purab am from India, Pune. I am WordPress and Drupal Architect and consultant. I worked on many technologies like Ruby on Rails, Flex, PHP, jQuery etc... Senior Technical Lead at Smartsourcing Global Pvt Ltd in Princeton, NJ, USA.I contributed to many wordpress plugins and Drupal Modules. I contributed to Drupal core also.Connect to purab on LinkedIn or Follow @purab on Twitter or Follow me on Google+ or Write Mail to or Chat with me on Skype : purabdk

This article has 1 comment

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>