How to run a local Linux web server running in a Windows 11 window
Windows Subsystem for Linux lets run an Apache web server on your Windows PC
If you're building a website or programming web tools using PHP, MySQL or other common languages, it helps to have a test site you can run on your local PC. However, most web hosting services, including the best web hosting services, use Linux, not Windows as their hosting environments. But what if you have a Windows PC and want to build a site that's going to live on a Linux server?
We have good news. Using Windows 11 or 10's built-in Windows Subsystem for Linux (WSL) service, you can create a local Linux web server that you can use locally for testing and building a site. You simply run Linux in a window and then you can visit the website in your Windows-based web browser and edit the key HTML, PHP or other files in a Windows-based editor (ex: Notepad++, my fave). You can even navigate your Linux server's file system using Windows Explorer.
Below, we'll show you how to create a local Linux web server running in WSL. We'll use AlmaLinux 9 rather than Ubuntu because AlmaLinux is one of the most commonly used server operating systems for web hosts and it's similar to CentOS which is also used by many hosts. Our server control panel will be Virtualmin, because it's a free, easy-to-use control panel. Many people prefer cPanel, but a license for cPanel costs money.
1. Install WSL (Windows Subsystem for Linux) if you don't have it installed already. You do this by entering wsl --install at a PowerShell command prompt.
2. Install AlmaLinux OS 9 for WSL. It's available in the Microsoft Store.
During this process, you will need to create a username and password for your AlmaLinux instance.
3. Update the Linux environment with the latest packages.
Stay On the Cutting Edge: Get the Tom's Hardware Newsletter
Get Tom's Hardware's best news and in-depth reviews, straight to your inbox.
sudo dnf update
4. Set a password for the root user.
sudo passwd root
5. Install VirtualMin by issuing the following commands. The downloaded file is a shell script that will run a series of commands. It is not best practice to install software via this method if the source is not trusted. You can read the contents of the script by typing less install.sh after it has been downloaded.
wget -O install.sh https://raw.githubusercontent.com/virtualmin/virtualmin-install/master/virtualmin-install.sh
chmod a+x install.sh
sudo ./install.sh
5. Navigate to the URL it gives you in your browser (either your machine name or IP address with :10000 at the end)
6. Log in as root with the password you created.
7. Navigate through the Post-Installation Wizard, leaving defaults except as noted below.
I recommend changing the MariaDB password to something easier to remember than the default.
Toggle "Skip check for resolvability" to on for the Primary nameserver
8. Click Add new virtual server.
9. Give your site a domain name, admin password and admin user. You may or may not need the username and password as you may prefer to remain logged in as root.
10. Install Phpmyadmin and / or Wordpress by navigating to Virtualmin -> Manage Web Apps, selecting them and clicking Show Install Options then choosing install options or accepting the defaults.
You should now be able to manage your databases by going to http://IPaddresss/phpmyadmin in your browser (ex: http://172.18.231.63/phpmyadmin in my case).
11. Assign a host name to your IP in C:\Windows\System32\drivers\etc\hosts. You do this by adding the IP address, hitting tab and then entering the plain-English name you want to use to access your site. This isn't necessary as you can always just go to the IP address in your browser, but it makes browsing your local site a little easier.
13. Give all users read and write access to your public_html directory so you can access all of the websites' folders via Windows Explorer.
If you set up a virtual server with a user that owns it -- in my case, I called that user "thuser" -- the folder containing the website files is /home/[USERNAME]/public_html (ex: /home/thuser/public_html). That's where you'll find the index.html file that has the default home page for your site.
If, for some reason, you set up the virtual server to run under the root user, the files for the site will be located in /var/www. But, if you followed the instructions above, they are in /home/[USERNAME]/public_html.
There's just one problem. If you try to navigate to /home/thuser/public_html in Windows Explorer, you get an error message like the one below.
To fix this problem, you must enter sudo chmod -R 777 [PATH] at the AlmaLinux command prompt. For example.
sudo chmod -R 777 /home/thuser
That will make the entire home folder available in Windows Explorer. Then you can use your favorite Windows code editing software -- Notepad++ for example, to open and edit files there.
Now you can work on your site and test it on your local PC. You can navigate to it in your browser either by going to the IP address or domain name you chose (ex: mysite). You can edit the files using Windows editing software and access the database by going to http://mysite/phpmyadmin.
Use the following addresses to get to where you need to.
Address (assuming mysite as name) | Use |
---|---|
http://mysite | Home page |
http://mysite:10000 | Virtualmin Control Panel |
http://mysite/phpmyadmin | Database Management |
\\wsl.localhost\AlmaLinux-9\home\thuser\public_html | Home page folder in Windows Explorer |
In order for this test site to work, you need to make sure that there's an AlmaLinux-9 window open in Windows. Otherwise, the web server won't work.