mac 10.9.4下配置apache


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 see
11     # http://httpd.apache.org/docs/2.2/mod/core.html#options
12     # for more information.
13     #
14     Options Indexes FollowSymLinks MultiViews
15 
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 Limit
20     #
21     AllowOverride None
22 
23     #
24     # Controls who can get stuff from this server.
25     #
26     Order allow,deny
27     Allow from all
28 
29 </Directory>
View Code

It works的內容,就在/Library/WebServer/Documents/index.html.en這個文件里,這是apache的默認頁,相當於windows下IIS的C:\inetpub\wwwroot\iisstart.htm

 

另外,如果要監聽多端口,可以修改http.conf,類似下面這樣:

Listen 80
Listen 81

這樣就同時監聽了80,81二個端口

 

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 MultiViews
3     AllowOverride All
4     Order allow,deny
5     Allow from all
6 </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 Sites
11 
12 #
13 # Users might not be in /Users/*/Sites, so use user-specific config files.
14 #
15 Include /private/etc/apache2/users/*.conf
16 <IfModule bonjour_module>
17        RegisterUserSite customized-users
18 </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 MultiViews
10                 AllowOverride None
11                 Order deny,allow
12                 Allow from all
13       </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/ 一樣

tips: 如果站點根目錄,想放到其它位置,在httpd.conf中找到 

1 <Directory />
2     Options FollowSymLinks
3     AllowOverride None
4     Order deny,allow
5     Deny from all
6 </Directory

把4,5行刪除掉就行了

 

5、URL轉發

先打開httpd.conf,確保下面這二行沒有被注釋掉:

1 LoadModule proxy_module libexec/apache2/mod_proxy.so
2 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

 

6、端口轉發

假如服務器上有一個應用 http://x.x.x.x:8080/ ,如果想通過類似 http://www.yjmyzz.com 的域名來直接訪問,就需要做端口轉發,仍然打開httpd.conf

1 LoadModule proxy_connect_module modules/mod_proxy_connect.so
2 LoadModule proxy_ftp_module modules/mod_proxy_ftp.so

在"5、URL轉發"的基礎上,再打開這二項

然后修改extra/httpd-vhosts.conf

 1 NameVirtualHost *:80
 2 
 3 <VirtualHost *:80>
 4         ProxyPreserveHost On
 5         ServerName www.yjmyzz.com
 6 
 7         ProxyPass / http://www.yjmyzz.com:8000/
 8         ProxyPassReverse / http://www.yjmyzz.com:8000/        
 9     
10         ServerAdmin webmaster@localhost
11 </VirtualHost>
View Code

這樣就相當於把 80端口轉發到8080端口上了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM