Nginx, PHP-FPM, APC, Varnish and Wordpress

Written by
Date: 2012-04-27 17:20:00 00:00


Installing needed software

I'm using Arch Linux as the server, so let's install needed software. First be sure you have the latest available software now install all the stuff.

pacman -Syu

Now install Nginx, PHP-FPM and MySQL

pacman -S nginx php-fpm mysql

Now let's first take some steps to be sure mysql installation is secure.

/etc/rc.d/mysqld start
mysql_secure_installation

In case you need help answering the questions, you can find some help in this page check the MySQL section.

Configure PHP-FPM

First let's be sure PHP-FPM is using a socks instead of listening to a port, that makes it more efficient. In my Arch Linux version, that is already that way. Anyway look for these lines.

;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock

And be sure that the one listening to a port is commented and not the one listening to a socket, if you need to add the line do it.

You also need to uncomment these lines from php.ini file:

;extension=mysqli.so
;extension=mysql.so

And make them look like this:

extension=mysqli.so
extension=mysql.so

This way PHP will be able to connect with MySQL, which is needed by Wordpress

Configure Nginx

Now that we have PHP-FPM working and listing to a socket, let's make Nginx send PHP requests to that socket.

Add this to your server snippet:

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
    }

We should now have a server capable to manage PHP scripts.

Configure MySQL

It is MySQL turn now, let's create the database:

mysql -u root -p

Now create the database, and grant access to the user.

CREATE DATABASE `wordpress`;
GRANT ALL PRIVILEGES ON `wordpress`.* TO 'wordpress'@'localhost' IDENTIFIED BY 'your-password';
FLUSH PRIVILEGES;
EXIT

Enable MySQL Query Cache

Add this to the my.cnf file in the [mysqld] section:

query_cache_limit = 1M
query_cache_size = 16M
query_cache_type = 1

This will make MySQL a little bit more efficient, as it will be able to cache some queries.

Install and enable APC

In my case with Arch Linux I did:

pacman -S php-apc

And to enable it:

  • Edit php.ini file
  • Add this line: extension=apc.so

APC is:

Alternative PHP Cache is a free, open source (PHP license) framework that optimizes PHP intermediate code and caches data and compiled code from the PHP bytecode compiler in shared memory. (Source: Wikipedia)

Install Varnish

APC will increase performance with logged users, but for not logged users, the best way to increase performance is by using Varnish Cache.

Installing Varnish Cache

pacman -S varnish

Configure Varnish Cache for Wordpress

Install Wordpress

You are now ready to go to wordpress site and download the latest version, and install it.

Full Configuration Files

If you want to get the full configuration files, download them here

Here is a description of what you will find:

  • /etc/nginx/conf/nginx.conf
  • /etc/php/php.ini
  • /etc/php/php-fpm
  • /etc/conf.d/varnish
  • /etch/varnish/default.vcl

Comparing Nginx and Apache

You can see some differences between Apache and Nginx while running Wordpress, with and without APC and Varnish