Tomcat 及Apache 配置
阿新 • • 發佈:2018-05-07
Tomcat 及Apache 配置TOMCAT_HOME /conf/server.xml中做如下配置:
<Host name="www.xxx.xxx" appBase="D:\web">
<Context path="/otdr" docBase="D:\web\apps" />
<Context path="/doc" docBase="D:\ecs\LAMP" />
//Context 虛擬目錄
</Host>
Apache的配置
Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改。
主站點的配置(基本配置)
基本配置:
ServerRoot "/mnt/software/apache2" #你的apache軟件安裝的位置。其它指定的目錄如果沒有指定絕對路徑,則目錄是相對於該目錄。
PidFile logs/httpd.pid #第一個httpd進程(所有其他進程的父進程)的進程號文件位置。
Listen 80 #服務器監聽的端口號。
ServerName www.clusting.com:80 #主站點名稱(網站的主機名)。
ServerAdmin [email protected] #管理員的郵件地址。
DocumentRoot "/mnt/web/clusting" #主站點的網頁存儲位置。
以下是對主站點的目錄進行訪問控制:
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
AllowOverride:允許存在於.htaccess文件中的指令類型(.htaccess文件名是可以改變的,其文件名由AccessFileName指令決定):
None: 當AllowOverride被設置為None時。不搜索該目錄下的.htaccess文件(可以減小服務器開銷)。
All: 在.htaccess文件中可以使用所有的指令。
Order:控制在訪問時Allow和Deny兩個訪問規則哪個優先:
Allow:允許訪問的主機列表(可用域名或子網,例如:Allow from 192.168.0.0/16)。
Deny:拒絕訪問的主機列表。
DirectoryIndex index.html index.htm index.php #主頁文件的設置(本例將主頁文件設置為:index.html,index.htm和index.php)
StartServers 5 #啟動apache時啟動的httpd進程個數。
MinSpareServers 5 #服務器保持的最小空閑進程數。
MaxSpareServers 10 #服務器保持的最大空閑進程數。
MaxClients 150 #最大並發連接數。
MaxRequestsPerChild 1000 #每個子進程被請求服務多少次後被kill掉。0表示不限制,推薦設置為1000。
在該工作模式下,服務器啟動後起動5個httpd進程(加父進程共6個,通過ps -ax|grep httpd命令可以看到)。當有用戶連接時,apache會使用一個空閑進程為該連接服務,同時父進程會fork一個子進程。直到內存中的空閑進程達到MaxSpareServers。該模式是為了兼容一些舊版本的程序。我缺省編譯時的選項。
worker:如果httpd -l列出worker.c,則需要對下面的段進行配置:
StartServers 2 #啟動apache時啟動的httpd進程個數。
MaxClients 150 #最大並發連接數。
MinSpareThreads 25 #服務器保持的最小空閑線程數。
MaxSpareThreads 75 #服務器保持的最大空閑線程數。
ThreadsPerChild 25 #每個子進程的產生的線程數。
MaxRequestsPerChild 0 #每個子進程被請求服務多少次後被kill掉。0表示不限制,推薦設置為1000。
該模式是由線程來監聽客戶的連接。當有新客戶連接時,由其中的一個空閑線程接受連接。服務器在啟動時啟動兩個進程,每個進程產生的線程數是固定的(ThreadsPerChild決定),因此啟動時有50個線程。當50個線程不夠用時,服務器自動fork一個進程,再產生25個線程。
perchild:如果httpd -l列出perchild.c,則需要對下面的段進行配置:
NumServers 5 #服務器啟動時啟動的子進程數
StartThreads 5 #每個子進程啟動時啟動的線程數
MinSpareThreads 5 #內存中的最小空閑線程數
MaxSpareThreads 10 #最大空閑線程數
MaxThreadsPerChild 2000 #每個線程最多被請求多少次後退出。0不受限制。
MaxRequestsPerChild 10000 #每個子進程服務多少次後被重新fork。0表示不受限制。
該模式下,子進程的數量是固定的,線程數不受限制。當客戶端連接到服務器時,又空閑的線程提供服務。 如果空閑線程數不夠,子進程自動產生線程來為新的連接服務。該模式用於多站點服務器。
持久性連接設置
KeepAlive On #開啟持久性連接功能。即當客戶端連接到服務器,下載完數據後仍然保持連接狀態。
MaxKeepAliveRequests 100 #一個連接服務的最多請求次數。
KeepAliveTimeout 30 #持續連接多長時間,該連接沒有再請求數據,則斷開該連接。缺省為15秒。
別名設置
對於不在DocumentRoot指定的目錄內的頁面,既可以使用符號連接,也可以使用別名。別名的設置如下:
Alias /download/ "/var/www/download/" #訪問時可以輸入:http://www.custing.com/download/
#對該目錄進行訪問控制設置
Options Indexes MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
CGI設置
ScriptAlias /cgi-bin/ "/mnt/software/apache2/cgi-bin/" # 訪問時可以:http://www.clusting.com/cgi-bin/ 。但是該目錄下的CGI腳本文件要加可執行權限!
#設置目錄屬性
AllowOverride None
Options None
Order allow,deny
Allow from all
個人主頁的設置 (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
用戶認證的配置
(1)in the httpd.conf:
AccessFileName .htaccess
Alias /download/ "/var/www/download/"
Options Indexes
AllowOverride AuthConfig
虛擬主機的配置
(1)基於IP地址的虛擬主機配置
Listen 80
DocumentRoot /www/example1
ServerName www.example1.com
DocumentRoot /www/example2
ServerName www.example2.org
(2) 基於IP和多端口的虛擬主機配置
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
DocumentRoot /www/example1-80
ServerName www.example1.com
DocumentRoot /www/example1-8080
ServerName www.example1.com
DocumentRoot /www/example2-80
ServerName www.example1.org
DocumentRoot /www/example2-8080
ServerName www.example2.org
(3)單個IP地址的服務器上基於域名的虛擬主機配置:
Listen 80
NameVirtualHost *:80
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
(4)在多個IP地址的服務器上配置基於域名的虛擬主機:
Listen 80
ServerName server.domain.com
DocumentRoot /www/mainserver
NameVirtualHost 172.20.30.50
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here ...
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here ...
(5)在不同的端口上運行不同的站點(基於多端口的服務器上配置基於域名的虛擬主機):
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
ServerName www.example1.com
DocumentRoot /www/domain-80
ServerName www.example1.com
DocumentRoot /www/domain-8080
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
(6)基於域名和基於IP的混合虛擬主機的配置:
Listen 80
NameVirtualHost 172.20.30.40
DocumentRoot /www/example1
ServerName www.example1.com
DocumentRoot /www/example2
ServerName www.example2.org
DocumentRoot /www/example3
ServerName www.example3.net
SSL加密的配置
(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
(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/
<Host name="www.xxx.xxx" appBase="D:\web">
<Context path="/otdr" docBase="D:\web\apps" />
<Context path="/doc" docBase="D:\ecs\LAMP" />
//Context 虛擬目錄
</Host>
假如想在tomcat下發布多個網站,則可做如下配置:
Tomcat 及Apache 配置