Tuesday, April 2, 2024

How to Install Multiple PHP Version with NGINX and PHP FPM

 Whether you're a developer or a system administrator, having the flexibility to run different PHP versions on your Ubuntu server is invaluable. Whether you need to test applications across various PHP environments or manage multiple projects with distinct PHP requirements, this step-by-step guide will help you install and configure multiple PHP versions with Nginx on Ubuntu effortlessly.



Overview

  • Update Ubuntu Packages: Ensure you have the latest package versions available.
  • Install Nginx: Install Nginx, the web server capable of handling multiple PHP versions through FastCGI.
  • Install PHP Versions: Utilize Ondřej Surý’s PHP PPA to install multiple PHP versions not available in Ubuntu's repositories.
  • Configure Nginx: Create separate server blocks for each PHP version and configure Nginx to use them.
  • Test Configuration: Verify Nginx configuration for syntax errors and apply changes by reloading Nginx.
  • Verify PHP Version: Confirm that Nginx is using the correct PHP version by accessing a PHP info page.

Step 1: Update Ubuntu Packages

Ensure you have the latest package versions by updating Ubuntu's package lists:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

Install Nginx, the high-performance web server:

sudo apt install nginx -y

Start the Nginx service:

sudo systemctl start nginx

Step 3: Install PHP Versions

Add Ondřej Surý’s PHP PPA to access multiple PHP versions:

sudo apt install software-properties-common -y

sudo add-apt-repository ppa:ondrej/php

sudo apt update

Install desired PHP versions. For example:

PHP 5.6:

sudo apt install php5.6 php5.6-fpm php5.6-mysql -y

PHP 7.4:

sudo apt install php7.4 php7.4-fpm php7.4-mysql -y

PHP 8.1:

sudo apt install php8.1 php8.1-fpm php8.1-mysql -y

Step 4: Configure Nginx to Use Different PHP Versions

Create configuration files for each PHP version in /etc/nginx/conf.d/:

sudo vi /etc/nginx/conf.d/php8.1.conf

Inside the configuration file, specify the PHP version using fastcgi_pass directive. For example:

location ~ \.php$ {

    include snippets/fastcgi-php.conf;

    fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Change to desired PHP version

}

Repeat this process for each PHP version installed.


Step 5: Test Configuration and Reload Nginx


Test Nginx configuration for syntax errors:

sudo nginx -t

If successful, reload Nginx:

sudo systemctl reload nginx

0 comments:

Post a Comment