How to Configure Apache and PHP for High Traffic Websites on Linux Server

Configure the Apache and PHP ,Almost each academic on a way to install the LAMP stack (Linux, , MysQL, PHP) will suggest which you use the integrated Apache module for processing PHP scripts. For instance, in Ubuntu you’ll permit this whilst you operate a command inclusive of sudo apt set up libapache2-mod-phpto installation a bundle. This would in turn force Apache to apply mpm_prefork. Every time a vacationer accesses your internet site, a new system can be released to address that connection. This works properly whilst site visitors is low.

But, it becomes a huge problem in case you get a sudden burst of visitors. For instance, a Reddit put up may additionally include your website, and if the publish receives popular, you can get heaps of site visitors in only some minutes.

Configure Apache and PHP ,In a nice case state of affairs, if Apache is able to deal with the site visitors burst, a few unlucky traffic may also should wait perhaps thirty to sixty seconds till the web page is loaded, that is absurd in nowadays’s international. to a worst case situation, the server will start to lag badly, and a few connections can be without a doubt dropped due to lack of sources. In this case visitors will see an errors in their browsers.

It’s no longer an amazing scenario either manner, since you probably lost the eye of masses or hundreds of interested readers, customers or fanatics.

Unfortunately, PHP is a pretty big aid hog either way. But, with mpm_event, Apache can take care of sudden bursts of site visitors in a much extra green way. It’s recommended, though, that your server have at the least 2GB of RAM and a couple of CPU cores, real or digital, or even greater if you anticipate to have intense site visitors spikes, such as greater than ten visitors in keeping with 2nd. If you’re the usage of a digital personal server, upload extra digital CPU cores in your state of affairs and SSD garage. RAM is secondary.

How MPM Event-FPM on Debian-Based Distributions

On Debian, Ubuntu or different distros from this circle of relatives virtually avoid putting in the “libapache2-mod-php” bundle. When you install Apache it uses MPM occasion with the aid of default. But upon installing the cited package, a script disables MPM occasion and allows MPM prefork. The Apache PHP module can simplest paintings (adequately) with mpm_prefork. Of path, without “libapache2-mod-php,” you haven’t any processor for PHP files. So you’ll use PHP-FPM as opposed to the PHP module protected in Apache. Here is how you would set up a LAMP stack on a fresh server. You can adapt the stairs according to your internet utility’s necessities.

First, log in as root. Then, install Apache.

apt update && apt install apache2

At this factor you could see that Apache does certainly deliver with MPM occasion enabled by means of default.

apachectl -V

Install PHP-FPM.

apt install php-fpm

You will see instructions on how to enable the PHP processor in Apache.

Enable FastCGI protocol.

a2enmod proxy_fcgi

Enable PHP-FPM default configuration for Apache.

a2enconf php7.0-fpm

Note- in future versions of Debian/Ubuntu, this command could change to something else, e.g. 

a2enconf php7.6-fpm
, because PHP-FPM would be a different version.

Restart Apache.

systemctl restart apache2

Install the rest of your requirements for your PHP application. Here’s an example:

apt install mariadb-server php-mysql

This would install a database server and the PHP MySQL module so that your PHP application can connect to a database.

How to Use Apache MPM Event and PHP-FPM on RedHat-Based Distributions

Configure Apache and PHP ,The different famous server distribution preference is RedHat, or CentOS. In the equal way as above, an instance of a smooth installation of Apache with MPM event enabled and PHP-FPM might be presented.

Log in as root and install Apache.

yum install httpd

Unlike Debian-based totally distros, right here you may see that Apache makes use of MPM prefork by default, at the least on the cutting-edge CentOS 7 to be had at the time of writing.

apachectl -V

To enable MPM event, you have to edit a configuration file.

sed -i '/mpm_prefork\.so$/s/^/#/' /etc/httpd/conf.modules.d/00-mpm.conf

This will add a 

 sign to comment (inactivate) the line 
LoadModule mpm_prefork_module modules/

Now uncomment (activate) the line 

#LoadModule mpm_event_module modules/
 by removing the preceding 
 sign with the next command.

sed -i '/mpm_event\.so$/s/^#//' /etc/httpd/conf.modules.d/00-mpm.conf

Start Apache and enable it to autostart at boot.

systemctl start httpd.service
systemctl enable httpd.service

Check if Apache now uses MPM event.

apachectl -V

Install PHP-FPM and FastCGI module.

yum install php-fpm mod_fcgid

Create “/etc/httpd/conf.d/php.conf” to instruct Apache on how to process PHP files. Copy ALL content below, and paste it all at once in the terminal, then press ENTER.

<<PASTE > /etc/httpd/conf.d/php.conf
# Redirect to local php-fpm if mod_php is not available
<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
# Enable http authorization headers
<IfModule setenvif_module>
SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
<FilesMatch ".+\.ph(p[3457]?|t|tml)$">
#SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost"
SetHandler "proxy:fcgi://"
<FilesMatch ".+\.phps$">
# 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
Require all denied
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[3457]?|t|tml|ps)$">
Require all denied

The credit for this great config goes to Debian. Other sources recommend a simple configuration file such as:

<FilesMatch \.php$>
SetHandler "proxy:fcgi://"

But that is at risk of some attacks, and if certain services fail, you could divulge PHP documents to the public, in flip potentially exposing saved passwords, code and sensitive statistics.

Restart Apache.

systemctl restart httpd.service

Start PHP-FPM and enable its autostart at boot.

systemctl start php-fpm.service
systemctl enable php-fpm.service


You now have an Apache server that scales a lot higher with site visitors. However, take into account that you’re using the default settings, as in what’s “quality” for the majority. If you really want to get the most out of your HTTP server, you have to read about diverse variables you can song. The proper values for those are exceedingly dependent on your server’s assets, expected site visitors and PHP application.