Essomia's Blog

my life in development

Virtual Host on Mac OS X with MAMP

Virtual hosting is a method for hosting multiple names domain on a server using a single IP address. Here, virtual host will help us to work on web sites in local with a web address corresponding to the name domain reserved for the futur website. This little tutorial will explain you who to make a virtual host ton your Mac.

Note : All my interfaces are in French but you can see them to guide you.

You will need :

- Mac OS X
- MAMP (Usual web server on Mac OS)
- Smultron (Smultron is a code editor)
- Onyx (Can easily change the system settings of MacOS.It will be used to see the hidden file on your computer)

Step 1 : Configure Apache Port 80 with MAMP

By default, the Apache server in MAMP is configured to listen on port 8888 instead of the standard port, ie port 80. This is not the usual configuration and this requires that the web address of your local server is http://localhost:8888, instead of http://localhost.

ScreenShoot Mamp Port 80 for Virtual Host

- Launch MAMP.
- Go to the MAMP window control on “Preferences”.
- Click on the “Ports” tab.
- Select “Use the standard settings” and confirm.
- After restarting MAMP, Apache and MySQL running in a conventional manner.
=> Now, your local adress should be : http://localhost.

Step 2 : Showing hidden files in Mac OS with Onyx

ScreenShoot Onyx Hidden Files for Virtual Host

- Launch Onyx.
- Display “Settings” Panel > “Finder”.
- Check the box “Show hidden files and folders”.
- Accept to restart the Finder.

Step 3 : Changes access permissions to the hosts file

ScreenShoot Finder for Virtual Host

- Open a Finder window.
- View the contents of folder “Macintosh HD/etc/”.
- In this folder, locate the file “hosts”.
- Do a Ctrl + Click on the hosts file to display the context menu and choose “Display Info”.
- At the bottom of the panel “Information”, click the small lock at the bottom right.
- In “sharing and permissions” select/add your own user name to become the owner of the hosts file.
- Secure access for other users/groups and others to “Reading and Writing”.

Step 4 : Changing the “hosts” file

- Do a Ctrl + click the hosts file and open it with Smultron.
- Add a new line in the file and type the IP of your local Web server.

127.0.0.1

- Make one or more spaces following the IP number and add the name domain of your site : “example.com” or “www.example.com”.
- You must add a line like this.

127.0.0.1 example.com

- Save the file host.

Step 5 : Changing the configuration file “httpd.conf”

- Open the file “/Applications/MAMP/conf/apache/httpd.conf”.
- Add the following lines at the end of the document:

NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
    DocumentRoot /Applications/MAMP/htdocs/
    ServerName localhost
</VirtualHost>
<VirtualHost 127.0.0.1:80>
    DocumentRoot  /Applications/MAMP/htdocs/example/
    ServerName example.com
    ServerAlias *.example.com
    <Directory “/Applications/MAMP/htdocs/example/“>   
        Options Indexes FollowSymLinks MultiViews   
        DirectoryIndex index.php index.html
        AllowOverride All
        Order allow,deny   
        Allow from all   
    </Directory>   
</VirtualHost>

- Replace the bold text by the values ​​corresponding to your home directory and virtual domain name you have chosen.
- Save your “httpd.conf” and restart MAMP server.

Note: Now by typing: “http://example.com” in your browser, you will go on your website in your local server.