Magento 2 - Configure multiple magento sites in your local setup with Nginx for multiple php versions

Hi there,

This post is about configuring Nginx for multiple Magento 2 versions (Ex: M2.4.2, M2.3.2) with multiple php versions (PHP 7.4, 7.2) to run without switching php versions.

It took me a while to get the settings correct so thought to write it down for someone who could save his time.

My setup was like this;
I wanted to run a Magento 2.4.2 and a 2.3.2 at the same time, and had to switch the php versions everytime when I wanted to check either of the sites. I had 2 virtual host files configured at default nginx location as below;
/etc/nginx/sites-enabled/m232, /etc/nginx/sites-enabled/m242

I added the following directive in each virtual host file
as below:

upstream fastcgi_backend_74 {
        server  unix:/run/php/php7.4-fpm.sock;
}

So /etc/nginx/sites-enabled/m242 now looks like below:

upstream fastcgi_backend_74 {
        server  unix:/run/php/php7.4-fpm.sock;
}

server {
        listen 80;
        server_name dev.m242.com;
        set $MAGE_ROOT /home/djbravo/projects/m242;

        include /home/djbravo/projects/m242/nginx.conf.sample;

        access_log      /var/log/nginx/m242.access;
        error_log       /var/log/nginx/m242.error;
}

Then I changed the value for following directive inside the default magento nginx.conf.sample under the project root.

<magento 2.4.2 project root>/nginx.conf.sample (in our case /home/djbravo/projects/m242)

Search for the fastcgi_pass directive (there were 3 places) inside above file and replace fastcgi_backend with fastcgi_backend_74 (configured for PHP 7.4) which was configured in our virtual host file for m242 project above.

Then, do the same for M2.3.2 project like below in the following default location:

/etc/nginx/sites-enabled/m232

Note the fastcgi_backend_72 this time for PHP 7.2

upstream fastcgi_backend_72 {
        server  unix:/run/php/php7.2-fpm.sock;
}

server {
        listen 80;
        server_name dev.m232.com;
        set $MAGE_ROOT /home/djbravo/projects/m232;

        include /home/djbravo/projects/m232/nginx.conf.sample;

        access_log      /var/log/nginx/m232.access;
        error_log       /var/log/nginx/m232.error;
}

Then do the same as we did for M2.4.2 project for the default magento nginx.conf.sample under the project root for M2.3.2 project.

<magento 2.3.2 project root>/nginx.conf.sample (in our case /home/djbravo/projects/m232)

Search for the fastcgi_pass directive (there were 3 places) inside above file and replace 'fastcgi_backend' with 'fastcgi_backend_72' (configured for PHP 7.2) which was configured in our virtual host file for m232 project above.

Make sure you don't have the same fastcgi_backend_72 or fastcgi_backend_74 configured inside /etc/nginx/sites-enabled/default virtual host.

Then do a sudo nginx -t to check the syntax of virtual host files, and if all okay, do a 'sudo service nginx restart'

Hope I didn't miss anything. If you have any issues pls let me know, I will try my best to help you. Btw, I'm not a Pro, so I may not able to answer all your questions.

Cheers

23