知行

在Mac OS X 10.10上怎样设置虚拟主机

在Mac OS X 10.10上怎样设置虚拟主机

通过配置Apache可以设置虚拟主机。在终端使用nanovim

允许虚拟主机配置

sudo nano /etc/apache2/httpd.conf

搜索vhosts并取消该行注释。

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

这将允许httpd-vhosts.conf文件的使用,关闭文件。打开下面这个文件:

sudo nano /etc/apache2/extra/httpd-vhosts.conf

然后添加虚拟主机:

<VirtualHost *:80>
    ServerName center.local
    ServerAlias www.center.local
    DocumentRoot "/Users/username/Sites/center"
    ErrorLog "/private/var/log/apache2/center.local-error_log"
    CustomLog "/private/var/log/apache2/center.local-access_log" common
        <Directory "/Users/username/Sites/center">
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
</VirtualHost>

上面的ServerNameServerAlias取你自己喜欢的名称。username替换为你的电脑用户名称。

修该hosts

sudo nano /etc/hosts

添加:

127.0.0.1 center.local
127.0.0.1 www.center.local

重启Apache

sudo apachectl restart

注意:在httpd.conf文件中在ServerName下添加

ServerName localhost:80