1. 程式人生 > >tomcat8.5 配置

tomcat8.5 配置

轉載自 https://www.zfl9.com/tomcat.html

 

Tomcat是一個開放原始碼、執行servlet和JSP Web應用軟體的基於Java的Web應用軟體容器;
隨著Catalina Servlet引擎的出現,Tomcat第四版號的效能得到提升,使得它成為一個值得考慮的Servlet/JSP容器,因此目前許多WEB伺服器都是採用Tomcat

java環境配置

 

SunJDK for Linux 配置

tomcat8.5

  1.   ## 下載 http://tomcat.apache.org/download-80.cgi
  2.    
  3.   mkdir -p /opt/tomcat/
  4.   tar xf tomcat.tar.gz -C /opt/tomcat/
  5.    
  6.   ## 環境變數 /etc/profile.d/tomcat.sh
  7.   export CATALINA_HOME=/opt/tomcat
  8.   export PATH=$PATH:$CATALINA_HOME/bin
  9.    
  10.   . /etc/profile
  11.    
  12.   # tomcat啟動、關閉
  13.   catalina.sh start |stop
  14.   or
  15.   startup.sh | shutdown.sh
  16.    
Bash Copy

熱部署

  1.   # webapps/ webapps目錄
  2.   物理目錄 訪問url
  3.   ROOT http://ip
  4.   zfl http://ip/zfl/
  5.   test http://ip/test/
  6.    
  7.   # 三種方式熱部署
  8.   1. 把web專案放置在webapps/ 訪問路徑 http://ip/專案名/
  9.    
  10.   2. conf/server.xml
  11.   <Engine name="Catalina" defaultHost="localhost">
  12.   <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
  13.   <Context debug="0" docBase="/opt/webapps/blog" path="/blog" privileged="true" reloadable="true"/>
  14.   </Host>
  15.   </Engine>
  16.   # docBase若為相對路徑是相對於webapps/ 訪問路徑 http://ip/blog/
  17.    
  18.   3. conf/Catalina/localhost/apps.xml
  19.   <?xml version="1.0" encoding="UTF-8"?>
  20.   <Context docBase="/opt/webapps/blog" reloadable="true" />
  21.   # 該方式配置時,訪問路徑為xml檔名
  22.    
Bash Copy

基於域名的虛擬主機

  1.   ## conf/server.xml
  2.   <Host name="www.zfl.com" appBase="/opt/webapps/www.zfl.com" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
  3.   <Context docBase="/opt/webapps/www.zfl.com/www" path="" reloadable="true" />
  4.   </Host>
  5.   <Host name="blog.zfl.com" appBase="/opt/webapps/blog.zfl.com" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
  6.   <Context docBase="/opt/webapps/blog.zfl.com/blog" path="" reloadable="true" />
  7.   </Host>
  8.    
  9.   www.zfl.com appbase目錄 /opt/webapps/www.zfl.com/ web專案 www http://www.zfl.com
  10.   blog.zfl.com appbase目錄 /opt/webapps/blog.zfl.com/ web專案 blog http://blog.zfl.com
  11.    
Bash Copy

apache + tomcat

  1.   ## 下載tomcat-connectors mod_jk
  2.   http://apache.mirror.iweb.ca/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.42-src.tar.gz
  3.    
  4.   ## 編譯
  5.   cd /usr/local/src/mod_jk/native/
  6.   ./configure --with-apxs =/opt/www/httpd/bin/apxs
  7.   make
  8.   libtool --finish /opt/www/httpd/modules/
  9.   make install
  10.    
  11.   cd ../conf/
  12.   cp -af httpd-jk.conf /opt/www/httpd/conf/extra/
  13.   cp -af uriworkermap.properties /opt/www/httpd/conf/extra/
  14.   cp -af workers.properties /opt/www/httpd/conf/
  15.    
  16.    
  17.   ## 配置Apache
  18.   --- conf/httpd.conf ---
  19.   Include conf/extra/httpd-jk.conf
  20.   AddType application/x-httpd-jsp .jsp
  21.    
  22.    
  23.   --- conf/extra/httpd-jk.conf ---
  24.   LoadModule jk_module modules/mod_jk.so
  25.   <IfModule jk_module>
  26.   JkWorkersFile conf/workers.properties
  27.   JkLogFile logs/mod_jk.log
  28.   JkLogLevel info
  29.   JkShmFile logs/mod_jk.shm
  30.   # 新增這兩行
  31.   JkMount /*.jsp tomcat1
  32.   JkMountCopy All
  33.   #
  34.   JkWatchdogInterval 60
  35.   <Location /jk-status>
  36.   JkMount jk-status
  37.   Order deny,allow
  38.   Deny from all
  39.   Allow from 127.0.0.1
  40.   </Location>
  41.   <Location /jk-manager>
  42.   JkMount jk-manager
  43.   Order deny,allow
  44.   Deny from all
  45.   Allow from 127.0.0.1
  46.   </Location>
  47.   </IfModule>
  48.    
  49.    
  50.   --- conf/workers.properties ---
  51.   worker.template.type =ajp13
  52.   worker.template.socket_connect_timeout =5000
  53.   worker.template.socket_keepalive =true
  54.   worker.template.ping_mode =A
  55.   worker.template.ping_timeout =10000
  56.   worker.template.connection_pool_minsize =0
  57.   worker.template.connection_pool_timeout =600
  58.   worker.template.reply_timeout =300000
  59.   worker.template.recovery_options =3
  60.   worker.list =tomcat1
  61.   worker.type =ajp3
  62.   worker.host =127.0.0.1
  63.   worker.port =8009
  64.    
  65.    
  66.   ## 配置Tomcat
  67.   # docBase改為Apache網站根目錄
  68.   --- conf/server.xml ---
  69.   <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
  70.   <Context path="" docBase="/opt/www/httpd/htdocs" debug="0"/>
  71.   </Host>
  72.    
  73.   ## 測試
  74.   service httpd start
  75.   catalina.sh start
  76.    
  77.   # test_page
  78.   --- /opt/www/httpd/htdocs/test.jsp ---
  79.   <%@page language="java" import="java.util.*" %>
  80.   Stay hungry Stay foolish !!!
  81.   Now the time is: <%out.println(new Date());%>
  82.    
Bash Copy

nginx + tomcat

  1.   ## 配置nginx
  2.   --- www.conf ---
  3.   server {
  4.   server_name www.zfl.com ;
  5.   root /opt/www/nginx/html ;
  6.   index index.jsp index.php index.html ;
  7.   location ~* \.php$ {
  8.   fastcgi_pass 127.0.0.1:9000 ;
  9.   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  10.   include fastcgi_params ;
  11.   }
  12.   location ~* \.jsp$ {
  13.   proxy_pass http://127.0.0.1:8080 ;
  14.   proxy_redirect off ;
  15.   proxy_set_header Host $host;
  16.   proxy_set_header X-Real-IP $remote_addr;
  17.   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  18.   }
  19.   }
  20.    
Bash Copy

自定義錯誤頁

  1.   --- conf/web.xml ---
  2.   <web-app>
  3.   ...
  4.   <error-page>
  5.   <error-code>404</error-code>
  6.   <location>/404.html</location>
  7.   </error-page>
  8.   ...
  9.   </web-app>
  10.    
Bash Copy

https相關

keytool用法

    1.   ## keytool
    2.   keytool -genkey -keyalg RSA -validity 3650 - alias tomcat -keystore tomcat.jks -storepass 123456 -keypass 123456 -dname "cn=www.zfl.com,ou=otokaze,o=otokaze,l=gd,st=gz,c=cn"
    3.    
    4.   ## 啟用https
    5.   --- conf/server.xml ---
    6.   ...
    7.   <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" />
    8.   ...
    9.   ...
    10.   <Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true">
    11.   <SSLHostConfig>
    12.   <Certificate
    13.   certificateKeystoreFile ="/etc/pki/tomcat/tomcat.jks"
    14.   certificateKeyAlias ="tomcat"
    15.   certificateKeystorePassword ="123456"
    16.   type="RSA" />
    17.   </SSLHostConfig>
    18.   </Connector>
    19.   ...
    20.   ...
    21.   <Connector port="8009" protocol="AJP/1.3" redirectPort="443"/>
    22.   ...
    23.    
    24.   ## 強制跳轉至https
    25.   --- conf/web.xml ---
    26.   <web-app>
    27.   ...
    28.   <security-constraint>
    29.   <web-resource-collection >
    30.   <web-resource-name >SSL</web-resource-name>
    31.   <url-pattern>/*</url-pattern>
    32.   </web-resource-collection>
    33.   <user-data-constraint>
    34.   <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    35.   </user-data-constraint>
    36.   </security-constraint>
    37.   ...
    38.   </web-app>
    39.    
    40.    
    41.   ## nginx 代理 tomcat (https)
    42.   nginx與client之間用https通訊 nginx與tomcat之間用http通訊
    43.   --- $nginx/conf.d/www.conf ---
    44.   server {
    45.   ...
    46.   location ... {
    47.   proxy_pass http://127.0.0.1:8888 ;
    48.   proxy_set_header Host $host;
    49.   proxy_set_header X-Real-IP $remote_addr;
    50.   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    51.   proxy_set_header X-Forwarded-Proto $scheme;
    52.   }
    53.   ...
    54.   }
    55.    
    56.   --- $tomcat/conf/server.xml ---
    57.   ...
    58.   <Host ...>
    59.   ...
    60.   <Valve className="org.apache.catalina.valves.RemoteIpValve"
    61.   remoteIpHeader ="X-Forwarded-For"
    62.   protocolHeader ="X-Forwarded-Proto"
    63.   protocolHeaderHttpsValue ="https"/>
    64.   ...
    65.   </Host>
    66.   ...
    67.