Friday, February 2, 2018

How to setup apache event mpm, php-fpm and mod_proxy_fcgi

In this article I will show you how to install Apache event mpm, php-fpm and mod_proxy_fcgi.

Also this article supposes that you have Ubuntu 16.04 and apache 2.4.18. Of course you can also do similar steps on other versions of Ubuntu, but keep in mind that you need to have Apache version of at least 2.4.10. For example, Ubuntu 14.04 has Apache 2.4.7 and some features that I use in this article do not work there (SetHandler with proxy:fcgi).



1. Install packages:
sudo apt-get install apache2 php-fpm

2. Check apache version:
apache2ctl -V
Make sure that this string is present: "Surver MPM: event". (if it's not there, then you need to enable mpm_event manually and dissable mpm_worker. You can do it with these commands: "sudo a2dismod mpm_worker", "sudo a2enmod mpm_event")

3. Create apache virtual host config file (/etc/apache2/sites-available/site.conf):
<VirtualHost *:80>
 ServerName site.com
 DocumentRoot /var/www/site
 <FilesMatch \.php>
  SetHandler proxy:fcgi://127.0.0.1:9000
 </FilesMatch>
</VirtualHost>

4. Enable proxy_fcgi module and site.conf:
sudo a2enmod proxy_fcgi
sudo a2ensite site
sudo service apache2 restart

5. Create file structure of your site:
sudo mkdir -p /var/www/site
sudo touch index.php
and add the following code to your index.php file:
<?php
phpinfo();
?>
Using this file we will check that php code works, but you will need to remove that file later, because it's not secure to print that info on live website.

6. Create pool for php-fpm.
Navigate to php-fpm pool directory:
cd /etc/php/7.0/fpm/pool.d/
Use default template as a base for the pool for our new website. So copy it:
sudo cp www.conf site.conf
Then open site.conf and change two parameters:
- pool name: replace [www] => [site]
- listen:
listen = /run/php/php7.0-fpm.sock
change it to this:
;listen = /run/php/php7.0-fpm.sock
listen = 127.0.0.1:9000
So it means that I use tcp sockets instead of file sockets. This is just for simplicity. You can change it to whatever value later, once the basic setup is working.

7. Restart php-fpm:
sudo service php7.0-fpm restart

Now you can check your website, by opening it in browser:
http://site.com/index.php

So this is just a basic setup of apache event mpm and php-fpm. There is a whole lot of different parameters that you can adjust depending on your needs and server characteristics. You can also watch this video or drop a comment below.

No comments:

Post a Comment