{"id":90,"date":"2008-08-19T20:59:32","date_gmt":"2008-08-19T12:59:32","guid":{"rendered":"http:\/\/www.sysadmindayph.com\/blog\/?p=90"},"modified":"2008-08-19T20:59:32","modified_gmt":"2008-08-19T12:59:32","slug":"configure-virtual-hosting-in-apache-web-server","status":"publish","type":"post","link":"https:\/\/www.sysadmindayph.com\/blog\/configure-virtual-hosting-in-apache-web-server\/","title":{"rendered":"Configure Virtual Hosting in Apache Web Server"},"content":{"rendered":"<p>When we say or mention the word Web Server, the first thing that comes to our mind is Apache, right? (if you don&#8217;t then what are you doing here reading this? :-))<\/p>\n<p>Apache is the most well known open source web server there is! Open source?? Make that the most wildly used webserver  on the internet, period!<\/p>\n<p>It is believed (and i know they made a survey or study about it) that Apache takes 90% of all web servers in the internet. Most web hosting company, and I&#8217;ve once worked in one, use Apache.<\/p>\n<p>This post will talk about how to setup Apache for Virtual Hosting<\/p>\n<p><!--more--><\/p>\n<p>Setting Up A Virtual Host in Apache<\/p>\n<p>create a completely seperate document root, cgi-bin directory, and logfile directory for each host. You can place these beneath the standard Debian prefix of \/var\/www or you may use a completely different root<\/p>\n<blockquote>\n<pre>root@irony:~# mkdir \/home\/www\r\n\r\nroot@irony:~# mkdir \/home\/www\/www.example.com\r\nroot@irony:~# mkdir \/home\/www\/www.example.com\/htdocs\r\nroot@irony:~# mkdir \/home\/www\/www.example.com\/cgi-bin\r\nroot@irony:~# mkdir \/home\/www\/www.example.com\/logs\r\n\r\nroot@irony:~# mkdir \/home\/www\/www.example.net\r\nroot@irony:~# mkdir \/home\/www\/www.example.net\/htdocs\r\nroot@irony:~# mkdir \/home\/www\/www.example.net\/logs\r\nroot@irony:~# mkdir \/home\/www\/www.example.net\/cgi-bin\r\n\r\nroot@irony:~# mkdir \/home\/www\/www.example.org\r\nroot@irony:~# mkdir \/home\/www\/www.example.org\/htdocs\r\nroot@irony:~# mkdir \/home\/www\/www.example.org\/logs\r\n\r\nroot@irony:~# mkdir \/home\/www\/www.example.org\/cgi-bin<\/pre>\n<\/blockquote>\n<p>NEt<br \/>\nHere we&#8217;ve setup three different directory trees, one for each site. If you wanted to have identical content it might make sense to only create one, and then use symbolic links instead.<\/p>\n<p>The next thing to do is to enable virtual hosts in your Apache configuration. The simplest way to do this is to create a file called \/etc\/apache2\/conf.d\/virtual.conf and include the following content in it:<\/p>\n<blockquote>\n<pre>#\r\n#  We're running multiple virtual hosts.\r\n#\r\nNameserver *\r\n\r\nNameVirtualHost *<\/pre>\n<\/blockquote>\n<p>(When Apache starts up it reads the contents of all files included in \/etc\/apache2\/conf.d, and files you create here won&#8217;t get trashed on package upgrades.)<\/p>\n<p>Once we&#8217;ve done this we can create the individual host configuration files. The Apache2 setup you&#8217;ll find on Debian GNU\/Linux includes two directories for locating your site configuration files:<\/p>\n<p>\/etc\/apache2\/sites-available<\/p>\n<p>This contains configuration files for sites which are available but not necessarily enabled.<br \/>\n\/etc\/apache2\/sites-enabled<\/p>\n<p>This directory contains site files which are enabled.<\/p>\n<p>As with the conf.d directory each configuration file in the sites-enabled directory is loaded when the server starts &#8211; whilst the files in sites-available are completely ignored.<\/p>\n<p>You are expected to create your host configuration files in \/etc\/apache2\/sites-available, then create a symbolic link to those files in the sites-enabled directory &#8211; this will cause them to be actually loaded\/read.<\/p>\n<p>Rather than actually messing around with symbolic links the Debian package includes two utility commands a2ensite and a2dissite which will do the necessary work for you as we will demonstrate shortly.<\/p>\n<p>Lets start with a real example. Create \/etc\/apache2\/sites-available\/www.example.com with the following contents:<\/p>\n<blockquote>\n<pre>#\r\n#  Example.com (\/etc\/apache2\/sites-available\/www.example.com)\r\n#\r\n&lt;VirtualHost *&gt;\r\nServerAdmin webmaster@example.com\r\nServerName  www.example.com\r\nServerAlias example.com\r\n\r\n# Indexes + Directory Root.\r\nDirectoryIndex index.html\r\nDocumentRoot \/home\/www\/www.example.com\/htdocs\/\r\n\r\n# CGI Directory\r\nScriptAlias \/cgi-bin\/ \/home\/www\/www.example.com\/cgi-bin\/\r\n&lt;Location \/cgi-bin&gt;\r\nOptions +ExecCGI\r\n&lt;\/Location&gt;\r\n\r\n# Logfiles\r\nErrorLog  \/home\/www\/www.example.com\/logs\/error.log\r\nCustomLog \/home\/www\/www.example.com\/logs\/access.log combined\r\n\r\n&lt;\/VirtualHost&gt;<\/pre>\n<\/blockquote>\n<p>ow we&#8217;ve got:<\/p>\n<ul>\n<li>Three directories which can be used to contain our content.<\/li>\n<li>Three directories which can be used to contain our logfiles.<\/li>\n<li>Three directories which can be used to contain our dynamic CGI scripts.<\/li>\n<li>Three configuration files which are being ignored by Apache.<\/li>\n<\/ul>\n<p>To enable the sites simply run:<\/p>\n<pre>root@irony:~# a2ensite www.example.com\r\nSite www.example.com installed; run \/etc\/init.d\/apache2 reload to enable.\r\n\r\nroot@irony:~# a2ensite www.example.net\r\nSite www.example.net installed; run \/etc\/init.d\/apache2 reload to enable.\r\n\r\nroot@irony:~# a2ensite www.example.org\r\nSite www.example.org installed; run \/etc\/init.d\/apache2 reload to enable.<\/pre>\n<p>This will now create the symbolic links so that <tt>\/etc\/apache2\/sites-enabled\/www.example.org<\/tt>, etc, now exist and will be read.<\/p>\n<p>Once we&#8217;ve finished our setup we can restart, or reload, the webserver as the output above instructed us to do with:<\/p>\n<pre>root@irony:~# \/etc\/init.d\/apache2 reload\r\nReloading web server config...done.\r\nroot@irony:~#<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When we say or mention the word Web Server, the first thing that comes to our mind is Apache, right? (if you don&#8217;t then what are you doing here reading this? :-)) Apache is the &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,14,9,3,49,54],"tags":[384,56,57,55,383,385],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-apache","category-installation","category-open-source","category-unix","category-web-hosting","category-web-server","tag-apache","tag-multiple-domain","tag-shared-server","tag-virtual-host","tag-web-hosting","tag-web-server"],"_links":{"self":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/comments?post=90"}],"version-history":[{"count":0,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}