Epidemiology & Technology

Apache PHP 7.4 WordPress Setup

Apache

sudo apt install  apache2 apachetop apache2-utils 
sudo a2enmod rewrite
sudo a2enmod deflate
sudo a2enmod expires
sudo a2enmod headers

sudo systemctl restart apache2
sudo systemctl status apache2

sudo usermod -a -G www-data vivekCode language: Bash (bash)

PHP 7.4

# FOR 18.04 - ADD the PPA
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/apache2
sudo apt-get update

sudo apt install php7.4-{cli,common,curl,intl,opcache,readline,mysql,json}
sudo apt install libapache2-mod-php7.4 
sudo a2enmod php7.4

## Essential PHP Extensions
sudo apt install php-xml
sudo apt install php-gd libgd-tools
sudo apt install php7.4-zip
sudo apt install php7.4-mbstring
sudo systemctl restart apache2

sudo journalctl -f -u apache2
apache2ctl -M

wget https://raw.githubusercontent.com/richardforth/apache2buddy/master/apache2buddy.pl
sudo perl apache2buddy.pl -O

# FPM
sudo apt install php7.4-fpm
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.4-fpm
sudo systemctl reload apache2
sudo journalctl -f -u apache2
a2query -m

## PHP settings
sudo cp /etc/php/7.4/apache2/php.ini   /etc/php/7.4/apache2/php.ini.orig
sudo sed -i "s/;max_input_vars = 1000/max_input_vars = 10000/" /etc/php/7.4/apache2/php.ini 
sudo cat /etc/php/7.4/apache2/php.ini  | grep max_input_vars
sudo cat /etc/php/7.4/apache2/php.ini  | grep upload_max_filesize
sudo cat /etc/php/7.4/apache2/php.ini  | grep post_max_size
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 15M/" /etc/php/7.4/apache2/php.ini 
sudo sed -i "s/post_max_size = .*/post_max_size = 50M/" /etc/php/7.4/apache2/php.ini 
sudo cat /etc/php/7.4/apache2/php.ini  | grep upload_max_filesize
sudo cat /etc/php/7.4/apache2/php.ini  | grep post_max_size
sudo sed -i "s/;session.cookie_secure =*/session.cookie_secure =1/" /etc/php/7.4/apache2/php.ini 

## PHP - FPM settings
sudo cp /etc/php/7.4/fpm/php.ini   /etc/php/7.4/fpm/php.ini.orig
sudo cat /etc/php/7.4/fpm/php.ini  | grep max_input_vars
sudo cat /etc/php/7.4/fpm/php.ini  | grep upload_max_filesize
sudo cat /etc/php/7.4/fpm/php.ini  | grep post_max_size
sudo sed -i "s/;max_input_vars = 1000/max_input_vars = 10000/" /etc/php/7.4/fpm/php.ini 
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 15M/" /etc/php/7.4/fpm/php.ini 
sudo sed -i "s/post_max_size = .*/post_max_size = 50M/" /etc/php/7.4/fpm/php.ini 
sudo cat /etc/php/7.4/fpm/php.ini  | grep max_input_vars
sudo cat /etc/php/7.4/fpm/php.ini  | grep upload_max_filesize
sudo cat /etc/php/7.4/fpm/php.ini  | grep post_max_size
sudo sed -i "s/;session.cookie_secure =*/session.cookie_secure =1/" /etc/php/7.4/fpm/php.ini 

sudo systemctl restart apache2
sudo systemctl restart php7.4-fpm
sudo systemctl status apache2
sudo systemctl status php7.4-fpm

Code language: Bash (bash)

WordPress

cd /tmp
curl -O https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
touch /tmp/wordpress/.htaccess
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
mkdir /tmp/wordpress/wp-content/upgrade
sudo cp -a /tmp/wordpress/. /var/www/html/
sudo chown -R www-data:www-data /var/www/html/

sudo find /var/www/html/ -type d -exec chmod 750 {} \;
sudo find /var/www/html/ -type f -exec chmod 640 {} \;Code language: JavaScript (javascript)

SALTs

curl -s https://api.wordpress.org/secret-key/1.1/salt/


define('AUTH_KEY',         'xxxxxxxxxxxxxxxxxx);
define('SECURE_AUTH_KEY',  'xxxxx);
define('LOGGED_IN_KEY',    'xxxxx');
define('NONCE_KEY',        'xxxxxx');
define('AUTH_SALT',        'xxxxxx');
define('SECURE_AUTH_SALT', 'xxxxx');
define('LOGGED_IN_SALT',   'xxxx');
define('NONCE_SALT',       'xxxxxxxx');
Code language: PHP (php)

SSL SITE

sudo sed -i "3 i \$_SERVER['HTTPS'] = 'on';"  /var/www/html/wp-config.phpCode language: JavaScript (javascript)

Database Connections

# WARNING - WILL NEED TO ESCAPE SPECIAL CHARACTERS IN USERNAME, PASSWORD ETC
sudo sed -i "s/define( 'DB_USER'.*/define( 'DB_USER', 'xxxxxxxxxxxx' );/"  /var/www/html/wp-config.php
sudo sed -i "s/define( 'DB_PASSWORD'.*/define( 'DB_PASSWORD', 'xxxxxxxxxxxxxx' );/"  /var/www/html/wp-config.php
sudo sed -i "s/define( 'DB_HOST'.*/define( 'DB_HOST', 'xxx.xxx.xxx.xxx' );/"  /var/www/html/wp-config.php
sudo sed -i "s/define( 'DB_CHARSET'.*/define( 'DB_CHARSET', 'utf8mb4' );/"  /var/www/html/wp-config.php

Code language: Bash (bash)

APACHE Site Config file

sudo nano /etc/apache2/sites-available/epidemtech.conf

<VirtualHost *:80>
    ServerName  epidemiology.tech
    ServerAlias epidemiology.tech
    ServerAdmin vivekgupta@epidem.tech
    DocumentRoot /var/www/html
    <Directory /var/www/html>
        Options FollowSymLinks
        AllowOverride all
        Order deny,allow
        Allow from all
        Require all granted
    </Directory>
   # PHP FPM Handler
   <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
   </FilesMatch>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


sudo a2dissite 000-default.conf 
sudo a2ensite epidemtech.conf Code language: Apache (apache)

CAUTION – BELOW ARE ADVANCED SETTINGS THAT MAY BREAK SITE

Better Apache using MPM_WORKER

Apache HTTP comes with three different MPM:

  • Pre-fork: Isolated, singular new process for each incoming connection: a safe way to run applications – DEFAULT
  • Worker: A parent process launches pool of child processes; each process is threaded (one connection); one process – several requests concurrently.
  • Event: enables the process to manage threads so that some threads are free to handle new incoming connections while others are kept bound to the live connections.

The MPM Event module a fast multi-processing module available on the Apache HTTP web server.

By combining the MPM WORKER or MPM EVENT in Apache HTTP with the PHP FastCGI Process Manager (PHP-FPM) a website can load faster and handle more concurrent connections while using fewer resources.

sudo a2dismod mpm_prefork
sudo a2enmod mpm_worker

sudo systemctl restart apache2
sudo systemctl status apache2


## Can tweak settings using 
sudo nano /etc/apache2/mods-available/mpm_worker.conf
<IfModule mpm_worker_module>
        ServerLimit              200
        StartServers             8  
        MinSpareThreads          50 
        MaxSpareThreads          100
        ThreadLimit              200
        ThreadsPerChild          60 
        MaxRequestWorkers        500
        MaxConnectionsPerChild   200
</IfModule>
Code language: Bash (bash)

PHP-FPM Tweaking

sudo nano /etc/php/7.4/fpm/pool.d/www.conf

[www]
user = www-data
group = www-data
listen = /run/php/php7.2-fpm.sock
listen.owner = www-data
listen.group = www-data

pm = dynamic
pm.max_children = 80
pm.start_servers = 12
pm.min_spare_servers = 10
pm.max_spare_servers = 14
pm.process_idle_timeout = 60s
pm.max_requests = 100


sudo systemctl restart php7.4-fpm.service
sudo systemctl status php7.4-fpm.service
Code language: JavaScript (javascript)

Load test and Monitor

ab -c 90 -t 60 'https://epidemiology.tech/'
watch -n 1 'ps wwaux | head -n 1 ; ps wwaux | grep apache | grep -v grep ; echo ; ps wwaux | grep php | grep -v grep ; echo ; free -m'Code language: JavaScript (javascript)

Related posts