You are here
Setting Apache Virtual Hosts on Windows & Mac
Virtual Host
Setting up virtual hosts gives us the flexibility of setting up multiple hosts on a local machine, in other words we can use this method to host more than one domain name on the same machine, including of course on servers running locally. Usually when you setup Apache, you dump all your sites under htdocs and then you access them on your browser by using localhost/[directory-name]. Although this is good, sometimes its not convenient. For instance, when using CMS applications such Drupal or WordPress Mu, you might want to be able to setup multiple sites under the same local installation so that it mimics your live site installation.
To setup multiple hosts, we're gonna edit two files under both Windows and Mac. One of this files will be used to create the hosts, and the other will be used to map the hosts to our local IP address. Also, in order to modify these files you must use an account with administrator permissions.
For our example we're gonna setup two hosts. The hosts will be named "serv1.moi.com" and "serv2.moi.com and I will have two directories under my Documents/Sites/, each with one of my sites. The directories will also be named "serv1.moi.com" and "serv2.moi.com", although you can name them differently from the hosts. so... let's begin
Note:
If you are following this instructions to setup a multisite installation under CMS Drupal or WordPress Mu, the directory name should match the host name, so that the CMS auto detects your sites.
Windows
httpd-vhosts.conf
Run the command prompt and open the file named, httpd-vhosts.conf; which should be under the directory where the Apache server is currently installed. On my machine my path goes like this: c:\www\Server\conf\extra\httpd-vhosts.conf. Under this file you will see a couple of dummy hosts like this one:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.localhost
DocumentRoot "C:/www/Server/docs/dummy-host.localhost"
ServerName dummy-host.localhost
ServerAlias www.dummy-host.localhost
ErrorLog "logs/dummy-host.localhost-error.log"
CustomLog "logs/dummy-host.localhost-access.log" common
</VirtualHost>
So what we are going to do is we are gonna modify and add a few directives so that we can point the domain "serv1.moi.com" to the directory which I have named the same as the host "serv1.moi.com". Now copy/paste the snippet below and add it to your httpd-vhosts.conf file. Make sure you don't delete any of the existing data, simply add this at the bottom of the file.
Notice that the DocumentRoot Directive & the Directory Directive have the path to the my Sites directory. Also, the ServerName Directive is the domain name we are going to use to browse to our site.
<VirtualHost *:80>
DocumentRoot "C:\Documents and Settings\Sites\serv1.moi.com"
ServerName serv1.moi.com
<Directory "C:\Documents and Settings\Sites\serv1.moi.com">
Options FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost *:80>
Save this file, but do not close the Command Prompt just yet. Finish up the last step on the below Windows & Mac section.
Mac
httpd-vhosts.conf
Run the Terminal and open the file named, httpd-vhosts.conf; which should be under the directory where the Apache server is installed. This is the path to my server install: /etc/apache2/extra/httpd-vhosts.conf. You may see some dummy host like this one:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/Users/Roldan/Sites/phpmyadmin"
ServerName roldan.pma
</VirtualHost>
So what we are going to do is we are gonna modify and add a few directives so that we can point the domain "serv1.moi.com" to the directory which I have named the same as the host "serv1.moi.com". Now copy/paste the snippet below and add it to your httpd-vhosts.conf file. Make sure you don't delete any of the existing data, simply add this at the bottom of the file.
Notice that the DocumentRoot Directive & the Directory Directive have the path to the my Sites directory. Also, the ServerName Directive is the domain name we are going to use to browse to our site.
<VirtualHost *:80>
DocumentRoot "/Sites/serv1.moi.com"
ServerName serv1.moi.com
<Directory "/Sites/serv1.moi.com">
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
Save this file, but do not close the Terminal just yet. Finish up the last step on the below Windows & Mac section.
Windows & Mac
hosts
Run the command prompt (Windows) or Terminal (Mac) and open the file named, hosts; which should be located under the \etc directory:
Windows C:\WINDOWS\system32\drivers\etc\hosts
Mac \etc\hosts
On this file we are going to map the virtual host we created on the previous step to our local server which has an IP address of 127.0.0.1. There should be an entry on this file for the virtual host name "localhost". So, we are gonna add our virtual host after the "localhost" entry - should look like this:
127.0.0.1 localhost
127.0.0.1 serv1.moi.com
Save this file and restart your server before opening the page on your browser.
Directives
These are the important directives worth knowing and reading about.
- VirtualHost Directive Holds the directives or properties of an specific host.
- DocumentRoot Directive This should be the path of the directory containing the site; e.g. serv1.moi.com
- ServerName The host name or domain which lets the server find itself.
- Directory Directive A set of directives that will apply only to this directory
lifestyle:
- Diego F. Roldán's blog
- Log in to post comments
- 5374 veces leido