Mac OS X中配置Apache

我的Mac OS X 版本是10.11.6。

启动Apache

打开“终端(terminal)”,输入 sudo apachectl -version

接着输入sudo apachectl start,这样Apache就启动了。
打开Safari浏览器地址栏输入http://localhost,可以看到内容为It works!的页面。其位于/Library/WebServer/Documents/下,这就是Apache的默认根目录。
Apache的安装目录在:/etc/apache2/,etc默认是隐藏的。可以直接在terminal 输入 open /etc

设置虚拟主机

  • 在终端运行sudo vi /etc/apache2/httpd.conf,打开Apche的配置文件
  • httpd.conf中找到#Include /private/etc/apache2/extra/httpd-vhosts.conf,去掉前面的“#”。
    找到下面内容:
<Directory />
    AllowOverride none
    Require all denied
</Directory>

改为:

<Directory />
    AllowOverride none
#    Require all denied
     Require all granted
</Directory>

保存并退出。

  • 运行sudo apachectl restart,重启Apache后就开启了虚拟主机配置功能。
  • 运行sudo vi /etc/apache2/extra/httpd-vhosts.conf,打开配置虚拟主机文件httpd-vhost.conf,配置虚拟主机。需要注意的是该文件默认开启了两个作为例子的虚拟主机:
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/usr/docs/dummy-host.example.com"
    ServerName 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
</VirtualHost>
 
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    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
</VirtualHost>

而实际上,这两个虚拟主机是不存在的,在没有配置任何其他虚拟主机时,可能会导致访问localhost时出现如下提示:
Forbidden
You don't have permission to access /XXX on this server

  • 增加如下配置
<VirtualHost *:80>
# This is not needed but seriously recommended
    ServerAdmin perry@zhuo.com
# Path to your folder
    DocumentRoot "/Users/aimoke/Documents/MyPHPWorkspace/dev"
# This line is your host name (example.dev if you prefere)
    ServerName www.zhuo.com
# You can add another server name using ServerAlias
    ServerAlias zhuo.dev
# For the error file, exactly like you did
    ErrorLog "/private/var/log/apache2/sites-error_log"
    CustomLog "/private/var/log/apache2/sites-access_log" common
# A little bonus that remove the server signature from http requests
    ServerSignature Off
# Here are options neeeded to authorizations and I added some classical options
 <Directory "/Users/aimoke/Documents/MyPHPWorkspace/dev">
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride all
    Require all granted
 </Directory>
</VirtualHost>

保存并退出,并重启Apache。这里的ServerAdminDocumentRoot,ServerName可以根据自己的实际情况进行修改。但是ServerName要和下一步骤hosts中对应起来。

  • 运行sudo vi /etc/hosts,打开hosts配置文件,加入127.0.0.1 www.zhuo.com,这样就可以配置完成www.zhuo.com虚拟主机了。
  • 打开Safari浏览器地址栏输入www.zhuo.com也许会出现403的错误。Permissions are missing on a component of the path。这时有两种可能:
  1. 这是因为你前面的DocumentRoot权限问题。
    这时我们检查下错误日志,在终端输入open /private/var/log/apache2/sites-error_log
    错误信息如下所示:
[Thu Feb 23 16:17:23.087509 2017] [core:error] [pid 44853] (13)Permission denied: [client ::1:62480] AH00035: access to / denied (filesystem path '/Users/user/Documents/MyPHPWorkspace') because search permissions are missing on a component of the path

根据日志中的错误信息,那么一定是在这个路径上,某一个或者多个文件夹不允许_www用户(httpd的运行用户)search(针对文件夹的search对应的就是文件夹权限的x)。那么就从最后向最前找权限限制,很容易就发现/Users/user/Documents这个文件夹不允许其他人读取。由于是自己内网开发测试用的,就允许其他用户读取好了。
打开终端进入Documents层级中输入 :chmod o+rx Documents

  1. 前面的DocumentRoot中无 index.html 文件。在终端输入open /private/var/log/apache2/sites-error_log。错误信息如下:
[Thu Feb 23 16:37:23.175036 2017] [negotiation:error] [pid 629] [client 127.0.0.1:49576] AH00687: Negotiation: discovered file(s) matching request: /Users/user/Documents/MyPHPWorkspace/dev/index.html (None could be negotiated).

新建index.html其内容为:

<html><body><h1>Hello!</h1></body></html>

浏览器中打开www.zhuo.com输出为:Hello!到此配置完成。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容