Creating Apache Virtual Hosts On Linux VPS


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:

cd /var/www/

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:

nano yourdomain.tld/public_html/index.html

Add some html code to the "index.html" file, such as:

<html>
<head>
<title>Demo Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

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:

nano /etc/httpd/yourdomain.tld.conf

Then insert the following code into the newly created ".conf" file.

<Virtual Host *:80>
ServerName www.yourdomain.tld
ServerAlias yourdomain.tld
DocumentRoot /var/www/yourdomain.tld/public_html
ErrorLog /var/www/yourdomain.tld/error.log
CustomLog /var/www/yourdomain.tld/requests.log combined
</VirtualHost>

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.

Related

  • apache, web server, virtual hosts, linux server, centos 8, centos, almalinux 8, rocky linux 8
  • 0 Los Usuarios han Encontrado Esto Útil
¿Fue útil la respuesta?

Related Articles

View Server PHP Environment with phpinfo.php

The phpinfo() function outputs a huge amount of information about the system you're using, such...

How to Create Sudo User on CentOS

This article provides step-by-step setup guide for adding Sudo user to CentOS system. The sudo...

How to Use Sudo

This article provides a guide to using a Sudo user on CentOS server. From the command line,...

Set Server Time Zones with Timedatectl

This article provides a guide to setting the server time and server time zone settings using...

508 Resource Limit is Reached

If your website is on a cPanel hosting account, you may have noticed the following error message:...