Apache

From Compilenix Wiki
Jump to: navigation, search

Apache on Debian with MPM-Worker, PHP-FPM, FastCGI and APC[1]

Installation

Install the required packages.

apt-get install apache2-mpm-worker libapache2-mod-fastcgi php5-fpm

If a dependency conflict like the following occur, ignoring it worked for me serval times...

[...]
dpkg: apache2-mpm-prefork: Abhängigkeitsprobleme, wird aber wie gefordert dennoch entfernt:
 apache2 hängt ab von apache2-mpm-worker (= 2.2.16-6+squeeze14) | apache2-mpm-prefork (= 2.2.16-6+squeeze14) | apache2-mpm-event (= 2.2.16-6+squeeze14) | apache2-mpm-itk (= 2.2.16-6+squeeze14); aber:
  Paket apache2-mpm-worker ist nicht installiert.
  Paket apache2-mpm-prefork soll entfernt werden.
  Paket apache2-mpm-event ist nicht installiert.
  Paket apache2-mpm-itk ist nicht installiert.
Entfernen von apache2-mpm-prefork ...
[...]
Entpacken von apache2-mpm-worker (aus .../apache2-mpm-worker_2.2.16-6+squeeze14_amd64.deb) ...
apache2-mpm-worker (2.2.16-6+squeeze14) wird eingerichtet ...
[...]

The Apache-Webserver should be restarted during the apt-get install [...] and probably will fail to start again.
Right above the failed! you find the cause -> go and fix your config(s).

Enable some apache modules, if not done jet.

a2enmod actions fastcgi

Now create a Web-Server-Wide handler for php-code.

/etc/apache2/conf.d/php-fpm

# Configure all that stuff needed for using PHP-FPM as FastCGI
 
# Set handlers for PHP files.
# application/x-httpd-php                        phtml pht php
# application/x-httpd-php3                       php3
# application/x-httpd-php4                       php4
# application/x-httpd-php5                       php
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>
# application/x-httpd-php-source                 phps
<FilesMatch ".+\.phps$">
    SetHandler application/x-httpd-php-source
    # Deny access to raw php sources by default
    # To re-enable it's recommended to enable access to the files
    # only in specific virtual host or directory
    Order Deny,Allow
    Deny from all
</FilesMatch>
 
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[345]?|t|tml|ps)$">
    Order Deny,Allow
    Deny from all
</FilesMatch>
 
# Define Action and Alias needed for FastCGI external server.
Action application/x-httpd-php /fcgi-bin/php5-fpm virtual
Alias /fcgi-bin/php5-fpm /fcgi-bin-php5-fpm

<Location /fcgi-bin/php5-fpm>
  # here we prevent direct access to this Location url,
  # env=REDIRECT_STATUS will let us use this fcgi-bin url
  # only after an internal redirect (by Action upper)
  Order Deny,Allow
  Deny from All
  Allow from env=REDIRECT_STATUS
</Location>

FastCgiExternalServer /fcgi-bin-php5-fpm -idle-timeout 250 -socket /var/run/php5-fpm.sock -pass-header Authorization

Create a new pool for PHP-FPM[2].

If you want to seperate Apache vHosts to respective php pools, for example if you have to set diffrent php_admin_value's for each/some vHosts; see #PHP-FPM-Pools.

/etc/php5/fpm/pool.d/fpm.conf

[www]
user = www
group = www
listen.owner = www
listen.group = www

listen = /var/run/php5-fpm.sock

pm = dynamic
pm.max_children = 60
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30

chdir = /

Example Apache vHost

This example will take advantage of the php-fpm service and utilizes the default pool defined right above ("www" in /etc/php5/fpm/pool.d/fpm.conf).

<VirtualHost *:80>
    ServerName example.org
    ServerAdmin webmaster@example.org
    DocumentRoot /var/www/example.org

    CustomLog ${APACHE_LOG_DIR}/access.example.org.log combined
    ErrorLog ${APACHE_LOG_DIR}/error.example.org.log
    LogLevel warn

    <Directory /var/www/example.org/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

Re-/Start services and mess around with the errors :)

service apache2 restart
service php5-fpm

PHP-FPM-Pools[3][4][5]

In this example i distinguish the php-fpm-pool ".conf" files (each fpm-pool) by Apache vHost. This step assumes that you alrady configured apache and php-fpm as described in #Installation.

First, create a additional PHP-FPM-Pool in:

/etc/php5/fpm/pool.d/MNEMONIC_DESCRIPTION.conf

[MNEMONIC_DESCRIPTION]
user = www
group = www
listen.owner = www
listen.group = www

listen = /var/run/php5-fpm-MNEMONIC_DESCRIPTION.sock

pm = dynamic
pm.max_children = 60
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30

chdir = /

#php_flag[display_errors] = off
#php_admin_flag[log_errors] = on
#php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
#php_admin_value[error_log] = /var/log/fpm-php.www.log
#php_admin_value[memory_limit] = 64M
#[...]

MNEMONIC_DESCRIPTION is a placeholder which should be replaced my anything meaningful. NOTE: the name of the pool and the path to the .sock MUST be unique, referring to all other php-fpm-pool's!

Re-/Start the php5-fpm service.

Next, if not already done, create the apache vHost like this:

<VirtualHost *:80>
    ServerName example.org
    ServerAdmin webmaster@example.org
    DocumentRoot /var/www/example.org

    Action application/x-httpd-php /fcgi-bin/php5-fpm-MNEMONIC_DESCRIPTION virtual # overwrite, if any, the default Action for "application/x-httpd-php" to the desired php pool
    Alias /fcgi-bin/php5-fpm-MNEMONIC_DESCRIPTION /fcgi-bin-php5-fpm-MNEMONIC_DESCRIPTION
    FastCgiExternalServer /fcgi-bin-php5-fpm-MNEMONIC_DESCRIPTION -socket /var/run/php5-fpm-MNEMONIC_DESCRIPTION.sock -pass-header Authorization

    CustomLog ${APACHE_LOG_DIR}/access.example.org.log combined
    ErrorLog ${APACHE_LOG_DIR}/error.example.org.log
    LogLevel warn

    <Directory /var/www/example.org/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

PHP-APC

This part is very simple.

apt-get install php5-apc
php5enmod apc

Edit /etc/php5/mods-available/apc.ini, and add your settings[6].

; configuration for php apc module
; priority=20
extension=apc.so
apc.enabled=1
apc.shm_size=512M
apc.num_files_hint=10000
apc.user_entries_hint=10000
apc.max_file_size=5M
apc.stat=0
apc.lazy_functions=1
apc.lazy_classes=1
apc.optimization=0
apc.shm_segments=1
apc.ttl=86400 # 24h
apc.user_ttl=7200 # 2h
apc.enable_cli=1
apc.cache_by_default=1
apc.include_once_override=1

Re-/Start services.

service apache2 restart & service php5-fpm &

If you want to check how well the APC-Cache[7] performes, get the apc.php from php.net: apc.php

References