Nginx and PHP-FPM for Ubuntu

1. Install and Start Nginx
(a) use apt-get
[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ su
$ apt-get install nginx
$ update-rc.d nginx defaults
$ /etc/init.d/nginx start

[/codesyntax]
(b) compile from source
[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ sudo apt-get install libpcre3 libpcre3-dev
$ sudo apt-get install libssl-dev libssl0.9.8
$ wget http://nginx.org/download/nginx-0.8.52.tar.gz
$ tar -xzf nginx-0.8.52.tar.gz
$ cd nginx-0.8.52
$ ./configure --prefix=/usr/local/webserber/nginx
$ make
$ sudo make install;
$ sudo /usr/local/webserber/nginx/sbin/nginx

[/codesyntax]

2. PHP Repository and Install php5-fpm (deprecated from php5.3.3)
[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ apt-get install python-software-properties
$ add-apt-repository ppa:brianmercer/php
$ apt-get update
$ apt-get install php5-fpm php5-cli php5-cgi php5-dev

[/codesyntax]

3. Config Nginx
[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ vi /etc/nginx/sites-available/default
server {
    listen 80;
    server_name localhost;
    access_log /var/log/nginx/localhost.access.log;

    location / {
        root /var/www/nginx-default;
        index index.php index.html index.htm;
    }

    ## Images and static content is treated different
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
        access_log off;
        expires 30d;
        root /var/www;
    }

    ## Parse all .php file in the /var/www/nginx-default directory
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
        include fastcgi_params;
        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_intercept_errors on;
        fastcgi_ignore_client_abort off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }

    ## Disable viewing .htaccess & .htpassword
    location ~ /\.ht {
        deny all;
    }
}

[/codesyntax]

4. Start PHP-FPM
[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ /etc/init.d/php5-fpm start

[/codesyntax]

Posted in Nginx, PHP, Ubuntu | Tagged , , | Leave a comment