Install Nginx with PHP5 on Linux

What follows is exactly what I did to install Nginx to serve PHP5 on Ubuntu Server 11.4 (32 bit).

Note: install as user, not as root. Edit in vi by basic command:

type “a” for insert.

press “Esc” for quit edit.

type “ZZ” for save & exit.

type “:q!” for not save & exit.

Update your system and install basic tools required

>sudo aptitude update  -y

>sudo locale-gen en_GB.UTF-8

>sudo /usr/sbin/update-locale LANG=en_GB.UTF8

>sudo aptitude safe-upgrade  -y

>sudo aptitude full-upgrade  -y

>sudo aptitude install build-essential -y

Install MySQL

>sudo aptitude install mysql-server mysql-client libmysqlclient15-dev -y

Install PHP5

>sudo aptitude install php5-cli php5-cgi php5-mysql php5-xcache -y

Note: Xcache is installed at this point and available for you to setup, but by default is not turned on.

Install Nginx

>sudo aptitude install nginx -y

Go to your IP address and you should now receive the message “Welcame to nginx!”

FastCGI Parameter Configuration

We will place all of our fastcgi parameters in single file which we can include in as necessary.

>sudo vi /etc/nginx/fastcgi_params

This will be a new empty file, add the following and save:

fastcgi_param     QUERY_STRIING       $query_string;

fastcgi_param     REQUEST_METHOD   $request_method;

fastcgi_param     CONTENT_TYPE          $content_type;

fastcgi_param     CONTENT_LENGTH     $content_length;

fastcgi_param     SCRIPT_NAME           $fastcgi_script_name;

fastcgi_param     REQUEST_URI              $request_uri;

fastcgi_param     DOCUMENT_ROOT       $document_root;

fastcgi_param     SERVER_PROTOCOL     $server_protocol;

fastcgi_param     GATEWAY_INTERFACE     CGI/1.1;

fastcgi_param     SERVER_SOFTWARE           nginx;

fastcgi_param      REMOTE_ADDR                $remote_addr;

fastcgi_param      REMOTE_PORT                 $remote_port;

fastcgi_param      SERVER_ADDR                 $server_addr;

fastcgi_param      SERVER_PORT                  $server_port;

fastcgi_param      SERVER_NAME                $server_name;

#PHP only, required if PHP was build with –enable-force-cgi-redirect

#fastcgi_param REDIRECT_STATUS    200;

Nginx configuration

>sudo vi /etc/nginx/sites-available/defaul

This is a pre-existing file. Find the part that looks similar to the following and edit it as so and save:

 location ~ \.php$ {


fastcgi_index     index.php;

fastcgi_param   SCRIPT_FILENAME  /var/www$fastcgi_script_name;

include  /etc/nginx/fastcgi_params;


We need to remember to restart Nginx

>sudo /etc/init.d/nginx restart


We still need a script to start our fast cgi processes. We will extract one from Lighttpd.

>mkdir ~/sources

>cd ~/sources


>tar -xvjf   lighttpd-1.4.18.tar.bz2

>cd lighttpd-1.4.18



>sudo cp src/spawn-fcgi  /usr/bin/spawn-fcgi

Let’s get automated!

>sudo touch /usr/bin/php-fastcgi

>sudo vi /usr/bin/php-fastcgi

This is a new empty file, add following and serve:


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


>sudo touch /etc/init.d/init-fastcgi

>sudo vi /etc/init.d/init-fastcgi

This is also a new empty file, add the following and save:




case “$1” in






killall -9 php




killall -9 php





echo “Usage: php-fastcgi {start|stop|restart}”

exit 1



exit $RETVAL

We need to change some permissions to make this all work.

>sudo chmod 755 /usr/bin/php-fastcgi

>sudo chmod 755 /etc/init.d/init-fastcgi

Test it.

>sudo vi /var/www/index.php

Let’s just print out the information page for our PHP installation




Start it up

>/etc/init.d/init-fastcgi start

Now go to your IP address/index.php and you should see the PHP info page displayed.

Set to startup automatically upon reboot

>sudo update-rc.d init-fastcgi defaults

You might want to test and make sure that it actually starts up upon reboot…

>sudo reboot

Good luck!


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s