How to setup a proxy and virtual hosts on Mac-Apache

Apache, Macintosh, Web Development, 2016-04-07 04:09:55 UTC

By default, Apache installed in Mac. In some case for development, We need to test it using a domain or virtual host. Let say We develop an Android or IOS app. The API serve by Ruby on Rails or NodeJS. For this case, We can't access the API through http://localhost:3000. If we do, We end up with 'mobile localhost' and a bunch of error. For this We just need a virtual host. 

 This is how I do it. Just follow these steps;

 

1. Open the httpd.conf

sudo nano /etc/apache2/httpd.conf

 

2. Edit the httpd.conf

Uncomment this line;

Include /private/etc/apache2/extra/httpd-vhosts.conf

 

and this line;

LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

 

3. Edit the fhosts.conf

sudo nano /etc/apache2/extra/httpd-vhosts.conf

 

Add these lines and change it according to your configuration

# local3000.local
<VirtualHost *:80>
    ServerName local3000.local
    ErrorLog /private/var/log/apache2/local3000.err.log
 
    ProxyRequests off
 
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
 
    <Location />
        ProxyPass http://localhost:3000/
        ProxyPassReverse http://localhost:3000/
    </Location>
</VirtualHost>

 

4. Map your virtual host

sudo nano /etc/hosts

 

add this line;

127.0.0.1       local3000.local

 

5. Restart Apache

sudo apachectl restart

 

6. Open http://local3000.local in you browser

 

Thats it! 


Share: