How to Setup Virtual Hosts with Apache Web Server on Linux? Last Updated : 04 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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.PrerequisitesApache server installed.Root or sudo accessSteps to set up virtual hosts with Apache web serverStep 1: Install ApacheIf you have not installed Apache on your system then run the following with root privileges.sudo apt updatesudo apt install apache2Step 2: Create Directory for Each WebsiteMake 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_htmlsudo mkdir -p /var/www/example.org/public_htmlSet the correct permissions for these directories:sudo chown -R $USER:$USER /var/www/example.com/public_htmlsudo chown -R $USER:$USER /var/www/example.org/public_htmlsudo chmod -R 755 /var/wwwStep 3: Create Sample Pages for Each WebsiteCreate 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.htmlFor example.org:echo " <html> <body> <h1>Welcome to example.org!</h1> </body> </html>" > /var/www/example.org/public_html/index.htmlStep 4: Create Virtual Host Configuration FilesFor 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.confAdd 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>For example.org:sudo nano /etc/apache2/sites-available/example.org.confAdd 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 HostsEnabling the virtual host files and restarting the Apachesudo a2ensite example.com.confsudo a2ensite example.org.confsudo systemctl restart apache2Step 6: Test the ConfigurationEdit your local /etc/hosts file to point the domain names to your server’s IP address for testing purposes.sudo nano /etc/hostsAdd the following lines:127.0.0.1 example.com127.0.0.1 example.orgSave 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.ConclusionFollowing 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. Comment More infoAdvertise with us Next Article How to Setup Virtual Hosts with Apache Web Server on Linux? F firozsh553l Follow Improve Article Tags : Linux-Unix Apache 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 How to Use Apache Webserver to Host a Website? Apache is a powerful and widely used web server across the industry to host websites efficiently. In this guide, we will understand the usage of Apache web servers for hosting a website. PrerequisitesApache Web ServerAdministrative AccessSteps to use Apache webserver to host a website:Step 1: Basic 2 min read Most Useful Commands to Manage Apache Web Server in Linux Prerequisite: How do Web Servers work? Apache is one of the most widely used free, open-source Web Server applications in the world, mostly used in Unix-like operating systems but can also be used in windows. As a developer or system administrator, it will be very helpful for you to know about the A 3 min read How to Install Apache Web Server in Linux: Ubuntu, Fedora, RHEL? If you're looking to install Apache on Linux, this guide will walk you through the steps required for different distributions, including Ubuntu, Fedora, and RHEL. The Apache web server is a popular choice for hosting websites and applications, known for its reliability and flexibility. Whether you'r 5 min read How to Host Apache HTTP Server on Microsoft Windows? Apache HTTP Server, commonly known as Apache, is a widely used, powerful, and secure web server it is an ideal choice for hosting web applications. Popular deployment options include Apache Lounge, Bitnami WAMP Stack, WampServer, and XAMPP. Each option has its advantages and disadvantages, but in th 3 min read Like