For the case of a VPS user who wants to host more than a domain on your server, you will have to create corresponding hosts on the webserver. This will allow you to deliver multiple contents depending on different requests. In this article, you will be able to learn how to create Apache virtual hosts on your Linux based VPS through this guide. The Linux distribution used in this guide is CentOS 7.
Step 1: Apache Installation
Before the tutorial start, make sure you are connected to your VPS using SSH and have root access to it. Then we can proceed with installing Apache by using the following command in your terminal.
sudo yum -y install httpd
After completing the installation, enable Apache to run on system boot for CentOS service using the following command.
sudo systemctl enable httpd.service
After completing the above steps, try visiting your IP address 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. To do that, first, we will have to direct to “/var/www/” with the following command.
After directing to “/var/www/”, make a unique document root for each virtual host you need with the following command.
mkdir -p domainname.com/public_html
Replace the “domainname.com” with your domain name instead. After that, the directory needs to be made accessible by Apache. To do that, use the chown option to change ownership and chmod to set permissions for the entire web directory, by following the command below.
chown -R apache:apache /var/www/domainname.com/public_html
chmod -R 755 /var/www
Remember to replace “domainname.com” similarly with the example above. With the command above, Apache has the right to create directories and serve content for incoming queries.
Step 3: Create a Demo page
The reason for creating a demo page is to verify whether the host is working properly or not. It is recommended to do this before actually moving the files of your website.
To do so, create an “index.html” file in “domainname.com/public_html” directory using any text editor of your preference. For the following command, it will be using a nano editor.
Then type in any short html code of your preference or just paste the html code below.
Then save the file using Ctrl+X and press Y for save changes confirmation.
Step 4: Create Virtual Host
For the final step, we will finally create the virtual host. To achieve that, firstly, create the new virtual host “.conf” file in the Apache configuration directory using a nano text editor with the following command.
Then insert the following code into the newly created “.conf” file.
<Virtual Host *:80>
CustomLog /var/www/domainname.com/requests.log combined
Do note to change all “domainname.com” to your chosen domain. In the content above, Apache uses port 80 for communication. In addition, the directories for the website file and error logs had been specified too. Restart Apache in order for the changes to take effect using the following command.
systemctl restart httpd.service
With this, you have successfully created an Apache virtual host for the domain that needs it. Try accessing the host and you should be able to see your demo page with the word “Hello World!” that was created earlier.