A tutorial on configuring Apache Virtual Hosts on CentOS 8

Apache Virtual Host allows you to run more than one website on a single server machine. With Virtual Host, you can define the root of the site document (the directory that contains the website files), create separate security policies for each site, use multiple SSL certificates and much more.

In this tutorial, we’ll provide step-by-step instructions on how to set up Virtual Apache host On CentOS 8 server.

precondition

Make sure you meet the following prerequisites before continuing with this tutorial:

  • You have a domain name that points to the IP address of the public server. we will use example.com.
  • Install Apache by following these instructions.
  • Log in as root or a user with sudo privileges.

Create the directory structure

The document root is the directory where the domain name website files are stored and provides information in response to visitor requests. You can set the document root anywhere you want, in this directory we will use the following directory structure:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html
├── domain3.com
│   └── public_html

Basically, we will create a separate directory for each domain we want to host on our server within the directory /var/www. Inside each of these directories, we’ll create one public_html Which will store domain site files.

Start by creating the domain root directory example.com:

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

For testing purposes, create a index.html In the document root directory for the domain.

Open a text editor nano And create an experimental file:

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

Copy and paste the following code

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Selamat Datang</title>
  </head>
  <body>
    <h1>Testing VirtualHost di CentOS 8 Berhasil!</h1>
  </body>
</html>

In this directory we run the command as sudo user and the newly created files and directories automatically belong to the root (super admin).

To avoid security permissions issues, we will change the ownership of the domain document’s root directory to the user apache Using the chown command:

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

Create a virtual host file

There are several ways to set up a virtual host. You can add all virtual host directives in one file or create a different configuration file for each virtual host directive. Personally, I prefer the second approach because it is more defensible.

By default, Apache is configured to load all configuration files ending in .conf From the directory /etc/httpd/conf.d/.

To create a virtual host for a specific website, open the text editor of your choice and create the following basic Virtual Host configuration file:

sudo nano /etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/example.com/public_html

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

    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
  • ServerName: Domain must match virtual host configuration. The server name must contain the domain name.
  • ServerAlias: If you want the domain to be www.example.com, use ServerAlias.
  • DocumentRoot: The directory where Apache will serve your domain files and also where you will store your website files.
  • Options: These directives control the server features available in a particular directory.
  • -Indexes Prevents Apache from listing directories.
  • FollowSymLinks: This option tells the web server to follow symbolic links.
  • AllowOverride: Specifies the directives declared in the .htaccess file that can replace the configuration directives.
  • ErrorLogAnd the CustomLog : Specifies a location for the log files.

You can name the configuration file however you want, but the best practice is to use the domain name as the name of the virtual host configuration file. Note that all Apache configuration files must end with the .conf.

Test the structure of the configuration file with the command:

sudo apachectl configtest

If there are no errors, the command will output the following:

Syntax OK

To activate the newly created virtual host, restart the Apache service with:

sudo systemctl restart httpd

Verify that everything is working as expected, by going to your domain:

VirtualHost has been successfully tested on CentOS 8

Conclusion

In this tutorial, we learned how to configure an Apache virtual host. You can repeat the steps above and create additional virtual hosts for all domains.

If you want to secure your website with a free SSL certificate, you can install Apache and Let’s encrypt SSL certificate.

.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *