User Tools

Site Tools


linux_server_manuals:apache_http_2_php-fpm

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux_server_manuals:apache_http_2_php-fpm [2018/01/13 15:17] adminlinux_server_manuals:apache_http_2_php-fpm [2021/08/18 14:46] (current) ronney
Line 1: Line 1:
-====== Apache with mpm_event, mod_proxy_fcgi, php-fpm and HTTP/2======+====== Apache Web Server with mpm_event, mod_proxy_fcgi, php-fpm and HTTP/2 ====== 
 + 
 +When installing a web server, there are certain chances that you will use webapplications which depend on PHP. Unfortunately php (7) is not thread safe. This means when using Apache, you'll have to use mpm_prefork, which is slow when there are a lot of conections, uses a lot of memory and only partially supports HTTP/2 (As it seems in newer versions of apache it's not supported at all). Therefore, to use mpm_event with php, we need to use mod_proxy_fcgi (otherwise, php will be run directly by apache, which is a problem with mpm_event, since it uses threads). The overhead of running php outside of apache seems to be negligible. HTTP/2 offers huge performance improvements for clients, so we want it! The manual is written for Debian Stretch (Debian Version 9).
  
-When installing a web server, there are certain chances that you will use webapplications which depend on PHP. 
-Unfortunately php (7) is not thread safe. This means when using Apache, you'll have to use mpm_prefork, which is slow when there are a lot of conections, uses a lot of memory and only partially supports HTTP/2 (As it seems in newer versions of apache, it doesn't support it at all). Therefore, to use mpm_event with php, we need to use mod_proxy_fcgi (otherwise, php will be run directly by apache, which is a problem with mpm_event, since it uses threads). 
 ===== Enable php-fpm, mod_proxy_fcgi and mpm_event ===== ===== Enable php-fpm, mod_proxy_fcgi and mpm_event =====
  
Line 8: Line 8:
  
 If previously default mod-php was installed, remove it: If previously default mod-php was installed, remove it:
 +
 <code> <code>
-aptitude purge libapache2-mod-php +aptitude purge libapache2-mod-php 
 </code> </code>
  
-Then install php-fpm,activate the proxy_fcgi module and enable mpm_event:+Then install php-fpm,activate the proxy_fcgi module, enable the php config which sets the proper php handler and enable mpm_event: 
 <code> <code>
 aptitude install php-fpm aptitude install php-fpm
-a2enmod proxy_fcgi +a2enmod proxy_fcgi 
 +a2enconf php7.0-fpm
 a2dismod mpm_prefork a2dismod mpm_prefork
 a2enmod mpm_event a2enmod mpm_event
 +
 </code> </code>
  
-We need to tell apache how to handle php files. We use the default unix sockets provided by php-fpm (if you can, always use unix sockets instead of network sockets, they've got less overhead). +Then restart apache
-So create the file /etc/apache2/mods-enabled/proxy_fcgi.conf +
- +
-<file> +
-<IfModule proxy_fcgi_module> +
-        <FilesMatch "\.php$"> +
-            <If "-f %{REQUEST_FILENAME}"> +
-                SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost/" +
-            </If> +
-        </FilesMatch> +
-</IfModule> +
-</file>+
  
-Then restart apache 
 <code> <code>
 systemctl restart apache2 systemctl restart apache2
 +
 </code> </code>
  
-For further information, see [[https://wiki.apache.org/httpd/PHP-FPM]] +For further information, see [[https://wiki.apache.org/httpd/PHP-FPM|https://wiki.apache.org/httpd/PHP-FPM]] especially about ulimit and future issues if too many connection are opened. Take a look at the caveeats! Don't allow document upload into document root! Instead, let the application put uploads for example into /var/www/<appdir> . 
-especially about ulimit and future issues if too many connection are opened. + 
-Take a look at the caveeats! Don't allow document upload into document root! Instead, let the application but uploads for example into /var/www/<appdir> .+==== Increase pm.max_children ==== 
 + 
 +PHP-fpm has a maximal amount of children processes which it uses to serve requests. The default setting of 5 most propably is too low for almost any kind of server. Check for similar messages in your log /var/log/php7.3-fpm.log<code> 
 + 
 +[05-Mar-2019 13:01:21] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 3 idle, and 40 total children 
 +[05-Mar-2019 13:01:22] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it 
 + 
 +</code> 
 + 
 +To change it edit /etc/php/7.3/fpm/. The following values are no recommendations in any way. I've just copied it from a server, with relatively low load. You have to consider the expected amount of requests, but also your server resources, especially the memory. 
 + 
 +<code> 
 +pm.max_children = 20 
 +pm.start_servers = 5 
 +pm.min_spare_servers = 1 
 +pm.max_spare_servers = 5 
 + 
 +</code> 
 + 
 +==== Increase PHP Execution Timeout ==== 
 + 
 +If you use php scripts which run for a long time (>30s) then you need to change several settings, otherwise the script will timeout and the connection aborted. 
 + 
 +To increase the php max execution time, change following line in the file /etc/php/7.3/fpm/php.ini 
 + 
 +<code> 
 +#Max time you want a script to be able to be running 
 +max_execution_time = 300 
 + 
 +</code> 
 + 
 +That alone won't work, you will still get following error (or similar) if the script runs longer than 30 seconds: 
 + 
 +<code> 
 +[proxy_fcgi:error] [pid 11716:tid 140000557524736] (70007)The timeout specified has expired: [client 186.96.123.18:45986] AH01075: Error dispatching request to : (polling) 
 + 
 +</code> 
 + 
 +To avoid that, edit the file /etc/apache2/apache2.conf 
 + 
 +<code> 
 +# Timeout for proxy_fcgi module 
 +# This line needs to be added 
 +ProxyTimeout 300 
 + 
 +# That line already exists and is the general apache timeout. 
 +# If you want to increase the timeout over 300, you also need to change that line 
 +Timeout 300 
 + 
 +</code>
  
 ===== Enable HTTP/2 support ===== ===== Enable HTTP/2 support =====
  
 Enable HTTP/2 modules: Enable HTTP/2 modules:
 +
 <code> <code>
 a2enmod http2 a2enmod http2
 +
 </code> </code>
  
-Create file /etc/apache2/mods-enalbed/http2.conf+Create file /etc/apache2/mods-enabled/http2.conf (This is not necessary anymore with Debian 10 or newer) 
 <file> <file>
 <IfModule http2_module> <IfModule http2_module>
Line 58: Line 104:
         H2Direct on         H2Direct on
 </IfModule> </IfModule>
 +
 </file> </file>
 +
 +===== Activate .htaccess files =====
 +
 +The default setup of the apache package will not use the .htaccess files (they are not very efficient). Most probably your php applications will use them, so to activate them, edit /etc/apache2/apache2.conf, and activate "allowOverride" in following config part:
 +
 +<code>
 +<Directory /var/www/>
 +    Options Indexes FollowSymLinks
 +    AllowOverride All
 +    Require all granted
 +</Directory>
 +
 +</code>
 +
 +
linux_server_manuals/apache_http_2_php-fpm.1515856631.txt.gz · Last modified: 2018/01/13 15:17 by admin