Mac自身带了apache,可以直接用来搭建本地web容器,配置过程基本同于windows,但有几个点需要注意一下(避免踩坑)
Update: 2018-7-23
apache
sudo apachectl start[/restart/stop] #开启等
sudo apachectl -v #查看版本
sudo /usr/sbin/httpd -k start #当配置文件出错时,可通过这个方式查看具体出错位置
使用前切记开启一个选项
Mac下apache默认不开启php,需要手动开启
LoadModule php5_module libexec/apache2/libphp5.so
Apache修改web目录
默认目录为:
/Library/WebServer/Documents
而往往这个目录用起来是不方便的,一是权限问题,毕竟我们不想每做一次修改,都要带个sudo;二是Finder中打开不方便。所以需要修改web目录。
237 DocumentRoot "/Users/username/wwwroot"
238 <Directory "/Users/username/wwwroot">
配置vhost
先开启vhost扩展(去掉注释#即可)
sudo vi /etc/apache2/httpd.conf
Include /private/etc/apache2/extra/httpd-vhosts.conf
编辑vhost文件
sudo vi /etc/apache2/extra/httpd-vhost.conf
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/Users/username/wwwroot"
ServerName localhost
ErrorLog "/private/var/log/apache2/local-error_log"
CustomLog "/private/var/log/apache2/local-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/Volumes/FAT/bugs"
ServerName wooyun.sb
ErrorLog "/private/var/log/apache2/wooyun-error_log"
CustomLog "/private/var/log/apache2/wooyun-access_log" common
<Directory "/Volumes/FAT/bugs">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
只允许本机访问
为了安全期间,我打算将apache服务配置成只允许本机访问。网上方法不太适用,经过多次尝试,以下方法可行:
<Directory "/Users/username/sites">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
# add 访问控制
Order Deny,Allow
Deny from all #网上很多给出的方法不加这里,只有allow那里,实测并不适用,必须加上这一行~
Allow from 127.0.0.1
</Directory>
一些问题处理
1、403 Forbidden
当日志文件log配置出错事,会出现403,处理方法是/var/log/apache2/清空这里边的日志。
也有可能是没有开启PHP扩展。
2、Vhost配置时出现403 Forbidden
注意需要配置文件加入<Directory……部分,如上。
3、httpd.conf修改配置
<Directory />
AllowOverride none
Options All
allow from all
</Directory>
4、通过/var/logs/apache2/error.log查看403原因
比如:
[Mon Jul 23 09:42:13.394930 2018] [autoindex:error] [pid 2326] [client 127.0.0.1:55713] AH01276: Cannot serve directory /Users/xdl/wwwroot/: No matching DirectoryIndex (index.html,index.html,index.php) found, and server-generated directory index forbidden by Options directive
就知道是因为Options设置问题了,改成 Options All即可!
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论