Apache with php-fpm: Difference between revisions

From Alpine Linux
m (Edited from Server to new subcategory: Web Server)
m (→‎See Also: Changed a link to https)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
===Apache and php===
{{Merge|Apache|Everything in one place seems logical}}
There are 4 different ways to use php with apache
* 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) ===
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 [https://php-fpm.org/ here].
'''install apache2 and php5-fpm'''
{{Cmd|apk add apache2-proxy php5-fpm}}


'''configure apache2'''
== Instructions ==


in /etc/apache2/httpd.conf
=== Installation ===


Comment line:
Install required packages
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
and uncomment line:
#LoadModule mpm_event_module modules/mod_mpm_event.so


''This step is not needed any more''
{{Cmd|# apk add apache2-proxy php8-fpm}}


copy the mpm config, and adapt it to suit you needs.
Run and add services to startup
{{Cmd|cp /etc/apache2/original/extra/httpd-mpm.conf /etc/apache2/conf.d/}}


put the following in /etc/apache2/conf.d/slotmem_shm.conf
{{Cmd|# rc-service php-fpm8 start
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
# rc-update add php-fpm8
# rc-service apache2 start
# rc-update add apache2}}


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


'''tune php-fpm'''
=== Configure ===


edit the file /etc/php/php-fpm.conf to suit you needs
==== Configure Apache ====


'''(re)start apache2 and php-fpm'''
Uncomment the mpm_event module and comment the mpm_prefork module like so:
{{Cmd|/etc/init.d/apache2 restart && /etc/init.d/php-fpm restart }}
 
  LoadModule mpm_event_module modules/mod_mpm_event.so
  #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
 
Add the following lines to {{Path|/etc/apache2/httpd.conf}}:
 
<FilesMatch \.php$>
    SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
 
Comment out the following lines in {{Path|/etc/apache2/conf.d/php8-module.conf}}:
 
  #LoadModule php_module modules/mod_php8.so
 
  #DirectoryIndex index.php index.html
 
  #<FilesMatch \.php$>
  #    SetHandler application/x-httpd-php
  #</FilesMatch>
 
 
==== Configure PHP-FPM ====
 
Edit the file {{Path|/etc/php8/php-fpm.conf}} to suit your needs.
 
In the configuration you may need to change the default user and group from nobody to another user such as <code>apache</code>:
 
user = apache
group = apache
 
Restart apache2 and PHP-FPM after editing the configuration:
 
{{Cmd|# rc-service php-fpm8 reload && rc-service apache2 reload}}
 
 
== See Also ==
 
* [[Apache]]
* [https://wiki.apache.org/httpd/PHP-FPM Apache Wiki entry on PHP-FPM]
* [https://php.net/manual/en/install.fpm.configuration.php PHP-FPM Manual]


===More info===
[https://wiki.apache.org/httpd/PHP-FPM apache wiki]
[http://php.net/manual/en/install.fpm.configuration.php php-fpm manual]
[[Category:Web Server]]
[[Category:Web Server]]
[[Category:PHP]]

Latest revision as of 20:40, 26 July 2023

This material is proposed for merging ...

It should be merged with Apache. Everything in one place seems logical (Discuss)

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.

Instructions

Installation

Install required 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

Configure Apache

Uncomment the mpm_event module and comment the mpm_prefork module like so:

 LoadModule mpm_event_module modules/mod_mpm_event.so
 #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

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

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

Comment out the following lines in /etc/apache2/conf.d/php8-module.conf:

 #LoadModule php_module modules/mod_php8.so
 
 #DirectoryIndex index.php index.html
 
 #<FilesMatch \.php$>
 #    SetHandler application/x-httpd-php
 #</FilesMatch>


Configure PHP-FPM

Edit the file /etc/php8/php-fpm.conf to suit your 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:

# rc-service php-fpm8 reload && rc-service apache2 reload


See Also