標籤:des style blog http color os io strong
mac 10.9.x已經內建了apache,可按如下步驟開啟:
1、啟動
sudo apachectl start
啟動後,訪問 http://localhost/ 應該能看到"It works!"的初始頁面,如果對初始頁面的內容感到好奇,可以開啟"/etc/apache2/httpd.conf",197行可以看到如下程式碼片段:
1 <Directory "/Library/WebServer/Documents"> 2 # 3 # Possible values for the Options directive are "None", "All", 4 # or any combination of: 5 # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews 6 # 7 # Note that "MultiViews" must be named *explicitly* --- "Options All" 8 # doesn‘t give it to you. 9 #10 # The Options directive is both complicated and important. Please see11 # http://httpd.apache.org/docs/2.2/mod/core.html#options12 # for more information.13 #14 Options Indexes FollowSymLinks MultiViews15 16 #17 # AllowOverride controls what directives may be placed in .htaccess files.18 # It can be "All", "None", or any combination of the keywords:19 # Options FileInfo AuthConfig Limit20 #21 AllowOverride None22 23 #24 # Controls who can get stuff from this server.25 #26 Order allow,deny27 Allow from all28 29 </Directory>
View Code
It works的內容,就在/Library/WebServer/Documents/index.html.en這個檔案裡,這是apache的預設頁,相當於windows下IIS的C:\inetpub\wwwroot\iisstart.htm
2、重啟/停止
sudo apachectl restart
sudo apachectl stop
3、建立個人網站目錄
cd ~/
mkdir Sites
echo "hello" >> index.html
sudo apachectl restart
然後再訪問 http://localhost/~jimmy/ 應該就能看到"hello"的個人目錄初始頁面(註:~jimmy需換成~你的使用者名稱)
如果失敗,請檢查"/etc/apache2/users"目錄下,是否有名為“jimmy.conf”的設定檔(同樣:jimmy需換成你的使用者名稱),如果沒有,手動建立一個,內容參考下面:
1 <Directory "/Users/jimmy/Sites/">2 Options FollowSymLinks Indexes MultiViews3 AllowOverride All4 Order allow,deny5 Allow from all6 </Directory>
View Code
如果好奇目錄名為什麼是Sites? 可以查看"/etc/apache2/extra/httpd-userdir.conf"
1 # Settings for user home directories 2 # 3 # Required module: mod_userdir 4 5 # 6 # UserDir: The name of the directory that is appended onto a user‘s home 7 # directory if a ~user request is received. Note that you must also set 8 # the default access control for these directories, as in the example below. 9 #10 UserDir Sites11 12 #13 # Users might not be in /Users/*/Sites, so use user-specific config files.14 #15 Include /private/etc/apache2/users/*.conf16 <IfModule bonjour_module>17 RegisterUserSite customized-users18 </IfModule>
View Code
第10行就是答案
4、啟用虛擬機器主機
預設情況下,apache的虛擬機器主機功能是關閉的,在“/etc/apache2/httpd.conf”中找到下面這行:
#Include /private/etc/apache2/extra/httpd-vhosts.conf
將前面的#去掉,然後再開啟“/etc/apache2/extra/httpd-vhosts.conf”,內容修改成類似下面的樣子:
1 NameVirtualHost *:80 2 3 <VirtualHost *:80> 4 DocumentRoot "/Users/jimmy/Sites" 5 ServerName www.yjmyzz.com 6 ErrorLog "/Users/jimmy/Sites/log/error.log" 7 CustomLog "/Users/jimmy/Sites/log/access.log" common 8 <Directory /> 9 Options Indexes FollowSymLinks MultiViews10 AllowOverride None11 Order deny,allow12 Allow from all13 </Directory>14 </VirtualHost>
View Code
註:
a) /Users/jimmy/Sites/log/ 記錄檔目錄,必須存在,否則apache啟動將失敗,而且不會有任何錯誤提示
b) 虛擬機器主機的網站根目錄,建議放在~/Sites/下,否則mac環境中會報類似“無許可權訪問”的錯誤。
這段配置綁定了一個不存在的網域名稱www.yjmyzz.com到網站http://localhost/~jimmy/,為了驗證網域名稱綁定的效果,手動修改下hosts檔案
sudo vi /etc/hosts 以管理員身份開啟hosts檔案,追加一行
127.0.0.1 www.yjmyzz.com
儲存退出,重啟apache,再次瀏覽 http://www.yjmyzz.com 應該ok了,看到的內容跟http://localhost/~jimmy/ 一樣
5、URL轉寄
先開啟httpd.conf,確保下面這二行沒有被注釋掉:
1 LoadModule proxy_module libexec/apache2/mod_proxy.so2 LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
然後在httpd.conf最後加上
1 ProxyPass /HelloApp http://localhost:8080/HelloApp/2 ProxyPassReverse /HelloApp http://localhost:8080/HelloApp/
這樣訪問 http://localhost/HelloApp、http://ip/HelloApp、http://www.yjmyzz.com/HellpApp 都相當於訪問 http://localhost:8080/HelloApp