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

Free Website Hosting Control Panel for Linux

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

2 weeks 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 weeks ago

🧾 Basic Linux Command Cheat Sheet for Beginners

Whether you're a new Linux user or a beginner sysadmin, mastering the terminal is a…

3 weeks ago

Best Free CMS Platforms for Self-Hosted Blogs and Websites [2025 Edition]

Choosing the right Content Management System (CMS) is key when you're building a blog, portfolio,…

4 weeks ago

🛡️ Protect Your Ubuntu Server with Fail2Ban: Step-by-Step Guide

Securing your Linux server is a critical task for every sysadmin. One of the simplest…

4 weeks ago

📡 Getting Started with tcpdump: A Beginner’s Guide for Linux Sysadmins

As a Linux system administrator, understanding what happens on your network is crucial. One of…

1 month ago

This website uses cookies.