As for curiosity and having a full control over AWS EC2 instances I am tempted to setup a PHP-FPM. An alternative PHP Fast CGI implementation, it may be overkill to have it setup on a new website but just for curiosity sake I set it up anyways.
At work we use PHP-FPM for our Development Environment and I love how I can control these php instances, restarting it anytime I can whenever I change PHP settings. So as it turns out it is a very straightforward approach to set it up.
Why PHP-FPM?
PHP-FPM has come a long way and with the recent version it has a pretty neat features that will help your website specially if you are running in a server which power is controlled and very limited.
First things first
You need to understand that PHP-fpm is the another application layer that you need to install and run on your server.
$ yum install php-fpm
Run PHP-FPM
I find this a bit tricky since most of the tutorials I ran into are talking about turning on “php-fpmâ€. Apparently after installation the service name for the PHP-FPM is totally different. So I had to run this command to check the correct service name
$ system list-unit-files
And its name “rh-php71-php-fpm†so I run it as.
$ service “rh-php71-php-fpm†start
And to check its status you can check it via.
$ service “rh-php71-php-fpm†status
Getting Dirty
Now that we already have the service running let us tell Apache to load PHP-FPM in a particular website.
On your virtual host configuration, which is commonly found under /etc/httpd/conf.modules.d/vhosts.conf. Add a proxy_module node on your Virtual Host file configuration as is
<IfModule proxy_module> ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/laravel/public/$1 DirectoryIndex /index.php index.php </IfModule>
So on your configuration it would look something similar to this.
<VirtualHost *:80> ServerName www.foobar.com DocumentRoot “<path/to/directory>†<IfModule proxy_module> ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/laravel/public/$1 DirectoryIndex /index.php index.php </IfModule> <Directory "<path/to/directory>"> Options Indexes FollowSymLinks AllowOverride All Require all granted Order Allow,Deny Allow from all </Directory> </VirtualHost>
The above code tells the Apache Server that whenever there is a request for a particular website spawn a php instance with these specific settings for this particular website, which spins out a process on the server.
Common Issues
Most common issues I encounter is during installation process. I ran into something weird after I switched to a different PHP version since my PHP-FPM is using an old version of PHP my website always loads the old version even if already setup the new PHP as part of my server environment so just make sure PHP-FPM is using the correct PHP.
Another common issue is running PHP-FPM after installation, like I mention on my previous blogs you need to make sure you are running the service properly meaning you need to make sure you are running the correct service name since most of the tutorial I notice is suggesting to use “php-fpm†as for my case its “rh-php7-fpm†a very specific name if you ask me.
What now?
If you run process check on your server to check how many PHP-FPM.
$ ps aux | grep php-fpm
And similarly you can check the status via.
$ service “rh-php71-php-fpm†status
It will lists all the spawned php-fpm instance breaking the workers thus giving a parallel computing. Which in theory can lead to a performance enhancement.