Introduction In previous post Deploy Scalable and Reliable WordPress Site on LEMP(2), we successful set up Linux+Nginx+PHP+MySQL (LEMP) stack to hold
Introduction
In previous post Deploy Scalable and Reliable WordPress Site on LEMP(2), we successful set up Linux+Nginx+PHP+MySQL (LEMP) stack to hold WordPress site. However, Nginx and PHP services were enabled on the same server WEBo1.
In this lab, we will separate PHP to an external server PHP01 and leave WEB01 as Nginx web server only. It adds some flexibility scalability strategy. For performance enhancement details, please refer to Scaling PHP apps via dedicated PHP-FPM nodes, a test post I found online.
Deployment Steps
This lab involves PHP01 deployment and configuration change on WEB01 to forward PHP requests to PHP01. The topology is as below:
Step 1 – Configure PHP01 as php-fpm node
Boot another $5 ubuntu server from DigitalOcean, details available in Deploy Scalable and Reliable WordPress Site on LEMP(1).
1 2 3 4 5 6 7 8 9 10 11 |
#log onto PHP01 #update and install glusterfs client, php-fpm and php-mysql services on PHP01 <span style="color:#0000ff;">sudo apt-get update sudo apt-get -y install glusterfs-client php-fpm php-mysql</span> #make a folder called 'gluster' under root <span style="color:#0000ff;">sudo mkdir /gluster</span> #mount the 'file_store' volume on FS01 to '/gluster'on PHP01. '10.132.43.212' is FS01's private IP. 'glusterfs' is the filesystem type. <span style="color:#0000ff;">sudo mount -t glusterfs 10.132.43.212:/file_store /gluster</span> # add the partition to fstab so it mounts automatically at boot time. <span style="color:#0000ff;">sudo echo "10.132.43.212:/file_store /gluster glusterfs defaults,_netdev 0 0" >> /etc/fstab</span> |
Verify PHP01 can access the same WordPress folder we created on FS01 before, by executing the following command on PHP01.
1 2 |
<span style="color:#0000ff;">sudo ls -la /gluster/www </span> |
Step 2 – WEB01 forward PHP requests to PHP01
Log onto WEB01, edit the nginx site default file by executing the following command:
1 |
<span style="color:#0000ff;">sudo nano /etc/nginx/sites-enabled/default</span> |
Update the file as below; where ‘10.132.19.6’ is PHP01’s private IP, and 9000 is the port used by fastcgi. Comment the local fastcgi socket ‘unix:/run/php/php7.0-fpm.sock’.
1 2 3 4 5 6 7 8 9 10 |
# pass the PHP scripts to FastCGI server listening on the php-fpm socket location ~ \.php$ { try_files $uri =404; <span style="color:#0000ff;">#fastcgi_pass unix:/run/php/php7.0-fpm.sock;</span> <span style="color:#0000ff;">fastcgi_pass 10.132.19.6:9000;</span> fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } |
Restart Nginx service by executing the following command:
1 |
service nginx restart |
Let’s now log onto the WordPress site, see whether it is still working as expected in Deploy Scalable and Reliable WordPress Site on LEMP(2).
Unfortunately, we get ‘502 Bad Gateway’ message this time.
Fortunately, Nginx provides us error log. Execute the following command on WEB01 to view the log.
1 |
cat /var/log/nginx/error.log |
Nginx error log reveals the following:
1 |
2016/11/04 10:25:21 [error] 18867#18867: *1 connect() failed (111: <span style="color:#0000ff;">Connection refused</span>) while connecting to upstream, client:x.x.x.x, server:y.y.y.y, request: "GET / HTTP/1.1", upstream: "<span style="color:#0000ff;">fastcgi://10.132.19.6:9000</span>", host: "y.y.y.y" |
OK…it appears WEB01 passed on the request to PHP01, but PHP01 refused the connection. Step 3 will help resolve the issue.
Step 3 – Allow PHP01 to Listen WEB01
On PHP01, edit PHP ‘www.conf’ file to allow listen on WEB01.
1 |
sudo nano /etc/php/7.0/fpm/pool.d/www.conf |
Perform the following changes in ‘www.conf’.
1 2 3 4 5 6 7 8 |
#Add the following line to allow WEB01's private IP <span style="color:#0000ff;">listen.allowed_clients = 10.132.84.104</span> #Comment the following line by adding ";" in the front. <span style="color:#0000ff;">;</span>listen = /run/php/php7.0-fpm.sock #Add the following line to have php-fpm listen on port 9000 <span style="color:#0000ff;">listen = 9000</span> |
Restart PHP on PHP01 and Nginx on WEB01.
1 2 3 4 5 |
#On PHP01 restart php service <span style="color:#0000ff;">service php7.0-fpm restart</span> #On WEB01 restart nginx service <span style="color:#0000ff;">service nginx restart </span> |
Let’s now access the WordPress site again. ‘Hello world!’ – it’s working!
COMMENTS