Skip to content

deepyes02/wsl-2-apache-virtual-host

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

APACHE Web Server on WSL-2 / Ubuntu

Configure Apache2 Web Server with Virtual Hosts

Linux commands and configurations, starting with the freshly installed apache server and php that shows up on localhost.

Requirements

  1. Ubuntu in WSL-2 (Development Env)
  2. Fresh Apache2 Installation.
  3. PHP 7^ installed and ready

Codes

  1. Document Directory should look like this
/var/www/
├── html
│   └── index.php
├── deepesh.test
│   └── public_html
│   	└── index.html
├── shisir.test
│   └── public_html
│   	└── index.html
  1. You can achieve this my a one liner in wsl
# make directories
sudo mkdir -p /var/www/deepesh.test/public_html
sudo mkdir -p /var/www/shisir.test/public_html
# make html files and code a little
touch /var/www/deepesh.test/public_html/index.html
touch /var/www/shisir.test/public_html/index.html
  1. Then code some html in each
<html>
	<head>
		<title>deepesh.test</title>
	</head>
	<body>
		<h2>Hello World from deepesh.test</h2>
	</body>
</html>
  1. do the same with shisit.test
/var/www/shisir.test/public_html/index.html
  1. Also make sure there's something in /var/www/html/index.php (index.html) which shows up when you type 'localhost' in the browser. A simple 'Hello World from localhost' would do.

Starting Virtual Host

Virtual Hosting is a real life configuration, since web applications are served through such methods in production. Hence learning this is useful. On windows, a simple application like XAMPP and/or laragon is enough to configure, however, in production environment, all these things need to be done with hand. So I hope this is a great hands on experience.

  1. Provide ownership and execution/read/write access to 'www-data' instead of 'root' or 'your-user-name' to fix apache permission issues to domains.
sudo chown -R www-data: /var/www/deepesh.test
sudo chown -R www-data: /var/www/shisit.test
sudo chown -R www-data: /var/www/html
  1. Enable your user to edit apache files (Optional but recommended)
# This will enable you to edit configuration files inside the apache2 server without permission issues in vs code and other editors. 
sudo chown -R deepesh /etc/apache2/
  1. Create a Virtual Host configuration file in /etc/apache2/sites-available/ directory
#remember to create the file ending with '.conf' extension followed by previously defined domain name
cd /etc/apache2/sites-available
touch deepesh.test.conf
nano deepesh.test.conf  			#	I'm using vs code remote extension
# code deepesh.test.conf
# or vim deepesh.test.conf
# nano vim code all are good as long as they work
  1. Fill the file with the following code
<VirtualHost *:80>
    ServerName deepesh.test
    ServerAlias www.deepesh.test
    ServerAdmin webmaster@deepesh.test
    DocumentRoot /var/www/deepesh.test/public_html

    <Directory /var/www/deepesh.test/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/deepesh.test-error.log
    CustomLog ${APACHE_LOG_DIR}/deepesh.test-access.log combined
</VirtualHost>
  1. Enable the site. There are two ways to do it.
# 1. Create a symlink manually
sudo ln -s /etc/apache2/sites-available/domain1.com.conf /etc/apache2/sites-enabled/

# 2. Use a2ensite site script provided by apache
sudo a2ensite deepesh.test

## Result
#	deepesh@LAP-DEEPESH:/etc/apache2$ sudo a2ensite deepesh.test
#	Enabling site deepesh.test.
#	To activate the new configuration, you need to run:
#	sudo service apache2 reload
  1. Test your configuration for any errors
sudo apachectl configtest
## Outputs
# Syntax OK
## if the syntax OK does not return there is some mistake. Check your code again 
  1. Restart the Apache server and open the link in the browser
sudo service apache2 restart
  1. Check if you can access the server through curl
curl deepesh.test
# should output the following: 
#	<!DOCTYPE html>
#	<html>
#        <head>
#                <title>deepesh.test</title>
#        </head>
#        <body>
#                <h2>Hello world from deepesh.test</h2>
#        </body>
#	</html>
  1. Update the host name and ip on windows host file. Open powershell or cmd
#powershell
# check wsl ip
wsl hostname -I
# Output
# 172.20.81.56
  1. Open windows host file C:\Windows\System32\drivers\etc\hosts with notepad or other editors
# powershell
notepad hosts
  1. In the hosts file, assuming our wsl ip is '172.20.81.56', enter the following C:\Windows\System32\drivers\etc\hosts
172.20.58.21		deepesh.test		#wsl magic

make sure the above line is not commented with '#'.

Windows Host File Example

Windows Host File 14. Enter wsl and restart apache just for fun

# bash
sudo service apache2 restart
  1. Visit 'deepesh.test' on your windows browser

Virtual Host Running Successfully

Chrome running virtual Host

by - Deepesh Dhakal

About

Configure apache2 to run virtual host in wsl development environement. This requires editing windows host file as well.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published