Automatic Virtual Hosts with MAMP Pro?

2020-03-04 10:03发布

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.

标签: mamp
1条回答
小情绪 Triste *
2楼-- · 2020-03-04 10:48

I had a similar thought since I wanted to integrate drush (a command line tool to manage drupal 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 the httpd.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 in Library/Application Support/appsolute/MAMP PRO/ (check httpd.conf as well as hosts 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 file de.appsolute.mamppro.plist directly. Reading and writing to plist files can be achieved using PlistBuddy (/usr/libexec/PlistBuddy see man PlistBuddy).

You can extract the information in the plist file using the command:

/usr/libexec/PlistBuddy -c Print ~/Library/Preferences/de.appsolute.mamppro.plist

A typical (single) record of a virtual host with this command would look like this:

    Dict {
        MultiViews = false
        documentRoot = /home/test/openpublish-2.3
        Order = 0
        ExecCGI = false
        serverAliases = Array {
        }
        dyndns = Dict {
            displayName = -
        }
        serverName = openpublish-2.3
        FollowSymLinks = true
        AllowOverride = 0
        local = true
        Allow = 0
        Includes = true
        Indexes = false
        SymLinksifOwnerMatch = false
    }

where documentRoot and serverName 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:

/usr/libexec/PlistBuddy -x -c Print ~/Library/Preferences/de.appsolute.mamppro.plist

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 on virtualHosts array or say the second Dict (record) in the virtualHosts 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 whole virtualHosts array use the command:

PlistBuddy -c "Print :virtualHosts" ~/Library/Preferences/de.appsolute.mamppro.plist

to print the second record:

PlistBuddy -c "Print :virtualHosts:2" ~/Library/Preferences/de.appsolute.mamppro.plist

to print the documentRoot of the fifth virtualHosts record:

PlistBuddy -c "Print :virtualHosts:5:documentRoot" ~/Library/Preferences/de.appsolute.mamppro.plist

According to the man page PlistBuddy -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 in php or bashscript 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 the plist file as follows:

$ alias PlistBuddy="/usr/libexec/PlistBuddy"
$ cp ~/Library/Preferences/de.appsolute.mamppro.plist safe_to_play.plist

To add a new Dict to virtualHosts array:

PlistBuddy -c "Add :virtualHosts: dict" safe_to_play.plist

to find the index of the new virtual host entry then do something like (bash script assumed):

PlistBuddy -c "Print :virtualHosts: dict" safe_to_play.plist  | grep documentRoot | wc -l

and to add the documentRoot entry for this new record:

PlistBuddy -c "Add :virtualHosts:XXX:documentRoot string /home/path" safe_to_play.plist

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 single Dict 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 your plist file!!!

I hope this helps you produce the script you wanted.

查看更多
登录 后发表回答