How to install Nginx + php + MySQL on WSL Windows 10

Although Nginx is available for Windows 10/8/7, however, to really understand, experience, build or test web application around, I recommend using it on Linux. And the Windows 10 WSL is the best option to run Linux+Nginx+PHP+MySQL stack to get a complete Linux based web server without really installing a separate Linux distro.

Thus, let’s see how to install Linux+Nginx+PHP+MySQL stack on Windows 10 WSL (Windows Subsystem for Linux).

What is Nginx?

Nginx (engine x) is a high-performance HTTP and reverse proxy web server that also provides IMAP/pop3/smtp services.

It is distributed under a BSD-like agreement and characterized by less memory and strong concurrent power. Nginx can be compiled and run on most Unix & Linux os and has a Windows port too.

In the case of high concurrency, Nginx is a good alternative to the Apache service: Nginx is one of the software platforms that dominating web hosting business supporting responses up to 50 000 concurrent connections thanks to Nginx for choosing Epoll and Kqueue as the development model.

The Nginx code is written entirely from the c language and has been ported to many architectures and operating systems including Linux, FreeBSD, Solaris, mac os x, AIX, and Microsoft windows.

Nginx has its own library of functions, and in addition to zlib, PCRE, and OpenSSL, standard modules only use system C library functions. Also, these third-party libraries may not be used if you do not need or consider potential authorization conflicts.

Step 1: Install Windows 10 WSL for Nginx + php

If you don’t have Windows 10 WSL (Windows Subsystem for Linux) enabled on your system yet, then simply go to the search section of Windows 10 and type “Turn Windows feature on or off” after that scroll and look for Windows subsystem for Linux option, check it and click on the OK button. This will enable it on your system. For step by step guide see this: How to enable WSL on Windows 10.

Step 2: Choose Linux Distro App for WIndows 10 WSL

Once you enabled WSL on your system, the next step is to procure some Linux distro app from Microsoft store. Here we are installing and using Ubuntu app on Windows 10 WSL. Just search for Microsoft store on your Windows 10 system and then in the search box type: Run Linux on Windows. The instructions of installing the Nginx stack will be the same for Debian and Kali Linux WSL images.

And select Ubuntu and then Get it.

Run Linux on WIndows 10 WSL

Step 3: Run Ubuntu to install Nginx + PHPs on Windows 10 WSL

Once you open the Linux Ubuntu 18.04 WSL on your Windows 10 system it will exactly look and behave like any other Linux command terminal.

The first thing which we do is to update the Ubuntu Wsl, use the below-given command:

sudo apt-get update
sudo apt-get upgrade

Second is running of commands to install Nginx on Windows 10 Ubuntu WSL:

sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install -y nginx

Step 4: Start Nginx web server service on WSL

We have successfully installed the Nginx on our Windows 10 WSL Linux app, now the thing which we have to do is starting off its service. For that use the below command

sudo service nginx start

Start Nginx web server service on WSL

Step 5: Test Nginx Webserver

Open your Windows 10 browser and type http:localhost:80

It will show the welcome screen of this web server as shown below in the screenshot.

"Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. 
Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx."

nginx web server is successfully installed on WIndows 10 WSL

Step 6: Installing PHP for Nginx on Windows 10 WSL

The Webserver is ready now we have to install and configure PHP to use with Nginx open-source web server. Here we install modules PHP-FPM and PHP-MySQL to use PHP with both Nginx and MySQL.

Add repo:

sudo add-apt-repository ppa:ondrej/php

Check the latest PHP version available to install

sudo apt-cache show php

According to the available version, install the following PHP modules, in our case the latest version was php7.4

sudo apt-get install php7.4-cli php7.4-fpm php7.4-curl php7.4-gd php7.4-mysql php7.4-mbstring zip unzip

Check the installed version

php --version

 

Step 7: Start PHP-fpm service

Here is the command to start the installed PHP-fpm service

sudo service php7.4-fpm start

Step 8:  Nginx Error (optional)

In case the following error such as occurs:

"502 Bad Gateway”
“502 Bad Gateway NGINX”
“502 Proxy Error”
“502 Service Temporarily Overloaded”
“Error 502”
“HTTP Error 502 – Bad Gateway”
“HTTP 502 Bad Gateway”

We have to configure PHP-fpm for Nginx otherwise PHP would not be able to contact Nginx:

Thus, open the php-fpm configuration file

sudo nano /etc/php/7.4/fpm/pool.d/www.conf

In the file find the PHP-fpm listening socket path:

listen =  127.0.0.1:9000

Change that to

listen= /run/php/php7.4-fpm.sock

Note: If you want to use some other version of PHP then replace the php7.4 with that version.

Configure PHP for Nginx Default site configuration

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

In the default site configuration, to use PHP with Nginx, first, we have to add index.php in that…

Find the below line and add index.php to the list.

index index.html index.htm index.nginx-debian.html;

# For example: 

index index.php index.html index.htm index.nginx-debian.html;

Now find the below lines and do editing as mentioned below:

#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}

Remove the # or uncomment the following things which as we have done here…

location ~ \.php$
{
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

After all the changes press CTRL+X and type Y and then press the Enter button to save the changes.

Restart Nginx and PHP-FPM services

sudo service nginx reload
sudo service php7.4-fpm restart

Step 9: Create a test PHP file

Create an index.php file

sudo touch /var/www/html/index.php

Open it:

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

And add the following lines in that

<?php
phpinfo();

Again press CTRL+X and type Y then press the enter button to save it.

Note: The root directory to save your project, so that you can call it using Nginx in the browser is just like Apache, i.e /var/www/

Step 10: Access the PHP info on Nginx WSL Windows 10 webserver

Finally, open the http:localhost or http:ip_address_of_your_system

Step 11: Install MySQL with Nginx on Windows 10 (optional)

If you also want to install MySQL on Windows WSL along with Nginx and PHP then here is the command:

sudo apt install mysql-server

To secure MySQL the command is:

sudo mysql_secure_installation

In this way, we can set up Linux, Nginx, PHP & MySQL on Windows WSL (Windows Subsystem for Linux)

 

11 thoughts on “How to install Nginx + php + MySQL on WSL Windows 10”

  1. thanks for this..now how do i get the root file? like where to save all my project files from so i can have other pages from the root file please. how can start using VScode on it?

    Reply
  2. thanks very much sir..but how do i create a public folder and then insert my projects inside of it and access them from aan editor?

    Reply
  3. Thanks for the clear explanation.
    May I suggest before going to this step “sudo mysql_secure_installation”
    To actually start the mysql server first using: sudo service mysql start
    After this is will go much better.
    Without starting the mysql server you will get this error:
    ‘Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)’

    Reply
  4. You have a typo near `fastcgi_pass unix: /var/run/php/php7.4-fpm.sock;`

    There is an extra space after `unix: ` that should not be there and throws an error when restarting nginx

    Reply
  5. Hi. I got this for “sudo mysql_secure_installation” :
    Error: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
    and for “sudo apt install mysql-server” I got this:
    invoke-rc.d: could not determine current runlevel
    * Stopping MySQL database server mysqld [ OK ]
    update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
    Renaming removed key_buffer and myisam-recover options (if present)
    Cannot open /proc/net/unix: No such file or directory
    Cannot stat file /proc/1/fd/5: Operation not permitted
    Cannot stat file /proc/1/fd/10: Operation not permitted
    Cannot stat file /proc/1/fd/6: Operation not permitted
    Cannot stat file /proc/8/fd/7: Operation not permitted
    Cannot stat file /proc/8/fd/10: Operation not permitted
    Cannot stat file /proc/8/fd/5: Operation not permitted
    Cannot stat file /proc/9004/fd/17: Operation not permitted
    Cannot stat file /proc/9005/fd/19: Operation not permitted
    Cannot stat file /proc/9006/fd/21: Operation not permitted
    Cannot stat file /proc/9008/fd/23: Operation not permitted
    Cannot stat file /proc/9030/fd/9: Operation not permitted
    dpkg: error processing package mysql-server-5.7 (–configure):
    installed mysql-server-5.7 package post-installation script subprocess returned error exit status 1
    dpkg: dependency problems prevent configuration of mysql-server:
    mysql-server depends on mysql-server-5.7; however:
    Package mysql-server-5.7 is not configured yet.

    dpkg: error processing package mysql-server (–configure):
    dependency problems – leaving unconfigured
    Processing triggers for libc-bin (2.27-3ubuntu1.4) …
    No apport report written because the error message indicates its a followup error from a previous failure.
    Processing triggers for systemd (237-3ubuntu10.48) …
    Processing triggers for man-db (2.8.3-2ubuntu0.1) …
    Processing triggers for ureadahead (0.100.0-21) …
    Errors were encountered while processing:
    mysql-server-5.7
    mysql-server
    E: Sub-process /usr/bin/dpkg returned an error code (1)

    A little help, please?

    Reply
    • sudo apt install mysql-server

      If you get error then follow below commands-

      Assign a home directory to the MySQL user-

      sudo usermod -d /var/lib/mysql/ mysql
      Now, change the port for MySQL service:

      sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

      Find the port, uncomment it (means remove the #) and then change the 3306 to 8060

      user = mysql
      # pid-file = /var/run/mysqld/mysqld.pid
      # socket = /var/run/mysqld/mysqld.sock
      port = 8060
      # datadir = /var/lib/mysql

      To save the file press Ctrl+X, type Y, and then hit the Enter button.
      sudo chmod 777 /var/run/mysqld/mysqld.sock
      sudo apt install mysql-server
      sudo service mysql start

      Reply
  6. TBH I never commented on a tutorial before, but this time I had to. Thanks a lot!!! Had no issues at all and everything just went like a charm :=)

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.