Apache2 using mpm_event

Install and tweak Apache2 & PHP7.1

Install Apache2 and choose the preferred Muti-Processing-Modul (MPM) mpm_event_module (documentation) as your worker. Run-time configuration directives are identical to those provided by worker, with the only addition of the AsyncRequestWorkerFactor.

sudo -s
apt install apache2

Install mod-fastcgi


» Optional:
Install PHP 7.1 if not already done:
(1) apt install language-pack-en-base -y && sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php -y
(2) apt update && apt install php7.1-fpm php7.1-gd php7.1-mysql php7.1-curl php7.1-xml php7.1-zip php7.1-intl php7.1-mcrypt php7.1-mbstring php-apcu php-imagick php7.1-json php7.1-bz2 php7.1-zip -y
(3) apt install libapache2-mod-php7.1 -y
« Optional


apt install libapache2-mod-fastcgi

Modify the mpm_event configuration file:

vi /etc/apache2/mods-available/mpm_event.conf

Change at least the “MaxConnectionsPerChild” value to 1000:

#### optional:
StartServers 2
###
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 1000

StartServers-Parameter is optional but recommended for oDroidC2.

Create the apache configuration:

vi /etc/apache2/conf-available/php7.1-fpm.conf

<IfModule mod_fastcgi.c>
 AddHandler php.fcgi .php
 Action php.fcgi /php.fcgi
 Alias /php.fcgi /usr/lib/cgi-bin/php.fcgi
 FastCgiExternalServer /usr/lib/cgi-bin/php.fcgi -socket /run/php/php7.1-fpm.sock -pass-header Authorization -idle-timeout 3600
 <Directory /usr/lib/cgi-bin>
 Require all granted
 </Directory>
</IfModule>

Enable the new created configuration and mods:

a2enconf php7.1-fpm  && a2dismod mpm_prefork && a2enmod actions mpm_even

Restart all services:

service php7.1-fpm restart && service apache2 stop && service apache2 start

Create the PHPInfo (info.php):

chown -R www-data:www-data /var/www && sudo -u www-data vi /var/www/html/info.php

Paste the following code:

<?php
phpinfo();
?>

Call the PHPinfo in your browser:

http://<ip-adress>/info.php

You wil find “Server API   FPM/FastCGI”:

Apache2 is now configured for FPM/FastCGI properly.

Check PHP as well.

apachectl -V
Server version: Apache/2.4.18 (Ubuntu)
Server built: 2017-09-18T15:09:02
Server's Module Magic Number: 20120211:52
Server loaded: APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture: 64-bit
Server MPM: event
 threaded: yes (fixed thread count)
 forked: yes (variable process count)
Server compiled with....

PHP 7.1 is configured properly as well. Finally have a look at all the enabled Apache2-Modules:

apachectl -M

I strongly recommend to disable Apaches module “status” for security reasons as long as you won’t need it in particular:

a2dismod status

Configure and tweak PHP:

cp /etc/php/7.1/fpm/pool.d/www.conf /etc/php/7.1/fpm/pool.d/www.conf.bak 
cp /etc/php/7.1/cli/php.ini /etc/php/7.1/cli/php.ini.bak
cp /etc/php/7.1/fpm/php.ini /etc/php/7.1/fpm/php.ini.bak
cp /etc/php/7.1/apache2/php.ini /etc/php/7.1/apache2/php.ini.bak
cp /etc/php/7.1/fpm/php-fpm.conf /etc/php/7.1/fpm/php-fpm.conf.bak
sed -i "s/;env\[HOSTNAME\] = /env[HOSTNAME] = /" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/;env\[TMP\] = /env[TMP] = /" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/;env\[TMPDIR\] = /env[TMPDIR] = /" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/;env\[TEMP\] = /env[TEMP] = /" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/;env\[PATH\] = /env[PATH] = /" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/pm.max_children = .*/pm.max_children = 240/" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/pm.start_servers = .*/pm.start_servers = 20/" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/pm.min_spare_servers = .*/pm.min_spare_servers = 10/" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/pm.max_spare_servers = .*/pm.max_spare_servers = 20/" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/;pm.max_requests = 500/pm.max_requests = 500/" /etc/php/7.1/fpm/pool.d/www.conf
sed -i "s/output_buffering =.*/output_buffering = Off/" /etc/php/7.1/cli/php.ini
sed -i "s/max_execution_time =.*/max_execution_time = 1800/" /etc/php/7.1/cli/php.ini
sed -i "s/max_input_time =.*/max_input_time = 3600/" /etc/php/7.1/cli/php.ini
sed -i "s/post_max_size =.*/post_max_size = 10240M/" /etc/php/7.1/cli/php.ini
sed -i "s/;upload_tmp_dir =.*/upload_tmp_dir = \/upload_tmp/" /etc/php/7.1/cli/php.ini
sed -i "s/upload_max_filesize =.*/upload_max_filesize = 10240M/" /etc/php/7.1/cli/php.ini
sed -i "s/max_file_uploads =.*/max_file_uploads = 100/" /etc/php/7.1/cli/php.ini
sed -i "s/;date.timezone.*/date.timezone = Europe\/\Berlin/" /etc/php/7.1/cli/php.ini
sed -i "s/;session.cookie_secure.*/session.cookie_secure = True/" /etc/php/7.1/cli/php.ini
sed -i '$aapc.enable_cli = 1' /etc/php/7.1/cli/php.ini
sed -i "s/output_buffering =.*/output_buffering = Off/" /etc/php/7.1/fpm/php.ini
sed -i "s/max_execution_time =.*/max_execution_time = 1800/" /etc/php/7.1/fpm/php.ini
sed -i "s/max_input_time =.*/max_input_time = 3600/" /etc/php/7.1/fpm/php.ini
sed -i "s/post_max_size =.*/post_max_size = 10240M/" /etc/php/7.1/fpm/php.ini
sed -i "s/;upload_tmp_dir =.*/upload_tmp_dir = \/upload_tmp/" /etc/php/7.1/fpm/php.ini
sed -i "s/upload_max_filesize =.*/upload_max_filesize = 10240M/" /etc/php/7.1/fpm/php.ini
sed -i "s/max_file_uploads =.*/max_file_uploads = 100/" /etc/php/7.1/fpm/php.ini
sed -i "s/;date.timezone.*/date.timezone = Europe\/\Berlin/" /etc/php/7.1/fpm/php.ini
sed -i "s/;session.cookie_secure.*/session.cookie_secure = True/" /etc/php/7.1/fpm/php.ini
sed -i "s/;opcache.enable=.*/opcache.enable=1/" /etc/php/7.1/fpm/php.ini
sed -i "s/;opcache.enable_cli=.*/opcache.enable_cli=1/" /etc/php/7.1/fpm/php.ini
sed -i "s/;opcache.memory_consumption=.*/opcache.memory_consumption=128/" /etc/php/7.1/fpm/php.ini
sed -i "s/;opcache.interned_strings_buffer=.*/opcache.interned_strings_buffer=8/" /etc/php/7.1/fpm/php.ini
sed -i "s/;opcache.max_accelerated_files=.*/opcache.max_accelerated_files=10000/" /etc/php/7.1/fpm/php.ini
sed -i "s/;opcache.revalidate_freq=.*/opcache.revalidate_freq=1/" /etc/php/7.1/fpm/php.ini
sed -i "s/;opcache.save_comments=.*/opcache.save_comments=1/" /etc/php/7.1/fpm/php.ini
sed -i "s/;emergency_restart_threshold =.*/emergency_restart_threshold = 10/" /etc/php/7.1/fpm/php-fpm.conf
sed -i "s/;emergency_restart_interval =.*/emergency_restart_interval = 1m/" /etc/php/7.1/fpm/php-fpm.conf
sed -i "s/;process_control_timeout =.*/process_control_timeout = 10s/" /etc/php/7.1/fpm/php-fpm.conf
sed -i "s/output_buffering =.*/output_buffering = Off/" /etc/php/7.1/apache2/php.ini
sed -i "s/max_execution_time =.*/max_execution_time = 1800/" /etc/php/7.1/apache2/php.ini
sed -i "s/max_input_time =.*/max_input_time = 3600/" /etc/php/7.1/apache2/php.ini
sed -i "s/post_max_size =.*/post_max_size = 10240M/" /etc/php/7.1/apache2/php.ini
sed -i "s/;upload_tmp_dir =.*/upload_tmp_dir = \/upload_tmp/" /etc/php/7.1/apache2/php.ini
sed -i "s/upload_max_filesize =.*/upload_max_filesize = 10240M/" /etc/php/7.1/apache2/php.ini
sed -i "s/max_file_uploads =.*/max_file_uploads = 100/" /etc/php/7.1/apache2/php.ini
sed -i "s/;date.timezone.*/date.timezone = Europe\/\Berlin/" /etc/php/7.1/apache2/php.ini
sed -i "s/;session.cookie_secure.*/session.cookie_secure = True/" /etc/php/7.1/apache2/php.ini
sed -i "s/;opcache.enable=.*/opcache.enable=1/" /etc/php/7.1/apache2/php.ini
sed -i "s/;opcache.enable_cli=.*/opcache.enable_cli=1/" /etc/php/7.1/apache2/php.ini
sed -i "s/;opcache.memory_consumption=.*/opcache.memory_consumption=128/" /etc/php/7.1/apache2/php.ini
sed -i "s/;opcache.interned_strings_buffer=.*/opcache.interned_strings_buffer=8/" /etc/php/7.1/apache2/php.ini
sed -i "s/;opcache.max_accelerated_files=.*/opcache.max_accelerated_files=10000/" /etc/php/7.1/apache2/php.ini
sed -i "s/;opcache.revalidate_freq=.*/opcache.revalidate_freq=1/" /etc/php/7.1/apache2/php.ini
sed -i "s/;opcache.save_comments=.*/opcache.save_comments=1/" /etc/php/7.1/apache2/php.ini 
sed -i "s/memory_limit = 128M/memory_limit = 512M/" /etc/php/7.1/apache2/php.ini

Restart PHP and Apache2

service php7.1-fpm restart && service apache2 restart

Future prospect (Nextcloud)

If you plan to install Nextcloud and want to use “pretty urls for Nextcloud“, don’t forget to set IgnoreFrontController’ => true at the end of the following procedure:

sudo -u www-data vi /var/www/nextcloud/config/config.php

Paste the following rows to Nextclouds config.php:

'overwrite.cli.url' => 'https://YOUR.DEDYN.IO',
'htaccess.RewriteBase' => '/',

Then update the .htaccess to obtain pretty urls using this command:

sudo -u www-data php /var/www/nextcloud/occ maintenance:update:htaccess

Finally add the “IgnoreFrontController” statement to Nextclouds config.php:

sudo -u www-data vi /var/www/nextcloud/config/config.php

Add this row:

'htaccess.IgnoreFrontController' => true,

Nextcloud urls won’t contain the index.php-string anymore.


Carsten Rieger