To set up multiple PHP versions with Nginx and PHP-FPM, you can follow these steps:
Install Nginx: Start by installing Nginx on your system. The process may vary depending on your operating system, so refer to the official documentation for your platform for instructions.
Install PHP-FPM: Install the different versions of PHP and PHP-FPM that you want to use. Again, the installation method depends on your operating system, so consult the relevant documentation for your platform.
Configure PHP-FPM: Each PHP version should have its own PHP-FPM configuration file. Locate the PHP-FPM configuration directory (e.g., /etc/php/7.4/fpm
for PHP 7.4) and create a new configuration file for each PHP version. Modify the listen
directive to use different ports for each version (e.g., 9000 for PHP 7.4, 9001 for PHP 7.3, etc.).
Configure Nginx: Open the Nginx configuration file (usually located at /etc/nginx/nginx.conf
or /etc/nginx/sites-available/default
) and add the following location block for each PHP version:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; # Use the appropriate PHP-FPM pool port for each version
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Make sure to update the fastcgi_pass
directive to point to the correct PHP-FPM pool port for each PHP version.
sudo nginx -t
If there are no errors, proceed to the next step. Otherwise, fix any issues before proceeding.
Restart Nginx and PHP-FPM: Restart both Nginx and PHP-FPM services for the changes to take effect. The specific commands to restart the services depend on your operating system and how you installed Nginx and PHP-FPM.
Test PHP version: Finally, you can test each PHP version individually by creating a simple PHP file with the phpinfo()
function and accessing it through your web browser. This will display the PHP version and configuration details, confirming that the correct PHP version is being used.
Repeat these steps for each PHP version you want to have available. Remember to use different ports for each PHP-FPM pool.