【后端笔记】MacOS 下 apache 配置 PHP 和 多个虚拟主机

Apache官网:http://www.apache.org/

Apache 命令:

启动Apache:sudo apachectl start
关闭Apache:sudo apachectl stop
重启Apache:sudo apachectl restart

配置 PHP:

1、打开终端,输入命令:sudo vim /etc/apache2/httpd.conf
2、找到 #LoadModule php5_module libexec/apache2/libphp5.so,去掉注释(删除前面的 #)。

注:创建 phpinfo.php,添加 内容 。可通过浏览器 访问 phpinfo.php 获取 php 信息。

配置 Virtual hosts:

1.、打开终端,输入命令:sudo vim /etc/apache2/httpd.conf
2、找到下面的配置,去掉 #Include /private/etc/apache2/extra/httpd-vhosts.conf 的注释(删除前面的 #)

原始配置:

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

修改后的配置:

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

3、找到下面的配置并修改保存退出

原始的配置:


    AllowOverride none
    Require all denied

修改后的配置:


    Options  Indexes  FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all

4、找到下面的配置,注释掉 AllowOverride None,添加 AllowOverride All

原始配置:

DocumentRoot "/Library/WebServer/Documents"

    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks Multiviews
    MultiviewsMatch Any

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted

修改后的配置:

DocumentRoot "/Library/WebServer/Documents"

    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks Multiviews
    MultiviewsMatch Any

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    #AllowOverride None
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted

5、打开终端,输入命令:sudo vim /etc/apache2/extra/httpd-vhosts.conf

6、注释掉下列配置:

#
#    ServerAdmin [email protected]
#    DocumentRoot "/usr/docs/dummy-host.example.com"
#    ServerName dummy-host.example.com
#    ServerAlias www.dummy-host.example.com
#    ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
#    CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
#

#
#    ServerAdmin [email protected]
#    DocumentRoot "/usr/docs/dummy-host2.example.com"
#    ServerName dummy-host2.example.com
#    ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
#    CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
#

7、添加下列配置,这里添加了 localhost 和 project.com 两个虚拟主机;"/Library/WebServer/Documents" 为默认目录,"/Users/mac168/sites" 为自定义目录。


    DocumentRoot "/Library/WebServer/Documents"
    ServerName localhost
    ErrorLog "/private/var/log/apache2/localhost-error_log"
    CustomLog "/private/var/log/apache2/localhost-access_log" common



    DocumentRoot "~/sites"
    ServerName project.com
    ErrorLog "/private/var/log/apache2/sites-error_log"
    CustomLog "/private/var/log/apache2/sites-access_log" common
    
                Options Indexes FollowSymLinks MultiViews
                AllowOverride none
                Order deny,allow
                Allow from all
    

8、打开终端,输入命令:sudo vim /etc/hosts

9、修改为下列配置

127.0.0.1       localhost
127.0.0.1       project.com
255.255.255.255 broadcasthost
::1             localhost
::1             project.com

注意事项:

1、DocumentRoot 根目录如果没有 index 索引,访问 localhost 会提示 403 Forbidden。
2、自定义 DocumentRoot 根目录时,根目录的每一级都要有访问权限,否则同样提示 403 Forbidden。可通过 终端命令 chmod 修改,也可通过 command + i,显示目录简介,在 共享与权限 中修改(修改为 everyone 修改为 只读即可)。

你可能感兴趣的:(【后端笔记】MacOS 下 apache 配置 PHP 和 多个虚拟主机)