I’ll be using Ubuntu 16.04. So let’s start.

I don’t want to mess up with a server, so a local virtual machine should be enough:

mkdir -p ~/projects/php-fpm
cd ~/projects/php-fpm

vagrant init ubuntu/xenial64

Edit Vagrantfile to have an active port:

config.vm.network "forwarded_port", guest: 80, host: 3380

Run and connect to Ubuntu Server:

vagrant up
vagrant ssh

Install apache and PHP:

apt-get install \
 apache2 \
 php \
 libapache2-mod-fastcgi 

See your server working: ubuntu-apache-welcome

Ubuntu already created an init script to execute php-fpm and actually it’s already running:

# service php7.0-fpm status
● php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2016-11-24 18:16:50 UTC; 9min ago
  Process: 1034 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS)
 Main PID: 1045 (php-fpm7.0)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 3
   Memory: 26.3M
      CPU: 135ms
   CGroup: /system.slice/php7.0-fpm.service
           ├─1045 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)                      
           ├─1048 php-fpm: pool www                                                            
           └─1049 php-fpm: pool www                                                            

Nov 24 18:16:50 ubuntu-xenial systemd[1]: Starting The PHP 7.0 FastCGI Process Manager...
Nov 24 18:16:50 ubuntu-xenial systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

Check where the socket where PHP-FPM is running on it’s default pool config: /etc/php/7.0/fpm/pool.d/www.conf

listen = /run/php/php7.0-fpm.sock

So with this information let’s setup apache to use php-fpm. You know test driven development? We need PHP because wordpress doesn’t work without it. So let’s download Wordpress first:

wget https://wordpress.org/latest.zip
unzip latest.zip 'wordpress/*' -d /var/www/html/
chown -R www-data:www-data /var/www/html

Load the server to see that PHP is not working at all: wordpress-not-working

Edit the default apache host to allow PHP content: /etc/apache2/sites-enabled/000-default.conf

        <Directory /usr/lib/cgi-bin>
                Require all granted
        </Directory>

Edit Apache config to enable PHP-FPM serve: /etc/apache2/conf-enabled/php7.0-fpm.conf

        <IfModule mod_fastcgi.c>
                AddHandler php7-fcgi .php
                Action php7-fcgi /php7-fcgi
                Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
                FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization
        </IfModule>

And restart:

service apache2 restart

Now PHP is working. php-is-working

But we also need a mysql database to run wordpress so…

apt-get install mysql-server php-mysql

Remember to create the database:

mysql -p
create database wordpress;
exit

And wordpress setup is working now:

Follow the steps and that’s it!

wordpress-is-working

Now let’s optimize!

Server Software:        Apache/2.4.18
Server Hostname:        127.0.0.1
Server Port:            3380

Document Path:          /
Document Length:        10849 bytes

Concurrency Level:      10
Time taken for tests:   9.795 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      11090000 bytes
HTML transferred:       10849000 bytes
Requests per second:    102.10 [#/sec] (mean)
Time per request:       97.946 [ms] (mean)
Time per request:       9.795 [ms] (mean, across all concurrent requests)
Transfer rate:          1105.72 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:    39   97  16.0     97     154
Waiting:       39   97  15.9     96     153
Total:         40   98  16.0     97     154

102req/s, not bad.

Configure opcache to minimize disk usage:

/etc/php/7.0/fpm/php.ini

opcache.enable=1

This didn’t improved execution performance. Maybe PHP7 is fast enough by itself with SSD Disk.

Let’s try how a full cached website performs with apache… install and configure W3-Total-Cache…

Here we go!

Server Software:        Apache/2.4.18
Server Hostname:        127.0.0.1
Server Port:            3380

Document Path:          /
Document Length:        0 bytes

Concurrency Level:      10
Time taken for tests:   3.963 seconds
Complete requests:      1000
Failed requests:        0
Non-2xx responses:      1000
Total transferred:      196000 bytes
HTML transferred:       0 bytes
Requests per second:    252.32 [#/sec] (mean)
Time per request:       39.633 [ms] (mean)
Time per request:       3.963 [ms] (mean, across all concurrent requests)
Transfer rate:          48.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       4
Processing:    11   39   7.9     39      74
Waiting:       11   39   7.9     38      74
Total:         12   39   7.9     39      74