Skip to content

Instantly share code, notes, and snippets.

@kevyworks
Last active June 22, 2021 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevyworks/7b7a415122ae0a01df12e0d21c86e757 to your computer and use it in GitHub Desktop.
Save kevyworks/7b7a415122ae0a01df12e0d21c86e757 to your computer and use it in GitHub Desktop.
WSL2 Nginx, PHP, MySQL - Different PHP versions

Different PHP Version

Installation:

$ sudo apt-get update -y
$ sudo apt-get install nginx mariadb-server -y
$ sudo service nginx start
$ sudo service mariadb start
$ sudo apt-get install software-properties-common -y

PHP:

$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update -y
$ sudo apt-get install php7.1 php7.1-fpm php7.4 php7.4-fpm zip unzip -y
$ sudo service php7.1-fpm start && sudo service php7.4-fpm start

Nginx:

$ sudo mkdir /var/www/html/php71
$ sudo mkdir /var/www/html/php74

Index File:

$ sudo nano /var/www/html/php71/index.php

Paste:

<?php phpinfo(); ?>

Copy and set Correct Permissions:

$ sudo cp /var/www/html/php71/index.php /var/www/html/php74/index.php
$ sudo chown -R www-data:www-data /var/www/html/php71
$ sudo chown -R www-data:www-data /var/www/html/php74

Nginx Configurations:

$ sudo nano /etc/nginx/sites-available/php71.conf

Paste:

server {
   listen 8071;
   root /var/www/html/php71/;
   index index.php;

   server_name _;

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

   location ~ \.php$ {
      try_files $uri =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
   }
}

For Php 7.4:

$ sudo nano /etc/nginx/sites-available/php74.conf

Paste:

server {
   listen 8074;
   root /var/www/html/php74/;
   index index.php;

   server_name _;

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

   location ~ \.php$ {
      try_files $uri =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
   }
}

Load Sites:

$ sudo ln -s /etc/nginx/sites-available/php71.conf /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/php74.conf /etc/nginx/sites-enabled/

Restart Services:

$ sudo service nginx restart && sudo service php7.1-fpm restart && sudo service php7.4-fpm restart

Test Sites:

Subfolder Different PHP Version

$ sudo nano /etc/nginx/sites-available/default

Check Configuration:

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

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

        server_name _;

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        #--------------------------------------------------------------------------------------
        # The application which only supports php version 7.1
        #--------------------------------------------------------------------------------------
        # might as well move this: include /var/www/sites.conf
        #
        
        location /old {
                alias /var/www/old;
                try_files $uri $uri/ /index.php$is_args$args;

                location ~ \.php$ {
                        include snippets/fastcgi-php.conf;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
                }
        }

        #--------------------------------------------------------------------------------------
        # Default PHP
        #--------------------------------------------------------------------------------------
        
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

WSL2 NGINX (DEVELOPMENT)

Nginx config file

server {
    listen 9000;
    listen [::]:9000;
    
    server_name dev.test;
    ...
}

Edit %windir%\system32\drivers\etc\hosts and append:

127.0.0.1 test.dev

Should be accessable at http://test.dev:9000

References:

How to install Nginx + php + MySQL on WSL Windows 10 Nginx running in WSL2 (Ubuntu 20.04) does not serve HTML page to Windows 10 host on another port than port 80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment