CentOS下整合Apache與Tomcat的一種方法

在這裏我所用的配置以下:html

  • CentOS:7.4
  • Apache:2.4.6
  • Tomcat:8.5.30
  • JDK:openjdk 1.8
  • Tomcat connectors:1.2.43

整合前所須要作的準備工做包括:java

  • 安裝JDK並配置環境變量
  • 安裝Apache並測試經過
  • 安裝Tomcat並測試經過

上述的安裝配置過程再也不贅述,下面從安裝Tomcat connectors開始:apache

  1. 整合Apache與Tomcat有多種方法,這裏選擇比較方便的也是比較經常使用的作法:經過Tomcat connectors進行二者間的連接。下載Tomcat connectors,地址

     

  2. 安裝。一般把下載好的安裝包放在/usr/local目錄下並解壓,解壓完成後可刪除該安裝包,進入安裝包目錄下的native目錄下進行配置、編譯並安裝
    [root@271d5811ce45 local]# tar -zxvf tomcat-connectors-1.2.43-src.tar.gz  
    [root@271d5811ce45 local]# cd tomcat-connectors-1.2.43-src/native/
    [root@271d5811ce45 native]# ./configure --with-apxs=/usr/local/apache2/bin/apxs [root@271d5811ce45 native]# make [root@271d5811ce45 native]# make install
  3. 上述步驟完成後在native/apache-2.0目錄下會產生一個mod_jk.so文件,把這個文件複製到Apache目錄下的modules目錄下
    [root@271d5811ce45 apache-2.0]# cp mod_jk.so /etc/httpd/modules/
  4. 在Apache的相關目錄(我放在了/etc/httpd/conf)下建立mod_jk.conf和workers.properties文件

    mod_jk.conf:tomcat

    #LoadModule jk_module modules/mod_jk.so                        
                                                                   
    JkWorkersFile /etc/httpd/conf/workers.properties               
                                                                   
    # Where to put jk logs                                         
    JkLogFile /etc/httpd/logs/mod_jk.log                           
                                                                   
    # Set the jk log level [debug/error/info]                      
    JkLogLevel info                                                
                                                                   
    # Select the log format                                        
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"                      
                                                                   
    # JkOptions indicate to send SSL KEY SIZE,                     
    JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
    # JkRequestLogFormat set the request format                    
    JkRequestLogFormat "%w %V %T"                                  
                                                                   
    JkMount /*WEB-INF worker1                                      
    JkMount /*.action worker1                                      
    JkMount /*.jsp worker1                                         
    JkMount /application/* worker1                                 
    JkMount /*.do worker1                                          
    JkMount /*.class worker1                                       
    JkMount /*/servlet/*  worker1                                  

    workers.properities:app

    # Defining a worker named worker1 and of type ajp13
    worker.list=worker1
    
    # Set properties for worker1
    worker.worker1.port=8009
    worker.worker1.host=localhost
    worker.worker1.type=ajp13
    worker.worker1.lbfactor=50
    worker.worker1.cachesize=10
    worker.worker1.cache_timeout=600
    worker.worker1.socket_keepalive=1
    worker.worker1.socket_timeout=300

    而後在Apache的配置文件中導入這兩個模塊,在httpd.conf文件的末尾加入如下內容:socket

    LoadModule jk_module modules/mod_jk.so
    Include /etc/httpd/conf/mod_jk.conf

  5. 至此完成了配置部分,重啓Apache服務
    [root@271d5811ce45 conf]# systemctl restart httpd
    在Apache的項目資源路徑下添加一個hello.jsp頁面進行驗證(咱們設Apache和Tomcat共享同一個項目路徑)
    <html>
    <head><title>Hello</title></head>
    <body>
    <%
    java.util.Date currentTime = new java.util.Date();
    %>
    <%=currentTime%>
    </body>
    </html>

    訪問該jsp頁面:jsp

    該路徑下的靜態頁面:測試

    至此動態和靜態頁面成功實現分離。spa

相關文章
相關標籤/搜索