我使用的Mac OS X版本是10.8.2,Mac自帶了Apache環境。php
打開「終端(terminal)」,輸入 sudo apachectl -v,(可能須要輸入機器祕密)。以下顯示Apache的版本web
接着輸入 sudo apachectl start,這樣Apache就啓動了。打開Safari瀏覽器地址欄輸入 「http://localhost」,能夠看到內容爲「It works!」的頁面。其位於「/Library(資源庫)/WebServer/Documents/」下,這就是Apache的默認根目錄。apache
Apache的安裝目錄在:/etc/apache2/,etc默認是隱藏的。有三種方式查看:瀏覽器
sudo vi /etc/apache2/httpd.conf
」,打開Apche的配置文件#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 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 /index.php on this server
最簡單的辦法就是在它們每行前面加上#,註釋掉就行了,這樣既能參考又不致使其餘問題。bash
<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/snandy/work" 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>
sudo vi /etc/hosts
」,打開hosts配置文件,加入"127.0.0.1 mysites
",這樣就能夠配置完成sites虛擬主機了,能夠訪問「http://mysites」了,在10.8以前Mac OS X版本其內容和「http://localhost/~[用戶名]」徹底一致。ErrorLog "/private/var/log/apache2/sites-error_log"
」也能夠刪掉,但記錄日誌實際上是一個好習慣,在出現問題時能夠幫助咱們判斷。若是保留這些log代碼,必定log文件路徑都是存在的,若是隨便修改一個不存在的,會致使Apache沒法服務而沒有錯誤提示,這個比較噁心。 9.關於mac下自帶的apache服務器的權限問題
sudo chown -R :_www /Library/WebServer # 遞歸給目錄設置屬主爲`_www` sudo chmod -R g+r /Library/WebServer # 遞歸給目錄寫讀權限 sudo apachectl restart # 重啓apache