Knowledgebase

Multiple Websites with Different PHP Versions and Nginx on Ubuntu 20.04 Print

  • 0

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

1. Install Software Packages

  1. Log in to the server as a non-root sudo user via SSH.

  2. Install Nginx.

     $ sudo apt -y install nginx
  3. 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
  4. List available PHP 5.6 packages.

     $ apt-cache search ^php5.6 | cut -f1 -d' '
  5. Install the packages required by the first website. For example:

     $ sudo apt -y install php5.6 php5.6-fpm php5.6-mysql
  6. List available PHP 8.0 packages.

     $ apt-cache search ^php8.0 | cut -f1 -d' '
  7. 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

  1. Create a dedicated user named site1.

     $ sudo adduser site1
  2. Create a new document root directory named /var/www/site1 to store the source code.

     $ sudo mkdir /var/www/site1
  3. 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.

  4. 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
  5. Make site1 the owner of all the source code files.

     $ sudo chown -R site1:site1 /var/www/site1

The Second Website

  1. Create a dedicated user named site2.

     $ sudo adduser site2
  2. Create a new document root directory named /var/www/site2 to store the source code.

     $ sudo mkdir /var/www/site2
  3. Upload the source code of the second website to the /var/www/site2 directory.

  4. 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
  5. 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

  1. 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
  2. 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
  3. Edit the main PHP configuration file to tell PHP to use the new time zone.

     $ sudo nano /etc/php/5.6/fpm/php.ini
  4. Find the line ;date.timezone =. Remove the semicolon and add your time zone. For example:

     date.timezone = America/New_York
  5. Here are the common settings that you can change if needed:

     max_execution_time
     memory_limit
     post_max_size
     upload_max_filesize
  6. Save the configuration file and exit.

  7. 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
  8. 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
  9. 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.

  10. Search for the following settings, then:

    • Replace [www] with [site1]
    • Replace user = www-data with user = site1
    • Replace group = www-data with group = site1 (do not touch the listen.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.

  11. 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.

  12. Save the configuration file and exit.

  13. 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
  14. 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
  15. Check the new configuration.

     $ sudo php-fpm5.6 -t
  16. 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

  1. Edit the main PHP configuration file to tell PHP to use your time zone.

     $ sudo nano /etc/php/8.0/fpm/php.ini
  2. Find the line ;date.timezone =. Remove the semicolon and add your time zone. For example:

     date.timezone = America/New_York
  3. Here are the common settings that you can change if needed:

     max_execution_time
     memory_limit
     post_max_size
     upload_max_filesize
  4. Save the configuration file and exit.

  5. 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
  6. 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
  7. Edit the PHP-FPM configuration file.

     $ sudo nano /etc/php/8.0/fpm/pool.d/site2.conf
  8. Search for the following settings, then:

    • Replace [www] with [site2]
    • Replace user = www-data with user = site2
    • Replace group = www-data with group = site2 (do not touch the listen.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.

  9. 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.

  10. Save the configuration file and exit.

  11. 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
  12. 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
  13. Check the new configuration.

     $ sudo php-fpm8.0 -t
  14. 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

  1. Create a configuration file for the first website.

     $ sudo nano /etc/nginx/sites-available/site1-http.conf
  2. 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.

  3. Save the configuration file and exit.

  4. 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

  1. Create a configuration file for the second website.

     $ sudo nano /etc/nginx/sites-available/site2-http.conf
  2. 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;
         }
     }
  3. Save the configuration file and exit.

  4. Enable the new configuration.

     $ sudo ln -s /etc/nginx/sites-available/site2-http.conf /etc/nginx/sites-enabled/site2-http.conf
  5. Add the www-data user to the site1 and site2 groups so that Nginx processes can access the source code of the two websites.

     $ sudo usermod -aG site1,site2 www-data
  6. Check the new configuration.

     $ sudo nginx -t
  7. Reload the Nginx service for the changes to take effect.

     $ sudo systemctl reload nginx.service

5. Verify the Setup

  1. Restart the server.

     $ sudo reboot
  2. 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
  3. You should see the correct PHP version for each website.

  4. 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.

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 and site2.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 ' /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 ' /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 with user = site1 Replace group = www-data with group = site1 (do not touch the listen.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. 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 with user = site2 Replace group = www-data with group = site2 (do not touch the listen.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. 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 the site1 and site2 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.

Was this answer helpful?
Back

Powered by WHMCompleteSolution