Signup/Sign In
PUBLISHED ON: MARCH 9, 2023

Setting Up a Virtual Host on Apache Server on Local Machine

Virtual hosting is a method used to host multiple websites on a single physical server. It allows you to host multiple websites with different domain names on the same machine, each with its own unique configuration and resources. In this tutorial, we will be setting up a virtual host on your local machine using Apache web server. This can be useful for testing websites or web applications before deploying them to a live server.

Setting Up Apache:

Before we can set up a virtual host, we need to make sure Apache is installed and running on your machine. If you are using a Linux or macOS operating system, Apache is most likely already installed. To check if it is running, open a terminal and enter the command "apachectl -v". If Apache is installed and running, it will display the version number. If it is not installed, you can follow the instructions for your specific operating system to install it.

Modifying the Apache Configuration File:

Next, we need to modify the Apache configuration file to allow virtual hosting. The location of this file will vary depending on your operating system, but it is usually located at "/etc/apache2/httpd.conf" on macOS and "/etc/httpd/conf/httpd.conf" or "/etc/apache2/apache.conf" on Linux. Open the file in a text editor (vi, nano, etc.) and uncomment the following line by removing the "#" symbol:

#Include conf/extra/httpd-vhosts.conf

This will include the virtual host configuration file in the main Apache configuration. Save the file and close it.

Creating a Virtual Host Configuration File:

Now we need to create the virtual host configuration file. This file will contain the configuration for our virtual host. By default, Apache looks for virtual host configuration files in the "/etc/apache2/extra" directory on macOS and "/etc/httpd/conf/extra" on Linux. Create a new file in this directory and name it "httpd-vhosts.conf".

In this file, we will define our virtual host by specifying the domain name, document root, and any other necessary configurations. For example:

<VirtualHost *:80>
ServerName mywebsite.local
DocumentRoot "/Users/username/Sites/mywebsite"
</VirtualHost>

Replace "mywebsite.local" with the domain name you want to use for your virtual host and "Users/username/Sites/mywebsite" with the path to the directory containing your website files. You can add as many virtual hosts as you like by simply repeating the above block of code with different domain names and document roots. Save the file and close it.

Testing the Virtual Host:

Now that we have set up our virtual host, we need to test it to make sure it is working correctly. To do this, we need to add an entry in our hosts file for the domain name we used in the virtual host configuration. The hosts file is a simple text file that maps domain names to IP addresses. It is located at "/etc/hosts" on both macOS and Linux. Open the file in a text editor and add the following line at the bottom:

127.0.0.1 mywebsite.local

This will map the domain name "mywebsite.local" to the IP address "127.0.0.1", which is the loopback address used to access the local machine. Save the file and close it.

Don't forget to restart Apache after making any changes to the configuration files. On macOS, you can use the command "sudo apachectl restart" to do this. On Linux, you can use the command "sudo systemctl restart httpd".

Now we can test our virtual host by opening a web browser and navigating to the domain name we used in the virtual host configuration. If everything is set up correctly, you should see your website or web application displayed in

Conclusion:

In this tutorial, we learned how to set up a virtual host on our local machine using Apache. We covered topics such as modifying the Apache configuration file, creating a virtual host configuration file, and testing the virtual host on our local browser. By following these steps, you should now have a basic understanding of how to set up a virtual host and be able to easily test web projects on your own machine.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.