Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Run Multiple Websites with Different PHP Versions in Nginx

How to Run Multiple Websites with Different PHP Versions in Nginx

Running multiple websites on a single Nginx server with different PHP versions is a common requirement for developers and system administrators. It allows you to host legacy applications alongside newer ones, each needing a specific PHP version to function correctly. This setup helps maintain compatibility, improves security, and streamlines management across diverse projects.

What You’ll Need

  • A Linux server with root or sudo access
  • Nginx installed and running
  • Multiple PHP versions installed (e.g., PHP 7.4, PHP 8.1)
  • PHP-FPM (FastCGI Process Manager) pools configured for each PHP version
  • Basic knowledge of Linux command line and Nginx configuration
  • Domain names or local host entries pointing to your server

Step-by-Step

  1. Install Multiple PHP Versions
    Use your package manager to install the PHP versions you need. For example, on Ubuntu/Debian:

    sudo apt update
    sudo apt install php7.4-fpm php8.1-fpm
    Ensure both PHP-FPM services are running:
    sudo systemctl status php7.4-fpm
    sudo systemctl status php8.1-fpm
  2. Verify PHP-FPM Pool Sockets or Ports
    Each PHP version runs its own PHP-FPM pool, usually listening on a Unix socket or TCP port. Check the pool configuration files located in /etc/php/7.4/fpm/pool.d/www.conf and /etc/php/8.1/fpm/pool.d/www.conf.
    Look for the listen directive. Example:

    • PHP 7.4: listen = /run/php/php7.4-fpm.sock
    • PHP 8.1: listen = /run/php/php8.1-fpm.sock

    If your pools use TCP ports instead (e.g., 127.0.0.1:9074), note those.

  3. Create Separate Nginx Server Blocks for Each Website
    In Nginx, each website typically has its own server block configuration file under /etc/nginx/sites-available/.
    For example, create example1.com and example2.com:

    sudo nano /etc/nginx/sites-available/example1.com
    sudo nano /etc/nginx/sites-available/example2.com
  4. Configure Each Server Block to Use the Correct PHP Version
    Inside each server block, configure the location ~ \.php$ directive to pass PHP processing to the right PHP-FPM socket or port.
    For example, for example1.com using PHP 7.4:

    server {
        listen 80;
        server_name example1.com www.example1.com;
        root /var/www/example1.com;
    
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }
    }

    And for example2.com using PHP 8.1:

    server {
        listen 80;
        server_name example2.com www.example2.com;
        root /var/www/example2.com;
    
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }
    }
  5. Enable the Server Blocks
    Create symbolic links in /etc/nginx/sites-enabled/:

    sudo ln -s /etc/nginx/sites-available/example1.com /etc/nginx/sites-enabled/
    sudo ln -s /etc/nginx/sites-available/example2.com /etc/nginx/sites-enabled/
  6. Test Nginx Configuration and Reload
    Always test your Nginx config before reloading:

    sudo nginx -t

    If syntax is OK, reload Nginx:

    sudo systemctl reload nginx
  7. Set Up Document Roots and Permissions
    Make sure the document roots exist and have the correct permissions:

    sudo mkdir -p /var/www/example1.com
    sudo mkdir -p /var/www/example2.com
    sudo chown -R www-data:www-data /var/www/example1.com /var/www/example2.com
    sudo chmod -R 755 /var/www/example1.com /var/www/example2.com
  8. Create a PHP Info File to Verify PHP Versions
    For each site, create an info.php file:

    echo "<?php phpinfo(); ?>" | sudo tee /var/www/example1.com/info.php
    echo "<?php phpinfo(); ?>" | sudo tee /var/www/example2.com/info.php

    Visit http://example1.com/info.php and http://example2.com/info.php in your browser to confirm the PHP versions served.

  9. Secure Your Setup
    After verifying, remove or restrict access to info.php files to avoid exposing sensitive information.

Common Mistakes

  • Using the same PHP-FPM socket for all sites, causing all sites to run the same PHP version.
  • Forgetting to reload or restart PHP-FPM services after changing configurations.
  • Incorrect file permissions preventing Nginx or PHP-FPM from reading files.
  • Not testing Nginx configuration before reloading, leading to downtime.
  • Misconfiguring fastcgi_pass directive with wrong socket or port.
  • Leaving debug files like info.php accessible publicly.

Tips

  • Consider using TCP ports for PHP-FPM pools if you prefer network sockets over Unix sockets, especially if using containers or remote PHP-FPM servers.
  • Use clearly named PHP-FPM pools if you need multiple pools per PHP version to isolate sites further.
  • Automate site setup with scripts or configuration management tools to reduce human error.
  • Keep PHP versions updated and only run legacy versions if absolutely necessary for security.
  • Use systemctl status phpX.Y-fpm to quickly check if PHP-FPM services are active.
  • Enable error logging in Nginx and PHP-FPM to quickly diagnose issues.

Troubleshooting

If your websites are not running the correct PHP version or showing errors, try the following steps:

  • Check Nginx error logs: Usually found in /var/log/nginx/error.log. Look for permission errors, socket connection failures, or syntax errors.
  • Verify PHP-FPM service status: Run sudo systemctl status php7.4-fpm and sudo systemctl status php8.1-fpm to ensure they are running.
  • Confirm socket or port availability: Use ss -l | grep php or netstat -l | grep php to check listening sockets.
  • Test PHP-FPM manually: Use curl --unix-socket /run/php/php7.4-fpm.sock http://localhost to test socket responsiveness.
  • Validate Nginx configuration: Run sudo nginx -t and fix reported issues.
  • Check file and directory permissions: Ensure Nginx user (usually www-data) can read site files and access PHP-FPM sockets.
  • Restart services: Sometimes restarting PHP-FPM and Nginx helps:
    sudo systemctl restart php7.4-fpm php8.1-fpm nginx
  • Verify DNS or hosts file: Make sure your domain names resolve to the server IP.

Frequently Asked Questions

Can I run more than two PHP versions on the same server?
Yes, you can install and configure as many PHP versions as your server resources allow, each with its own PHP-FPM pool and Nginx server block.
Is it better to use Unix sockets or TCP ports for PHP-FPM?
Unix sockets are slightly faster and more secure for local connections, but TCP ports offer flexibility for remote PHP-FPM or containerized setups.
How do I upgrade a specific PHP version without affecting others?
Use your package manager to upgrade the targeted PHP version and its PHP-FPM service independently. Test sites after upgrade to ensure compatibility.
What if PHP-FPM fails to start after configuration changes?
Check the PHP-FPM error logs (e.g., /var/log/php7.4-fpm.log), verify pool configs for syntax errors, and ensure no conflicting socket or port assignments.
Can I use PHP-FPM pools with different user permissions?
Yes, PHP-FPM pools can run under different users to enhance security and isolate sites or applications.
How do I ensure that PHP errors are logged properly?
Configure php.ini settings for error logging and check both PHP-FPM and Nginx logs. Enabling error_log helps track issues.

By following these steps and best practices, you can efficiently manage multiple websites running different PHP versions on a single Nginx server, ensuring compatibility and security for all your projects.

For additional use cases, such as SSL configuration or load balancing with multiple PHP versions, consider exploring advanced Nginx modules and PHP-FPM tuning options.

Related

How to Run Multiple Websites with Different PHP Versions in Nginx
Scroll to top