{"id":2125,"date":"2015-03-19T09:48:30","date_gmt":"2015-03-19T09:48:30","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=2125"},"modified":"2015-03-19T09:48:49","modified_gmt":"2015-03-19T09:48:49","slug":"adding-virtual-host-to-os-x-apache","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=2125","title":{"rendered":"Adding Virtual Host to OS X Apache"},"content":{"rendered":"<p>By default, the Apache Virtual Host configuration on Mac OS X is located in a single file:\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">\/etc\/apache2\/extra\/httpd-vhosts.conf<\/div><\/div>\n<p>. You need to edit the Apache configuration to include this file and enable virtual hosts.<\/p>\n<p>Over the years, I have created many virtual hosts. Each time editing\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">httpd-vhosts.conf<\/div><\/div>\n<p>. Furthermore, Apache configurations often get reset when upgrading Mac OS X. In the same amount of steps (two), you can adopt a more manageable configuration.<\/p>\n<h2 id=\"what-are-virtual-hosts\">What are Virtual Hosts?<\/h2>\n<p>From the\u00a0<a href=\"http:\/\/httpd.apache.org\/docs\/current\/vhosts\/index.html\">Apache Virtual Host documentation<\/a>:<\/p>\n<blockquote><p>The term Virtual Host refers to the practice of running more than one web site on a single machine.<\/p><\/blockquote>\n<p>By default, the Apache configuration on Mac OS X serves files from\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">\/Library\/WebServer\/Documents<\/div><\/div>\n<p>\u00a0accessed by the name\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">locahost<\/div><\/div>\n<p>. This is essentially a single site configuration. You could mimic multiple sites by creating subdirectories and access a site at\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">localhost\/somesite<\/div><\/div>\n<p>.<\/p>\n<p>This is not ideal for several reasons. Primarily, we would rather access the site using a name like\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">somesite.local<\/div><\/div>\n<p>. To do that, you need to configure virtual hosts.<\/p>\n<h2 id=\"a-cleaner-configuration\">A Cleaner Configuration<\/h2>\n<p>Before I being, I assume you already\u00a0<a href=\"http:\/\/jason.pureconcepts.net\/2014\/11\/install-apache-php-mysql-mac-os-x-yosemite\/\">installed and configured Apache on Mac OS X<\/a>.<\/p>\n<p>First, open the\u00a0<em>Terminal<\/em>\u00a0app and switch to the\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">root<\/div><\/div>\n<p>\u00a0user to avoid permission issues while running these commands.<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">sudo su -<\/div><\/div>\n\n<\/pre>\n<p>Edit the Apache configuration file:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">vi \/etc\/apache2\/httpd.conf<\/div><\/div>\n\n<\/pre>\n<p>Find the following line:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">#Include \/private\/etc\/apache2\/extra\/httpd-vhosts.conf<\/div><\/div>\n\n<\/pre>\n<p>Below it, add the following line:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Include \/private\/etc\/apache2\/vhosts\/*.conf<\/div><\/div>\n\n<\/pre>\n<p>This configures Apache to include all files ending in\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">.conf<\/div><\/div>\n<p>\u00a0in the\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">\/private\/etc\/apache2\/vhosts\/<\/div><\/div>\n<p>\u00a0directory. Now we need to create this directory.<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">mkdir \/etc\/apache2\/vhosts<br \/>\ncd \/etc\/apache2\/vhosts<\/div><\/div>\n\n<\/pre>\n<p>Create the default virtual host configuration file.<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">vi _default.conf<\/div><\/div>\n\n<\/pre>\n<p>Add the following configuration:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">&amp;lt;VirtualHost *:80&amp;gt;<br \/>\n&nbsp; &nbsp; DocumentRoot &quot;\/Library\/WebServer\/Documents&quot;<br \/>\n&amp;lt;\/VirtualHost&amp;gt;<\/div><\/div>\n\n<\/pre>\n<p>I create this file to serve as the default virtual host. When Apache can not find a matching virtual host, it will use the first configuration. By prefixing this file with an underscore, Apache will include it first. Techincally this file is not needed as it simply repeats the configuraton already in\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">httpd.conf<\/div><\/div>\n<p>. However, it provides a place to add custom configuration for the default virtual host (i.e.\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">localhost<\/div><\/div>\n<p>).<\/p>\n<p>Now you can create your first virtual host. The example below contains the virtual host configuration for my site. Of course, you will want to substitute\u00a0<em>jason.pureconcepts.net<\/em>\u00a0with your domain name.<\/p>\n<p>Create the virtual host configuration file:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">vi jason.pureconcepts.net.conf<\/div><\/div>\n\n<\/pre>\n<p>Add the following configuration:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">&amp;lt;VirtualHost *:80&amp;gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; DocumentRoot &quot;\/Users\/Jason\/Documents\/workspace\/jason.pureconcepts.net\/htdocs&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; ServerName jason.pureconcepts.local<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; ErrorLog &quot;\/private\/var\/log\/apache2\/jason.pureconcepts.local-error_log&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; CustomLog &quot;\/private\/var\/log\/apache2\/jason.pureconcepts.local-access_log&quot; common<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &amp;lt;Directory &quot;\/Users\/Jason\/Documents\/workspace\/jason.pureconcepts.net\/htdocs&quot;&amp;gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AllowOverride All<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Require all granted<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &amp;lt;\/Directory&amp;gt;<br \/>\n&amp;lt;\/VirtualHost&amp;gt;<\/div><\/div>\n\n<\/pre>\n<p>This\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">VirtualHost<\/div><\/div>\n<p>\u00a0configuration allows me to access my site from\u00a0<em>http:\/\/jason.pureconcepts.local<\/em>\u00a0for local development.<\/p>\n<p>Note: I use the extension\u00a0<em>local<\/em>. This avoids conflicts with any\u00a0<em>real<\/em>\u00a0extensions and serves as a reminder I am developing in my\u00a0<em>local<\/em>\u00a0environment.<\/p>\n<p>Note: The\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Require all granted<\/div><\/div>\n<p>\u00a0configuration became available in Apache 2.4 which comes with OS X Yosemite. If you are running a version of OS X before Yosemite, use the equivalent 2.2 configuration in the\u00a0<a href=\"http:\/\/httpd.apache.org\/docs\/2.4\/upgrading.html#run-time\">upgrading Apache examples<\/a>.<\/p>\n<p>The final step is to restart Apache:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">apachectl restart<\/div><\/div>\n\n<\/pre>\n<p>If you run into any problems, run:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">apachectl configtest<\/div><\/div>\n\n<\/pre>\n<p>This will test your Apache configuration and display any error messages.<\/p>\n<h2 id=\"mapping-the-local-extension\">Mapping the .local extension<\/h2>\n<p>In order to access sites locally you need to edit your\u00a0<em>hosts<\/em>\u00a0file.<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">vi \/etc\/hosts<\/div><\/div>\n\n<\/pre>\n<p>Add a line to the bottom of this file for your virtual host. It should match the value you used for the\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">ServerName<\/div><\/div>\n<p>\u00a0configuration. For example, my site:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">127.0.0.1 &nbsp; &nbsp; &nbsp; jason.pureconcepts.local<\/div><\/div>\n\n<\/pre>\n<p>I like to run the following to clear the local DNS cache:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">dscacheutil -flushcache<\/div><\/div>\n\n<\/pre>\n<p>Now you can access your site using the .local extension. For example,\u00a0<em>http:\/\/jason.pureconcepts.local<\/em>.<\/p>\n<h2 id=\"a-note-about-permissions\">A note about permissions<\/h2>\n<p>You may receive\u00a0<em>403 Forbidden<\/em>\u00a0when you visit your local site. This is likely a permissions issue. Simply put, the Apache user (<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">_www<\/div><\/div>\n<p>) needs to have access to read, and sometimes write, to your web directory.<\/p>\n<p>If you are not familiar with permissions,\u00a0<a href=\"http:\/\/www.library.yale.edu\/wsg\/docs\/permissions\/\">read more<\/a>. For now though, the easiest thing to do is ensure your web directory has permissions of\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">755<\/div><\/div>\n<p>. You can change permissions with the command:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">chmod 755 some\/web\/directory\/<\/div><\/div>\n\n<\/pre>\n<p>In my case, all my files were under my local\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">~\/Documents<\/div><\/div>\n<p>\u00a0directory. Which by default is only readable by me. So I had to change permissions from my web directory all the way up to\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">~\/Documents<\/div><\/div>\n<p>\u00a0to resolve the\u00a0<em>403 Forbidden<\/em>\u00a0issue.<\/p>\n<p>Note: There are many ways to solve permission issues. I have provided this as the\u00a0<em>easiest<\/em>\u00a0solution, not the\u00a0<em>best<\/em>.<\/p>\n<h2 id=\"in-closing\">In Closing<\/h2>\n<p>Any time you want to add a site to Apache on your Mac, simply create a virtual host configuration file for that site and map it in your\u00a0<em>hosts<\/em>\u00a0file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default, the Apache Virtual Host configuration on Mac OS X is located in a single file:\u00a0 \/etc\/apache2\/extra\/httpd-vhosts.conf . You need to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2125","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2125"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2125\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}