Apache has the capability of serving multiple domains from a single server by using a mechanism called “virtual hosts”. If a server has one IP address and two domains are hosted on it, each pointing to the same IP, Apache uses the domain name that the browser requests to serve the correct website.
Here’s a simple step-by-step guide to set up a virtual host:
1. Create a Directory for The Domain Document Root Create a directory that will hold the website’s files. This can be at any location but for demonstration, let’s put it in `/var/www/`.
sudo mkdir /var/www/your\_domain1. Grant Permissions Allow the `$USER` environment variable to hold your username.
sudo chown -R $USER:$USER /var/www/your\_domain sudo chmod -R 755 /var/www/your\_domain1. Create a New Virtual Host File Apache comes with a default virtual host file called `000-default.conf` that we can use as a jumping off point.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your\_domain.conf1. Configure the New Virtual Host File Open the new file in your editor with root privileges:
sudo nano /etc/apache2/sites-available/your\_domain.conf Modify it to look like the following:1. Enable New Virtual Host File
sudo a2ensite your\_domain.conf This creates a symbolic link from this file to the sites-enabled directory, which is read by Apache.1. Test Configuration Now it’s time to test our configuration file for syntax errors:
sudo apache2ctl configtest If everything is OK, it will return `Syntax OK`. If not, it will tell you what’s wrong.1. Restart Apache After all this, restart Apache for the changes to take effect:
sudo systemctl restart apache2Now, you should be able to access the domain your\_domain and see the content you’ve added to the respective directory.