Ubuntu Nginx and PHP5

Filed under Linux, Servers

This post will go over the steps you need to get nginx running with php5. I am going to use a list to show the steps to take, please leave a comment if you see something that needs to be corrected or just want to leave feedback.

Installation

  1. Update apt:
    sudo apt-get update

  2. Install nginx:
    sudo apt-get install nginx


  3. Install php-cgi for use with mysql:
    sudo apt-get install php5-cgi php5-mysql


  4. We need a binary that comes with lighttpd to launch the php5 cgi:
    sudo apt-get install lighttpd


  5. Now remove lighttpd from startup since we will be using nginx:
    sudo update-rc.d -f lighttpd remove

Configuration of nginx,startup scripts,fastcgi

  1. Create a bash script that will spawn the php fcgi handlers:
    sudo vim /usr/bin/php-fastcgi


  2. and enter the following:

    #!/bin/bash
    /usr/bin/spawn-fcgi -a 127.0.0.1 -p 10005 -u www-data -g www-data -f /usr/bin/php-cgi

    Make sure to set www-data to the user and group that you want php running as.

  3. Make the script executable:
    sudo chmod +x /usr/bin/php-fastcgi


  4. Now lets at create an init script that will execute the above script on boot (this script is from a blog that I can’t remember the name of):
    sudo vim /etc/init.d/init-fastcgi

    and enter the following

    #!/bin/bash
    PHP_SCRIPT=/usr/bin/php-fastcgi
    RETVAL=0
    case "$1" in
    start)
    $PHP_SCRIPT
    RETVAL=$?
    ;;
    stop)
    killall -9 php-cgi
    RETVAL=$?
    ;;
    restart)
    killall -9 php-cgi
    $PHP_SCRIPT
    RETVAL=$?
    ;;
    *)
    echo "Usage: php-fastcgi {start|stop|restart}"
    exit 1
    ;;
    esac
    exit $RETVAL


  5. Make the script executable:
    sudo chmod +x /etc/init.d/init-fastcgi


  6. Add the script to run at bootup:
    sudo update-rc.d init-fastcgi defaults


  7. Now we will begin configuring nginx to proxy php request. First we need to setup a include file that will hold some fastcgi_param’s.
    sudo vim /etc/nginx/fastcgi.conf

    and enter

    #fastcgi.conf
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    #To use Nginx + Virtual Host + PHP you should ommit the SCRIPT_NAME
    #variable in order for PHP to choose the correct DOCUMENT_ROOT
    #fastcgi_param SCRIPT_NAME $fastcgi_script_name;

    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param DOCUMENT_URI $document_uri;
    fastcgi_param DOCUMENT_ROOT $document_root;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    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 built with --enable-force-cgi-redirect
    #fastcgi_param REDIRECT_STATUS 200;


  8. Edit the server conf and tell it where to proxy requests to:
    sudo vim /etc/nginx/sites-enabled/default


  9. In the server section add the following:

    location ~ .*\.php$ {
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass 127.0.0.1:10005;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /home/xxx/public_html/domian/public/$fastcgi_script_name;
    }

    *Update SCRIPT_FILENAME to point to your doc root*

  10. Update index list to enable index.php: *Thanks Brian*

    index index.html index.htm index.php;


  11. Restart nginx and start the php cgi handlers:

    sudo /etc/init.d/nginx restart
    sudo /etc/init.d/init-fastcgi start

2 Trackbacks

  1. [...] Ubuntu Nginx and PHP5 – Frank Kumro [...]

  2. By compbrain.net » Nginx PHP5 Fastcgi on July 30, 2009 at 11:45 pm

    [...] Quick note for myself and anyone else looking to replace lighttpd or apache2 with nginx for serving php (ala wordpress): http://frankkumro.com/2009/01/03/ubuntu-nginx-and-php5/ [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*