Categories: How ToServers

Set Up Apache Virtual Hosts on CentOS 8

The Apache web server is the most popular way of serving web content on the internet. It accounts for more than half of all active websites on the internet and is extremely powerful and flexible. 

Apache Virtual Hosts allows you to run more than one website on a single machine. With Virtual Hosts, you can specify the site document root (the directory which contains the website files), create a separate security policy for each site, use different SSL certificates and much more.

In this tutorial we will show you how to set up Apache Virtual Host on a CentOS 8 server.

Installing Apache

Apache is available in the default CentOS repositories.

# dnf install httpd

Enable and start the Apache service.

# systemctl enable httpd
# systemctl start httpd

Open your web browser and go to your server. http://server-ip/ you will see something like this.

Create the directory structure for each host

We will create a separate directory for each domain we want to host on our server inside the /var/www directory.

# mkdir -p /var/www/example.com/

Apache should be made the owner of that folder.

# chown -R apache: /var/www/example.com

Creating Virtual Host

By default, Apache is configured to load all configuration files that ends with .conf from the /etc/httpd/conf.d/ directory.

# nano /etc/httpd/conf.d/example.com.conf

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com

DocumentRoot /var/www/example.com
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined

</VirtualHost>

To test the Apache configuration, run the following command.

# httpd -t
Syntax OK

Restart Apache service.

# systemctl restart httpd

Test your Results

To test your virtual host, you’ll have to create a phpinfo file to the document root directory.

# echo "<?php phpinfo();" > /var/www/example.com/index.php

Now we can test our webserver. Go to your browser and access your server hostname. You should see your server’s PHP information.

This means virtual host can run properly with Apache web server. You have successfully set it up.

ref: https://unihost.com/help/set-up-apache-virtual-hosts-on-centos-8/

ferisetyawanmyid

Share
Published by
ferisetyawanmyid

Recent Posts

Best SSH Clients for Linux: Top Tools for Secure Remote Connections

If you manage Linux servers regularly, you know how important SSH (Secure Shell) is. Whether…

5 days ago

🐧 Best Linux Distributions for Beginners and Daily Use (2025 Edition)

If you’re new to Linux or thinking about switching from Windows or macOS, this guide…

1 week ago

Essential SRE Tools That Run on Linux

Introduction Site Reliability Engineering (SRE) has become a critical discipline in modern IT operations. The…

4 weeks ago

Best Free CRM Websites for Businesses in 2025

Updated: August 17, 2025 A practical guide to the best free CRM (Customer Relationship Management)…

1 month ago

Free Website Hosting Control Panel for Linux

If you run a VPS or dedicated server with Linux, you probably know that managing…

2 months ago

How to Find Out Who Rebooted Your Ubuntu Server (Step-by-Step Guide)

Short summary: Use built-in Linux tools (`who`, `last`, `journalctl`, `/var/log/auth.log`) and optional audit logs to…

2 months ago

This website uses cookies.