In this article, We are going to perform How to setup Apache Virtual Host on Ubuntu 18.04/16.04 LTS or any other cloud platform like Amazon EC2, Azure VM, Google Cloud Compute, etc. with preinstalled Ubuntu OS.
Table of Contents
Introduction:
Apache is one of most used free and open source web server software which is used to run web applications, around 40% websites uses Apache web server.
What is Apache Virtual Host
Apache virtual host is used to run more than one website on Single Instance/Server/Virtual Machine using virtual host/vhost configuration file.
There are two types of Virtual Host.
- Name Based Virtual Host
- IP Based Virtual Host
Name Based Virtual Host: It is used to host multiple website on a single IP Address.
IP Based Virtual Host: It is used to used to host multiple websites on different IP Address.
Prerequisite:
- Ubuntu 18.04/16.04 LTS with Minimal Installation
- SSH Access with sudo privileges
- Open Firewall Port: 80,443
Update the system package
sudo apt-get update
Install Apache2 on Ubuntu
We have to install apache2 package to setup Apache virtual host. Run below command to install apache2 on Ubuntu.
sudo apt-get install apache2
Create a Project Directory Structure
By default Apache run “index” page from /var/www/html directory, if you want to run single website in Apache web server you can copy your project code in /var/www/html directory OR you can create directory as per your domain/project.
Here we are creating Project directory to store domain/website files.
sudo mkdir -p /var/www/test.fosstechnix.com/public_html
Creating Simple index.html file for testing
For testing create a simple index.html file
sudo nano /var/www/test.fosstechnix.com/public_html/index.html
Paste the below lines
<html> <head> <meta charset="utf-8"> <title>Welcome to test.fosstechnix.com</title> </head> <body> <h1>This is Testing Page</h1> </body> </html>
Next change the permission domain root directory to www-data Apache user.
sudo chown -R www-data:
/var/www/test.fosstechnix.com/public_html/
Setup Apache Virtual Host on Ubuntu
By default Apache Host configuration are stored in /etc/apache2/sites-available directory with .conf file extension and enabled by creating symbolic link to /etc/apache2/sites-enabled directory.
Open your favorite editor and create the virtual host configuration file.
sudo nano /etc/apache2/sites-available/test.fosstechnix.com.conf
Paste the below configuration file
<VirtualHost *:80> ServerAdmin [email protected] ServerName test.fosstechnix.com ServerAlias test.fosstechnix.com DocumentRoot /var/www/test.fosstechnix.com/public_html/ <Directory /var/www/test.fosstechnix.com/public_html/> Order allow,deny Allow from all Require all granted Options FollowSymLinks AllowOverride All </Directory> </VirtualHost>
Enable the virtual host configuration
sudo a2ensite test.fosstechnix.com.conf
To verify the syntax
sudo apachectl configtest
Output:
Syntax OK
Reload the Apache service
sudo systemctl reload apache2
Restart the apache2 service to take effect
sudo systemctl restart apache2
Testing the Virtual Host
To test virtual host file, Open /etc/hosts file add server IP and domain name OR if you have purchased domain name from domain provider like GoDaddy, namecheap, etc., then add A record with domain name and Server IP.
sudo nano /etc/hosts
.... 192.168.100.51 test.fosstechnix.com ....
Open your favorite browser and type the domain name, you will see index.html web page
http://test.fosstechnix.com
If you are seeing index web page, then we have successfully performed setup Apache virtual host on Ubuntu.
Redirect HTTP to HTTPS in Apache using Virtual Host
It is recommended to secure your domain using secure protocol and SSL certificate. Here we are redirecting http to https in apache using virtual host and adding SSL certificates.
First create a directory where you will add SSL certificates, here we are coping SSL certificates in /var/www/ssl directory
sudo mkdir -p /var/www/ssl
Add the below highlighted lines in virtual host configuration if you have already created virtual host like above
<VirtualHost *:80> ServerAdmin [email protected] ServerName test.fosstechnix.com ServerAlias test.fosstechnix.com Document Root /var/www/test.fosstechnix.com/public_html/ Redirect / https:// test.fosstechnix.com / <Directory /var/www/test.fosstechnix.com/public_html/> Order allow,deny Allow from all Require all granted Options FollowSymLinks AllowOverride All </Directory> RewriteEngine On RewriteCond %{HTTPS} Off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </VirtualHost> <VirtualHost *:443> ServerName test.fosstechnix.com ServerAlias test.fosstechnix.com SSLEngine On SSLCertificateFile /var/www/ssl/60a18ab0a4ef3259.crt SSLCertificateKeyFile /var/www/ssl/*.fosstechnix.com.key DocumentRoot /var/www/test.fosstechnix.com/public_html/ <Directory /var/www/test.fosstechnix.com/public_html/> Order allow,deny Allow from all Require all granted AllowOverride All </Directory> </VirtualHost>
After adding below configuration enable SSL and rewrite modules to redirect http to https.
sudo a2enmod rewrite
sudo a2enmod ssl
To verify the syntax
sudo apachectl configtest
Output:
Syntax OK
Reload the Apache service
sudo systemctl reload apache2
Restart the apache2 service to take effect
sudo systemctl restart apache2
Open your favorite browser and access your URL using https.
https://test.fosstechnix.com
Start/Stop/Restart/Enable Apache2 Service
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl enable apache2
Error: Invalid command ‘SSLEngine’, perhaps misspelled or defined by a module not included in the server configuration, Action ‘configtest’ failed.
Error: Invalid command ‘RewriteEngine’, perhaps misspelled or defined by a module not included in the server configuration,
Conclusion
In this article, We have covered How to setup Apache Virtual Host on Ubuntu and Redirect HTTP to HTTPS in Apache using Virtual Host.
Related Articles
How to Download and Install Oracle JAVA 8 on Ubuntu 18.04/16.04 LTS