Skip to content

Instantly share code, notes, and snippets.

@Sadhr
Created February 7, 2020 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sadhr/2e6c0aebc62e43620f253484685259f2 to your computer and use it in GitHub Desktop.
Save Sadhr/2e6c0aebc62e43620f253484685259f2 to your computer and use it in GitHub Desktop.
Configure Apache Virtual Hosts in Ubuntu
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain.test.conf
$ sudo vi /etc/apache2/sites-available/domain.test.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@domain.test
ServerName domain.test
ServerAlias www.domain.test
DocumentRoot /var/www/html/domain.test/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
Let us create a sample page for domain.test site. To do so, run:
$ sudo vi /var/www/html/domain.test/public_html/index.html
<html>
<head>
<title>www.ostechnix.lan</title>
</head>
<body>
<h1>Hello, This is a test page for ostechnix1.lan website</h1>
</body>
</html>
$ sudo mkdir -p /var/www/html/domain.test/public_html
The above directorie is owned by root user. We need to change the ownership to the regular user.
$ sudo chown -R $USER:$USER /var/www/html/domain.test/public_html
Next, set read permissions to the Apache root directory i.e /var/www/html/ using command:
$ sudo chmod -R 755 /var/www/html/
After making the necessary changes, disable the default virtual host config file i.e 000.default.conf, and enable all newly created virtual host config files as shown below.
$ sudo a2dissite 000-default.conf
$ sudo a2ensite domain.test.conf
Restart apache web server to take effect the changes.
$ sudo systemctl restart apache2
$ sudo apt-get install apache2
Open /etc/hosts file in any editor:
$ sudo vi /etc/hosts
Add all your virtual websites/domains one by one like below.
[...]
127.0.0.1 domain.test
[...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment