With MAMP Pro, you can create entries such "local.example.com" and point to the root directory for that site on your local machine.
Is it possible to automate this?
That is on my computer I have it setup like this:
User/hm/Sites/example.com/app User/hm/Sites/example2.com/app User/hm/Sites/example3.com/app etc...
Inside the app folder is the actual site for each respective domain.
While I of course can manually add each of those to MAMP Pro and add an asscoiated url "example1.com", "example2.com", "example3.com" etc for each respective site I was wondering if there is a way to automate this by editing a virtualhost template for MAMP Pro. So if I created a new folder called "example4.com" in my Sites's folder and put an app folder inside of it with a WordPress site, then as long as MAMP Pro is running I could go to example4.com in my browser and view that local site.
I saw an article on this for MAMP (not Pro) http://wp.tutsplus.com/tutorials/hosting/wordpress-development-and-deployment-with-mamp-git-and-dropbox/ but that one requires you to still manually add each entry to your host file too which defeats the purpose since I could just as easily add it MAMP Pro manually then.
I had a similar thought since I wanted to integrate
drush
(a command line tool to managedrupal
sites) with MAMP and MAMP Pro and make it possible to automate the creation of a virtual host for testing.The information you manually add to MAMP Pro gets stored in the file
~/Library/Preferences/de.appsolute.mamppro.plist
which is then used to modify/create/etc/hosts
and thehttpd.conf
file based on the template file. Actually the template files are stored in~/Library/Application Support/appsolute/MAMP PRO/templates
and the generated files inLibrary/Application Support/appsolute/MAMP PRO/
(checkhttpd.conf
as well ashosts
in this folder ).There is no point editing those files anyway since they are generated by MAMP every time you save the manual modifications and restart (well haven't investigated the details of it but I remember that when I tried to manually edit those files I had no hope).
What you need to do is to edit the
plist
filede.appsolute.mamppro.plist
directly. Reading and writing toplist
files can be achieved usingPlistBuddy
(/usr/libexec/PlistBuddy
seeman PlistBuddy
).You can extract the information in the
plist
file using the command:A typical (single) record of a virtual host with this command would look like this:
where
documentRoot
andserverName
are the main values you need for each virtual host. If you want the output to be in the form of an xml plist then add the-x
option:The single record above is one of many in an array which has the following structure:
virtualHosts = Array { Dict { ...Virtual Host 1 values... } Dict { ...Virtual Host 2 values... } Dict { ...etc... } }
The above commands spit out the whole
plist
file. Using the man page alone it was a long pain to find out how one can focus onvirtualHosts
array or say the secondDict
(record) in thevirtualHosts
array. Thanks to this page on Manipulating Plists I got it (in the following commands I skip the/usr/libexec/
part) right: to access the wholevirtualHosts
array use the command:to print the second record:
to print the
documentRoot
of the fifthvirtualHosts
record:According to the
man
pagePlistBuddy -c Add entry type [value]
would do the trick for you (i.e. add/write the required record) so your problem can be solved by combining the above info with the man page and the link (the link is useful if you would like to make a script).I have not completed my automated scripts yet (
drush
code inphp
orbash
script is my preference) but I think the following points will help you:Firs of all if you are doing any tests make sure you make a back-up of
de.appsolute.mamppro.plist
first or you copy this file and do all the tests on the copy. So in the following I assume you are on a terminal and you have already aliased the command and copied theplist
file as follows:To add a new
Dict
tovirtualHosts
array:to find the index of the new virtual host entry then do something like (
bash
script assumed):and to add the
documentRoot
entry for this new record:where
XXX
is the line number output from the previews command (since index of arrays starts with 0).Of course you have to repeat this for all the entries (
FollowSymLinks
,serverName
etc) making sure you respect the structure and type (you have arrays and integers and strings in the singleDict
entry). Last but not least MAMP Pro (or at least the servers) must be restarted for the changes in~/Library/Preferences/de.appsolute.mamppro.plist
to take effect. Once again do back-up yourplist
file!!!I hope this helps you produce the script you wanted.