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 ├── dev.yoursite.com │ └── html │ └── index.html ├── html └── prod.yoursite.com └── html
dev.yoursite.com and prod.yoursite.com 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/dev.yoursite.com sudo chown -R apache: /var/www/prod.yoursite.com
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. dev.yoursite.com.conf:
<VirtualHost *:80> ServerName dev.yoursite.com ServerAlias www.dev.yoursite.com ServerAdmin firstname.lastname@example.org DocumentRoot /var/www/dev.yoursite.com/html <Directory /var/www/dev.yoursite.com/html> Options -Indexes +FollowSymLinks AllowOverride All </Directory> ErrorLog /var/log/httpd/dev.yoursite.com-error.log CustomLog /var/log/httpd/dev.yoursite.com-access.log combined </VirtualHost>
Now, test the configuration file using this command:
sudo apachectl configtest
If everything is good and there are no syntax errors. You will get:
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.