centos下tomcat與apache整合

1. 首先需要安裝apache,安裝步驟參見其官網,然後安裝apache jk module

1.1. wget  http://mirror.bjtu.edu.cn/apache//tomcat/tomcat-connectors/jk/source/jk-1.2.31/tomcat-connectors-1.2.31-src.tar.gz
1.2. tar -xzvf tomcat-connectors-1.2.31-src.tar.gz
1.3. cd tomcat-connectors-1.2.31-src/native
1.4. ./configure --with-apxs=/usr/local/apache2/bin/apxs #/usr/local/apache2/是你apache的安裝目錄
1.5. make
1.6. cp apache-2.0/mod_jk.so /usr/local/apache2/modules/mod_jk.so, 然後重新啓動apache
1.7. 進入apache安裝目錄,cd /usr/local/apache2/

2. 配置mod_jk.conf
2.1. cd conf, vi mod_jk.conf
2.2. 編輯如下內容:

# global config
# just configure log and load module
# load module
LoadModule jk_module modules/mod_jk.so   #表示導入jk模塊

JkLogFile logs/mod_jk.log                # logs保存位置
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " #log時間戳格式
JkRequestLogFormat     "%w %V %U %T"          #log記錄的內容
JkLogLevel warn
JkWorkersFile conf/workers.properties      #加載worker配置文件

#all vhost should inherit this config
JkMountCopy All                      #所有的虛擬主機都複製該配置文件

2.3 JkRequestLogFormat參數說明:

%b    Bytes sent, excluding HTTP headers (CLF format)
%B    Bytes sent, excluding HTTP headers
%H    The request protocol
%m    The request method
%p    The canonical Port of the server serving the request
%q    The query string (prepended with a ? if a query string exists, otherwise an empty string)
%r    First line of request
%s    Request HTTP status code
%T    Request duration, elapsed time to handle request in seconds '.' micro seconds
%U    The URL path requested, not including any query string.
%v    The canonical ServerName of the server serving the request
%V    The server name according to the UseCanonicalName setting
%w    Tomcat worker name
%R    Session route name (available with 1.2.19 and up)

3. 配置workers.properties

vi workers.properties

# 所有可以訪問的工作機器,status用來監控jk的狀態,一個工作機器可以理解爲一個tomcat實例
worker.list=balancer,master,status 

#type表示工作機器的類型,一般是type=ajp13 和 lb,lb表示負載平衡
worker.gmaster.port=8009       
worker.master.host=10.3.1.22  
worker.master.type=ajp13
worker.master.lbfactor = 1    #負載平衡因子,如果想要本機承載的負載大些,可以設置值大些
    
#define web app slave host
worker.slave.port=9009   
worker.slave.host=localhost
worker.slave.type=ajp13
worker.slave.lbfactor = 1

#define web app balancer
worker.balancer.type=lb   #表示負載平衡
worker.balancer.balance_workers=master,lave  # 負載平衡工作機器,此列表的workers可以不放在worker.list
worker.balancer.sticky_session=True                         # 有session的請求要指到原來tomcat上


#define status
worker.status.type=status            #指明監控工作機器,可以不設置

4. 配置uriworkermap.properties,它用來匹配哪些url被轉發給tomcat

4.1. vi uriworkermap.properties

#configure status pattern
/jkstatus=status

#configure balancer patterns
#just forword *.jsp,*.do and page serverlet
/appname/*.jsp=balancer
/appname/*.do=balance
/appname/page=balance
/appname/*/page=balance
/appname/=balance
/appname=balance

# configure admin functionality,後臺管理程序可以只匹配到一個工作機器,因爲後臺管理用戶較少
/appname/admin/*.jsp=master
/appname/admin/*.do=master
/appname/admin/*.html=master
/appname/admin/page=master
/appname/admin/*/page=master
/appname/admin/=master

4.2. 匹配優先級說明:
The most restrictive URI pattern is applied first. More precisely the URI patterns are sorted by
the number of '/' characters in the pattern (highest number first), and rules with equal numbers
are sorted by their string length (longest first).

If both distinctions still do not suffice, then the defining source of the rule is considered.
 Rules defined in uriworkermap.properties come first, before rules defined by JkMount (Apache)
and inside workers.properties using the mount attribute.

All disabled rules are ignored. Exclusion rules are applied after all normal rules have been applied.

There is no defined behaviour, for the following configuration conflict: using literally the same
URI pattern in the same defining source but with different worker targets.


5.配置httpd.conf

vi httpd.conf

在最後面加載 Include conf/mod_jk.conf

6. 配置httpd-vhosts.conf

vi extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerName yourweb.com
    ServerAdmin your [email protected]
    DocumentRoot "/usr/local/apache2/your doc root"
        #放置Alias塊
    RewriteEngine on
    RewriteRule ^/logging.php* yourapp/admin/ [PT]

    JkMountFile conf/uriworkermap.properties #加載配置規則,僅對本虛擬主機生效
</VirtualHost>

PT 表示 passthrough,直接發個apache內部
R 表示 redirect
RewriteRule 處理快於jk匹配,Rewrite規則可以參見apache官網,它像一個鉤子,可以修改很多行爲

7. 使用apache Alias 實現apache 靜態文件轉發功能,即靜態文件請求tomcat,直接通過apache提供服務
把這一段放在 <VirtualHost *:80>  </VirtualHost>之間,一般放在DocumentRoot的下一行即可

Alias /yourapp/js /var/yourapp_static/js/
<Directory "/var/yourapp_static/js">
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

你也可以放置多個這樣的別名塊

8. 配置tomcat
8.1. 配置jvm內存,vi bin/catalina.sh, 在最前面添加: JAVA_OPTS='-Xms1000m -Xmx1000m'
8.2. 配置ajp connector,在server.xml中配置
 <Connector protocol="AJP/1.3" port="0"
        channelNioSocket.port="8009"
        channelNioSocket.redirectPort="8443"
        channelNioSocket.maxThreads="256"
        channelNioSocket.maxSpareThreads="97"
        channelNioSocket.minSpareThreads="50"
        channelNioSocket.URIEncoding="UTF-8"
        URIEncoding="UTF-8"
        channelNioSocket.connectionTimeout="20000"
        channelNioSocket.keepAliveTimeout="1000"
        channelNioSocket.bufferSize="16384"/>

    使用了nio
8.3. 生產環境中你可以關掉http connector

9. 配置操作系統文件描述符數量

vi /etc/profile

加入 ulimit -SHn 51200
後保存
 
source /etc/profile


#一般系統的文件描述符是1024,你可以使用ulimit -a查看,當有大量tcp連接時,文件描述符可能不夠用
參數說明:


-H      設置硬資源限制,一旦設置不能增加。     
-S     設置軟資源限制,設置後可以增加,但是不能超過硬資源設置。
-a     顯示當前所有的 limit 信息。
-c     最大的 core 文件的大小, 以 blocks 爲單位。
-d     進程最大的數據段的大小,以 Kbytes 爲單位。
-f     進程可以創建文件的最大值,以 blocks 爲單位。
-l     最大可加鎖內存大小,以 Kbytes 爲單位。
-m     最大內存大小,以 Kbytes 爲單位。
-n     可以打開最大文件描述符的數量。
-p     管道緩衝區的大小,以 Kbytes 爲單位。
-s     線程棧大小,以 Kbytes 爲單位。
-t     最大的 CPU 佔用時間,以秒爲單位。
-u     用戶最大可用的進程數。
-v     進程最大可用的虛擬內存,以 Kbytes 爲單位。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章