Name based+SSL

-------

Configure Name based Virtual Hosting on CentOS 7

Step: 1. To install apache

# yum install httpd -y

Installed:

httpd.x86_64 0:2.4.6-45.el7.centos.4

Dependency Installed:

apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7

httpd-tools.x86_64 0:2.4.6-45.el7.centos.4 mailcap.noarch 0:2.1.41-2.el7

Complete!

[root@p /]# systemctl restart httpd

[root@p /]# systemctl enable httpd

#  vi /etc/httpd/conf/httpd.conf

# Load config files in the "/etc/httpd/conf.d" directory, if any.

IncludeOptional conf.d/*.conf      # vhosts file Location

Step:2 To Create the directory for Virtual Host name

[root@mail htm]# mkdir -p /var/www/html/www.srini.com

         mkdir -p /var/log/httpd/www.srini.com

[root@mail htm]# mkdir -p /var/www/html/www.sk.com

           mkdir -p /var/log/httpd/www.sk.com

[root@mail htm]# mkdir -p /var/www/html/www.dilli.com

           mkdir -p /var/log/httpd/www.dilli.com

[root@mail htm]# ls

http://www.dilli.com http://www.sk.com http://www.srini.com

Step:3 To create index.html as per hosting namewise like, Similarly, to create sk and srini

#  echo "Welcome to My dilli Website" > /var/www/html/www.dilli.com/index.html

OR

# vi /var/www/html/index.html

<html>   <head>     <title>dilli.com</title>   </head>   <body>     <h1>dilli.com web page!</h1>   </body> </html>

                                ----------------vhost--------------------

<html>

     <head>

      <title>www.dilli.com</title>

 </head>

 <body>

     <h1>The dilli.com virtual host is working!</h1>

     </body>

</html>

                                     --------------------------

echo "Welcome to My WorldCM Website" > /var/www/html/index.html

Step:4 To setup ownership and permissions

[root@mail htm]# chown -R apache:apache /var/www/html/www.dilli.com/

[root@mail htm]# chown -R apache:apache /var/www/html/www.sk.com/

[root@mail htm]# chown -R apache:apache /var/www/html/www.srini.com/

[root@mail htm]# chmod -R 755 /var/www/html

Step:5 To setup Name based virtual hosting on apache

[root@ conf.d]# pwd

/etc/httpd/conf.d

[root@ conf.d]# vi vhost.conf

                              vi  httpd-vhost.conf

                                --------------------------------------------------------------------------------------

NameVirtualHost *:80           # if problem then ADD

# For original domain

<VirtualHost *:80>

DocumentRoot /var/www/html

ServerName www.worldcm.net

</VirtualHost>

# For virtual domain

<VirtualHost *:80>

ServerName www.dilli.com

ServerAlias dilli.com

DocumentRoot /var/www/html/www.dilli.com

ErrorLog /var/log/httpd/www.dilli.com/error.log

CustomLog /var/log/httpd/www.dilli.com/access.log combined

</VirtualHost>

<VirtualHost *:80>

ServerName www.sk.com

ServerAlias sk.com

DocumentRoot /var/www/html/www.sk.com

ErrorLog /var/log/httpd/www.sk.com/error.log

CustomLog /var/log/httpd/www.sk.com/access.log combined

</VirtualHost>

<VirtualHost *:80>

ServerName www.srini.com

ServerAlias srini.com

DocumentRoot /var/www/html/www.srini.com

ErrorLog /var/log/httpd/www.srini.com/error.log

CustomLog /var/log/httpd/www.srini.com/access.log combined

</VirtualHost>

                                                     ---------------------------------------------------x----------------------------------------------------------

[root@proxy conf.d]# apachectl configtest

Syntax OK

[root@proxy conf.d]# systemctl restart httpd

Step:6 To add the virtual hosts for DNS[root@proxy conf.d]#   vi /etc/hosts

127.0.0.1 localhost.localdomain localhost

::1 localhost6.localdomain6 localhost6

10.100.100.123 http://www.dilli.com

10.100.100.123 http://www.sk.com

10.100.100.123 http://www.srini.com

Step:7 Go to browser for Test

http://www.dilli.com

Result : The Dilli.com virtual host is working!

http://www.sk.com

Result : The SK.com virtual host is working!

http://www.srini.com

Result : The Srini.com virtual host is working!

                                                                                  --------SSL----------

To editing the SSL config file:

[root@web ssl]# vi /etc/httpd/conf.d/ssl.conf

Uncomment the DocumentRoot and ServerName like as follows:

DocumentRoot “/var/www/html”

ServerName 192.168.80.83:443

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/server.crt

SSLCertificateKeyFile /etc/pki/tls/certs/server.key

[root@web ssl]# systemctl restart httpd

For Virtual Apache Hosting as follow:

[root@web ssl]# vi /etc/httpd/conf.d/vhost.conf

NameVirtualHost *:443          # if problem then ADD

# For original domain

<VirtualHost *:443>

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/server.crt

SSLCertificateKeyFile /etc/pki/tls/certs/server.key

DocumentRoot /var/www/html

ServerName www.worldcm.net

</VirtualHost>

# For virtual domain

</VirtualHost>

<VirtualHost *:443>

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/server.crt

SSLCertificateKeyFile /etc/pki/tls/certs/server.key

ServerName www.dilli.com

ServerAlias dilli.com

DocumentRoot /var/www/html/www.dilli.com

ErrorLog /var/log/httpd/www.dilli.com/error.log

CustomLog /var/log/httpd/www.dilli.com/access.log combined

</VirtualHost>

<VirtualHost *:443>

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/server.crt

SSLCertificateKeyFile /etc/pki/tls/certs/server.key

ServerName www.sk.com

ServerAlias sk.com

DocumentRoot /var/www/html/www.sk.com

ErrorLog /var/log/httpd/www.sk.com/error.log

CustomLog /var/log/httpd/www.sk.com/access.log combined

</VirtualHost>

<VirtualHost *:443>

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/server.crt

SSLCertificateKeyFile /etc/pki/tls/certs/server.key

ServerName www.srini.com

ServerAlias srini.com

DocumentRoot /var/www/html/www.srini.com

ErrorLog /var/log/httpd/www.srini.com/error.log

CustomLog /var/log/httpd/www.srini.com/access.log combined

</VirtualHost>

systemctl restart httpd

------------------------------------------------------------------------------

#  vi /etc/httpd/conf.d/vhost.conf

#       DocumentRoot /var/www/html/postfixadmin/public/

#</VirtualHost>

<VirtualHost 105.229.122.123:80>

        ServerName worldcm.net

        ServerAlias www.worldcm.net

        DocumentRoot /var/www/html/worldcm.net/

</VirtualHost>

------------------------------------X------------------------------------------------

Apache Webserver Configuration for Virtual Hosting

Although I have discussed the complete web server configuration with Apache, MariaDB and PHP in my previous article, I will show the basic part of apache web server installation and configuration for Virtual Hosting here. So, issue the following command to install apache web server in your CentOS 7 Linux.

[root@webserver ~]# yum install httpd -y

The httpd package will be installed within a few second. After installing apache httpd package, we have to start the Apache service with the following command.

[root@webserver ~]# systemctl start httpd

Apache service is now active and running and waiting for the incoming web server (http) requests. The daemon will now answer any incoming http request.

But if your server gets rebooted in any case, the httpd daemon will not be stated automatically. So, run the following command to start apache service automatically if any system restart is occurred.

[root@webserver ~]# systemctl enable httpd.

Run the following firewall commands to allow http service through your firewall. Otherwise your webserver cannot be accessed from remote PC.

[root@webserver ~]# firewall-cmd –zone=public –add-service=http

[root@webserver ~]# firewall-cmd –permanent –zone=public –add-service=http

[root@webserver ~]# firewall-cmd –reload

We will now declare Virtual Hosting in apache configuration file located in /etc/httpd/conf directory. So, go to that directory and open apache configuration file (httpd.conf ) and add the following lines at the bottom.

[root@webserver ~]# cd /etc/httpd/conf

[root@webserver conf]# vim httpd.conf

<VirtualHost 192.168.40.101>

DocumentRoot /var/www/html/worldcm.net

ServerName worldcm.net

ServerAlias www.worldcm.net

</VirtualHost>

<VirtualHost 192.168.40.101>

DocumentRoot /var/www/html/worldcm.com

ServerName worldcm.com

ServerAlias www.worldcm.com

</VirtualHost>

<VirtualHost 192.168.40.101>

DocumentRoot /var/www/html/worldcm.org

ServerName worldcm.org

ServerAlias www.worldcm.org

</VirtualHost>

[root@webserver conf]# httpd -t

Syntax OK

As we have declared three root directories for three custom domains, we will now create these directories first. So, issue the following command to create three declared directories in html directory.

[root@webserver conf]# cd /var/www/html

[root@webserver html]# mkdir worldcm.net worldcm.com worldcm.org

Now go to worldcmnet directory and create an index file (index.html) and then put the following html content.

[root@webserver html]# cd worldcm.net

[root@webserver worldcm.net]# vim index.html

<html>

<head>

<title>worldcm.net</title>

</head>

<body>

<h1> Welcome to worldcm.net </h1>

</body>

</html>

Similarly, go to worldcm.com directory and create index file and put the following html content.

[root@webserver html]# cd worldcm.com

[root@webserver worldcm.com]# vim index.html

<html>

<head>

<title>worldcm.com</title>

</head>

<body>

<h1> Welcome to worldcm.com </h1>

</body>

</html>

Similarly, go to worldcm.org directory and create index file and put the following html content.

[root@webserver html]# cd worldcm.org

[root@webserverworldcm.org]# vim index.html

<html>

<head>

<title>worldcm.org</title>

</head>

<body>

<h1> Welcome to worldcm.org </h1>

</body>

</html>

Now open your browser and type your custom domain in URL bar. If everything is OK

www.worldcm.com

www.worldcm.net

www.worldcm.org

------------------

For example, suppose that you are serving the domain www.example.com and you wish to add the virtual host other.example.com, which points at the same IP address. Then you simply add the following to httpd.conf:

<VirtualHost *:80>     # This first-listed virtual host is also the default for *:80     ServerName www.example.com     ServerAlias example.com      DocumentRoot "/www/domain"</VirtualHost><VirtualHost *:80>     ServerName other.example.com     DocumentRoot "/www/otherdomain"</VirtualHost>

You can alternatively specify an explicit IP address in place of the * in <VirtualHost> directives. For example, you might want to do this in order to run some name-based virtual hosts on one IP address, and either IP-based, or another set of name-based virtual hosts on another address.

Many servers want to be accessible by more than one name. This is possible with the ServerAlias directive, placed inside the <VirtualHost> section. For example in the first <VirtualHost> block above, the ServerAlias directive indicates that the listed names are other names which people can use to see that same web site:

ServerAlias example.com *.example.com

                 -------------------------  OR  -----------------------------

#  vi /etc/httpd/conf.d/vhost.conf

<VirtualHost 115.129.127.223:80>

        ServerName worldcm.net

        ServerAlias www.worldcm.net

        DocumentRoot /var/www/html/cycloyarn.com/

</VirtualHost>

--------

#<VirtualHost 105.229.122.123:80>

#       ServerName mail.worldcm.net

#       DocumentRoot /var/www/html/rainloop/

#</VirtualHost>

#<VirtualHost 105.229.122.123:80>

#       ServerName mailadmin.worldcm.net