Set Up Apache Virtual Hosts on Ubuntu 16.04 LTS¶
Create Virtual Host File¶
Start by copying the file for the first domain:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
Open the new file in your editor with root privileges:
sudo nano /etc/apache2/sites-available/example.com.conf
The file will look something like this (I’ve removed the comments here to make the file more approachable):
<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 ls-summit.dev
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/ls-summit
<Directory /var/www/html/ls-summit>
Options Indexes FollowSymLinks
AllowOverride All
Allow from all
Require all granted
</Directory>
# 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>
Enable the Virtual Host Files¶
We can use the a2ensite tool to enable each of our sites like this:
sudo a2ensite example.com.conf
When you are finished, you need to restart Apache to make these changes take effect:
sudo service apache2 restart
Set Up Local Hosts File¶
If you are on a Mac or Linux computer, edit your local file with administrative privileges by typing:
sudo vi /etc/hosts
If you are on a Windows machine, you can find instructions on altering your hosts file here.
The details that you need to add are the public IP address of your VPS server followed by the domain you want to use to reach that VPS.
For the domains that I used in this guide, assuming that my VPS IP address is 111.111.111.111, I could add the following lines to the bottom of my hosts file:
127.0.0.1 localhost
127.0.1.1 latitude-3470
127.0.1.1 ls-summit.dev
127.0.1.1 gmovies-web.dev
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters