Last week a client asked me to setup a dev and production environments on their new Red Hat 8 box. I have setup Apache virtual hosts in Ubuntu in the past. I knew to setup Apache virtual hosts on Red Hat 8 should not be that difficult.

This is when I decided to take a walk by Pier 76 in New York City to recharge by brain.

First, make sure you have installed Apache in your Linux box, you’re logged in as root or a user with sudo and you’re pointing your domain name to your server’s public IP.

Create Your Directory Structure

This is called the document root for your site files. This can be set in any location in your Linux server, but we normally follow this structure example:

/var/www (this is the path for your web files)
├── cgi-bin
│   └── html
│       └── index.html
├── html
    └── html and are the two directories for your Apache server to serve your website from. We created another /html directory under each domain path. This is where your files will live.

Next, change the ownership of each document root. For example:

sudo chown -R apache: /var/www/
sudo chown -R apache: /var/www/

Apache by default loads all configuration files that end in .conf as the file extension inside the directory /etc/httpd/conf.d/

Next, using your favorite editor create a virtual host file inside /etc/httpd/conf.d/ directory. For example.

<VirtualHost *:80>
    DocumentRoot /var/www/
    <Directory /var/www/>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    ErrorLog /var/log/httpd/
    CustomLog /var/log/httpd/ combined

Now, test the configuration file using this command:

sudo apachectl configtest

If everything is good and there are no syntax errors. You will get:

Syntax OK

Then you need to activate the new virtual host by running this command in your Linux terminal:

sudo systemctl restart httpd

Visit your site by name or IP using your browser and let me know if all works.

Leave a comment

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