Apache with php-fpm: Difference between revisions

From Alpine Linux
m (Added category: PHP)
(Previous wiki was not accurate nor current)
Line 1: Line 1:
===Apache and php===
===Apache and PHP===
There are 4 different ways to use php with apache
PHP-FPM is the Fast Process Manager for PHP which runs as a service that spawns PHP processes as needed when files files are requested through the FastCGI interface. Passing this off to the PHP-FPM results in interpreting the PHP files much faster than having Apache spawn new processes each time a page is requested. You can read more about it here [https://php-fpm.org/] .
* cgi
* fcgi
* mod_php
* php-fpm
Here is a comparison why [http://php-fpm.org/about fpm] is the better way. In short it works with apache event mpm, has better security, can have per vhost pool configuration, better process management, ....
When used with apache event mpm, it's the most memory efficient setup that you can achieve with apache + php. Whether it's better than nginx + php-fpm depends on your setup, contents and preference.


===Step by step (apache2 event mpm + php+fpm) ===
===Step by Step Instructions===
'''install apache2 and php5-fpm'''
'''Install packages '''
{{Cmd|apk add apache2-proxy php5-fpm}}
{{Cmd|apk add apache2-proxy php8-fpm}}


'''configure apache2'''
'''Run and Add Services to Startup'''
{{Cmd|rc-service php-fpm8 start
rc-update add php-fpm8
rc-service apache2 start
rc-update add apache2}}


in /etc/apache2/httpd.conf
'''Configure Apache'''


Comment line:
Add the following lines to /etc/apache2/httpd.conf
  LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
  <FilesMatch \.php$>
and uncomment line:
    SetHandler "proxy:fcgi://127.0.0.1:9000"
  #LoadModule mpm_event_module modules/mod_mpm_event.so
  </FilesMatch>```


''This step is not needed any more''
'''Configure PHP-FPM'''


copy the mpm config, and adapt it to suit you needs.
You can edit the file /etc/php8/php-fpm.conf to suit you needs.
{{Cmd|cp /etc/apache2/original/extra/httpd-mpm.conf /etc/apache2/conf.d/}}


put the following in /etc/apache2/conf.d/slotmem_shm.conf
In the configuration you may need to change the default user and group from nobody to another user such as apache:
  LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
user = apache
  group = apache


put these lines in your httpd.conf or vhost files, don't forget to change the path
'''Restart apache2 and PHP-FPM'''
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/localhost/htdocs/$1                                         
DirectoryIndex index.php


'''tune php-fpm'''
After editing the configuration you must reload related services.
 
{{Cmd|rc-service php-fpm8 reload && rc-service php-fpm8 reload}}
edit the file /etc/php/php-fpm.conf to suit you needs
 
'''(re)start apache2 and php-fpm'''
{{Cmd|/etc/init.d/apache2 restart && /etc/init.d/php-fpm restart }}


===More info===
===More info===

Revision as of 18:40, 2 December 2021

Apache and PHP

PHP-FPM is the Fast Process Manager for PHP which runs as a service that spawns PHP processes as needed when files files are requested through the FastCGI interface. Passing this off to the PHP-FPM results in interpreting the PHP files much faster than having Apache spawn new processes each time a page is requested. You can read more about it here [1] .

Step by Step Instructions

Install packages

apk add apache2-proxy php8-fpm

Run and Add Services to Startup

rc-service php-fpm8 start rc-update add php-fpm8 rc-service apache2 start rc-update add apache2

Configure Apache

Add the following lines to /etc/apache2/httpd.conf

<FilesMatch \.php$>
   SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>```

Configure PHP-FPM

You can edit the file /etc/php8/php-fpm.conf to suit you needs.

In the configuration you may need to change the default user and group from nobody to another user such as apache:

user = apache
group = apache

Restart apache2 and PHP-FPM

After editing the configuration you must reload related services.

rc-service php-fpm8 reload && rc-service php-fpm8 reload

More info

apache wiki php-fpm manual