How to create Virtual Hosts on Apache Server?
Why do you need this?
The usage of virtual hosts on Apache, grants the ability to serve multiple websites in one machine. Each domain will direct the visitor to a specific directory holding your website. The creation of virtual hosts in your Apache server, however, doesn’t replace the need to create DNS records for resolving your host names. You must have the names in DNS, resolving to your server IP address.
How do you create virtual hosts?
First, you need to have Apache server installed in your machine. If you still don’t have it, just run the command below:
Ubuntu/Debian: sudo apt install apache2
CentOS/Fedora: sudo dnf install httpd
For Windows, you need to download Apache in the following link: https://httpd.apache.org/docs/current/platform/windows.html
If you also want to install the LAMP stack, read our article: How to install Apache, MySQL and PHP on Ubuntu Server?
Second, after having apache2 installed, you need to go to the folder /etc/apache2/sites-available
and create a file with the name of your site with the extension .conf
. For example: mysite.conf.
Third, you need to put the content:
define ROOT "full path to your website folder" #E.g: /var/www/html/mysite
define SITE "website domain" #E.g: mysite.com
<VirtualHost *:80> #The asterisks match all addresses, so the main server serves no requests.
DocumentRoot "${ROOT}"
ServerName ${SITE}
ServerAlias *.${SITE}
<Directory "${ROOT}">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:443> #The port 443 is for allowing https in your website.
DocumentRoot "${ROOT}"
ServerName ${SITE}
ServerAlias *.${SITE}
<Directory "${ROOT}">
AllowOverride All
Require all granted
</Directory>
#If you are defining the port 443, you should also introduce the path to SSL certificate file and key.
SSLEngine on
SSLCertificateFile full_path_to_certificate #E.g: /etc/ssl/mysite.crt
SSLCertificateKeyFile full_path_to_certificate_key #E.g: /etc/ssl/mysite.key
</VirtualHost>
You can see more helpful information in the following link: https://httpd.apache.org/docs/2.4/vhosts/examples.html
Fourth, by default your server is reading the configuration files of the folder sites-enabled
, so in here, you just need to put a shortcut/symbolic link to your configuration file on the folder sites-available
. Why do you need to this? For example, imagine if you require removing temporary the virtual host. Instead of deleting your configuration file, you just need to remove the shortcut/symbolic link.
For creating a symbolic link, just run the following command: ln -s /etc/apache2/sites-available/mysite.conf /etc/apache2/sites-enabled/mysite.conf
. If you are having trouble to create a symbolic link on Linux, follow the instructions of this article: https://linuxize.com/post/how-to-create-symbolic-links-in-linux-using-the-ln-command/
Finally, for the configurations having effect, you will need to restart your Apache server. For doing this, you can run the following command:
sudo service apache2 restart
or
sudo systemctl restart httpd.service