Mac自身帶了apache,可以直接用來搭建本地web容器,配置過程基本同于windows,但有幾個點需要注意一下(避免踩坑)
apache
sudo apachectl start[/restart/stop] #開啟等sudo apachectl -v #查看版本sudo /usr/sbin/httpd -k start #當配置文件出錯時,可通過這個方式查看具體出錯位置 |
使用前切記開啟一個選項
Mac下apache默認不開啟php,需要手動開啟
sudo vi /etc/apache2/httpd.conf |
LoadModule php5_module libexec/apache2/libphp5.so
Apache修改web目錄
默認目錄為:
/Library/WebServer/Documents |
而往往這個目錄用起來是不方便的,一是權限問題,畢竟我們不想每做一次修改,都要帶個sudo;二是Finder中打開不方便。所以需要修改web目錄。
sudo vi /etc/apache2/httpd.conf |
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 webmaster@dummy-host.example.com 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 webmaster@dummy-host2.example.com 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……部分,如上。
新聞熱點
疑難解答