Mac OSX 10.15 配置apache2 虚拟主机

admin PHP 2020年1月17日 17:24 查看677次

A very handy development option is to have multiple virtual hosts set up for you various projects. This means that you can set up names such as grav.mydomain.com which point to your Grav setup, or project-x.mydomain.com for a project-specific URL.

Apache generally performs name-based matching, so you don't need to configure multiple IP addresses. Detailed information can be found on the apache.org site.

Apache already comes preconfigured to support this behavior but it is not enabled. First you will need to uncomment the following lines in your /usr/local/etc/httpd/httpd.conf file:

LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so

and:

# Virtual hosts
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf

Then you can edit this referenced file and configure it to your needs:

$ code /usr/local/etc/httpd/extra/httpd-vhosts.conf

This file has some instructions already but the important thing to remember is that these rules are matched in order. When you set up virtual hosts, you will lose your older document root, so you will need to add back support for that first as a virtual host.

<VirtualHost *:80>
    DocumentRoot "/Users/your_user/Sites"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/your_user/Sites/grav-admin"
    ServerName grav-admin.test
</VirtualHost>

Don't forget to change your_user for your actual username on your Mac. For example: DocumentRoot "/Users/bernard/Sites"

As you set up your .test virtual hosts, you may receive a warning such as Warning: DocumentRoot [/Users/your_user/Sites/grav-admin] does not exist when restarting Apache. This just lets you know that the source directory listed for your virtual hosts is not present on the drive. It's an issue that can be resolved by editing this file with the corrected DocumentRoot.

源文地址:https://getgrav.org/blog/macos-catalina-apache-mysql-vhost-apc