如何在MAMP中创建虚拟主机? [英] How to create virtual hosts in MAMP?

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

问题描述

我是Mac的新手,但长期使用Ubuntu进行开发.我知道如何在Ubuntu中创建虚拟主机,但对Mac却一无所知.我创建了一个hosts条目,如下所示:

I am new to Mac but used Ubuntu for development for a long time. I know how to create virtual hosts in Ubuntu but have no idea about Mac. I have created a hosts entry like below :

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost mysite.loc
255.255.255.255 broadcasthost
::1             localhost

但是下一步该怎么做?

推荐答案

在进行谷歌搜索时,我发现了以下步骤可以轻松地在MAMP上创建虚拟主机:

While googling, I found these steps to easily create virtual hosts on MAMP:

  1. 在Mac中打开控制台,然后编辑主机文件

  1. Open your console in mac and edit your hosts file like this

sudo vim/etc/hosts

sudo vim /etc/hosts

这将打开一个包含以下行的系统文件:

This opens a system file that contains the following line:

127.0.0.1    localhost

在本地主机后添加所需的主机名:

add your desired host name after local host:

127.0.0.1    localhost mysite.loc

ESC ,然后按:wq!覆盖并关闭文件.

press ESC, then :wq! to overwrite and close the file.

  1. 现在转到您的MAMP目录,并在任何文本编辑器中打开位于Applications/MAMP/conf/apache/httpd.conf的apache配置文件,并找到以下行:

# Virtual Hosts
# Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

从以Include开头的行的开头删除井号(井号)

Remove the hash (pound) sign from the beginning of the line that begins with Include

# Virtual Hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

保存文件,然后打开Applications/MAMP/conf/apache/extra/httpd-vhosts.conf.在这里定义虚拟主机.

Save the file, and then open Applications/MAMP/conf/apache/extra/httpd-vhosts.conf. This is where you define the virtual hosts.

  1. 页面底部是两个如何在Apache中定义虚拟主机的示例.他们看起来像这样:

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/Applications/MAMP/Library/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/Applications/MAMP/Library/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

编辑两个示例.虚拟主机将覆盖现有的本地主机,因此第一个主机需要重新建立本地主机.编辑要添加的虚拟主机的第二个主机.仅需要DocumentRoot和ServerName指令.要为mysite添加虚拟主机,编辑后的定义应如下所示:

Edit both examples. Virtual hosts override the existing localhost, so the first one needs to re-establish localhost. Edit the second one for the virtual host you want to add. Only the DocumentRoot and ServerName directives are required. To add a virtual host for mysite, the edited definitions should look like this:

<VirtualHost *:80>
    DocumentRoot /Applications/MAMP/htdocs
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/username/Sites/mysite"
    ServerName mysite.loc
</VirtualHost>

这假定您要在站点"文件夹中找到mysite的文件.用您自己的Mac用户名替换第二个定义中的用户名".如果要将文件存储在其他位置,请相应地调整DocumentRoot的值.

This assumes that you want to locate the files for mysite in your Sites folder. Replace "username" in the second definition with your own Mac username. If you want to store the files in a different location, adjust the value of DocumentRoot accordingly.

如果要创建多个虚拟主机,请复制其中一个定义,然后进行相应的编辑.

If you want to create more than one virtual host, copy one of the definitions, and edit it accordingly.

保存所有已编辑的文件,然后在MAMP控制面板中重新启动服务器.现在,您应该可以使用以下URL访问虚拟主机: http://mysite.loc/.

Save all the files you have edited, and restart the servers in the MAMP control panel. You should now be able to access the virtual host with the following URL: http://mysite.loc/.

享受.. !!

这篇关于如何在MAMP中创建虚拟主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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