{"id":7881,"date":"2019-06-24T16:50:23","date_gmt":"2019-06-24T08:50:23","guid":{"rendered":"http:\/\/rmohan.com\/?p=7881"},"modified":"2019-06-24T16:50:26","modified_gmt":"2019-06-24T08:50:26","slug":"apache-based-web-virtual-host-under-linux","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7881","title":{"rendered":"Apache-based Web virtual host under Linux"},"content":{"rendered":"\n<ul><li><\/li><\/ul>\n\n\n\n<p>Web virtual host refers to running multiple web sites in the same server, each of which does not actually occupy the entire server. Therefore, it is called a \u201cvirtual\u201d web host, and the virtual web hosting service can make full use of the server. Hardware resources.<\/p>\n\n\n\n<p>Using httpd makes it easy to set up a virtual host server. It only needs to run an httpd service to support a large number of web sites at the same time. There are three types of virtual hosts supported by httpd (like the Windows IIS service):<\/p>\n\n\n\n<ol><li>A virtual host with the same IP, port number, and different domain name;<\/li><li>Virtual host with the same IP and different port numbers;<\/li><li>Virtual hosts with different IP addresses and the same port number;<\/li><\/ol>\n\n\n\n<p>Most O&amp;M personnel should adopt the first solution when setting up a virtual host. The virtual host is based on different domain names, which is also the most user-friendly solution.<\/p>\n\n\n\n<p>First, start building a domain-based virtual host:<\/p>\n\n\n\n<ol><li>Provide domain name resolution for virtual hosts<\/li><\/ol>\n\n\n<p>[root@localhost \/]<\/p>\n\n\n\n<p># vim \/etc\/named.conf   \n<\/p>\n\n\n\n<p>zone &#8220;mohan1.com&#8221; in {                                          <br>\n        type master;<br>\n        file &#8220;mohan1.com.zone&#8221;;<br>\n};<\/p>\n\n\n\n<p>zone &#8220;mohan2.com&#8221; in {<br>\n        type master;<br>\n        file &#8220;mohan2.com.zone&#8221;;<br>\n};<\/p>\n\n\n\n<p>root@localhost \/]# vim \/var\/named\/mohan1.com.zone<br>\n        in      ns      www.mohan1.com.<br>\nwww     in      a       192.168.1.1<\/p>\n\n\n<p>[root@localhost \/]<\/p>\n\n\n\n<p># vim \/var\/named\/mohan2.com.zone    \n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    in      ns      www.mohan2.com.<\/code><\/pre>\n\n\n\n<p>www     in      a       192.168.1.1<\/p>\n\n\n\n<p>2, prepare web documents for the virtual host<\/p>\n\n\n\n<p>Prepare website directories and web documents for each virtual web host. For the convenience of mohaning, each virtual web host is provided with a different home page file:<\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># mkdir -p \/var\/www\/mohan1com<br><\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># mkdir -p \/var\/www\/mohan2com<br><\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># echo &#8220;<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">www.mohan1.com<\/h1>\n\n\n\n<p>&#8221; &gt; \/var\/www\/mohan1com\/index.html<br><\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># echo &#8220;<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">www.mohan2.com<\/h1>\n\n\n\n<p>&#8221; &gt; \/var\/www\/mohan2com\/index.html\n<\/p>\n\n\n\n<p>3, add virtual host configuration<\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># vim \/usr\/local\/httpd\/conf\/extra\/httpd-vhosts.conf \n<\/p>\n\n\n\n<p>             \n    ServerAdmin admin@mohan.com              \n    DocumentRoot &#8220;\/var\/www\/mohan1com&#8221;            \n    ServerName www.mohan1.com                 \n    ErrorLog &#8220;logs\/mohan1-error_log&#8221;                      \n    CustomLog &#8220;logs\/mohan1-access_log&#8221; common           \n              \n    require all granted\n    \n<\/p>\n\n\n\n<p>mohan2<br>\n\n    ServerAdmin admin@mohan.com  \n    DocumentRoot &#8220;\/var\/www\/mohan2com&#8221;\n    ServerName www.mohan2.com\n    ErrorLog &#8220;logs\/mohan2-error_log&#8221;\n    CustomLog &#8220;logs\/mohan2-access_log&#8221; common\n    \n    require all granted\n    \n<\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># vim \/usr\/local\/httpd\/conf\/httpd.conf         \n<\/p>\n\n\n\n<p>Include conf\/extra\/httpd-vhosts.conf                <br>\n<\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># systemctl restart httpd                \n<\/p>\n\n\n\n<ol><li>Access the virtual web host in the client<\/li><\/ol>\n\n\n\n<p>Verify it, the result is as follows:<\/p>\n\n\n\n<p>Second, the virtual host based on IP address:<\/p>\n\n\n\n<p>(100,000 don&#8217;t want to write down, because the next content can be understood, it won&#8217;t be used, but\u2026.. Just in case, just write it)<\/p>\n\n\n\n<p>Note that there is no connection between each method. Don&#8217;t confuse IP-based virtual hosts with domain-based ones.<\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># vim \/usr\/local\/httpd\/conf\/extra\/httpd-vhosts.conf     <br>\n         \u2026\u2026\u2026\u2026..<br>\n                \n    ServerAdmin admin@mohan.com\n    DocumentRoot &#8220;\/var\/www\/mohan1com&#8221;\n    ErrorLog &#8220;mohan1-error_log&#8221;\n    CustomLog &#8220;mohan1-access_log&#8221; common\n    \n    require all granted\n    \n\n<\/p>\n\n\n\n<p>                \n    ServerAdmin admin@mohan.com\n    DocumentRoot &#8220;\/var\/www\/mohan2com&#8221;\n    ErrorLog &#8220;mohan2-error_log&#8221;\n    CustomLog &#8220;mohan2-access_log&#8221; common\n    \n    require all granted\n    \n<\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># vim \/usr\/local\/httpd\/conf\/httpd.conf          <br>\n                \u2026\u2026\u2026\u2026\u2026\u2026\u2026.             <br>\nInclude conf\/extra\/httpd-vhosts.conf          \n<\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># systemctl restart httpd   \n<\/p>\n\n\n\n<p>Second, the port-based virtual host:<\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># vim \/usr\/local\/httpd\/conf\/extra\/httpd-vhosts.conf      \n<\/p>\n\n\n\n<p>                     \n    ServerAdmin admin@mohan.com\n    DocumentRoot &#8220;\/var\/www\/mohan1com&#8221;                       \n    ErrorLog &#8220;mohan1-error_log&#8221;\n    CustomLog &#8220;mohan1-access_log&#8221; common\n    \n    require all granted\n    \n<\/p>\n\n\n\n<p>                     \n    ServerAdmin admin@mohan.com\n    DocumentRoot &#8220;\/var\/www\/mohan2com&#8221;              \n    ErrorLog &#8220;mohan2-error_log&#8221;\n    CustomLog &#8220;mohan2-access_log&#8221; common\n    \n    require all granted\n    \n<\/p>\n\n\n\n<p>listen 8000                  <\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># vim \/usr\/local\/httpd\/conf\/httpd.conf          <br>\n                \u2026\u2026\u2026\u2026\u2026\u2026\u2026.             <br>\nInclude conf\/extra\/httpd-vhosts.conf           \n<\/p>\n\n\n<p>[root@localhost named]<\/p>\n\n\n\n<p># systemctl restart httpd <\/p>\n\n\n\n<ul><li><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"\n<p>Web virtual host refers to running multiple web sites in the same server, each of which does not actually occupy the entire server. Therefore, it is called a \u201cvirtual\u201d web host, and the virtual web hosting service can make full use of the server. Hardware resources.<\/p>\n<p>Using httpd makes it easy to set up [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7881"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7881"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7881\/revisions"}],"predecessor-version":[{"id":7882,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7881\/revisions\/7882"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}