How to Setup Virtual Hosts with Apache Web Server on Linux?
Last Updated :
04 Jul, 2024
Setting up a virtual host in Apache web server on Linux allows us to host multiple websites on a single server. This virtual host is useful when one wants to host multiple projects for businesses managing multiple domains with a single server.
Prerequisites
Steps to set up virtual hosts with Apache web server
Step 1: Install Apache
- If you have not installed Apache on your system then run the following with root privileges.
sudo apt update
sudo apt install apache2
Step 2: Create Directory for Each Website
- Make a directory to house each domain's website files. We'll make directories for example.com and example.org in this case.
sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/example.org/public_html
- Set the correct permissions for these directories:
sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.org/public_html
sudo chmod -R 755 /var/www
Step 3: Create Sample Pages for Each Website
- Create a simple index.html file for each website to test the setup.
For example.com:
echo "
<html>
<body>
<h1>Welcome to example.com!</h1>
</body>
</html>" > /var/www/example.com/public_html/index.html
For example.org:
echo "
<html>
<body>
<h1>Welcome to example.org!</h1>
</body>
</html>" > /var/www/example.org/public_html/index.html
Step 4: Create Virtual Host Configuration Files
- For every virtual host, create a different configuration file. For these configurations, Apache usually uses the sites-available directory.
- For example.com:
sudo nano /etc/apache2/sites-available/example.com.conf
- Add the following configuration:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
sudo nano /etc/apache2/sites-available/example.org.conf
- Add the following configuration:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.org
ServerAlias www.example.org
DocumentRoot /var/www/example.org/public_html
ErrorLog ${APACHE_LOG_DIR}/example.org_error.log
CustomLog ${APACHE_LOG_DIR}/example.org_access.log combined
</VirtualHost>
Step 5: Enable the Virtual Hosts
- Enabling the virtual host files and restarting the Apache
sudo a2ensite example.com.conf
sudo a2ensite example.org.conf
sudo systemctl restart apache2
Step 6: Test the Configuration
- Edit your local /etc/hosts file to point the domain names to your server’s IP address for testing purposes.
sudo nano /etc/hosts
127.0.0.1 example.com
127.0.0.1 example.org
- Save and close the file and open a web browser and navigate to http://example.com or http://example.org to verify that each site is correctly fetched.
Conclusion
Following the detailed steps in this article you can setup multiple virtual hosts on single Apache server in a Linux environment. This setup helps us host multiple websites with different domains improving the resource utilization and simplifying server management.
Similar Reads
How to Install Apache Web Server on Linux Cloud Server? Apache is an open-source web server used widely for hosting websites and web applications. It's popular on the internet and supports platforms like Linux, Windows, and macOS. Installing Apache on a Linux-based cloud server is straightforward. Cloud servers offer flexibility and scalability, enabling
5 min read
How To Install the Apache Web Server on CentOS 7 Apache Web Server, commonly known as Apache is a free, open-source, and one of the most widely used web servers in the world. Apache web server is developed and maintained by Apache Software Foundation. Apache is not any physical server, it is a software application running either on a physical/virt
4 min read
How to install and set up Apache Virtual Hosts on Ubuntu? Every website that is published on the Internet is housed on a web server (host), which is able to handle requests for web pages made by clients using browsers like Chrome, Firefox, or Internet Explorer and is connected to the network with a public IP address. Install a web server before hosting a w
4 min read
How to Set Up Apache Web Server in AWS EC2 Linux (Ubuntu) Instance? In this article, we will look into the process of setting up Apache Web Server in AWS EC2 Linux Instance.This tutorial has been done on a system running Windows 10 Home (Version 20H2).Implementation:The steps taken to complete this tutorial are being stated below:Step 1: Go to portal.aws.amazon.com
4 min read
How To Host Website On Tomcat Server ? Deploying a website on a Tomcat server is a direct interaction that permits you to host and manage web applications built by utiliz ng Java innovations. Apache Tomcat, commonly referred to as Tomcat, is an open-source web server and servlet container created by the Apache Software Foundation. It is
6 min read
How To Install the Apache Web Server on Debian 11? Apache is an open-source web server thatâs available for Linux servers free of charge. Installing an Apache web server on Linux is a straightforward process. In this article, we will install Apache Web Server Debian 11 (Bullseye). Steps to Install Apache Web Server in LinuxStep 1: Update Your System
3 min read