Starting from version 2.4 Apache offers 3 MPM (Multi-processing Module) we can choose from, depending on your needs.
- The
prefork MPM
uses multiple child processes without threading. Each process handles one connection at a time without creating separate threads for each. Without going into too much detail, we can say that you will want to use this MPM only when debugging an application that uses, or if your application needs to deal with, non-thread-safe modules like mod_php. - The
worker MPM
uses several threads per child processes, where each thread handles one connection at a time. This is a good choice for high-traffic servers as it allows more concurrent connections to be handled with less RAM than in the previous case. - Finally, the
event MPM
is the default MPM in most Apache installations for versions 2.4 and above. It is similar to the worker MPM in that it also creates multiple threads per child process but with an advantage: it causes KeepAlive or idle connections (while they remain in that state) to be handled by a single thread, thus freeing up memory that can be allocated to other threads. This MPM is not suitable for use with non-thread-safe modules like mod_php, for which a replacement such a PHP-FPM must be used instead.
To check the MPM used by your Apache installation, run:
1 |
httpd -V |
output
To change mod_mpm_prefork to mod_mpm_event, you will need to edit:
1 2 |
# /etc/httpd/conf.modules.d/00-mpm.conf [On RedHat/CentOS based systems] # /etc/apache2/mods-available/.load [On Debian/Ubuntu based systems] |
and uncomment the line that loads the desired module like so:
1 |
LoadModule mpm_event_module modules/mod_mpm_event.so |
Make sure to comment ...mod_mpm_prefork.so
line so only one MPM module will be loaded at the time.
Note: To make the event MPM work in Debian, you may have to install the libapache2-mod-fastcgi package from the non-free repositories.
Additionally, for CentOS you will need php-fpm (along with fcgi and mod_fcgid) whereas in Debian it’s called php5-fpm (along with apache2-mpm-event).
So, install the necessary packages.
For CentOS:
1 |
yum -y install php-fpm |
For Debian/Ubuntu:
1 |
sudo apt-get install php5-fpm |
In /etc/httpd/conf.d/php.conf
(CentOS) add a handler for .php files and comment an existing one:
1 2 3 4 |
<FilesMatch \.php$> #SetHandler application/x-httpd-php SetHandler "proxy:fcgi://127.0.0.1:9000" </FilesMatch> |
…driving php requests to fcgi pool.
Now we must tell fcgi where to look for site php files. In case you have only one site on your server you can place the following lines somewhere in global section of httpd.conf
or place it in /etc/httpd/conf.modules.d/10-php.conf
like I did:
1 2 3 4 |
#if only one vhost <IfModule mpm_event_module> ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1 </IfModule> |
If you have multiple vhosts configured, you should place the lines above in every respective vhost section and define the proper path to site’s files. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<VirtualHost _default_:443> ... ServerName www.example.com <IfModule mpm_event_module> ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/site1/$1 </IfModule> ... </VirtualHost> ... ServerName www.example2.com <VirtualHost _default_:443> ... <IfModule mpm_event_module> ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/site2/$1 </IfModule> ... </VirtualHost> |
Now restart the web server and the newly installed php-fpm (or php5-fpm) service.
On RedHat/CentOS:
1 |
# systemctl restart httpd php-fpm && systemctl enable httpd php-fpm |
On Debian/Ubuntu:
1 |
# systemctl restart apache2 php5-fpm && systemctl enable apache2 php5-fpm |
If you got an error:
Failed to start apache : Starting httpd: Syntax error on line 31 of /etc/httpd/conf.d/php.conf: Invalid command ‘php_value’, perhaps misspelled or defined by a module not included in the server configuration
open php.conf
and edit it as follows:
1 2 3 4 |
<IfModule mpm_prefork_module> php_value session.save_handler "files" php_value session.save_path "/var/lib/php/session" </IfModule> |
if you think you may need to back to mod_prefork
. Or just comment them or delete if you don’t think you will back to mod_prefork
.
Restart Apache and now check it with httpd -V
command. you should get something similar to this:
In CentOS 7, you will need to make sure that the http and https services are enabled through the firewall, and that the network interface(s) are properly added to the default zone.
1 2 3 4 5 6 7 8 |
# firewall-cmd --zone=internal --add-interface=tun6to4 # firewall-cmd --zone=internal --add-interface=tun6to4 --permanent # firewall-cmd --set-default-zone=internal # firewall-cmd --add-service=http # firewall-cmd --add-service=https # firewall-cmd --add-service=http --permanent # firewall-cmd --add-service=https --permanent # firewall-cmd --reload |
Good luck!