A question I got a lot from people when I started documenting my efforts was how to setup Apache so it could use multiple domains on the same server. So here is how I’ve used it.
The file we’ll need to edit is the Apache configuration file. It is located in ‘/Library/Apache2/conf’ and is called ‘httpd.conf’. For this configuration example, we’ll assume that you have your web sites located in separate folders in the ‘/Library/Apache2/htdocs’ directory. Each web site has a sub-folder of its own under that folder, like this:
In this example the domain names for the two sites are site1.com and site2.com. We’re going to set up virtual hosts for those two sites using those domain names
Open your httpd.conf file in your editor and go to section 3 almost at the bottom. You should see an example of a virtual host there. Each line of that example will begin with a hash (#). The hash character marks the line as a comment, so the example is not executed. Add the following lines below that example (change ‘your.external.ip.address’ into the ip-address your server is configured with):
NameVirtualHost your.external.ip.address:80 <VirtualHost your.external.ip.address:80> DocumentRoot /Library/Apache2/htdocs/site1.com ServerName site1.com </VirtualHost> <VirtualHost your.external.ip.address:80> DocumentRoot /Library/Apache2/htdocs/site2.com ServerName site2.com </VirtualHost>
If you want to use www.site1.com you’ll have to add another entry like:
<VirtualHost your.external.ip.address:80> DocumentRoot /Library/Apache2/htdocs/site1.com ServerName www.site1.com </VirtualHost>
That’s all there is to it! Save and close the file and restart Apache. That will tell the Apache server everything it needs to know in order for it to serve the pages using the domain names.
You can set many other configuration settings for your domain based webserver like webmaster email and logfiles like:
<VirtualHost your.external.ip.address:80> DocumentRoot /Library/Apache2/htdocs/site1.com ServerName www.site1.com ServerAdmin firstname.lastname@example.org ErrorLog logs/site1.com.error_log CustomLog logs/site1.com.access_log combined </VirtualHost>
You might want to read the official Apache documentation on Name-based virtual hosting to see all the configuration possibilities.