{"id":11713,"date":"2019-03-15T23:26:11","date_gmt":"2019-03-15T23:26:11","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11713"},"modified":"2019-03-15T23:26:11","modified_gmt":"2019-03-15T23:26:11","slug":"apache-virtual-hosting-ip-based-and-name-based-virtual-hosts-in-rhel-centos-fedora","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/apache-virtual-hosting-ip-based-and-name-based-virtual-hosts-in-rhel-centos-fedora\/","title":{"rendered":"Apache Virtual Hosting: IP Based and Name Based Virtual Hosts in RHEL\/CentOS\/Fedora"},"content":{"rendered":"<p>As we all are aware that\u00a0<strong>Apache<\/strong>\u00a0is a very powerful, highly flexible and configurable Web server for\u00a0<strong>Nix OS<\/strong>. Here in this tutorial, we are going to discuss one more feature of\u00a0<strong>Apache<\/strong>\u00a0which allows us to host more than one website on a single Linux machine. Implementing virtual hosting with Apache web server can help you to save costs you are investing on your server maintenance and their administration.<\/p>\n<p><b>Don\u2019t Miss<\/b>:\u00a0<a href=\"https:\/\/www.tecmint.com\/nginx-name-based-and-ip-based-virtual-hosts-server-blocks\/\" target=\"_blank\" rel=\"noopener\">NGINX Name-based and IP-based Virtual Hosting (Server Blocks)<\/a><\/p>\n<div id=\"attachment_5276\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/Apache-Virtual-hosting.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5276\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/Apache-Virtual-hosting.jpeg\" alt=\"Apache Virtual Hosting in Linux\" width=\"420\" height=\"280\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Apache Virtual Hosting in Linux<\/p>\n<\/div>\n<p>Concept of\u00a0<strong>Shared web hosting<\/strong>\u00a0and\u00a0<strong>Reseller web hosting<\/strong>\u00a0is based on this facility of\u00a0<strong>Apache<\/strong>\u00a0only.<\/p>\n<h4>Types of Virtual Host<\/h4>\n<p>There are two types of virtual hosting is available with Apache.<\/p>\n<h5>Name Based Virtual Hosting<\/h5>\n<p>With the name based virtual hosting you can host several\u00a0<strong>domains<\/strong>\/<strong>websites<\/strong>\u00a0on a single machine with a single<strong>IP<\/strong>. All domains on that server will be sharing a single IP. It\u2019s easier to configure than IP based virtual hosting, you only need to configure\u00a0<strong>DNS<\/strong>\u00a0of the domain to map it with its correct IP address and then configure Apache to recognize it with the domain names.<\/p>\n<div id=\"attachment_5272\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/Name-based-Virtual-hosting.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-5272\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/Name-based-Virtual-hosting-478x450.jpeg\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/Name-based-Virtual-hosting-478x450.jpeg 478w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/Name-based-Virtual-hosting.jpeg 502w\" alt=\"Name Based Virtual Hosting\" width=\"478\" height=\"450\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Name Based Virtual Hosting<\/p>\n<\/div>\n<h5>IP Based Virtual Hosting<\/h5>\n<p><center>With the IP based virtual hosting, you can assign a separate IP for each domain on a single server, these IP\u2019s can be attached to the server with single NIC cards and as well as multiple NICs.<\/center><\/p>\n<div id=\"attachment_5273\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/IP-based-Virtual-Hosting.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5273\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/IP-based-Virtual-Hosting.jpeg\" alt=\"IP Based Virtual Hosting\" width=\"440\" height=\"356\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">IP Based Virtual Hosting<\/p>\n<\/div>\n<p>Lets set up\u00a0<strong>Name Based Virtual Hosting<\/strong>\u00a0and\u00a0<strong>IP based Virtual hosting<\/strong>\u00a0in\u00a0<strong>RHEL, CentOS<\/strong>\u00a0and\u00a0<strong>Fedora<\/strong>.<\/p>\n<h5>Testing Environment<\/h5>\n<ol>\n<li><strong>OS<\/strong>\u00a0\u2013\u00a0<strong>CentOS 6.5<\/strong><\/li>\n<li><strong>Application<\/strong>\u00a0\u2013\u00a0<strong>Apache Web Server<\/strong><\/li>\n<li><strong>IP Address<\/strong>\u00a0\u2013\u00a0<strong>192.168.0.100<\/strong><\/li>\n<li><strong>IP Address<\/strong>\u00a0\u2013\u00a0<strong>192.168.0.101<\/strong><\/li>\n<li><strong>Domain<\/strong>\u00a0\u2013\u00a0<strong>www.example1.com<\/strong><\/li>\n<li><strong>Domain<\/strong>\u00a0\u2013\u00a0<strong>www.example2.com<\/strong><\/li>\n<\/ol>\n<h3>How to Setup IP Based and Name Based Apache Virtual Hosts<\/h3>\n<p>Before setting up virtual hosting with\u00a0<strong>Apache<\/strong>, your system must have Apache Web software installed. if not, install it using default package installer called yum.<\/p>\n<pre>[root@tecmint ~]# yum install httpd<\/pre>\n<h4>Setup Name Based Virtual Host<\/h4>\n<p>But, before creating a virtual host, you need to create a directory where you will keep all your website\u2019s files. So, create directories for these two virtual hosts under\u00a0<strong>\/var\/www\/html<\/strong>\u00a0folder. Please remember\u00a0<strong>\/var\/www\/html<\/strong>\u00a0will be your default\u00a0<strong>Document Root<\/strong>\u00a0in the Apache virtual configuration.<\/p>\n<pre>[root@tecmint ~]# mkdir \/var\/www\/html\/example1.com\/\r\n[root@tecmint ~]# mkdir \/var\/www\/html\/example2.com\/<\/pre>\n<p>To set up Name based virtual hosting you must need to tell Apache to which IP you will be using to receive the Apache requests for all the websites or domain names. We can do this with\u00a0<strong>NameVirtualHost<\/strong>\u00a0directive. Open Apache main configuration file with\u00a0<strong>VI<\/strong>\u00a0editor.<\/p>\n<pre>[root@tecmint ~]# vi \/etc\/httpd\/conf\/httpd.conf<\/pre>\n<p>Search for\u00a0<strong>NameVirtualHost<\/strong>\u00a0and uncomment this line by removing the\u00a0<strong>#<\/strong>\u00a0sign in front of it.<\/p>\n<pre>NameVirtualHost<\/pre>\n<p>Next add the IP with possible in which you want to receive Apache requests. After the changes, your file should look like this:<\/p>\n<pre>NameVirtualHost 192.168.0.100:80<\/pre>\n<p>Now, it\u2019s time to setup Virtual host sections for your domains, move to the bottom of the file by pressing\u00a0<strong>Shift + G<\/strong>. Here in this example, We are setting up virtual host sections for two domains<\/p>\n<ol>\n<li><strong>www.example1.com<\/strong><\/li>\n<li><strong>www.example2.com<\/strong><\/li>\n<\/ol>\n<p>Add the following two virtual directives at the bottom of the file. Save and close the file.<\/p>\n<pre>&lt;VirtualHost 192.168.0.100:80&gt;\r\n    ServerAdmin webmaster@example1.com\r\n    DocumentRoot \/var\/www\/html\/example1.com\r\n    ServerName www.example1.com\r\nErrorLog logs\/www.example1.com-error_log\r\nCustomLog logs\/www.example1.com-access_log common\r\n&lt;\/VirtualHost&gt;\r\n\r\n&lt;VirtualHost *:80&gt;\r\n    ServerAdmin webmaster@example2.com\r\n    DocumentRoot \/var\/www\/html\/example2.com\r\n    ServerName www.example2.com\r\nErrorLog logs\/www.example2.com-error_log\r\nCustomLog logs\/www.example2.com-access_log common\r\n&lt;\/VirtualHost&gt;<\/pre>\n<p>You are free to add as many directives you want to add in your domains virtual host section. When you are done with changes in\u00a0<strong>httpd.conf<\/strong>\u00a0file, please check the syntax of files with following command.<\/p>\n<pre>[root@tecmint ~]# httpd -t\r\n\r\nSyntax OK<\/pre>\n<p>It is recommended to check the syntax of the file after making some changes and before restarting the Web server because if any syntax goes wrong Apache will refuse to work with some errors and eventually affect your existing web server go down for a while. If syntax is\u00a0<strong>OK<\/strong>. Please restart your Web server and add it to\u00a0<strong>chkconfig<\/strong>to make your web server start in\u00a0<strong>runlevel 3<\/strong>\u00a0and\u00a0<strong>5<\/strong>\u00a0at the boot time only.<\/p>\n<pre>[root@tecmint ~]# service httpd restart\r\nStopping httpd:                                            [  OK  ]\r\nStarting httpd:                                            [  OK  ]<\/pre>\n<pre>[root@tecmint ~]# chkconfig --level 35 httpd on<\/pre>\n<p>Now it\u2019s time to create a test page called\u00a0<strong>index.html<\/strong>\u00a0add some content to the file so we will have something to check it, when the IP calls the virtual host.<\/p>\n<pre>[root@tecmint ~]# vi \/var\/www\/html\/example1.com\/index.html<\/pre>\n<pre>&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;www.example1.com&lt;\/title&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;h1&gt;Hello, Welcome to www.example1.com.&lt;\/h1&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<pre>[root@tecmint ~]# vi \/var\/www\/html\/example2.com\/index.html<\/pre>\n<pre>&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;www.example2.com&lt;\/title&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;h1&gt;Hello, Welcome to www.example2.com.&lt;\/h1&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>Once you\u2019re done with it, you can test the setup by accessing both the domains in a browser.<\/p>\n<pre>http:\/\/www.example1.com\r\nhttp:\/\/www.example2.com<\/pre>\n<h5>Preview: www.example1.com<\/h5>\n<div id=\"attachment_5274\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/example1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-5274\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/example1-620x440.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/example1-620x440.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/example1.png 998w\" alt=\"Virtual Hosting: www.example1.com\" width=\"620\" height=\"440\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Virtual Hosting: www.example1.com<\/p>\n<\/div>\n<h5>Preview: www.example2.com<\/h5>\n<div id=\"attachment_5275\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/example2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-5275\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/example2-620x438.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/example2-620x438.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/01\/example2.png 1004w\" alt=\"Virtual Hosting: www.example2.com\" width=\"620\" height=\"438\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Virtual Hosting: www.example2.com<\/p>\n<\/div>\n<h4>Setup IP Based Virtual Hosting Linux<\/h4>\n<p>To setup IP based virtual hosting, you must have more than one\u00a0<strong>IP address<\/strong>\/<strong>Port<\/strong>\u00a0assigned to your server or your Linux machine.<\/p>\n<p>It can be on a single\u00a0<strong>NIC<\/strong>\u00a0card , For example:\u00a0<strong>eth0:1<\/strong>,\u00a0<strong>eth0:2<\/strong>,\u00a0<strong>eth0:3<\/strong>\u00a0\u2026 so forth. Multiple\u00a0<strong>NIC<\/strong>\u00a0cards can also be attached. If you don\u2019t know how to create multiple IP\u2019s on single\u00a0<strong>NIC<\/strong>, follow the below guide, that will help you out in creating.<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/create-multiple-ip-addresses-to-one-single-network-interface\/\" target=\"_blank\" rel=\"noopener\">Create Multiple IP Addresses to One Single Network Interface<\/a><\/li>\n<\/ol>\n<p>Purpose of implementing\u00a0<strong>IP based virtual hosting<\/strong>\u00a0is to assign implementing for each domain and that particular IP will not be used by any other domain.<\/p>\n<p>This kind of set up required when a website is running with\u00a0<strong>SSL<\/strong>\u00a0certificate (<strong>mod_ssl<\/strong>) or on different ports and IPs. And You can also run multiple instances of Apache on a single machine. To check the IPs attached in your server, please check it using\u00a0<a href=\"https:\/\/www.tecmint.com\/ifconfig-command-examples\/\" target=\"_blank\" rel=\"noopener\">ifconfig command<\/a>.<\/p>\n<pre>root@tecmint ~]# ifconfig<\/pre>\n<h5>Sample Output<\/h5>\n<pre> \r\neth0      Link encap:Ethernet  HWaddr 08:00:27:4C:EB:CE  \r\n          inet addr:192.168.0.100  Bcast:192.168.0.255  Mask:255.255.255.0\r\n          inet6 addr: fe80::a00:27ff:fe4c:ebce\/64 Scope:Link\r\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\r\n          RX packets:17550 errors:0 dropped:0 overruns:0 frame:0\r\n          TX packets:15120 errors:0 dropped:0 overruns:0 carrier:0\r\n          collisions:0 txqueuelen:1000\r\n          RX bytes:16565983 (15.7 MiB)  TX bytes:2409604 (2.2 MiB)\r\n\r\neth0:1    Link encap:Ethernet  HWaddr 08:00:27:4C:EB:CE  \r\n          inet addr:192.168.0.101  Bcast:192.168.0.255  Mask:255.255.255.0\r\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\r\n\r\nlo        Link encap:Local Loopback  \r\n          inet addr:127.0.0.1  Mask:255.0.0.0\r\n          inet6 addr: ::1\/128 Scope:Host\r\n          UP LOOPBACK RUNNING  MTU:16436  Metric:1\r\n          RX packets:1775 errors:0 dropped:0 overruns:0 frame:0\r\n          TX packets:1775 errors:0 dropped:0 overruns:0 carrier:0\r\n          collisions:0 txqueuelen:0\r\n          RX bytes:3416104 (3.2 MiB)  TX bytes:3416104 (3.2 MiB)<\/pre>\n<p>As you can see in above output, two IPs\u00a0<strong>192.168.0.100<\/strong>\u00a0(<strong>eth0<\/strong>) and\u00a0<strong>192.168.0.101<\/strong>\u00a0(<strong>eth0:1<\/strong>) is attached to the server, both IPs are assigned to the same physical network device (<strong>eth0<\/strong>).<\/p>\n<p>Now, assign a specific\u00a0<strong>IP<\/strong>\/<strong>Port<\/strong>\u00a0to receive http requests, you can simply do it by changing\u00a0<strong>Listen<\/strong>\u00a0directive in\u00a0<strong>httpd.conf<\/strong>\u00a0file.<\/p>\n<pre>[root@tecmint ~]# vi \/etc\/httpd\/conf\/httpd.conf<\/pre>\n<p>Search for word \u201c<strong>Listen<\/strong>\u201d, You find a section where the short description about Listen directive is written. In that section, comment the original line and write your own directive below that line.<\/p>\n<pre># Listen 80\r\n\r\nListen 192.168.0.100:80<\/pre>\n<p>Now,\u00a0 create a Virtual host sections for both the domains. Go the bottom of the file and add the following virtual directives.<\/p>\n<pre>&lt;VirtualHost 192.168.0.100:80&gt;\r\n    ServerAdmin webmaster@example1.com\r\n    DocumentRoot \/var\/www\/html\/example1\r\n    ServerName www.example1.com\r\nErrorLog logs\/www.example1.com-error_log\r\nTransferLog logs\/www.example1.com-access_log\r\n&lt;\/VirtualHost&gt;\r\n\r\n&lt;VirtualHost 192.168.0.101:80&gt;\r\n    ServerAdmin webmaster@example2.com\r\n    DocumentRoot \/var\/www\/html\/example2\r\n    ServerName www.example2.com\r\nErrorLog logs\/www.example2.com-error_log\r\nTransferLog logs\/www.example2.com-access_log\r\n&lt;\/VirtualHost&gt;<\/pre>\n<p>Now, since you have modified main Apache conf file, you need to restart the http service like below.<\/p>\n<pre>[root@tecmint ~]# service httpd restart\r\nStopping httpd:                                            [  OK  ]\r\nStarting httpd:                                            [  OK  ]<\/pre>\n<p>Test your IP based Virtual hosting setup by accessing the\u00a0<strong>URLs<\/strong>\u00a0on web browser as shown below.<\/p>\n<pre>http:\/\/www.example1.com\r\nhttp:\/\/www.example2.com<\/pre>\n<p>That\u2019s all with Apache virtual host today, If you\u2019re looking to secure and harden your Apache configuration, then read our article that guides.<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/apache-security-tips\/\" target=\"_blank\" rel=\"noopener\">13 Apache Web Server Security and Hardening Tips<\/a><\/li>\n<\/ol>\n<h3>Reference Links<\/h3>\n<p><a href=\"https:\/\/httpd.apache.org\/docs\/2.2\/vhosts\/\" target=\"_blank\" rel=\"noopener\">Apache Virtual Host Documentation<\/a><\/p>\n<p>I\u2019ll be again come with some other Apache tips and trick in my future articles, till then Stay Geeky and connected to Tecmint.com. Do not forget to leave your suggestions about the article in our comment section below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/apache-ip-based-and-name-based-virtual-hosting\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we all are aware that\u00a0Apache\u00a0is a very powerful, highly flexible and configurable Web server for\u00a0Nix OS. Here in this tutorial, we are going to discuss one more feature of\u00a0Apache\u00a0which allows us to host more than one website on a single Linux machine. Implementing virtual hosting with Apache web server can help you to save &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/apache-virtual-hosting-ip-based-and-name-based-virtual-hosts-in-rhel-centos-fedora\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Apache Virtual Hosting: IP Based and Name Based Virtual Hosts in RHEL\/CentOS\/Fedora&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11713","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11713"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11713\/revisions"}],"predecessor-version":[{"id":11714,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11713\/revisions\/11714"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}