Introduction
Nginx is a lightweight and high-performance web server. PHP-FPM is a robust FastCGI Process Manager for PHP. This guide will help you set up multiple websites with Nginx and different PHP versions on a single Ubuntu 20.04 server.
For simplicity, this tutorial assumes you want to set up two websites, the first running with PHP 5.6 and the second running with PHP 8.0.
Prerequisites
- Deploy a new Ubuntu 20.04 server at Rcs.
- Follow Rcs's best practices guides to create a sudo user and update the Ubuntu server.
- Have two fully qualified domain names that point to your server's IP address, such as
site1.example.com
andsite2.example.com
.
1. Install Software Packages
Log in to the server as a non-root sudo user via SSH.
Install Nginx.
$ sudo apt -y install nginx
Add the
ppa:ondrej/php
repository, a long-time and community-trusted repository that offers both PHP 5.6, PHP 7.0 – 7.4, and PHP 8.0.$ sudo LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php
List available PHP 5.6 packages.
$ apt-cache search ^php5.6 | cut -f1 -d' '
Install the packages required by the first website. For example:
$ sudo apt -y install php5.6 php5.6-fpm php5.6-mysql
List available PHP 8.0 packages.
$ apt-cache search ^php8.0 | cut -f1 -d' '
Install the packages required by the second website. For example:
$ sudo apt -y install php8.0-cli php8.0-fpm php8.0-pgsql
You now have two different versions of PHP coexisting on your server. In command-line mode, the PHP 5.6 interpreter is php5.6
, and the PHP 8.0 interpreter is php8.0
.
Currently, php
is an alias of php8.0
. If you want to make php
an alias of php5.6
, run the following command and select the appropriate option.
$ sudo update-alternatives --config php
You can also install other PHP versions if needed.
2. Upload Source Code
For security, create a dedicated user for each website. Each user will own all the source code files of the respective website. In addition, the PHP-FPM processes for each website will also run under the corresponding user so the two websites will be completely independent of each other.
The First Website
Create a dedicated user named
site1
.$ sudo adduser site1
Create a new document root directory named
/var/www/site1
to store the source code.$ sudo mkdir /var/www/site1
Use your file transfer tool, such as
rsync
or FileZilla, to upload the source code of the first website to the/var/www/site1
directory.Create a PHP file, which displays the PHP information, to confirm the PHP version.
$ echo '<?php phpinfo();' | sudo tee /var/www/site1/info.php > /dev/null
Make
site1
the owner of all the source code files.$ sudo chown -R site1:site1 /var/www/site1
The Second Website
Create a dedicated user named
site2
.$ sudo adduser site2
Create a new document root directory named
/var/www/site2
to store the source code.$ sudo mkdir /var/www/site2
Upload the source code of the second website to the
/var/www/site2
directory.Create a PHP file, which displays the PHP information, to confirm the PHP version.
$ echo '<?php phpinfo();' | sudo tee /var/www/site2/info.php > /dev/null
Make
site2
the owner of all the source code files.$ sudo chown -R site2:site2 /var/www/site2
3. Configure PHP
Configure PHP 5.6 for the First Website
List all the time zones that your Ubuntu system supports. Use the Up / Down keys to move through the list, and press Q to exit.
$ timedatectl list-timezones
Copy 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_York
Edit the main PHP configuration file to tell PHP to use the new time zone.
$ sudo nano /etc/php/5.6/fpm/php.ini
Find the line
;date.timezone =
. Remove the semicolon and add your time zone. For example:date.timezone = America/New_York
Here are the common settings that you can change if needed:
max_execution_time memory_limit post_max_size upload_max_filesize
Save the configuration file and exit.
Create the PHP-FPM configuration file from the default one.
$ sudo cp /etc/php/5.6/fpm/pool.d/www.conf /etc/php/5.6/fpm/pool.d/site1.conf
Rename the default file to disable it and keep it as a backup.
$ sudo mv /etc/php/5.6/fpm/pool.d/www.conf /etc/php/5.6/fpm/pool.d/www.conf.default
Edit the PHP-FPM configuration file.
$ sudo nano /etc/php/5.6/fpm/pool.d/site1.conf
In the configuration file, any line starting with
;
is a comment.Search for the following settings, then:
- Replace
[www]
with[site1]
- Replace
user = www-data
withuser = site1
- Replace
group = www-data
withgroup = site1
(do not touch thelisten.group = www-data
setting)
Make sure the
listen = /run/php/php5.6-fpm.sock
setting does not start with;
. This setting makes PHP-FPM listen on a Unix socket specified by the/run/php/php5.6-fpm.sock
file.- Replace
Copy and paste the following settings to the end of the file.
catch_workers_output = yes php_flag[display_errors] = off php_admin_flag[log_errors] = on php_admin_value[error_log] = /var/log/php-fpm/site1/error.log php_admin_value[session.save_path] = /var/lib/php/sessions/site1
Those settings make PHP-FPM log error messages to the
/var/log/php-fpm/site1/error.log
file instead of displaying them to website users and store session data in the/var/lib/php/sessions/site1
directory.Save the configuration file and exit.
Create two directories to store PHP logs and session data.
$ sudo mkdir -p /var/log/php-fpm/site1 $ sudo mkdir -p /var/lib/php/sessions/site1
Update the ownership and permissions of the two directories so that only the PHP-FPM processes of the first website can access them.
$ sudo chown site1:site1 /var/log/php-fpm/site1 $ sudo chmod 700 /var/log/php-fpm/site1 $ sudo chown site1:site1 /var/lib/php/sessions/site1 $ sudo chmod 700 /var/lib/php/sessions/site1
Check the new configuration.
$ sudo php-fpm5.6 -t
Restart the PHP-FPM service for the changes to take effect.
$ sudo systemctl restart php5.6-fpm.service
Configure PHP 8.0 for the Second Website
Edit the main PHP configuration file to tell PHP to use your time zone.
$ sudo nano /etc/php/8.0/fpm/php.ini
Find the line
;date.timezone =
. Remove the semicolon and add your time zone. For example:date.timezone = America/New_York
Here are the common settings that you can change if needed:
max_execution_time memory_limit post_max_size upload_max_filesize
Save the configuration file and exit.
Create the PHP-FPM configuration file from the default one.
$ sudo cp /etc/php/8.0/fpm/pool.d/www.conf /etc/php/8.0/fpm/pool.d/site2.conf
Rename the default file to disable it and keep it as a backup.
$ sudo mv /etc/php/8.0/fpm/pool.d/www.conf /etc/php/8.0/fpm/pool.d/www.conf.default
Edit the PHP-FPM configuration file.
$ sudo nano /etc/php/8.0/fpm/pool.d/site2.conf
Search for the following settings, then:
- Replace
[www]
with[site2]
- Replace
user = www-data
withuser = site2
- Replace
group = www-data
withgroup = site2
(do not touch thelisten.group = www-data
setting)
Make sure the
listen = /run/php/php8.0-fpm.sock
setting does not start with;
. This setting makes PHP-FPM listen on a Unix socket specified by the/run/php/php8.0-fpm.sock
file.- Replace
Copy and paste the following settings to the end of the file.
catch_workers_output = yes php_flag[display_errors] = off php_admin_flag[log_errors] = on php_admin_value[error_log] = /var/log/php-fpm/site2/error.log php_admin_value[session.save_path] = /var/lib/php/sessions/site2
Those settings make PHP-FPM log error messages to the
/var/log/php-fpm/site2/error.log
file instead of displaying them to website users and store session data in the/var/lib/php/sessions/site2
directory.Save the configuration file and exit.
Create two directories to store PHP logs and session data.
$ sudo mkdir -p /var/log/php-fpm/site2 $ sudo mkdir -p /var/lib/php/sessions/site2
Update the ownership and permissions of the two directories so that only the PHP-FPM processes of the second website can access them.
$ sudo chown site2:site2 /var/log/php-fpm/site2 $ sudo chmod 700 /var/log/php-fpm/site2 $ sudo chown site2:site2 /var/lib/php/sessions/site2 $ sudo chmod 700 /var/lib/php/sessions/site2
Check the new configuration.
$ sudo php-fpm8.0 -t
Restart the PHP-FPM service for the changes to take effect.
$ sudo systemctl restart php8.0-fpm.service
4. Configure Nginx
Configure Nginx for the First Website
Create a configuration file for the first website.
$ sudo nano /etc/nginx/sites-available/site1-http.conf
Paste the following into the file:
server { listen 80; listen [::]:80; # Declare the domain name of the website server_name site1.example.com; # Declare the document root directory root /var/www/site1; index index.html index.php; # Pass PHP files to PHP-FPM listening on /run/php/php5.6-fpm.sock location ~ \.php$ { try_files $uri =404; # Mitigate https://httpoxy.org/ vulnerabilities fastcgi_param HTTP_PROXY ""; fastcgi_pass unix:/run/php/php5.6-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } }
The above configuration contains the most basic directives for a PHP website. You may add more directives to fit your website requirements.
Save the configuration file and exit.
Enable the new configuration.
$ sudo ln -s /etc/nginx/sites-available/site1-http.conf /etc/nginx/sites-enabled/site1-http.conf
Configure Nginx for the Second Website
Create a configuration file for the second website.
$ sudo nano /etc/nginx/sites-available/site2-http.conf
Paste the following into the file:
server { listen 80; listen [::]:80; # Declare the domain name of the website server_name site2.example.com; # Declare the document root directory root /var/www/site2; index index.html index.php; # Pass PHP files to PHP-FPM listening on /run/php/php8.0-fpm.sock location ~ \.php$ { try_files $uri =404; # Mitigate https://httpoxy.org/ vulnerabilities fastcgi_param HTTP_PROXY ""; fastcgi_pass unix:/run/php/php8.0-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } }
Save the configuration file and exit.
Enable the new configuration.
$ sudo ln -s /etc/nginx/sites-available/site2-http.conf /etc/nginx/sites-enabled/site2-http.conf
Add the
www-data
user to thesite1
andsite2
groups so that Nginx processes can access the source code of the two websites.$ sudo usermod -aG site1,site2 www-data
Check the new configuration.
$ sudo nginx -t
Reload the Nginx service for the changes to take effect.
$ sudo systemctl reload nginx.service
5. Verify the Setup
Restart the server.
$ sudo reboot
Wait 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.php
You should see the correct PHP version for each website.
For security, log in to the server as a non-root sudo user via SSH again, then delete the
info.php
files.$ sudo rm /var/www/site1/info.php $ sudo rm /var/www/site2/info.php
You now have two websites running with two different versions of PHP on the same server. You can set up as many websites as you want as long as your server has enough resources.