Introduction
PHP-FPM is a robust FastCGI Process Manager for PHP that has some advanced features useful for heavy-loaded sites. PHP-FPM allows grouping processes into pools, each running under a separate user/group with its own settings. This guide explains how to use PHP-FPM pools to secure multiple websites with Apache on a single CentOS 7 server.
For simplicity, this guide assumes you want to set up two websites. But, you can set up as many websites as you want as long as your server has enough resources.
Prerequisites
- Deploy a new CentOS 7 server (with SELinux disabled) at Rcs.
- Create a non-root user with sudo privileges.
- Update the server.
- Configure the CentOS firewall with ports 80 and 22 open.
- Have two fully qualified domains, for example,
site1.example.comandsite2.example.com, that point to your server's IP address. - Log in to your server as the non-root user.
1. Install Apache
The version of Apache in the official CentOS repositories is 2.4.6, so it is pretty old. Instead, you will add the IUS repository to get a recent version of Apache with improvements and bug fixes.
Add the IUS repository.
$ sudo yum -y install https://repo.ius.io/ius-release-el7.rpmThe above command also adds the Extra Packages for Enterprise Linux (EPEL) repository because the IUS repository depends on it.
Install the main Apache package.
$ sudo yum -y install httpd24uEnable the Apache service so that it starts at boot time.
$ sudo systemctl enable httpd.serviceFor high performance and memory saving, switch the Multi-Processing Module (MPM) from
preforktoevent.Edit the MPM configuration file.
$ sudo nano /etc/httpd/conf.modules.d/00-mpm.confFind the
LoadModule mpm_prefork_module modules/mod_mpm_prefork.soline and put the#character at the beginning of it.Find the
LoadModule mpm_event_module modules/mod_mpm_event.soline and remove the#character at the beginning of it.
Save the configuration file and exit.
Start the Apache service.
$ sudo systemctl start httpd.service
2. Install PHP
The official CentOS 7 repositories only offer PHP 5.4, which has reached its End Of Life in September 2015. Therefore, you will add the Remi repository, a long-time and community-trusted repository that offers PHP from version 5.4 to the latest version.
You should install only supported PHP versions. This guide uses PHP 7.4, the most widely-used version at the time of writing. Make sure to replace the
74string in the code examples with the string that matches the PHP version of your choice, for example,73or80.
Add the Remi repository.
$ sudo yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpmList all available PHP 7.4 packages in the Remi repository.
$ yum --disablerepo="*" --enablerepo="remi" list available | grep '^php74\S\+'The result looks like this.
php74.x86_64 1.0-3.el7.remi remi php74-build.x86_64 1.0-3.el7.remi remi php74-php.x86_64 7.4.24-1.el7.remi remi php74-php-ast.x86_64 1.0.14-1.el7.remi remi php74-php-bcmath.x86_64 7.4.24-1.el7.remi remi php74-php-brotli.x86_64 0.13.1-1.el7.remi remi php74-php-channel-horde.noarch 1.0-2.el7.remi remi php74-php-cli.x86_64 7.4.24-1.el7.remi remi ...* The first column contains package names. Although the names include the
.x86_64suffix, you can omit it when installing.- The second column contains package versions.
- The last column contains the repository ids.
Install the PHP interpreter and the PHP-FPM extension.
$ sudo yum -y install php74-php-cli php74-php-fpmInstall other PHP extensions (from the above list) needed to run your two websites. For example:
$ sudo yum -y install php74-php-gd php74-php-intl php74-php-mbstring php74-php-mysqlnd php74-php-opcache php74-php-pecl-redis5 php74-php-tidy php74-php-xmlEnable the PHP-FPM service so that it starts at boot time.
$ sudo systemctl enable php74-php-fpm.serviceStart the service.
$ sudo systemctl start php74-php-fpm.serviceSet PHP 7.4 as the default PHP version on the system.
$ sudo ln -sf /opt/remi/php74/root/usr/bin/* /usr/bin/Confirm the default version is 7.4.
$ php -v
3. Configure PHP
Customize Common Settings For Both Websites
List all the time zones that your CentOS system supports. Use the Up / Down / Pgup / Pgdn keys to move through the list, and press Q to exit.
$ timedatectl list-timezonesCopy an appropriate time zone from the list, for example, America/New_York. Then update the operating system with that time zone.
$ sudo timedatectl set-timezone America/New_YorkEdit the main PHP configuration file to tell PHP to use the new time zone.
$ sudo nano /etc/opt/remi/php74/php.iniFind the line
;date.timezone =, then remove the;character and add your time zone. For example:date.timezone = America/New_YorkHere are the common settings that you can customize if needed:
max_execution_time memory_limit post_max_size upload_max_filesizeSave the configuration file and exit.
Configure the First Pool for the First Website
For security, create a dedicated user and a dedicated PHP-FPM pool for each website. Each user owns all website source code files and other PHP-FPM-related files, such as log and session files. Therefore, the two websites are completely independent of each other.
Create a dedicated user named
site1.$ sudo adduser site1Set a strong password for the user. You may need it when transferring the source code of your website.
$ sudo passwd site1Rename the default configuration file to disable it and keep it as a template.
$ sudo mv /etc/opt/remi/php74/php-fpm.d/www.conf /etc/opt/remi/php74/php-fpm.d/www.conf.defaultCreate a configuration file for the first pool from the template.
$ sudo cp /etc/opt/remi/php74/php-fpm.d/www.conf.default /etc/opt/remi/php74/php-fpm.d/site1.confEdit the file.
$ sudo nano /etc/opt/remi/php74/php-fpm.d/site1.confIn the file, any line starting with
;is a comment.Search for the following settings, then:
- Replace
[www]with[site1] - Replace
user = apachewithuser = site1 - Replace
group = apachewithgroup = site1 - Replace
slowlog = /var/opt/remi/php74/log/php-fpm/www-slow.logwithslowlog = /var/opt/remi/php74/log/php-fpm/site1/slow.log - Remove the
;character at the beginning of;catch_workers_output = yesand;php_flag[display_errors] = off. - Replace
php_admin_value[error_log] = /var/opt/remi/php74/log/php-fpm/www-error.logwithphp_admin_value[error_log] = /var/opt/remi/php74/log/php-fpm/site1/error.log - Replace
php_value[session.save_path] = /var/opt/remi/php74/lib/php/sessionwithphp_value[session.save_path] = /var/opt/remi/php74/lib/php/session/site1 - Replace
php_value[soap.wsdl_cache_dir] = /var/opt/remi/php74/lib/php/wsdlcachewithphp_value[soap.wsdl_cache_dir] = /var/opt/remi/php74/lib/php/wsdlcache/site1 - Make sure the
listen = 127.0.0.1:9000setting does not start with;because Apache will forward FastCGI requests to the first pool using the TCP socket address127.0.0.1:9000.
Those settings make PHP-FPM log error messages to the
/var/opt/remi/php74/log/php-fpm/site1/error.logfile instead of displaying them to website users, store session data in the/var/opt/remi/php74/lib/php/session/site1directory, and store WSDL cache in the/var/opt/remi/php74/lib/php/wsdlcache/site1directory.- Replace
Save the configuration file and exit.
Create three directories to store PHP logs, session data, and WSDL cache.
$ sudo mkdir -p /var/opt/remi/php74/log/php-fpm/site1 $ sudo mkdir -p /var/opt/remi/php74/lib/php/session/site1 $ sudo mkdir -p /var/opt/remi/php74/lib/php/wsdlcache/site1Update the ownership and permissions of the directories so that only the first pool's processes can access them.
$ sudo chown site1:site1 /var/opt/remi/php74/log/php-fpm/site1 $ sudo chmod 700 /var/opt/remi/php74/log/php-fpm/site1 $ sudo chown site1:site1 /var/opt/remi/php74/lib/php/session/site1 $ sudo chmod 700 /var/opt/remi/php74/lib/php/session/site1 $ sudo chown site1:site1 /var/opt/remi/php74/lib/php/wsdlcache/site1 $ sudo chmod 700 /var/opt/remi/php74/lib/php/wsdlcache/site1
Configure the Second Pool for the Second Website
The configuration of the second pool is similar to that of the first pool except for some minor changes.
Create a dedicated user named
site2.$ sudo adduser site2Set a strong password for the user.
$ sudo passwd site2Copy the configuration of the first pool.
$ sudo cp /etc/opt/remi/php74/php-fpm.d/site1.conf /etc/opt/remi/php74/php-fpm.d/site2.confUpdate the new file by replacing all occurrences of
site1withsite2.$ sudo sed -i 's|site1|site2|g' /etc/opt/remi/php74/php-fpm.d/site2.confChange the TCP socket address of the second pool to
127.0.0.1:9001.$ sudo sed -i 's|127.0.0.1:9000|127.0.0.1:9001|g' /etc/opt/remi/php74/php-fpm.d/site2.confCreate three directories to store PHP logs, session data, and WSDL cache.
$ sudo mkdir -p /var/opt/remi/php74/log/php-fpm/site2 $ sudo mkdir -p /var/opt/remi/php74/lib/php/session/site2 $ sudo mkdir -p /var/opt/remi/php74/lib/php/wsdlcache/site2Update the ownership and permissions of the directories so that only the second pool's processes can access them.
$ sudo chown site2:site2 /var/opt/remi/php74/log/php-fpm/site2 $ sudo chmod 700 /var/opt/remi/php74/log/php-fpm/site2 $ sudo chown site2:site2 /var/opt/remi/php74/lib/php/session/site2 $ sudo chmod 700 /var/opt/remi/php74/lib/php/session/site2 $ sudo chown site2:site2 /var/opt/remi/php74/lib/php/wsdlcache/site2 $ sudo chmod 700 /var/opt/remi/php74/lib/php/wsdlcache/site2Restart the PHP-FPM service for the changes to take effect.
$ sudo systemctl restart php74-php-fpm.service
4. Prepare Source Code
The First Website
Create a new document root directory named
/var/www/site1to store the source code.$ sudo mkdir /var/www/site1Make
site1the owner of the directory.$ sudo chown site1:site1 /var/www/site1Use your file transfer tool, such as
rsyncor FileZilla, to transfer the source code of the first website to the/var/www/site1directory.Create a PHP file that displays the PHP information to confirm that the PHP-FPM pool running this website is the first pool.
$ echo '<?php phpinfo();' | sudo tee /var/www/site1/info.php > /dev/nullMake sure
site1is the owner of all the source code files.$ sudo chown -R site1:site1 /var/www/site1
The Second Website
Create a new document root directory named
/var/www/site2to store the source code.$ sudo mkdir /var/www/site2Make
site2the owner of the directory.$ sudo chown site2:site2 /var/www/site2Transfer the source code of the second website to the
/var/www/site2directory.Create a PHP file to confirm the PHP-FPM pool running this website is the second pool.
$ echo '<?php phpinfo();' | sudo tee /var/www/site2/info.php > /dev/nullMake sure
site2is the owner of all the source code files.$ sudo chown -R site2:site2 /var/www/site2
5. Configure Apache
Configure the First Website
Create a configuration file for the first website.
$ sudo nano /etc/httpd/conf.d/site1.confPaste the following into the file:
<VirtualHost *:80> ServerName site1.example.com # For Apache 2.4.9 or higher # Using SetHandler avoids issues with using ProxyPassMatch in combination # with mod_rewrite or mod_autoindex <FilesMatch \.php$> SetHandler proxy:fcgi://127.0.0.1:9000 # for Unix sockets, Apache 2.4.10 or higher # SetHandler proxy:unix:/path/to/fpm.sock|fcgi://dummy </FilesMatch> DocumentRoot /var/www/site1 DirectoryIndex index.php index.html <Directory /var/www/site1> # enable the .htaccess rewrites AllowOverride All # disable autoindex Options -Indexes Require all granted </Directory> ErrorLog /var/log/httpd/site1_error.log CustomLog /var/log/httpd/site1_access.log combined </VirtualHost>The above configuration contains the most basic directives for running PHP scripts with PHP-FPM. You may add more directives to fit your website requirements.
Save the configuration file and exit.
Configure the Second Website
The configuration of the second website is similar to that of the first website except for some minor changes.
Copy the configuration of the first website.
$ sudo cp /etc/httpd/conf.d/site1.conf /etc/httpd/conf.d/site2.confUpdate the new file by replacing all occurrences of
site1withsite2.$ sudo sed -i 's|site1|site2|g' /etc/httpd/conf.d/site2.confChange the TCP socket address of the second pool to
127.0.0.1:9001.$ sudo sed -i 's|127.0.0.1:9000|127.0.0.1:9001|g' /etc/httpd/conf.d/site2.confSave the configuration file and exit.
Apply the New Configuration
Add the
apacheuser to thesite1andsite2groups so that Apache processes can access the source code of the two websites.$ sudo usermod -aG site1,site2 apacheCheck the new configuration. Make sure you see
Syntax OKin the output.$ sudo apachectl configtestRestart the Apache service for the changes to take effect.
$ sudo systemctl restart httpd.service
6. Verify the Setup
Restart the server.
$ sudo rebootWait a moment for the operating system to boot, then open the following URLs in your browser.
http://site1.example.com/info.php http://site2.example.com/info.phpChecking the Environment section on each website, you will see that the USER variable has a value of site1 for the first website and a value of site2 for the second.
For security, log in to the server as a non-root sudo user via SSH again, then delete the
info.phpfiles.$ sudo rm /var/www/site1/info.php $ sudo rm /var/www/site2/info.php
You can set up as many websites as you want as long as your server has enough resources.
More Information
- FastCGI Process Manager Configuration
- Apache MPM event
- Apache Module mod_proxy_fcgi
- High-performance PHP on Apache httpd 2.4.x using mod_proxy_fcgi and PHP-FPM.
- A sample configuration for the Symfony framework using mod_proxy_fcgi with Apache 2.4