Apache 配置方法(虛擬目錄、域名、虛擬主機等)

基本配置

Define AROOT "C:/Apache24"        #宏定義一個根目錄常量,最後不能帶"/"或"\",由於後面配置的目錄開頭已經帶有"/"或"\"
ServerRoot "${AROOT}"               #你的Apache軟件安裝的位置。其它指定的目錄若是沒有指定絕對路徑,則目錄是相對於該目錄。
PidFile logs/httpd.pid                  #第一個httpd的進程號文件(全部其餘進程的父進程)位置。
Listen 80                          #服務器監聽的端口號。
ServerName www.wcwnina.com:80        #網站域名(須要跟DNS指向的域名一致。在Windows的hosts文件中修改)。
ServerAdmin admin@example.com         #管理員的郵件地址。php

將"LoadModule access_compat_module modules/mod_access_compat.so"取消註釋!html

如下是對主站點的目錄進行訪問控制:

DocumentRoot "${AROOT}/web"       #主站點根目錄
<Directory "${AROOT}/web">       #根目錄訪問權限
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None
    #
    # Controls who can get stuff from this server.
    #
    # Require all granted     # Apache2.4及以上版本寫法,以替換下面兩句
    Order allow,deny      #(注意:逗號先後不能有空格!!)
    Allow from all           
</Directory>web

在上面這段目錄屬性配置中,主要有下面的選項:

Options:配置在特定目錄使用哪些特性,經常使用的值和基本含義以下:apache

    Indexes: 當用戶訪問該目錄時,若是用戶找不到DirectoryIndex指定的主頁文件(例如index.html),則返回該目錄下的文件列表給用戶。
    FollowSymLinks: 在該目錄下容許文件系統使用符號鏈接。瀏覽器

    ExecCGI: 在該目錄下容許執行CGI腳本。    
    SymLinksIfOwnerMatch: 當使用符號鏈接時,只有當符號鏈接的文件擁有者與實際文件的擁有者相同時才能夠訪問。
    (其它可用值和含義請參閱:http://www.clusting.com/Apache/ApacheManual/mod/core.html#options )服務器

AllowOverride:容許存在於.htaccess文件中的指令類型(.htaccess文件名是能夠改變的,其文件名由AccessFileName指令決定):
    None: 當AllowOverride被設置爲None時。不搜索該目錄下的.htaccess文件(能夠減少服務器開銷)。
    All: 在.htaccess文件中能夠使用全部的指令。
    (其餘的可用值及含義(如:Options FileInfo AuthConfig Limit等),請參看: http://www.clusting.com/Apache/ApacheManual/mod/core.html#AllowOverride )併發

Order:控制在訪問時Allow和Deny兩個訪問規則哪一個優先:
    Allow:容許訪問的主機列表(可用域名或子網,例如:Allow from 192.168.0.0/16)。
    Deny:拒絕訪問的主機列表。
    (更詳細的用法可參看:http://www.clusting.com/Apache/ApacheManual/mod/mod_access.html#order)app

<IfModule dir_module>
    DirectoryIndex index.html index.htm index.php  #主頁文件的設置
</IfModule>ide

工具

DirectoryIndex index.html index.htm index.php

虛擬目錄配置

1)建立你的工程目錄文件夾,如「C:\Apache24\web」。

2)配置虛擬目錄

  在httpd.conf最後面地方添加以下代碼:
Alias "/myweb"  "${AROOT}/web"       # 「${AROOT}/web」,或「C:/Apache/web」,真實目錄
<Directory "${AROOT}/web">          # 目錄權限配置使用<Directory>
    Options Indexes FollowSymLinks
    AllowOverride None
    Order Allow,Deny    #(注意:逗號先後不能有空格!!)
    Allow From All
</Directory>

3)重啓Apache(httpd -n apache -k restart)測試,輸入Http://localhost:8080/myweb/index.html打開頁面。

虛擬主機配置

1)打開httpd.conf,找到以下代碼:
# Virtual hosts
# Include conf/extra/httpd-vhosts.conf
把Include前面的#去掉。

2)配置httpd-vhosts.conf

打開Apache安裝目錄中的conf\extra文件夾中的「httpd-vhosts.conf」,找到以下代碼根據實際修改。

<VirtualHost _default_:8080>
  DocumentRoot "${AROOT}/web"
  #ServerName www.example.com:80
</VirtualHost>

再在最後添加以下代碼:

<VirtualHost *:8080>
    ServerAdmin webmaster@example.com            #管理員郵件設置    
    DocumentRoot "${AROOT}/web"                 #DocumentRoot 真實站點根目錄    
    DirectoryIndex index.html index.htm index.php          #這裏配置歡迎首頁面    
    ServerName www.wcwnina.com                 #ServerName是網站域名(虛擬主機名),須要跟DNS指向的域名一致
    ErrorLog "logs/wcwnina-error.log"
    CustomLog "logs/wcwnina-access.log" common
</VirtualHost>

  由於上面的http-vhosts.conf中的"VirtualHost *:8080"設計爲全部主機都能訪問。把*號改成一個ip地址,其餘ip地址就不能訪問;內網不能修改內網其餘人apache上的地址映射,不容許訪問他人修改的地址映射。

3)添加IP與虛擬主機的映射

打開C:\Windows\System32\drivers\etc\hosts,添加以下代碼:

127.0.0.1  www.wcwnina.com

4)重啓Apachehttpd -n apache -k restart測試,輸入www.wcwnina.com會打開首頁。

服務器的優化 (MPM: Multi-Processing Modules)

  apache2主要的優點就是對多處理器的支持更好,在編譯時同過使用--with-mpm選項來決定apache2的工做模式。若是要知道當前的apache2使用什麼工做機制,能夠經過「httpd -l」命令列出apache的全部模塊,就能夠知道其工做方式:
prefork:若是「httpd -l」列出prefork.c,則須要對下面的段進行配置:
<IfModule prefork.c>
  StartServers 5            #啓動apache時啓動的httpd進程個數。
  MinSpareServers 5         #服務器保持的最小空閒進程數。
  MaxSpareServers 10         #服務器保持的最大空閒進程數。
  MaxClients 150          #最大併發鏈接數。
  MaxRequestsPerChild 1000     #每一個子進程被請求服務多少次後被kill掉。0表示不限制,推薦設置爲1000。
</IfModule>
  在該工做模式下,服務器啓動後起動5個httpd進程(加父進程共6個,經過ps -ax|grep httpd命令能夠看到)。當有用戶鏈接時,apache會使用一個空閒進程爲該鏈接服務,同時父進程會fork一個子進程。直到內存中的空閒進程達到MaxSpareServers。該模式是爲了兼容一些舊版本的程序。我缺省編譯時的選項。

  worker:若是「httpd -l」列出worker.c,則須要對下面的段進行配置:
<IfModule worker.c>
  StartServers 2            #啓動apache時啓動的httpd進程個數。
  MaxClients 150          #最大併發鏈接數。
  MinSpareThreads 25      #服務器保持的最小空閒線程數。
  MaxSpareThreads 75     #服務器保持的最大空閒線程數。
  ThreadsPerChild 25       #每一個子進程的產生的線程數。
  MaxRequestsPerChild 0      #每一個子進程被請求服務多少次後被kill掉。0表示不限制,推薦設置爲1000。
</IfModule>
  該模式是由線程來監聽客戶的鏈接。當有新客戶鏈接時,由其中的一個空閒線程接受鏈接。服務器在啓動時啓動兩個進程,每一個進程產生的線程數是固定的(ThreadsPerChild決定),所以啓動時有50個線程。當50個線程不夠用時,服務器自動fork一個進程,再產生25個線程。

  perchild:若是「httpd -l」列出perchild.c,則須要對下面的段進行配置:
<IfModule perchild.c>
  NumServers 5          #服務器啓動時啓動的子進程數
  StartThreads 5           #每一個子進程啓動時啓動的線程數
  MinSpareThreads 5        #內存中的最小空閒線程數
  MaxSpareThreads 10       #最大空閒線程數
  MaxThreadsPerChild 2000    #每一個線程最多被請求多少次後退出。0不受限制。
  MaxRequestsPerChild 10000   #每一個子進程服務多少次後被從新fork。0表示不受限制。
</IfModule>
  該模式下,子進程的數量是固定的,線程數不受限制。當客戶端鏈接到服務器時,又空閒的線程提供服務。 若是空閒線程數不夠,子進程自動產生線程來爲新的鏈接服務。該模式用於多站點服務器。

HTTP返回頭信息配置

ServerTokens Prod   #該參數設置http頭部返回的apache版本信息,可用的值和含義以下:
  Prod:僅軟件名稱,例如:apache
  Major:包括主版本號,例如:apache/2
  Minor:包括次版本號,例如:apache/2.0
  Min:僅apache的完整版本號,例如:apache/ 2.0.54
  OS:包括操做系統類型,例如:apache/2.0.54(Unix)
  Full:包括apache支持的模塊及模塊版本號,例如:Apache/2.0.54 (Unix) mod_ssl/2.0.54 OpenSSL/0.9.7g
ServerSignature Off   #在頁面產生錯誤時是否出現服務器版本信息。推薦設置爲Off

持久性鏈接設置

KeepAlive On          #開啓持久性鏈接功能。即當客戶端鏈接到服務器,下載完數據後仍然保持鏈接狀態。
MaxKeepAliveRequests 100   #一個鏈接服務的最多請求次數。
KeepAliveTimeout 30      #持續鏈接多長時間,該鏈接沒有再請求數據,則斷開該鏈接。缺省爲15秒。

CGI設置

ScriptAlias /cgi-bin/ "/mnt/software/apache2/cgi-bin/"   # 訪問時能夠:http://www.clusting.com/cgi-bin/ 。可是該目錄下的CGI腳本文件要加可執行權限!

<Directory "/usr/local/apache2/cgi-bin">   #設置目錄屬性
  AllowOverride None
  Options None
  Order allow,deny
  Allow from all
</Directory>

我的主頁的設置 (public_html)

UserDir public_html     (間用戶的主頁存儲在用戶主目錄下的public_html目錄下 URL http://www.clusting.com/~bearzhang/file.html 將讀取 /home/bearzhang/public_html/file.html 文件)
chmod 755 /home/bearzhang   #使其它用戶可以讀取該文件。
UserDir /var/html      (the URL http://www.clusting.com/~bearzhang/file.html 將讀取 /var/html/bearzhang/file.html)
UserDir /var/www/*/docs   (the URL http://www.clusting.com/~bearzhang/file.html 將讀取 /var/www/bearzhang/docs/file.html)

日誌的設置

(1)錯誤日誌的設置
ErrorLog logs/error_log   #日誌的保存位置
LogLevel warn       #日誌的級別
  顯示的格式日下:
[Mon Oct 10 15:54:29 2005] [error] [client 192.168.10.22] access to /download/ failed, reason: user admin not allowed access

(2)訪問日誌設置
  日誌的缺省格式有以下幾種:
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common   #common爲日誌格式名稱
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog logs/access_log common

格式中的各個參數以下:
%h  --客戶端的ip地址或主機名
%l   --The 這是由客戶端 identd 判斷的RFC 1413身份,輸出中的符號 "-" 表示此處信息無效。
%u  --由HTTP認證系統獲得的訪問該網頁的客戶名。有認證時纔有效,輸出中的符號 "-" 表示此處信息無效。
%t   --服務器完成對請求的處理時的時間。
"%r"   --引號中是客戶發出的包含了許多有用信息的請求內容。
%>s   --這個是服務器返回給客戶端的狀態碼。
%b  --最後這項是返回給客戶端的不包括響應頭的字節數。
"%{Referer}i"    --此項指明瞭該請求是從被哪一個網頁提交過來的。
"%{User-Agent}i" --此項是客戶瀏覽器提供的瀏覽器識別信息。
下面是一段訪問日誌的實例:
192.168.10.22 - bearzhang [10/Oct/2005:16:53:06 +0800] "GET /download/ HTTP/1.1" 200 1228
192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/blank.gif HTTP/1.1" 304 -
192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/back.gif HTTP/1.1" 304 -

  各參數的詳細解釋,請參閱:http://www.clusting.com/Apache/ApacheManual/logs.html

用戶認證的配置(Linux)

(1)in the httpd.conf:
AccessFileName .htaccess
.........
Alias /download/ "/var/www/download/"
<Directory "/var/www/download">
  Options Indexes
  AllowOverride AuthConfig
</Directory>

(2) create a password file:
/usr/local/apache2/bin/htpasswd -c /var/httpuser/passwords bearzhang

(3)onfigure the server to request a password and tell the server which users are allowed access.
vi /var/www/download/.htaccess:
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /var/httpuser/passwords
Require user bearzhang
#Require valid-user #all valid user

SSL加密的配置(Linux)

  首先在配置以前先來了解一些基本概念:
  證書的概念:首先要有一個根證書,而後用根證書來簽發服務器證書和客戶證書,通常理解,服務器證書和客戶證書是平級關係。SSL必須安裝服務器證書來認證。 所以,在此環境中,至少必須有三個證書:根證書,服務器證書,客戶端證書。 在生成證書以前,通常會有一個私鑰,同時用私鑰生成證書請求,再利用證書服務器的根證來簽發證書。
  SSL所使用的證書能夠本身生成,也能夠經過一個商業性CA(如Verisign 或 Thawte)簽署證書。
  簽發證書的問題:若是使用的是商業證書,具體的簽署方法請查看相關銷售商的說明;若是是知己簽發的證書,能夠使用openssl自帶的CA.sh腳本工具。
  若是不爲單獨的客戶端簽發證書,客戶端證書能夠不用生成,客戶端與服務器端使用相同的證書。

(1) conf/ssl.conf 配置文件中的主要參數配置以下:
Listen 443
SSLPassPhraseDialog buildin
#SSLPassPhraseDialog exec:/path/to/program
SSLSessionCache dbm:/usr/local/apache2/logs/ssl_scache
SSLSessionCacheTimeout 300
SSLMutex file:/usr/local/apache2/logs/ssl_mutex

<VirtualHost _default_:443>
  # General setup for the virtual host
  DocumentRoot "/usr/local/apache2/htdocs"
  ServerName www.example.com:443
  ServerAdmin you@example.com
  ErrorLog /usr/local/apache2/logs/error_log
  TransferLog /usr/local/apache2/logs/access_log

  SSLEngine on
  SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL

  SSLCertificateFile /usr/local/apache2/conf/ssl.crt/server.crt
  SSLCertificateKeyFile /usr/local/apache2/conf/ssl.key/server.key
  CustomLog /usr/local/apache2/logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b"
</VirtualHost>

(2) 建立和使用自簽署的證書:
a.Create a RSA private key for your Apache server
/usr/local/openssl/bin/openssl genrsa -des3 -out /usr/local/apache2/conf/ssl.key/server.key 1024

b. Create a Certificate Signing Request (CSR)
/usr/local/openssl/bin/openssl req -new -key /usr/local/apache2/conf/ssl.key/server.key -out /usr/local/apache2/conf/ssl.key/server.csr

c. Create a self-signed CA Certificate (X509 structure) with the RSA key of the CA
/usr/local/openssl/bin/openssl req -x509 -days 365 -key /usr/local/apache2/conf/ssl.key/server.key -in /usr/local/apache2/conf/ssl.key/server.csr -out /usr/local/apache2/conf/ssl.crt/server.crt

/usr/local/openssl/bin/openssl genrsa 1024 -out server.key
/usr/local/openssl/bin/openssl req -new -key server.key -out server.csr
/usr/local/openssl/bin/openssl req -x509 -days 365 -key server.key -in server.csr -out server.crt

(3) 建立本身的CA(認證證書),並使用該CA來簽署服務器的證書。
mkdir /CA
cd /CA
cp openssl-0.9.7g/apps/CA.sh /CA
./CA.sh -newca
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.csr newreq.pem
./CA.sh -sign
cp newcert.pem /usr/local/apache2/conf/ssl.crt/server.crt
cp server.key /usr/local/apache2/conf/ssl.key/

   

  至此,轉載請註明出處。

相關文章
相關標籤/搜索