Welcome to part 5 of the MAMP tutorial. In this post we will go over setting up friendly URLs on your Mac server. We will be adding a new site called http://mysite.localhost
You will also learn how to run multiple domain sites on your local server. Having Virtual Hosts enabled is really useful when developing sites on your localhost. It’s also super easy to setup.
What green means: any lines that you need to replace in this tutorial will be in green.
1. Enable virtual hosts
1. Open your Terminal (found in your Utilities folder which is inside your Applications folder)
Then you will need to open the file Apache httpd.conf file with the Nano editor and uncomment the virtual hosts line.
2. Type the following line in your Terminal window
sudo nano /etc/apache2/httpd.conf
3. With the file open find the line where the Virtual Hosts file is commented out.
#Include /private/etc/apache2/extra/httpd-vhosts.conf
4. Remove the # in front of the line. This will enable the Virtual Hosts configuration file.
Include /private/etc/apache2/extra/httpd-vhosts.conf
5. Save and exit the file. Hold control + x to save and exit. Type Y to confirm. Then press enter.
2. Configure the virtual hosts file to setup the directories and logs for Apache
Next you want to configure your Virtual Host file. To do that you first need to open the virtual hosts file using Nano.
1. Type the following command in your Terminal window.
sudo nano /etc/apache2/extra/httpd-vhosts.conf
2. Scroll down to the end of the file and add the following.
<VirtualHost *:80>
DocumentRoot "/User/username/Sites"
</VirtualHost>
<VirtualHost *:80> DocumentRoot "/path/to/web/folder/where/site/is/located" ServerName mysite.localhost ErrorLog "/path/to/error/log/mysite.localhost.error_log" <Directory "/path/to/web/folder/where/site/is/located"> AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
The first VirtualHost record tells the server where to go if only http://localhost is entered in the browser. You can set this to any location you want.
The second VirtualHost record sets up your paths for a specific local domain. You will need to create a new record like this for each new site you want to add.
3. Save and exit the file. Hold control + x to save and exit. Type Y to confirm. Then press enter.
3. Adding the url record to your hosts file
This is very easy to do.
1. Open the hosts file in your nano editor using Terminal.
sudo nano /etc/hosts
2. Add a virtual hostname for each website.
Scroll down to the end of the file and add a new record.
127.0.0.1 mysite.localhost
Just like the virtual host file above you will need to add a new record foreach new website you want to add.
3. Save and exit the file. Hold control + x to save and exit. Type Y to confirm. Then press enter.
4. Restart Apache
Open terminal and run the following command to restart your Apache server
sudo apachectl restart
5. (Optional) Give Apache permission to access your site.
If you are getting a Forbidden message or you store your website folder in a location other than the /Users/username/Sites/ directory you will need to give Apache permission to access it.
Here’s how you do it.
1. Open Terminal and navigate to base of your site. For example if your site is stored in /srv/www/mysite/ go to it’s parent directory
cd /srv/www/
2. Next give the Apache user ownership rights of your site’s folder
sudo chown -R :_www mysite
3. Lastly, give the Apache group now associated with your folder permissions to access and write to it
sudo chmod -R g+w mysite
Restart Apache again and Congrats. Open your browser and go to the url http://mysite.localhost/
You will see your site there.
I created an index.php file to demonstrate how this works.