How To: Install NginX, PHP-FPM, MySQL, PHP 5.3.10 & WordPress on Ubuntu (Part 2)

by Pranav Rastogi on September 13, 2010

Ubuntu + nginx + php + php-fpm + wordpress

How To: Install NginX, PHP-FPM, MySQL, PHP 5.3.10 & WordPress on Ubuntu (Part 2 of 2)

This is the 2nd part of the tutorial on How To: Install NginX, PHP-FPM, MySQL, PHP 5.3.10 & WordPress on Ubuntu Server. I’ve used a Linode 512MB VPS running Ubuntu 10.04 LTS for this tutorial.

After installing everything, it is time to configure PHP-FPM and NginX. First, start with editing the php-fpm.conf file. In this case we have the file at location /etc/php/etc/php-fpm.conf. So use your favorite text editor to edit the file, I use nano.

nano /etc/php/etc/php-fpm.conf

PHP-FPM will not start with the default configuration. Uncomment/Change the values of the following lines in the file:

pid = /etc/php/var/run/php-fpm.pid
error_log = /etc/php/var/log/php-fpm.log
log_level = notice
pm = dynamic
pm.max_children = 8
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 1000
emergency_restart_threshold = 4
emergency_restart_interval = 10s
process_control_timeout = 10s
daemonize = yes
listen.backlog = -1
listen = 127.0.0.1:9000
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
user = www-data
group = www-data

rlimit_files = 32768

rlimit_core = unlimited

You can use pm = static in the above config, but pm = dynamic is recommended for optimal performance. You can also change the other variables to suit your needs.

Now, we have to edit NginX’s configuration. To do that, open up /etc/nginx/nginx.conf in your text editor.

nano /etc/nginx/nginx.conf

I have the following configuration:

user www-data;
worker_processes  4;
worker_rlimit_nofile 32768;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  4096;
    multi_accept on;
    accept_mutex_delay 50ms;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;
    expires max;
    server_tokens off;
    gzip  on;
    gzip_static on;
    gzip_proxied any;
    gzip_types text/plain text/css application/x-javascript text/xml text/javascript application/xml;
    gzip_vary  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

You can adjust keepalive_timeout if you have a website which has more user interaction such as forums, video sites, etc. then you can adjust it to make user experience better. Pages are served faster to the user if keepalive_timeout is more. server_tokens disables showing the version of NginX it HTTP headers. worker_processes should be equal to the number of CPUs, run the following command to check the number of CPUs

grep processor /proc/cpuinfo

worker_connections should be equal to the number of connections each worker_process can handle. The total no. of connections this configuration can handle is: 16384. worker_rlimit_nofile is the maximum number of descriptors that can be opened by Nginx’s process. You can set it to a higher/lower value depending upon your usage. You’ll find rlimit errors in Nginx logs if this variable is set to a lesser value.

Gzip helps serve the files faster as they’re compressed before they are sent to the user.

Now, restart NginX and PHP-FPM to make sure everything’s fine in the configuration.

service php-fpm restart
service nginx restart

Finally, it’s time to create the Nginx zone file for your WordPres blog. Start by creating a file in the location /etc/nginx/sites-enabled named after your website, it can be anything. If you’re managing many sites and blogs together then it can very useful and time saving. For this blog, I created a file called tutspundit.com for this blog installation.

My zone file is as follows:

server {

            listen   80;
            server_name  www.tutspundit.com;
            rewrite ^/(.*) http://tutspundit.com/$1 permanent;

           }


server {
  listen   80;
  access_log  /var/www/tutspundit.com/log/access.log;
 error_log      /var/www/tutspundit.com/log/error.log info;
server_name     tutspundit.com;
root /var/www/tutspundit.com/public_html;

location / {
index index.php;
# if the requested file exists, return it immediately
if (-f $request_filename) {
        break;
}

# all other requests go to WordPress
if (!-e $request_filename) {
        rewrite . /index.php last;
}
}

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


## Parse all .php file in the /var/www directory
    location ~ ^.+\.php {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/tutspundit.com/public_html$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     on;
        fastcgi_read_timeout 180;

    }

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


upstream backend {
        server 127.0.0.1:9000;

}

The first server{} block is used to redirect all requests from www.tutspundit.com to tutspundit.com. The 2nd server{} block is used to create the rules for the server listening to tutspundit.com. The rules inside the location / {} block are used to enable WordPress URL rewriting.

All the files for the domain are stored at /var/www/tutspundit.com/. /var/www/tutspundit.com/public_html serves the files to the users. You must make your own changes reflecting the location of your WordPress install. upstream backend{} block is used to add PHP-FPM as a backend for processing PHP.

Save & close the file making required changes. Make a directory at /var/www/ named after your domain name (It can be anything, really. It should reflect the configuration in your zone file.)

mkdir /var/www/tutspundit.com/public_html
mkdir /var/www/tutspundit.com/log

Restart Nginx again to make sure everything’s fine. Change your directory to /var/www/tutspundit.com/public_html and install WordPress in it. Now, you must change the ownership of the files and do chmod 755. It enables WordPress to: use the installation/update feature, make changes to the files, etc.

chown www-data:www-data -R /var/www/tutspundit.com/public_html
chmod 755 -R /var/www/tutspundit.com/public_html

That’s it! You’ve got WordPress installed on your VPS running Nginx. Feel free to drop a comment on this article if you need some help.

Previous post:

Next post: