How to Installing Nginx with PHP5-FastCGI and MySql Support on Ubuntu Server 12.04 LTS

in Web Server

This guide will walk you explain a little bit about How to Installing nginx with php5-fastcgi and mysql Support on Ubuntu Server 12.04 LTS, Nginx (pronounced “engine x”) is a free, open-source  and high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption.

Follow the step by step installing and setup nginx with php5-fastcgi and mysql Support on Ubuntu Server 12.04 LTS in below

Step 1. Before you begin installing and configuring nginx, Set the hostname ubuntu server. in this case I’ll use host name “precise.public“.  if you want to change hostname you can edit the file /etc/hosts.

Step 2. Install package Engine X, PHP and MySql

sudo apt-get install nginx mysql-server mysql-client spawn-fcgi php5 php5-cli php5-common php5-suhosin php5-cgi php-pear php5-mysql

Step 3. Create file /usr/sbin/fastcgi-php 

sudo touch /usr/sbin/php-fastcgi
sudo nano /usr/sbin/php-fastcgi

Put the following script on file /usr/sbin/php-fastcgi

#!/bin/bash
FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
ADDRESS=127.0.0.1
PORT=9000
#SOCKET=/var/run/php-fastcgi/php-fastcgi.socket # UNIX Socket
PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
CHILDREN=6
PHP5=/usr/bin/php5-cgi
/usr/bin/spawn-fcgi -a $ADDRESS -p $PORT -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

Make the file /usr/sbin/php-fastcgi executable

sudo chmod +x /usr/sbin/php-fastcgi

Step 3. Create directories to hold content and log files:

sudo mkdir -p /srv/www/precise.public/public_html
sudo mkdir /srv/www/precise.public/logs
sudo chown -R www-data:www-data /srv/www/precise.public

Step 4. Define the site’s virtual host file, This example uses a TCP Sockets Configuration

sudo touch /etc/nginx/sites-available/precise.public
sudo nano /etc/nginx/sites-available/precise.public

Put this configuration on file /etc/nginx/sites-available/precise.public

server {
 server_name www.precise.public precise.public;
 access_log /srv/www/precise.public/logs/access.log;
 error_log /srv/www/precise.public/logs/error.log;
 root /srv/www/precise.public/public_html;
location / {
 index index.html index.htm;
 }
location ~ \.php$ {
 include /etc/nginx/fastcgi_params;
 fastcgi_pass 127.0.0.1:9000; # TCP Socket
# fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket; # UNIX Socket
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME /srv/www/precise.public/public_html $fastcgi_script_name;
 }
}

and create a symbolic link to /etc/nginx/sites-enabled

sudo ln -s /etc/nginx/sites-available/precise.public /etc/nginx/sites-enabled/

Step 5. Create a file named /etc/init.d/php-fastcgi with the following contents:

sudo touch /etc/init.d/php-fastcgi
sudo nano /etc/init.d/php-fastcgi

Insert this script on file /etc/init.d/php-fastcgi

#!/bin/bash
PHP_SCRIPT=/usr/sbin/php-fastcgi
FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
PID_DIR=/var/run/php-fastcgi
PID_FILE=/var/run/php-fastcgi/php-fastcgi.pid
RET_VAL=0
case "$1" in
 start)
 if [[ ! -d $PID_DIR ]]
 then
 mkdir $PID_DIR
 chown $FASTCGI_USER:$FASTCGI_GROUP $PID_DIR
 chmod 0770 $PID_DIR
 fi
 if [[ -r $PID_FILE ]]
 then
 echo "php-fastcgi already running with PID `cat $PID_FILE`"
 RET_VAL=1
 else
 $PHP_SCRIPT
 RET_VAL=$?
 fi
 ;;
 stop)
 if [[ -r $PID_FILE ]]
 then
 kill `cat $PID_FILE`
 rm $PID_FILE
 RET_VAL=$?
 else
 echo "Could not find PID file $PID_FILE"
 RET_VAL=1
 fi
 ;;
 restart)
 if [[ -r $PID_FILE ]]
 then
 kill `cat $PID_FILE`
 rm $PID_FILE
 RET_VAL=$?
 else
 echo "Could not find PID file $PID_FILE"
 fi
 $PHP_SCRIPT
 RET_VAL=$?
 ;;
 status)
 if [[ -r $PID_FILE ]]
 then
 echo "php-fastcgi running with PID `cat $PID_FILE`"
 RET_VAL=$?
 else
 echo "Could not find PID file $PID_FILE, php-fastcgi does not appear to be running"
 fi
 ;;
 *)
 echo "Usage: php-fastcgi {start|stop|restart|status}"
 RET_VAL=1
 ;;
esac
exit $RET_VAL

Then Make the  file /etc/init.d/php-fastcgi executabe

sudo chmod +x /etc/init.d/php-fastcgi

Step 6. Start php-fastcgi and nginx, then make /etc/init.d/php-fastcgi autoload at startup

sudo /etc/init.d/php-fastcgi start
sudo /etc/init.d/nginx restart
sudo update-rc.d php-fastcgi defaults

Note: If you running sudo /etc/init.d/php-fastcgi start and get a message like this “spawn-fcgi: bind failed: Address already in use“, run the command: sudo netstat -tpan |grep “LISTEN”|grep :9000

Step 7. Test PHP with FastCGI, Create a file /srv/www/example.com/www/public_html/info.php

sudo touch /srv/www/example.com/www/public_html/info.php
sudo nano /srv/www/example.com/www/public_html/info.php
<?php phpinfo(); ?>

visit http://precise.public/info.php in your browser, the standard “PHP info” output is shown. Congratulations, you’ve configured the nginx web server to use PHP-FastCGI

Reference :

Advertisement:
  • Jay

    got this error:
    invalid parameter “$fastcgi_script_name” in /etc/nginx/sites-enabled/precise.public

    because there was a space before between
    fastcgi_param SCRIPT_FILENAME /srv/www/precise.public/public_html $fastcgi_script_name;
    and
    $fastcgi_script_name

    It worked after I changed it to:
    fastcgi_param SCRIPT_FILENAME /srv/www/precise.public/public_html$fastcgi_script_name;

    Also, I had to change the line for the phpinfo() file to:
    sudo touch /srv/www/precise.public/public_html/info.php

    sudo nano /srv/www/precise.public/public_html/info.php

    I had to add “precise.public” and the server IP to /etc/hosts to make it resolve in the browser, accessing the webserver via IP address worked out of the box.

    Thanks!

  • http://www.facebook.com/Constructed.mzk Roman Evgenievich Makarov

    I doesn’t work :(
    Just index.php downloading and that’s all…

    php-fastcgi succefully running, nginx too

    Anybody help!

    • Leo Goile

      what webserver are you running?

      Post the config for me.

Previous post:

Next post: