Coolestguidesontheplanet.com | Neil Gee

coolest guides on the planet

Coolest Guides On The Planet

Neil Gee

  • Home
  • macOS
  • WebDev
  • All Posts
  • Contact

How to set up Virtual Hosts in Apache on Mac OSX 10.9 Mavericks and OSX 10.8 Mountain Lion

September 28, 2013 5 Comments

Setting up Virtual Hosts in Apache on Mac OSX 10.9 Mavericks or Mountain Lion is straight forward after you have your local Web Development environment up and running – get your web development up and running first following the AMP stack guide here 10.8 or 10.9 if required.

The process of setting up Virtual Hosts is done easier in the Terminal either using nano or vi with sudo or as a root user,  or you can you a GUI visual editor like Text Wrangler which allows access to the /private/etc directory by clicking ‘Show Everything” in the open dialog box.

Allow the vhosts configuration from the Apache configuration file httpd.conf

sudo nano /etc/apache2/httpd.conf

Search for ‘vhosts’ and uncomment the include line

# Virtual hosts

Include /private/etc/apache2/extra/httpd-vhosts.conf
This will allow usage of the httpd-vhosts.conf file, open this file to add in the vhost.
sudo nano /etc/apache2/extra/httpd-vhosts.conf
An example in the file is given of the format required to add additional domains, just follow this to create your new virtual host:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
We can take this example and extend on it, if you wanted a domain named apple.com for example, you can copy the existing text block and edit to suit:
<VirtualHost *:80>

    ServerName apple.com

    ServerAlias www.apple.com

    DocumentRoot "/Users/username/Sites/apple"

    ErrorLog "/private/var/log/apache2/apple.com-error_log"

   CustomLog "/private/var/log/apache2/apple.com-access_log" common

    ServerAdmin [email protected]

        <Directory "/Users/neilg/Sites/apple">

            Options Indexes FollowSymLinks

            AllowOverride All

            Order allow,deny

            Allow from all

        </Directory>

</VirtualHost>
So here I am creating a vhost for apple.com and making the document root in my Sites folder, in the text block above I have also added in some directory permissions and a server alias to also use the ‘www’ prefix, what you need to change is the document root location, email address and domain name to suit your needs. Finish and save the file.
One of the better workflows is to create a ‘Sites’ folder and store your vhosts in it – but now also you need to spoof the IP address to be the local one, the set up of these using the User/Sites folder is outlined in the links above for the initial AMP stack guides.

Spoof Your IP address to the Domain

sudo nano /etc/hosts
Add the Domain to resolve to the local address
127.0.0.1 apple.com
127.0.0.1 www.apple.com

Restart Apache

sudo apachectl restart
Check out your local vhost domain in the browser

Losing Localhost

One caveat to note about virtual hosts is that once set up you lose your older document root previously at /LIbrary/WebServer/Documents or accessed in the browser at http://localhost what happens is that you get a 403 Forbidden Error. But the ~/username document root is still compatible.
To get around this, you need to add in a vhost for localhost and declare this vhost before any of the others, in the same file:
sudo nano /etc/apache2/extra/httpd-vhosts.conf

Add in:

<VirtualHost *:80>
ServerName localhost
DocumentRoot /Library/WebServer/Documents/
</VirtualHost>
Then restart Apache and all should be good.

Video Guide on vhosts

Share this:

  • Twitter
  • Google
  • Facebook

Related

Cats: macOS, WebRoot

Subscribe

Coolest Guides on the Planet
  • Latest
  • Popular
  • Comments
  • Install and Update Sass 3.4.0 on macOS Sierra and OSX 10.11 El Capitan Install and Update Sass 3.4.0 on macOS Sierra and OSX 10.11 El Capitan
  • Installing Homebrew on macOS Sierra, Package Manager for Unix Apps Installing Homebrew on macOS Sierra, Package Manager for Unix Apps
  • Get Apache, MySQL, PHP and phpMyAdmin working on macOS Sierra Get Apache, MySQL, PHP and phpMyAdmin working on macOS Sierra
  • How to Change the MySQL root Password in Linux or macOS via Command Line How to Change the MySQL root Password in Linux or macOS via Command Line
  • iOS IPSW Firmware Download Links 10.0.2, 10.0.0, 9.3.5, 9.3.4, 9.3.3, 9.3.2, 9.3.1, 9.3.0, 9.2.1, 9.2.0, 9.1.0, 9.0.2, 9.0.1, 9.0.0, 8.4.1, 8.4, 8.3, 8.2, 8.1.3, 8.1.2, 8.1.1, 8.1.0, 8.0.2, 8.0.1, 8, 7.1.2, 7.1.1, 7.1, 7.0.6, 7.0.5, 7.0.4, 7.0.3, 7.0.2, 7.0.1, 7, 6.1.3, 6.1.2, 6.1, 6.0.1, 6, 5.1.1, 5.1, 5.0.1 iOS IPSW Firmware Download Links 10.0.2, 10.0.0, 9.3.5, 9.3.4, 9.3.3, 9.3.2, 9.3.1, 9.3.0, 9.2.1, 9.2.0, 9.1.0, 9.0.2, 9.0.1, 9.0.0, 8.4.1, 8.4, 8.3, 8.2, 8.1.3, 8.1.2, 8.1.1, 8.1.0, 8.0.2, 8.0.1, 8, 7.1.2, 7.1.1, 7.1, 7.0.6, 7.0.5, 7.0.4, 7.0.3, 7.0.2, 7.0.1, 7, 6.1.3, 6.1.2, 6.1, 6.0.1, 6, 5.1.1, 5.1, 5.0.1
  • Today Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Today Week Month All
  • bash-alias-OSX Make an Alias in Bash Shell in OS X Terminal
Ajax spinner

Tags

apache backup baseband Bing boot Coolest Guy on the Planet Coolest Guy Planet cpanel css custom database drupal error Firmware Google image instadmg ios iphone jailbreak keys Keywords lion mac macos sierra mysql OSX PageRank php private public Rank Rank Checker ranking redirect remote rsa SEO SERPS shell ssh terminal Twitter unstoppables Yahoo
WordPress Workflow
Get DesktopServer
StudioPress Premium WordPress Themes

Donate a Beer to the Coolest Guides

Copyright © 2016· Neil Gee - All Rights Reserved - Hosted by Servint