User Tools

Site Tools


linux_server_manuals:apache_http_2_php-fpm

This is an old revision of the document!


Apache 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 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). The overhead of running php outside of apache seems to be negligible.
HTTP/2 offers huge performance improvements for clients, so we want it!

Enable php-fpm, mod_proxy_fcgi and mpm_event

So let's go:

If previously default mod-php was installed, remove it:

aptitude purge libapache2-mod-php 

Then install php-fpm,activate the proxy_fcgi module and enable mpm_event:

aptitude install php-fpm
a2enmod proxy_fcgi 
a2dismod mpm_prefork
a2enmod mpm_event

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).
So create the file /etc/apache2/mods-enabled/proxy_fcgi.conf

<IfModule proxy_fcgi_module>
        <FilesMatch "\.php$">
            <If "-f %{REQUEST_FILENAME}">
                SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost/"
            </If>
        </FilesMatch>
</IfModule>

Then restart apache

systemctl restart apache2

For further information, see 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 but uploads for example into /var/www/<appdir> .

Enable HTTP/2 support

Enable HTTP/2 modules:

a2enmod http2

Create file /etc/apache2/mods-enalbed/http2.conf

<IfModule http2_module>
        #The preference order of protocols.
        # h2c is http/2 without encryption.
        Protocols h2 h2c http/1.1
        #maybe can make initiazion of http/2 connection faster. Not a necessary parameter
        H2Direct on
</IfModule>
You could leave a comment if you were logged in.
linux_server_manuals/apache_http_2_php-fpm.1515856901.txt.gz · Last modified: 2018/01/13 15:21 by admin