Table of Contents
- What is the hosts file and how to edit it?
- Virtualhost and HTTP requests for localhost
- What are a Host Name (ServerName), DirectoryIndex and DocumentRoot?
- Configure Virtualhost
- Restart MAMP
- How to check the MAMP error log
- Conclusion
After our previous blogs on WordPress Development Setup, About WordPress and How to install MAMP? Now let’s configure virtualhost in MAMP for WordPress and other PHP Projects. If you need an easy setup and maintenance for your local server then MAMP is the best option. MAMP is free and comes with Apache, MySQL and PHP.
Why do we need virtualhost? let’s understand it. In the case of a local server, there is only one domain i.e localhost. Localhost’s default configuration is to execute web pages from the www or htdocs folder. You are limited to a single local host. But if you are working on multiple websites then you need to define various domains. To serve pages, every site requires its respective folder. Here virtualhost will help you to overcome these limitations and allow you to configure multiple websites.
Let’s see how to configure virtualhost. The MAMP Pro version makes this process super easy with a userfriendly dashboard but is not free. We are using a free version, so we need to configure each virtual host manually. It is effortless and just follows steps.
What is the hosts file and how to edit it?
We will start the process by understanding the hosts file and its role. Every operating system has its own hosts file, the format stays mostly the same across all platforms. The hosts file defines the local host’s Internet Protocol name and internet address. This file is used by the operating system to map a hostname to an internet address or IP address. The first part is the IP address, the second is a domain name, and the third is a comment, all separated by space ( Tab ).
In macOS, the hosts file is hidden and located in the /private/etc folder. It is protected, so we have to use sudo to open and edit it.
% sudo nano /private/etc/hosts
Enter your admin password and open the hosts file. You will see something like this below.
We will not delete anything but will add a new line to the bottom of the file.
#Virtuals Hosts
127.0.0.1 vedigurukul.lan
(Replace vedigurukul.lan with your local domain name). Now our hosts file will look as follow.
Save and close the file.
Virtualhost and HTTP requests for localhost
Before setting up virtualhost manually, Apache serves HTTP requests for localhost from the default directory i.e. /Applications/MAMP/htdocs. When you configure your virtualhost, the HTTP request will search for a matching set of virtualhost. If the request successfully matches appropriate the configuration then Apache serve pages from the folder ( DocumentRoot ) defined in the structure. But if it is failed then it will be picked up by whichever exits the first virtual host setup as default. To avoid such a situation we will configure one default virtual host that may serve as a list of your development projects or can continue with the MAMP virtual host.
What are a Host Name (ServerName), DirectoryIndex and DocumentRoot?
The Local Host’s name ( Local Server Name / Local Domain ) must be unique and not conflict with the outside domain. It is called ServerName. To serve the website, every local host will have its separate folder where all html, php pages and images will be stored. The location of a directory calls DocumentRoot. DirectoryIndex defines your landing page. DirectoryIndex will tell the server which file will load as the landing page. You can enter multiple pages according to your preferences.
Configure Virtualhost
Let’s start setting up virtualhost. The first step is to include the httpd-vhosts.conf file. This file is important because you will define each website’s details here in this file i.e. ServerName, DirectoryIndex and DocumentRoot. So let’s locate MAMP installed directory. Open httpd.conf file from /Applications/MAMP/conf/apache/ in your favourite editor. Here you will see lots of stuff, but search the below line that includes the httpd-vhosts.conf file.
#Virtual hosts
#Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
You need to do here uncomment 2nd line that includes httpd-vhosts.conf. It will look as follow.
#Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Now time to define the details of our projects. Before proceeding, you need to create the project folder in /Applications/MAMP/htdocs/, here we created a folder called vedigurukul. Now open /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf file and comment all existing settings. Enter a new setting as follow.
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/vedigurukul"
ServerName vedigurukul.lan
DirectoryIndex home.php
</VirtualHost>
If you do not want to create the project folder in MAMP’s htdocs then you can create it at your convenient location. That folder must have Read & Write permission. Enter the folder path in DocumentRoot in the configuration rest settings will remain the same.
<VirtualHost *:80>
DocumentRoot "/Users/arjunondkar/sites/quiz"
ServerName quiz.lan
DirectoryIndex home.php
</VirtualHost>
Restart MAMP
Finally, you completed all steps now our local development websites are ready. Restart Apache to get new configurations in effect. Always restart the server whenever you make changes in the .conf file.
How to check the MAMP error log
In the development phase, error logs tell you about issues in the project. MAMP create error logs for you. Just navigate to the MAMP installation folder where you can see the logs folder for Apache, MySQL and PHP errors. One of the best practices is to generate error logs and check them before going to trial and error troubleshooting. If you faced any issue with MAMP, the error log will help or suggest you the steps to resolve the issue.
Conclusion
So after a small intro about WordPress, and WordPress development setup, how to install MAMP. Now we successfully set up our local development environment with MAMP. We discussed virtualhost and its benefits, editing hosts and httpd files, and how configuring multiple local domains. If you faced any issues during the process please let us know so we can help out. It will also help us to improve the knowledge that we could pass on to other people.
Adv