How to Setup Apache Virtual Host on CentOS 7

In this article, We are going to perform How to setup Apache Virtual Host on CentOS 7 or any other cloud platform like Amazon EC2, Azure VM, Google Cloud Compute, etc. with preinstalled CentOS 7.

Introduction:

Apache is one of most used free and open source web server software which is used to run web applications, around 40% websites uses Apache web server.

What is Apache Virtual Host

Apache virtual host is used to run more than one website on Single Instance/Server/Virtual Machine using virtual host/vhost configuration file.

There are two types of Virtual Host.

  • Name Based Virtual Host
  • IP Based Virtual Host

Name Based Virtual Host: It is used to host multiple website on a single IP Address.

IP Based Virtual Host: It is used to used to host multiple websites on different IP Address.

Prerequisite:

  • CentOS 7 with Minimal Installation
  • SSH Access with sudo privileges
  • Open Firewall Port: 80,443

Update the system package

$ sudo yum update

Install Apache on CentOS 7

We have to install apache package to setup Apache virtual host. Run below command to install apache on CentOS 7.

$ sudo yum install httpd -y

Create a Project Directory Structure

By default Apache run “index” page from /var/www/html directory, if you want to run single website in Apache web server you can copy your project code in /var/www/html directory OR you can create directory as per your domain/project.

Here we are creating Project directory to store domain/website files.

$ sudo mkdir -p /var/www/test.fosstechnix.com/public_html

Creating Simple index.html file for testing

For testing create a simple index.html file

$ sudo nano /var/www/test.fosstechnix.com/public_html/index.html

Paste the below lines

<html>
        <head>
           <meta charset="utf-8">
             <title>Welcome to test.fosstechnix.com</title>
       </head>
          <body>
               <h1>This is Testing Page</h1>
          </body>
</html>

Next change the permission domain root directory to apache user and set the permission to /var/www/ directory with chmod.

$ sudo chown -R apache:apache /var/www/yourdomain.com/public_html
$ sudo chmod -R 755 /var/www

How to setup Apache Virtual Host on CentOS 7

Create Apache Host configuration  in /etc/httpd/conf.d/ directory with .conf file extension.

Open your favorite editor and create the virtual host configuration file.

$ sudo nano /etc/httpd/conf.d/test.fosstechnix.com.conf

Paste the below configuration file

<VirtualHost *:80>

        ServerAdmin [email protected]

        ServerName test.fosstechnix.com

        ServerAlias test.fosstechnix.com

        DocumentRoot /var/www/test.fosstechnix.com/public_html/

                <Directory /var/www/test.fosstechnix.com/public_html/>

                 Order allow,deny

                Allow from all

                Require all granted

                Options FollowSymLinks

                AllowOverride All

       </Directory>

       </VirtualHost>

To verify the syntax

$ sudo apachectl configtest

OR

$ httpd -t

Output:

Syntax OK

Restart the httpd service to take effect

$ sudo systemctl restart httpd

Testing the Virtual Host

To test virtual host file, Open /etc/hosts file add server IP and domain name OR if you have purchased domain name from domain provider like GoDaddy, namecheap, etc., then add A record with domain name and Server IP.

$ sudo nano /etc/hosts
....

192.168.100.51 test.fosstechnix.com

....

Open your favorite browser and type the domain name, you will see index.html web page

http://test.fosstechnix.com

If you are seeing index web page, then we have successfully performed setup Apache virtual host on CentOS 7.

Redirect HTTP to HTTPS in Apache using Virtual Host

It is recommended to secure your domain using secure protocol and SSL certificate. Here we are redirecting http to https in apache using virtual host and adding SSL certificates.

First create a directory where you will add SSL certificates, here we are coping SSL certificates in /var/www/ssl directory

$ sudo mkdir -p /var/www/ssl

Add the below highlighted lines in virtual host configuration if you have already created virtual host like above

<VirtualHost *:80>

        ServerAdmin [email protected]

        ServerName test.fosstechnix.com

        ServerAlias test.fosstechnix.com

        Document Root /var/www/test.fosstechnix.com/public_html/

        Redirect / https:// test.fosstechnix.com /

        <Directory /var/www/test.fosstechnix.com/public_html/>

                 Order allow,deny

                Allow from all

                Require all granted

                Options FollowSymLinks

                AllowOverride All

       </Directory>

       RewriteEngine On

       RewriteCond %{HTTPS} Off

       RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</VirtualHost>

<VirtualHost *:443>

        ServerName test.fosstechnix.com

        ServerAlias test.fosstechnix.com

        SSLEngine On

        SSLCertificateFile /var/www/ssl/60a18ab0a4ef3259.crt

        SSLCertificateKeyFile /var/www/ssl/*.fosstechnix.com.key

        DocumentRoot /var/www/test.fosstechnix.com/public_html/

        <Directory /var/www/test.fosstechnix.com/public_html/>

               Order allow,deny

               Allow from all

               Require all granted

               AllowOverride All

       </Directory>

</VirtualHost>

After adding below configuration enable Rewrite module to redirect http to https.

$ sudo nano /etc/httpd/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so

Redirect HTTP to HTTPS using .htaccess

If you are using .htaccess file in your web server , add below lines to redirect http to https,

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

To verify the syntax

sudo apachectl configtest

OR

$ httpd -t

Output:

Syntax OK

Reload the Apache service

$ sudo systemctl reload httpd

Restart the httpd service to take effect

$ sudo systemctl restart apache2

Add Firewall Rule

If you are using firewall on centos 7, open port 80 and 443 using below command.

$ sudo firewall-cmd --permanent --add-port=80/tcp

$ sudo firewall-cmd --permanent --add-port=443/tcp

$ sudo firewall-cmd --reload

Open your favorite browser and access your URL using https.

https://test.fosstechnix.com

Successfully we have covered How to setup Apache Virtual Host on CentOS 7.

Start/Stop/Restart/Enable httpd Service on CentOS

$ sudo systemctl start https
$ sudo systemctl stop httpd
$ sudo systemctl restart httpd
$ sudo systemctl enable httpd

Conclusion

In this article, We have covered How to setup Apache Virtual Host on CentOS 7 and Redirect HTTP to HTTPS in Apache using Virtual Host, Redirect HTTP to HTTPS using .htaccess,Start/Stop/Restart/Enable httpd Service on CentOS.

Related Articles

How to Setup Apache Virtual Host on Ubuntu 18.04/16.04 LTS

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link