Apache Web Server Centos 7

  



Virtual Hosting is a method of hosting multiple domains on single web server. If you have multiple domains (such as domain1.com, domain2.com and so on) and want to host on single web server, Virtual Web Hosting is your right choice. Apache web server provides an easy way to manage Virtual Hosting. Virtual Hosting can be either Name Based or IP Based. In Name Based Virtual Hosting, multiple domains can be hosted on single IP address. On the other hand, in IP Based Virtual Hosting, each domain is mapped on a dedicated IP address. In my previous article, I discussed how to configure Named Based Apache Virtual Web Hosting on CentOS 7 Linux. In this article, I will discuss how to configure IP Based Apache Virtual Hosting on CentOS 7 Linux.

IP Based Apache Virtual Hosting Configuration

On a CentOS server, the package manager used to install the Apache web server will default to placing the main Apache configuration file in of one of several locations on the server. If you have access to the server via command line, you can confirm the exact location that Apache is loading its configuration file from. In this chapter, we will learn a little about the background of how Apache HTTP Server came into existence and then install the most current stable version on CentOS Linux 7. Brief History on Apache WebServer. Apache is a web server that has been around for a long time. In fact, almost as long as the existence of http itself!

We are now going to start Virtual Hosting configuration with apache web server. It is assumed that you have already installed CentOS 7 Server on your physical or virtual machine. If you don’t have CentOS 7 Server installed, take a look of my previous article about CentOS 7 installation and configuration with GNOME Desktop and install CentOS 7 accordingly and then follow this article. Complete Apache Virtual Hosting configuration on CentOS 7 can be divided into two parts.

  • DNS configuration for the hosting domains
  • Apache webserver configuration for Virtual Hosting

Part 1: DNS Configuration for the Hosting Domains

If you have registered domains and want to host them on your own web server, create a Host record (A record) with wildcard value (@) that will point to your webserver. In this case, your webserver must have on public IP addresses.

For this article configuration, we will use custom domains (systemzone.net, systemzone.com and systemzone.org) and these domains entry will keep in a local DNS Server. So, our webserver is also on local IP address.

In my previous article, I discussed how to configure a local DNS server on CentOS 7 with BIND package. According to that configuration, DNS zone entries are in named.rfc1912.zones file. So, open this file and put the following entries (for the three custom domains) at the bottom.

[root@ns1 ~]# vim /etc/named.rfc1912.zoneszone “systemzone.net” IN {

type master;

file “systemzone.net.for”;

allow-update { none; };

};

zone “systemzone.com” IN {

type master;

file “systemzone.net.for”;

Web

allow-update { none; };

};

zone “systemzone.org” IN {

type master;

file “systemzone.net.for”;

allow-update { none; };

};

Notice that the three custom domains are pointing the same zone file (systemzone.net.for). So, open that zone file and put Host record (A record) in this file for the custom domains. Your zone file looks like the following example.

[root@ns1 ~]# vim /var/named/systemzone.net.for$TTL 1D

@ IN SOA ns1.systemzone.net. root.systemzone.net. (

0 ; serial

1D ; refresh

1H ; retry

1W ; expire

3H ) ; minimum

@ IN NS ns1.systemzone.net.

$ORIGIN systemzone.net.

ns1 IN A 192.168.40.100

@ IN A 192.168.40.101

www IN A 192.168.40.101

$ORIGIN systemzone.com.

@ IN A 192.168.40.102

www IN A 192.168.40.102

$ORIGIN systemzone.org.

@ IN A 192.168.40.103

www IN A 192.168.40.103

Notice that each custom domain’s wildcard (@) Host record is pointing to different IP address. Actually, these IP addresses are our Apache Web Server’s IP addresses.

We will now configure IP Based Virtual Hosting in our apache webserver so that each domain gets different IP address.

Part 2: Apache Web Server Configuration for IP Based Virtual Hosting

We will first install and enable Apache Web Server and then configure IP Based Virtual Hosting. 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.

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

As we have planned three custom domains (systemzone.net, systemzone.com and systemzone.org) will be mapped with the three IP addresses (192.168.40.101, 192.168.40.102 and 192.168.40.103) respectively, put these secondary IP addresses in your CentOS 7 Linux with the nmtui tool. Your nmtui configuration GUI will look like the following image.

We will now declare Virtual Host 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/systemzone.net

ServerName systemzone.net

ServerAlias www.systemzone.net

</VirtualHost>

<VirtualHost 192.168.40.102>

DocumentRoot /var/www/html/systemzone.com

ServerName systemzone.com

ServerAlias www.systemzone.com

</VirtualHost>

<VirtualHost 192.168.40.103>

Apache on centos

DocumentRoot /var/www/html/systemzone.org

ServerName systemzone.org

ServerAlias www.systemzone.org

</VirtualHost>

In the above lines, we have declared three VIrtualHost on our webserver for the three custom domains. Each VirtualHost has the following three properties.

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 systemzone.net systemzone.com systemzone.org

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

[root@webserver html]# cd systemzone.net

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

<html>

<head>

<title>systemzone.net</title>

</head>

<body>

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

</body>

</html>

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

[root@webserver html]# cd systemzone.com[root@webserver systemzone.com]# vim index.html

<html>

<head>

<title>systemzone.com</title>

</head>

<body>

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

</body>

Apache web server tutorialInstall apache web server on centos 7

</html>

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

[root@webserver html]# cd systemzone.com[root@webserver systemzone.com]# vim index.html

<html>

<head>

<title>systemzone.com</title>

</head>

<body>

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

Apache Web Server Centos 7

</body>

</html>

Now open your browser and type your custom domain in URL bar. If everything is OK, your will get the respected content of each domain.

If you face any confusion to follow the above steps carefully, watch the following video about IP Based Apache Virtual Hosting. I hope, it will reduce your any confusion.

How to configure IP Based Apache Virtual Hosting on CentOS 7 Linux has been discussed in this article. I hope you will now be able to create IP Based Apache Virtual Hosting for your domains following the above steps properly. However, if you face any confusion to create Virtual Hosting with Apache web server, feel free to discuss in comment or contact with me from Contact page. I will try my best to stay with you.

Apache HTTP server is the most popular web server in the world. It is a free, open-source and cross-platform HTTP server providing powerful features which can be extended by a wide variety of modules. The following instructions describe how to install and manage the Apache web server on your CentOS 7 machine.

Apache is available in the default CentOS repositories and the installation is pretty straight forward. On CentOS and RHEL the Apache package and the service is called httpd. To install the package run the following command:

Once the installation is completed, enable and start the Apache service:

If your server is protected by a firewall you need to open HTTP and HTTPS ports, 80 and 443. Use the following commands to open the necessary ports:

Now that we have Apache installed and running on our CentOS 7 server we can check the status and the version of the Apache service, with:

Finally to verify if everything works properly, open your server IP address http://YOUR_IP in your browser of choice, and you will see the default CentOS 7 Apache welcome page as shown below:

We can manage the Apache service same as any other systemd unit.

Setup Apache Web Server Centos 7

To stop the Apache service, run:

To start it again, type:

How To Harden The Apache Web Server On Centos 7

To restart the Apache service:

To reload the Apache service after you made some configuration changes:

If you want to disable the Apache service to start at boot:

And to re-enable it again:

  • All Apache configuration files are located in the /etc/httpd directory.
  • The main Apache configuration file is /etc/httpd/conf/httpd.conf.
  • All config files ending with .conf located in the /etc/httpd/conf.d directory are included in main Apache configuration file.
  • Configuration files which are responsible for loading various Apache modules are located in the /etc/httpd/conf.modules.d directory.
  • For better maintainability it is recommended to create a separate configuration file (vhost) for each domain.
  • New Apache vhost files must end with .conf and be stored in /etc/httpd/conf.ddirectory. You can have as many vhosts as you need.
  • It is a good idea to follow a standard naming convention, for example if your domain name is mydomain.com then you the configuration file should be named /etc/httpd/conf.d/mydomain.com.conf
  • Apache log files (access_log and error_log) are located in the /var/log/httpd/ directory. It is recommended to have a different access and error log files for each vhost.
  • You can set your domain document root directory to any location you want. The most common locations for webroot include:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>