標籤:apache mac os x
文章松陽
本文出自 阿修羅道,禁止用於商業用途,轉載請註明出處。
原文連結:http://blog.csdn.net/fansongy/article/details/43449337
基本知識
下面是Apache的一些基本設定 參考這篇文章
開啟“終端(terminal)”,輸入
sudo apachectl -v
可顯示Apache的版本
接著輸入 sudo apachectl start 可以啟動Apache。開啟Safari瀏覽器地址欄輸入 “http://localhost”,可以看到內容為“It works!”的頁面。其位於“/Library(資產庫)/WebServer/Documents/”下,這就是Apache的預設根目錄。
Apache的安裝目錄在:/etc/apache2/,etc預設是隱藏的。有三種方式查看:
- dock下右鍵Finder,選擇"前往檔案夾",輸入"/etc"
- 在finder下----》前往---》前往檔案夾,然後輸入/etc
- 可以在terminal 輸入 "open /etc"
設定虛擬機器主機
- 在終端運行“sudo vi /etc/apache2/httpd.conf”,開啟Apche的設定檔
- 在httpd.conf中找到“#Include /private/etc/apache2/extra/httpd-vhosts.conf”,去掉前面的“#”,儲存並退出。
- 運行“sudo apachectl restart”,重啟Apache後就開啟了虛擬機器主機配置功能。
- 運行“sudo vi /etc/apache2/extra/httpd-vhosts.conf”,就開啟了配置虛擬機器主機檔案httpd-vhost.conf,配置虛擬機器主機了。需要注意的是該檔案預設開啟了兩個作為例子的虛擬機器主機:
<VirtualHost *:80> ServerAdmin [email protected] 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 [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</VirtualHost>
而實際上,這兩個虛擬機器主機是不存在的,在沒有配置任何其他虛擬機器主機時,可能會導致訪問localhost時出現如下提示.
ForbiddenYou don‘t have permission to access /index.php on this server
最簡單的辦法就是在它們每行前面加上#,注釋掉就好了,這樣既能參考又不導致其他問題。
增加如下配置
<VirtualHost *:80> DocumentRoot "/Library/WebServer/Documents" ServerName localhost ErrorLog "/private/var/log/apache2/localhost-error_log" CustomLog "/private/var/log/apache2/localhost-access_log" common</VirtualHost> ? <VirtualHost *:80> DocumentRoot "/Users/fansy/Sites" ServerName mysites ErrorLog "/private/var/log/apache2/sites-error_log" CustomLog "/private/var/log/apache2/sites-access_log" common <Directory /> Options Indexes FollowSymLinks MultiViews AllowOverride None Order deny,allow Allow from all </Directory></VirtualHost>
儲存退出,並重啟Apache。
運行sudo vi /etc/hosts,開啟hosts設定檔,加入127.0.0.1 mysites,這樣就可以配置完成sites虛擬機器主機了,可以訪問http://mysites了,在10.8之前Mac OS X版本其內容和“http://localhost/~[使用者名稱]”完全一致。
注意,記錄log的“ErrorLog "/private/var/log/apache2/sites-error_log"”也可以刪掉,但記錄日誌其實是一個好習慣,在出現問題時可以協助我們判斷。如果保留這些log代碼,一定log檔案路徑都是存在的,如果隨便修改一個不存在的,會導致Apache無法服務而沒有錯誤提示,這個比較噁心。
Forbidden
經過了上面的設定後 依舊報錯 :
ForbiddenYou don‘t have permission to access / on this server.
查詢原因是 Yosemite 權限原則更改了。需要更改一些配置,我參考了這篇文章
核心就是開啟libphp5.so和mod_userdir.so這兩個庫的載入,啟用httpd-userdir.conf配置。當然我們剛才配置好的虛擬機器主機也不耽誤,能夠一起使用。
Mac Yosemite 配置Apache