Friday, July 19, 2013

Nginx server set up with PHP on Ubuntu

Nginx is a alternative Web server for Apache. Nginx mainly focus on non-blocking IO request/responds and it handles many concurrent access without much CPU resources

Nginx Installation

Nginx is available as a package for Ubuntu which we can install as follows:
    apt-get install nginx
Start nginx afterwards:
    /etc/init.d/nginx start  
Type in your web server's IP address or hostname into a browser (e.g. http://localhost) , and you should see "Nginx Welcome message" The default nginx document root on Ubuntu is /usr/share/nginx/www.

Install PHP5 with Nginx
We can make PHP5 work in nginx through PHP-FPM (PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites) which we install as follows:
apt-get install php5-fpm 
PHP-FPM is a daemon process (with the init script /etc/init.d/php5-fpm) that runs a FastCGI server on the socket /var/run/php5-fpm.sock.

Configure Nginx
The virtual hosts are defined in server {} containers. The default vhost is defined in the file /etc/nginx/sites-available/default - let's modify it as follows:
server {
        listen   81 default_server; ## listen for ipv4; this line is default and implied
        listen   [::]:81 default_server ipv6only=on; ## listen for ipv6

        root /usr/share/nginx/www;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

      location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

}
Finally reload nginx:
/etc/init.d/nginx reload 

1 comment: