This article provides a guide for creating Apache virtual hosts on a Linux VPS. This guide helps VPS users who want to host multiple websites, domains, or implementations in a single server. This guide assumes a RHEL8-based distribution such as CentOS 8, AlmaLinux 8, or Rocky Linux 8.
Step 1: Apache Installation
Make sure you are connected to your VPS using SSH and have root access to it. Then proceed with installing Apache by using the following command in your terminal:
sudo dnf -y install httpd
After completing the installation, enable Apache to run on system boot using the following command:
sudo systemctl enable httpd.service
After completing the above steps, visit your server's IP address in a browser to verify if Apache is running or not. You should be able to see a verification regarding Apache being properly installed.
Step 2: Creating Directory Tree
To hold website data, a directory tree is needed. First, we will have to move to "/var/www/" with the following command:
Next, make a unique document root for each virtual host with the following command (replacing "yourdomain.tld" with your actual domain):
mkdir -p yourdomain.tld/public_html
Now, we must give Apache access to the directory. Use the chown option to change ownership and chmod to set permissions for the entire web directory, by issuing the following commands:
chown -R apache:apache /var/www/yourdomain.tld/public_html
chmod -R 755 /var/www
Now, Apache has ownership of the directory, allowing it to manage folders and serve contents from this space.
Step 3: Create a Demo page
It is recommended to create a demo page before moving the files of your website to verify the web server is installed and running as expected.
To do so, create an "index.html" file in "yourdomain.tld/public_html" directory using using your preferred text-editor:
Add some html code to the "index.html" file, such as:
Then save the file using Ctrl+X and press Y to save the changes.
Step 4: Create Virtual Host
Now we will create the virtual host. To begin, create the new virtual host ".conf" file in the Apache configuration directory using text editor:
Then insert the following code into the newly created ".conf" file.
<Virtual Host *:80>
CustomLog /var/www/yourdomain.tld/requests.log combined
Don't forget to change all "yourdomain.tld" to your domain. In the content above, Apache uses port 80 for communication. In addition, the directories for the website files and error logs have been specified too.
Now, restart Apache in order for the changes to take effect using the following command:
systemctl restart httpd.service
With this, you have successfully completed creating Apache virtual hosts on Linux VPS for your domains.
To test it, try to access the domain from your preferred web browser. You should be able to see your demo page that was created earlier.