使用MAMP Pro的自动虚拟主机? [英] Automatic Virtual Hosts with MAMP Pro?

查看:80
本文介绍了使用MAMP Pro的自动虚拟主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MAMP Pro,您可以创建条目"local.example.com",并指向本地计算机上该站点的根目录.

With MAMP Pro, you can create entries such "local.example.com" and point to the root directory for that site on your local machine.

是否可以自动化?

在我的计算机上,我的设置如下:

That is on my computer I have it setup like this:

User/hm/Sites/example.com/app 用户/hm/Sites/example2.com/app 用户/hm/Sites/example3.com/app 等等...

User/hm/Sites/example.com/app User/hm/Sites/example2.com/app User/hm/Sites/example3.com/app etc...

app文件夹内是每个域的实际站点.

Inside the app folder is the actual site for each respective domain.

我当然可以手动将每个添加到MAMP Pro,并为每个站点添加一个关联的网址"example1.com","example2.com","example3.com"等,但我想知道是否有一个通过编辑MAMP Pro的virtualhost模板来自动执行此操作的方法.因此,如果我在站点的文件夹中创建了一个名为"example4.com"的新文件夹,并将其中的应用程序文件夹与WordPress站点一起放在其中,那么只要MAMP Pro正在运行,我就可以在浏览器中访问example4.com并查看该本地站点.

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.

我看到了有关MAMP(非专业版)的文章

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.

推荐答案

我有一个类似的想法,因为我想将drush(用于管理drupal站点的命令行工具)与MAMP和MAMP Pro集成在一起,并使其成为现实.可以自动创建虚拟主机进行测试.

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.

您手动添加到MAMP Pro的信息存储在文件~/Library/Preferences/de.appsolute.mamppro.plist中,该文件随后用于基于模板文件修改/创建/etc/hostshttpd.conf文件.实际上,模板文件存储在~/Library/Application Support/appsolute/MAMP PRO/templates中,生成的文件存储在Library/Application Support/appsolute/MAMP PRO/中(检查httpd.conf以及该文件夹中的hosts).

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 ).

由于这些文件是由MAMP每次您保存手动修改并重新启动时生成的,因此无论如何都无法编辑这些文件(虽然我们没有研究它的详细信息,但是我记得当我尝试手动编辑那些文件时,没有希望).

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).

您需要做的是直接编辑plist文件de.appsolute.mamppro.plist.可以使用PlistBuddy(/usr/libexec/PlistBuddy请参阅man PlistBuddy)实现对plist文件的读写.

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).

您可以使用以下命令将信息提取到plist文件中:

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
    }

其中,documentRootserverName是每个虚拟主机所需的主要值. 如果希望输出为xml plist形式,则添加-x选项:

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 =阵列{ 字典{ 虚拟主机1的值 } 字典{ 虚拟主机2的值 } 字典{ ...等等... } }

virtualHosts = Array { Dict { ...Virtual Host 1 values... } Dict { ...Virtual Host 2 values... } Dict { ...etc... } }

以上命令吐出整个plist文件.仅使用手册页,要弄清楚如何才能专注于virtualHosts数组,或者说出virtualHosts数组中的第二个Dict(记录),就很费劲.感谢关于操纵列表的页面,我明白了(在以下命令中,我跳过了/usr/libexec/部分)对: 要访问整个virtualHosts数组,请使用以下命令:

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

打印第二条记录:

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

打印第五条virtualHosts记录的documentRoot:

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

根据manPlistBuddy -c Add entry type [value]将为您解决问题(即添加/写入所需的记录),因此可以通过将上述信息与手册页和链接结合在一起来解决您的问题(链接为如果您想编写脚本很有用).

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).

我还没有完成自动化脚本(我更喜欢phpbash脚本中的drush代码),但我认为以下几点将对您有所帮助:

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:

如果要进行任何测试,请确保首先备份de.appsolute.mamppro.plist,或者复制此文件并对该副本进行所有测试.因此,在下面的内容中,我假设您在终端上,并且已经对该命令进行了别名并按如下所示复制了plist文件:

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

要将新的Dict添加到virtualHosts阵列:

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

查找新虚拟主机条目的索引,然后执行类似操作(假定为bash脚本):

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

,并为此新记录添加documentRoot条目:

and to add the documentRoot entry for this new record:

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

其中XXX是Previews命令输出的行号(因为数组的索引以0开头).

where XXX is the line number output from the previews command (since index of arrays starts with 0).

当然,您必须对所有条目(FollowSymLinksserverName等)重复此操作,以确保尊重结构和类型(在单个Dict条目中具有数组,整数和字符串).最后但并非最不重要的一点是,必须重新启动MAMP Pro(或至少是服务器)才能使~/Library/Preferences/de.appsolute.mamppro.plist中的更改生效.再次备份您的plist文件!

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.

这篇关于使用MAMP Pro的自动虚拟主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆