Skip to content

Instantly share code, notes, and snippets.

@Ian-FR
Last active February 23, 2024 15:38
Show Gist options
  • Save Ian-FR/5735ca3783916b0d593fa5d6e5f63076 to your computer and use it in GitHub Desktop.
Save Ian-FR/5735ca3783916b0d593fa5d6e5f63076 to your computer and use it in GitHub Desktop.
WSL as Web Server

WSL as Web Server

This tutorial will show you how to set up a local unix web server on your Windows computer without headaches

Pre-Requisites

Some requirements for the steps ahead

Enable WSL

Enable the WSL (Windows Subsystem for Linux) option in Windows Optional Features panel. Open PowerShell as Administrator and run

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Open de Microsoft Store, choose Ubuntu Linux distribuition and install it

VI Commands

Some commands for use in the VI text editor

CMD Description
:wq Save and quit
:q Quit
/string Find a string in the text
INSERT Insert enable \ disable

Distro configs

With WSL shell openned, update the distro's packages

sudo apt update && sudo apt -y upgrade && sudo apt -y dist-upgrade

Remove sudo password requests creating one sudoers profile file and adding <USERNAME> ALL=(ALL) NOPASSWD:ALL

sudo vi /etc/sudoers.d/<USERNAME>

Database

MySQL

To install MySQL type it:

sudo apt install mysql-server

sudo mysql_secure_installation

PostgreSQL

To install PostgreSQL type it:

sudo apt install postgresql postgresql-contrib

sudo passwd postgres

sudo -u postgres psql 

alter role postgres with password 'postgres'

\q

sudo service postgresql start

PHP

Install PHP and extensions with apt install php-<entension-name> command

sudo apt install php7.4-fpm php7.4-pgsql php7.4-mbstring php7.4-gd php7.4-curl php7.4-dom php7.4-soap php7.4-zip php7.4-bcmath php7.4-xml php7.4-json php7.4-gmp

To avoid a PHP's security problem it is necessary to adjust one value in the /etc/php/[php-version]/fpm/php.ini file, open the same

sudo vi /etc/php/7.4/fpm/php.ini

Find ;cgi.fix_pathinfo=1 directive. Adjust to cgi.fix_pathinfo=0 and save the file. After this, restart the php7.4-fpm service

sudo service php7.4-fpm start

Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

NGINX

To install the nginx type

sudo apt install nginx

Copy the default server block conifiguration file to example.com and open the same

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

sudo vi /etc/nginx/sites-available/example.com

Configure the server block to the example.com project (See the example.com file)

Enable the example.com by creating a symbolic link from example.com configuration file to the /etc/nginx/sites-enabled/ directory

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

To avoid a possible hash bucket memory problem that can arise from adding more server names, it is necessary to adjust a single value in the /etc/nginx/nginx.conf file. Open the file

sudo vi /etc/nginx/nginx.conf

Check the syntax of the nginx.conf file typping

sudo nginx -t

Restart the nginx service

sudo service nginx restart

xDebug (Optional)

Install the xDebug and open the /etc/php/7.2/mods-available/xdebug.ini file to modify zend_extension directive

sudo apt install php-xdebug

sudo vi /etc/php/7.4/mods-available/xdebug.ini

Update zend_extension=xdebug.so to zend_extension=/usr/lib/php/20190902/xdebug.so and save file (Note that 20190902 can be different because it is the version of php extension)

Now open the /etc/php/7.4/fpm/php.ini file

sudo vi /etc/php/7.4/fpm/php.ini

Add the settings below and save the file

[Xdebug]
xdebug.remote_enable=1
xdebug.remote_port=9999
xdebug.remote_autostart=1
xdebug.remote_connect_back=1
;xdebug.remote_host=127.0.0.1
xdebug.idekey="vscode"
;xdebug.idekey="PHPSTORM"

Restart the php7.4-fpm service

sudo service php7.4-fpm restart

NodeJS, NPM and Yarn

NodeJS

Install the Node.js v12.x:

sudo curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

sudo apt install -y nodejs

sudo npm i -g yarn

Conclusion

Now you have a local unix web server on your Windows computer and will can debug your web applications easily congratulations!

server {
listen 80;
listen [::]:80;
server_name backend.example.com;
root /usr/share/nginx/example/backend;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP FPM configuration.
location ~ \.php$ {
fastcgi_buffering off; # Just for de WSL1
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
listen [::]:80;
server_name example.com;
root /usr/share/nginx/example/frontend;
index index.html;
location / {
try_files $uri $uri/ /index.html =404;
}
location ~ /\.ht {
deny all;
}
}
# /etc/wsl.conf
[network]
generateResolvConf = false
# /etc/resolv.conf
nameserver 8.8.8.8
nameserver 1.1.1.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment