Linux commands and configurations, starting with the freshly installed apache server and php that shows up on localhost.
- Ubuntu in WSL-2 (Development Env)
- Fresh Apache2 Installation.
- PHP 7^ installed and ready
- Document Directory should look like this
/var/www/
├── html
│ └── index.php
├── deepesh.test
│ └── public_html
│ └── index.html
├── shisir.test
│ └── public_html
│ └── index.html
- 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
- Then code some html in each
<html>
<head>
<title>deepesh.test</title>
</head>
<body>
<h2>Hello World from deepesh.test</h2>
</body>
</html>
- do the same with shisit.test
/var/www/shisir.test/public_html/index.html
- 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.
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.
- 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
- 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/
- 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
- Fill the file with the following code
<VirtualHost *:80>
ServerName deepesh.test
ServerAlias www.deepesh.test
ServerAdmin [email protected]
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>
- 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
- 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
- Restart the Apache server and open the link in the browser
sudo service apache2 restart
- 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>
- 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
- Open windows host file C:\Windows\System32\drivers\etc\hosts with notepad or other editors
# powershell
notepad hosts
- 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 '#'.
14. Enter wsl and restart apache just for fun
# bash
sudo service apache2 restart
- Visit 'deepesh.test' on your windows browser
by - Deepesh Dhakal