Centos

How To Set Up Apache Virtual Hosts on CentOS 8

If you’re running multiple websites on a single server, configuring Apache Virtual Hosts is a must. Virtual hosts allow you to host multiple domains on a single server without needing separate IP addresses. In this post, we will walk through the simple steps to set up Apache Virtual Hosts on CentOS 8, enabling you to manage your sites efficiently.

Prerequisites

Before we start, ensure you have:

  • A CentOS 8 server with Apache installed. If you haven’t installed Apache yet, you can do so using the following commands: sudo dnf update sudo dnf install httpd
  • Basic knowledge of command-line usage.
  • Root privileges or a user with sudo access.

Step 1: Check Apache Status

After installing Apache, check to ensure that it is running:

sudo systemctl status httpd

If it’s not running, you can start it with the following command:

sudo systemctl start httpd

Also, enable Apache to start on boot:

sudo systemctl enable httpd

Step 2: Create Directory Structure

Next, you need to create directory structures for your virtual hosts. Let’s assume we want to set up two domains – example1.com and example2.com. Use the following commands to create directories:

sudo mkdir -p /var/www/example1.com/public_html
sudo mkdir -p /var/www/example2.com/public_html

Adjust the permissions to make them accessible:

sudo chown -R $USER:$USER /var/www/example1.com/public_html
sudo chown -R $USER:$USER /var/www/example2.com/public_html

Step 3: Create Sample Pages

Create a simple index.html file for each website to ensure they’re working:

For example1.com:

echo "<h1>Welcome to Example1.com</h1>" > /var/www/example1.com/public_html/index.html

For example2.com:

echo "<h1>Welcome to Example2.com</h1>" > /var/www/example2.com/public_html/index.html

Step 4: Configure Apache Virtual Hosts

Next, you will create the configuration files for each virtual host.

  1. Create the configuration file for example1.com: sudo nano /etc/httpd/conf.d/example1.com.conf Add the following content: <VirtualHost *:80> ServerName example1.com ServerAlias www.example1.com DocumentRoot /var/www/example1.com/public_html&lt;Directory /var/www/example1.com/public_html&gt; AllowOverride All Require all granted &lt;/Directory&gt; ErrorLog /var/log/httpd/example1.com-error.log CustomLog /var/log/httpd/example1.com-access.log combined</VirtualHost>
  2. Create the configuration file for example2.com: sudo nano /etc/httpd/conf.d/example2.com.conf Add the following content: <VirtualHost *:80> ServerName example2.com ServerAlias www.example2.com DocumentRoot /var/www/example2.com/public_html&lt;Directory /var/www/example2.com/public_html&gt; AllowOverride All Require all granted &lt;/Directory&gt; ErrorLog /var/log/httpd/example2.com-error.log CustomLog /var/log/httpd/example2.com-access.log combined</VirtualHost>

Step 5: Check Configuration Syntax

Before restarting Apache, check your configuration for syntax errors:

sudo apachectl configtest

If you see Syntax OK, you can proceed.

Step 6: Restart Apache

To apply the changes, restart the Apache service:

sudo systemctl restart httpd

Step 7: Update Firewall Settings

If you have a firewall running, you may need to allow HTTP traffic. You can do this with:

sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --reload

Step 8: Update DNS Records

Point each domain to your server’s IP address by updating the DNS records at your domain registrar. Make sure to create A records for both example1.com and example2.com pointing to your server’s IP.

Step 9: Access Your Websites

After updating the DNS records (which may take some time), you should be able to access your sites:

  • http://example1.com
  • http://example2.com

Conclusion

Congratulations! You’ve successfully set up Apache Virtual Hosts on CentOS 8, enabling you to host multiple domains on a single server. This method provides you with a flexible solution for managing your websites effectively. For additional security, consider setting up SSL certificates using Let’s Encrypt or another provider.

If you have any questions or run into any issues, feel free to leave a comment below, and happy hosting!