I recently retooled a Zend development environment on a mac laptop running OS X. I had been using the free version of MAMP for awhile but found the free version too limited. MAMP is useful for getting Apache, PHP, and MySQL stack up and working together quickly on a mac. One thing lacking in MAMP though is an actual http.conf file like you probably deal with on a production server.
XAMP is another free AMP stack which includes an editable httpd.conf file making configuring Apache to test SSL connections much more standardized if not straight forward. Instead of dealing with a GUI one deals with the Apache configuration files directly. While it’s nice to be able to customize XAMPs Apache installation getting the configuration right can take some time. If your really uncomfortable meddling with Apache configuration files you should consider just purchasing a licensed version of MAMP which does most of the stuff through a graphical interface. Of course once you go into production you’ll probably going to have to deal with Apache’s httpd.conf file anyway so you might as well front load the pain now and be better prepared when things actually do go into production.
So without further ado I now present the first post on the new acwolf blog: how to configure XAMP virtual hosts on a mac.
Step One, Download and configure XAMP
First download XAMP for mac.
Once you have XAMP running on you mac you’re ready to begin configuring virtual hosts. Using Mac Finder go to Applications > xampp > etc and open the httpd.conf file. Near the bottom of the file look for the following lines:
# Virtual hosts
#Include /Applications/xampp/etc/extra/httpd-vhosts.conf
The pound symbol (#) denotes a comment. Since we want to setup virtual hosts go ahead and uncomment the second line by removing the pound symbol like so
# Virtual hosts
Include /Applications/xampp/etc/extra/httpd-vhosts.conf
With the above line uncommented Apache will now look to the httpd-vhosts.conf file for instructions on how to configure virtual hosts. Go to Applications > xampp > etc > extra and open httpd-vhosts.conf to begin configuring virtual hosts for XAMP. An imported (and practically undocumented) fist step is to include localhost has the default named virtual host. Do this by adding the following to the httpd-vhosts.conf:
<VirtualHost *:80>
ServerName localhost
DocumentRoot “/Applications/xampp/htdocs”
<Directory “/Applications/xampp/htdocs”>
Options Indexes FollowSymLinks Includes execCGI
AllowOverride None
Order Allow,Deny
Allow From All
</Directory>
</VirtualHost>
With that in place you are now ready to add your other virtual hosts, in my case I’m developing a Zend application called zendapplication. It’s located in a folder called myZendApps in the User > Documents folder.
<VirtualHost *:80>
ServerName zendapplication.dev
ServerAlias wwww.zendapplication.dev
DocumentRoot “/Users/aaronwolf/Documents/myZendApps/zendapplication/public”
<Directory “/Users/aaronwolf/Documents/myZendApps/zendapplication/public”>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog “logs/zendapplication-error_log”
CustomLog “logs/zendapplication-access_log” common
</VirtualHost>
For all the details and options involved with setting up a virtual host refer to the apache documentation. The two critical configuration options here are ServerName and Document root. ServerName denotes what domain this particular virtual hosts responds to. DocumentRoot tells Apache where the files associated with this particular domain live. Since Zend Framework applications (like Rails applications) only make the public directory accessible to the web I have the document root pointing to the public directory of my Zend application.
So far we,
- Created a localhost vertual host pointing to XAMPs default root folder (/Applications/xampp/htdocs).
- Created a virtual host for a zend application located in Users > aaronwolf >Documents > myZendApps > zendapplication >public.
- Assinded a server name (the local domain name) of our choosing to the new virtual host.
Note: I used the .dev extension for my server name. It’s best not to use .com or .net domain names when configuring local applications, you’ll see why once you…
Configure the hosts file
The next step is to configure your host file. A hosts file works like a DNS server associating domain names with IP address except domain names configured in your local hosts file only effect your local development machine.
In the example presented here we have configured Apache to respond to requests for zendapplication.dev now we want to be able to type zendapplication.dev in to a browser and play with our application. Since zendapplication.dev only exists on our local development machine (not the internet) we’ll need to configure our local host file to send requests for the zendapplication.dev domain to our development computers IP address (127.0.0.1) to be handled by Apache server. Sounds complicated but to get it done is very simple.
Fist you’ll need to open your hosts file. On a mac you can find the hosts file in the /etc directory (you’ll need root permission to edit the file).
Open a concole window and type:
sudo nano /etc/hosts
Add the following line to the bottom of your hosts file (be sure to sperate the IP number and the domain name with a tab).
127.0.0.1 zendapplication.dev
Now when you type zendapplication.dev in to the address bar of you local browser you’ll should see…
You don’t have permission to access /xampp/index.php on this server.
Nice! Turns out there’s one more bit of configuration to be done (at least if you’re storing you applications under the User > Documents directory on a mac like I am). You’ll need to tell the Apache process to run with your user permissions (something that should never be done on a production machine). Back to Application > xamp > etc, open httpd.conf and look for the following block:
<IfModule !mpm_netware_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User nobody
Group admin
</IfModule>
Notice by default Apache is set up to run as user “nobody” which lacks permissions to access files in your Documents directory. To fix this simply change nobody to the username you uses when logging into your machine, so in my case…
User aaronwolf
Group admin
That should about do it. Type zendapplication.dev into your browser and go to town.
If you notice your application’s indexpage redirects to /xampp/index.php you’ll just clear your browsers chache.
Hopfully that will save someone a few hours…