Run multiple PHP version in Debian 11

Prerequisites

apt update
apt-get -y install wget gnupg2 ca-certificates lsb-release apt-transport-https
sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -
apt update

apt install nginx
systemctl start nginx
systemctl status nginx
systemctl enable nginx

Installing PHP 5.6 and PHP 8.2

apt install php8.2-fpm
apt install php5.6-fpm

systemctl status php8.2-fpm
systemctl status php5.6-fpm

ls /var/run/php/php8.2-fpm.sock
ls /var/run/php/php5.6-fpm.sock

mkdir /var/www/php82
mkdir /var/www/php56

Configuring nginx nano /etc/nginx/sites-available/subdomain.mywebsite.com.conf

server {
        listen 80;
        listen [::]:80;

        root /var/www/php82;
        index index.html index.htm index.nginx-debian.html;

        server_name subdomain.mywebsite.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

save the file and create a soft link in sites-available directory

ln -s /etc/nginx/sites-available/subdomain.mywebsite.com.conf /etc/nginx/sites-enabled/
systemctl reload nginx

Comments

  • sachin
    edited August 28

    Installing SSL by Letsencrypt

    apt install certbot python3-certbot-nginx
    certbot --nginx
    cd /etc/nginx/sites-available/
    

    Configure port based PHP version. Edit nano subdomain.mywebsite.com.conf

    server {
        root /var/www/php82;
        index index.html index.htm index.nginx-debian.html;
    
        server_name subdomain.mywebsite.com;
    
        location / {
                try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/subdomain.mywebsite.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/subdomain.mywebsite.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    }
    
    server {
        root /var/www/php56;
        index index.html index.htm index.nginx-debian.html;
    
        server_name subdomain.mywebsite.com;
    
        location / {
                try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        listen [::]:4443 ssl ipv6only=on; # managed by Certbot
        listen 4443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/subdomain.mywebsite.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/subdomain.mywebsite.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    }
    
    server {
        if ($host = subdomain.mywebsite.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
        listen 80;
        listen [::]:80;
    
        server_name subdomain.mywebsite.com;
        return 404; # managed by Certbot
    }
    

    Reload Nginx Server

    systemctl reload nginx
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!