• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Set Up Apache Virtual Hosts on CentOS 8

Written by Admin, Updated On May 9, 2020
apache, centos, httpd, virtualhost
How to Set Up Apache Virtual Hosts on CentOS 8

Using Apache Virtual Hosts, you can encapsulate configuration details and host multiple websites on a single server. Using it you can customize each website by creating separate document root, security policy and different SSL certificates. This tutorial covers how to set up Apache Virtual Hosts on CentOS 8 server.

Prerequisites#

  • A CentOS 8 server with a non-root user with sudo privileges.
  • Apache should installed and configured, as shown in How to Install Apache on CentOS 8.
  • A domain name should pointing to your server IP address.

Create Directory Structure#

First, we will create a directory where website code files of a domain will store and serve response to visitors. Generally, it called DocumentRoot. You can set the document root to any location as per your choice but it’s best practice to set in directory structure. Commonly, at /var/www directory.

/var/www/
├── example1.com
│   └── public_html
├── example2.com
│   └── public_html

Here, we need to create separate directory inside /var/www directory for each domain which we want to host on our server.

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

For testing purpose we will create a index.html file inside the domain document root directory. This page will be show by default when visitors will visit your site.

Creating a new index.html file using your favorite text editor by typing :

sudo nano /var/www/example1.com/public_html/index.html

Add the below lines into it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Welcome!!</title>
  </head>
  <body>
    <h1>Success! example1.com set up completed!</h1>
  </body>
</html>

Since all commands are executed as sudo user, so newly created files and directories are owned by the root user. We will change document root directories ownership to avoid permission issue for our regular user. Therefore, regular user can modify files in our web directories without any issues.

sudo chown -R apache: /var/www/example1.com

Creating Virtual Host Files#

There are multiple ways to set up a virtual host. You can make separate file for each Virtual Host Directive or you can add all Virtual Host Directives in a single file. It’s recommended to make separate file for each domain because of it’s maintainability.

On CentOS, Apache will load all .conf files from /etc/httpd/conf.d/ directory because of it’s default configuration. So now we will create a separate virtual host.

Create a new file using your choice text editor by typing :

sudo nano /etc/httpd/conf.d/example1.com.conf
<VirtualHost *:80>
    ServerName example1.com
    ServerAlias www.example1.com
    DocumentRoot /var/www/example1.com/public_html

    <Directory /var/www/example1.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog /var/log/httpd/example1.com-error.log
    CustomLog /var/log/httpd/example1.com-access.log combined
</VirtualHost>
  • ServerName: This should be your domain name and match with virtual host configuration.
  • ServerAlias: All other domains or subdomains that should match for this virtual host as well, usually the www subdomain.
  • DocumentRoot: Path of virtual host directory that from which Apache will serve the domain files.
  • Options: This directive controls which server features are available in a specific directory.
  • -Indexes: It will prevent directory listings.
  • FollowSymLinks: Apache will follow the symbolic links if this option is enabled.
  • AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
  • ErrorLog, CustomLog: Specifies the location for log files.

You can give any names to your configuration file but it’s recommended to give file name same as domain name.

Next, check the syntax by type :

sudo apachectl configtest

It will show below output if there are no errors:

Output
Syntax OK

You must restart apache2 service to make active newly created virtual hosts :

sudo systemctl restart httpd

Finally, you can verify by opening your http://example1.com to your web browser and it will show you as following :

Apache Virtual Hosts

Conclusion#

In this tutorial you have learned how to create and set up Apache Virtual Hosts on CentOS 8 system. You can repeat same steps for multiple domains.

Please leave a comment below, if you have any question or feedback.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Set Up a Firewall with UFW on Ubuntu 20.04
Next Article   How to Install Yarn on Ubuntu 20.04 LTS

Related Posts

  • How to Install GCC Compiler on Debian 11

    How to Install GCC Compiler on Debian 11

    December 11, 2022
  • How to Install Apache Cassandra on Ubuntu 22.04

    How to Install Apache Cassandra on Ubuntu 22.04

    December 3, 2022
  • How to Install Apache on Debian 11

    How to Install Apache on Debian 11

    November 10, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Jenkins on Debian 11
    How to Install Jenkins on Debian 11 January 5, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook