Sometimes you may need to host multiple websites on a single Apache server. You can easily do this using virtual hosts. In this case, you just need to setup separate virtual host for each website/domain and Apache will automatically direct incoming requests to the appropriate folder, depending on the requested domain name. In this article, we will look at how to configure multiple virtual host in Apache server.
Apache Configure Multiple Virtual Hosts
Here are the steps to configure multiple virtual hosts in Apache. Let us say you want to host 2 domains website1.com and website2.com on a single Apache server.
1. Install Apache
If you have not installed Apache server yet, open terminal and run the following command to do so.
$ sudo apt update $ sudo apt-get install apache2
2. Create directories
Create two directories, one for each domain website1.com and website2.com.
$ sudo mkdir /var/www/html/website1.com $ sudo mkdir /var/www/html/website2.com
3. Create index.html files
Next, we will create index.html files for each domain.
Run the following command to create index.html file for website1.com
$ sudo vi /var/www/html/website1.com/index.html
Add the following lines to index.html
<html> <title>www.website1.com</title> <h1>Welcome to www.website1.com Website</h1> <p>Index page for website1.com</p> </html>
Save and close the file. Similarly, create index.html file for website2.com.
$ sudo vi /var/www/html/website2.com/index.html
Add the following lines to index.html file of example2.com
<html> <title>www.website2.com</title> <h1>Welcome to www.website2.com Website</h1> <p>Index page for website2.com</p> </html>
Save and close the file.
After we setup virtual host for each domain, we will open their URL on web browser and see the right index.html file for each domain, indicating that Apache is correctly serving both domains.
4. Set File ownership
By default, all the files & folders we just created will have owners as the user have logged in as. Apache needs www-data to be the owner of website files & folders to be able to easily access and modify them. So we will set the owner of the above 2 folders and their contents to be www-data.
$ sudo chown -R www-data:www-data /var/www/html/website1.com $ sudo chown -R www-data:www-data /var/www/html/website2.com
5. Create Virtual Host Files
You need to create one virtual host file for each domain. In case of Apache server, all virtual host files must be placed at /etc/apache2/sites-available folder.
$ sudo vi /etc/apache2/sites-available/website1.com.conf
Add the following line to the above file.
<VirtualHost *:80> ServerAdmin admin@website1.com ServerName website1.com DocumentRoot /var/www/html/website1.com DirectoryIndex index.html ErrorLog ${APACHE_LOG_DIR}/website1.com_error.log CustomLog ${APACHE_LOG_DIR}/website1.com_access.log combined </VirtualHost>
In the above configuration, we specify the virtual host to run on port 80. We also mention that this virtual host is meant for example1.com domain using ServerName directive, with its root folder located at /var/www/html/website1.com specified using DocumentRoot directive.
Similarly, create virtual host file for second domain.
$ sudo vi /etc/apache2/sites-available/website2.com.conf
Add the following lines to your virtual host file.
<VirtualHost *:80> ServerAdmin admin@website2.com ServerName website2.com DocumentRoot /var/www/html/website2.com DirectoryIndex index.html ErrorLog ${APACHE_LOG_DIR}/website2.com_error.log CustomLog ${APACHE_LOG_DIR}/website2.com_access.log combined </VirtualHost>
6. Enable virtual hosts
Run the following commands to enable the two virtual hosts. You need to specify the file name of your virtual host files, without .conf extension, after a2ensite command below.
$ sudo a2ensite website1.com $ sudo a2ensite website2.com
7. Restart Apache Server
Restart Apache server to apply changes.
$ sudo service apache2 restart
8. Test virtual hosts
Open browser and visit http://website1.com. You will see the index.html page of website1.com.
Similarly, open browser and visit http://website2.com. You will see the index.html page of website2.com.
That’s it. Now you can access both your website using a single server.
You can use the above approach to host more than 2 websites on a single server. You need to create 1 folder for each domain, to host its files. You also need to create 1 virtual host file for each domain and enable it using a2site command.
Also read:
How to Clean up disk space in Linux
How to Add Response Header in NGINX
How to Redirect to Parent Folder in Apache
How to List Open Ports in Ubuntu
How to Rewrite URL to Subdirectory in Apache
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.