
As server hardware gets faster and more efficient, it's no longer uncommon to host multiple websites on Nginx web server. For better performance, process isolation, and security, consider using separate PHP FastCGI Process Manager (PHP-FPM) pools for each website. This article describes the basics of using separate PHP-FPM pools for multiple websites on Nginx with PHP 7.4, using a Vultr Ubuntu 20.04 LTS cloud server instance. This allows you to host multiple virtual domains using Nginx server blocks.
site1.example.com and site2.example.com, which point to the same IP address for the server.Nginx installs a default site which is redundant for this article. To remove the default site, SSH to the server as a non-root sudo user and run:
Each site needs to run as a different user for security purposes and isolation. To do that, create two user accounts and assign the www-data user to their respective groups. This allows the web server to interact with the users and vice-versa. Do not give the site1 and site2 users login privileges or associate any other information to the accounts. To create the users, run:
Create two directories and lock down the permissions to prepare the server for the two separate sites.
The Unix permissions are 770. Each user and the user's associated group has full permissions (7) on the directory, but the world has no (0) permission. This setting restricts the site1 user from seeing data for site2 and vice-versa.
Copy the default PHP-FPM pool as a template for the two new user pools:
Delete the unneeded default pool.
Each pool has an associated user and Unix socket. Edit the first configuration file:
Change four lines:
[www] to [site1].user = www-data to user = site1.group = www-data to group = site1.listen = /var/run/php/php7.4-fpm.sock to listen = /var/run/php/php7.4-site1-fpm.sock.Save the file and exit.
Edit the second configuration file:
Change four lines:
[www] to [site2].user = www-data to user = site2.group = www-data to group = site2.listen = /var/run/php/php7.4-fpm.sock to listen = /var/run/php/php7.4-site2-fpm.sock.Save the file and exit.
Restart the PHP-FPM daemon.
The daemon should restart without errors. Verify two separate PHP-FPM pools are running.
If the FPM service pools are correct they are visibly forked in the process list:
The actual process IDs may differ from the ones listed above.
The server needs two new sites to use the two PHP-FPM pools, one associated with each respective pool.
Create a configuration file for site1:
Paste the following into the file:
Save and exit the file. The important changes above are:
server_name - the fully qualified DNS name of the hostaccess_log - the location and name of the access logerror_log - the location and name of the error logroot - the location on the files on disk that the web server usesfastcgi_pass - the location of the PHP-FPM sock created by the poolMake a similar file for site2.
The contents of this file matches site1, except it has the information for site2:
Save and exit the file.
Link the files to the respective Nginx directories.
Restart Nginx.
This should start without error.
Add a test file in the root of each site to test the configuration and verify which user serves each web site.
Create an index file for site1.
Paste the following into the file:
Save and exit the file.
Create an index file for site2.
Paste the following into the file for site2:
Save and exit the file.
Open a browser and visit http://site1.example.com and http://site2.example.com.
Examine the PHP Information page for each site. In the PHP Variables section, the variable $_SERVER['USER'] should be site1 for site1.example.com, and site2 for site2.example.com.
Creating separate PHP-FPM pools for each website served on a single server gives the administrator stronger security, more defined boundaries, and makes it easier to troubleshoot problems associated with individual sites. It's also possible to individually tune site settings for performance. These settings are in the /etc/php/7.4/fpm/pool.d configuration files. The main setting that is often changed is the pm setting, which controls process creation, with settings like dynamic, static or ondemand. See the FastCGI Process Manager Configuration for more information.
0 Comments
Be the first to comment and share your perspective with the community.