So, you are the fortunate owner of a Macbook Pro and are working on several projects. How to setup Apache to work with multiple hosts/websites on just your local IP?
The first thing you want to do is spoof your IP to some domains you want to use so those domains are routed to your local IP. To do this you need to edit your hosts file:
1 |
sudo nano /etc/hosts |
Next, add the domains to be resolved to your local IP:
1 2 3 4 |
127.0.0.1 sangatpedas.local 127.0.0.1 www.sangatpedas.local 127.0.0.1 google.local 127.0.0.1 www.google.local |
Save and close, you can instantly test it by opening http://microsoft.local in your browser, you should get the start page of your Apache server.
Next, you need to enable the use of virtual hosts in Apache.
1 |
sudo nano /etc/apache2/httpd.conf |
Next look for this line:
1 |
#Include /private/etc/apache2/extra/httpd-vhosts.conf |
Change it to:
1 |
Include /private/etc/apache2/extra/httpd-vhosts.conf |
Save and exit. Now you have enable the use of virtual hosts in Apache and the only thing left is defining the virtual hosts. For this you need to edit the file httpd-vhosts.conf:
1 |
sudo nano /etc/apache2/extra/httpd-vhosts.conf |
Here’s an example of how you can configure the 3 local domains we’ve defined earlier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<VirtualHost *:80> ServerAdmin dude@sangatpedas.com ServerName sangatpedas.local ServerAlias www.sangatpedas.local DocumentRoot "/Library/WebServer/Documents/sangatpedas" ErrorLog "/private/var/log/apache2/sangatpedas.local-error_log" CustomLog "/private/var/log/apache2/sangatpedas.local-access_log" common <Directory "/Library/WebServer/Documents/sangatpedas"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:80> ServerAdmin dude@sangatpedas.com ServerName google.local ServerAlias www.google.local DocumentRoot "/Library/WebServer/Documents/google" ErrorLog "/private/var/log/apache2/google.local-error_log" CustomLog "/private/var/log/apache2/google.local-access_log" common <Directory "/Library/WebServer/Documents/google"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> |
Now make sure the folders for your websites exist and have something in it, restart Apache and check the domains in your browser.
The only comment here is that if you already had a website running in your root (/Library/WebServer/Documents/) then that one won’t work anymore. No worries, best to create a new sub-folder, move the website there and create another virtual host for it.