tomcat8.5 配置
阿新 • • 發佈:2019-01-05
轉載自 https://www.zfl9.com/tomcat.html
Tomcat是一個開放原始碼、執行servlet和JSP Web應用軟體的基於Java的Web應用軟體容器;
隨著Catalina Servlet引擎的出現,Tomcat第四版號的效能得到提升,使得它成為一個值得考慮的Servlet/JSP容器,因此目前許多WEB伺服器都是採用Tomcat
java環境配置
tomcat8.5
-
## 下載 http://tomcat.apache.org/download-80.cgi
- mkdir -p /opt/tomcat/
- tar xf tomcat.tar.gz -C /opt/tomcat/
- ## 環境變數 /etc/profile.d/tomcat.sh
-
export CATALINA_HOME=/opt/tomcat
- export PATH=$PATH:$CATALINA_HOME/bin
- . /etc/profile
- # tomcat啟動、關閉
-
catalina.sh start
|stop
- or
- startup.sh | shutdown.sh
熱部署
- # webapps/ webapps目錄
- 物理目錄 訪問url
- ROOT http://ip
- zfl http://ip/zfl/
- test http://ip/test/
- # 三種方式熱部署
- 1. 把web專案放置在webapps/ 訪問路徑 http://ip/專案名/
- 2. conf/server.xml
- <Engine name="Catalina" defaultHost="localhost">
- <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
- <Context debug="0" docBase="/opt/webapps/blog" path="/blog" privileged="true" reloadable="true"/>
- </Host>
- </Engine>
- # docBase若為相對路徑是相對於webapps/ 訪問路徑 http://ip/blog/
- 3. conf/Catalina/localhost/apps.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <Context docBase="/opt/webapps/blog" reloadable="true" />
- # 該方式配置時,訪問路徑為xml檔名
基於域名的虛擬主機
- ## conf/server.xml
- <Host name="www.zfl.com" appBase="/opt/webapps/www.zfl.com" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
- <Context docBase="/opt/webapps/www.zfl.com/www" path="" reloadable="true" />
- </Host>
- <Host name="blog.zfl.com" appBase="/opt/webapps/blog.zfl.com" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
- <Context docBase="/opt/webapps/blog.zfl.com/blog" path="" reloadable="true" />
- </Host>
- www.zfl.com appbase目錄 /opt/webapps/www.zfl.com/ web專案 www http://www.zfl.com
- blog.zfl.com appbase目錄 /opt/webapps/blog.zfl.com/ web專案 blog http://blog.zfl.com
apache + tomcat
- ## 下載tomcat-connectors mod_jk
- http://apache.mirror.iweb.ca/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.42-src.tar.gz
- ## 編譯
- cd /usr/local/src/mod_jk/native/
- ./configure --with-apxs =/opt/www/httpd/bin/apxs
- make
- libtool --finish /opt/www/httpd/modules/
- make install
- cd ../conf/
- cp -af httpd-jk.conf /opt/www/httpd/conf/extra/
- cp -af uriworkermap.properties /opt/www/httpd/conf/extra/
- cp -af workers.properties /opt/www/httpd/conf/
- ## 配置Apache
- --- conf/httpd.conf ---
- Include conf/extra/httpd-jk.conf
- AddType application/x-httpd-jsp .jsp
- --- conf/extra/httpd-jk.conf ---
- LoadModule jk_module modules/mod_jk.so
- <IfModule jk_module>
- JkWorkersFile conf/workers.properties
- JkLogFile logs/mod_jk.log
- JkLogLevel info
- JkShmFile logs/mod_jk.shm
- # 新增這兩行
- JkMount /*.jsp tomcat1
- JkMountCopy All
- #
- JkWatchdogInterval 60
- <Location /jk-status>
- JkMount jk-status
- Order deny,allow
- Deny from all
- Allow from 127.0.0.1
- </Location>
- <Location /jk-manager>
- JkMount jk-manager
- Order deny,allow
- Deny from all
- Allow from 127.0.0.1
- </Location>
- </IfModule>
- --- conf/workers.properties ---
- worker.template.type =ajp13
- worker.template.socket_connect_timeout =5000
- worker.template.socket_keepalive =true
- worker.template.ping_mode =A
- worker.template.ping_timeout =10000
- worker.template.connection_pool_minsize =0
- worker.template.connection_pool_timeout =600
- worker.template.reply_timeout =300000
- worker.template.recovery_options =3
- worker.list =tomcat1
- worker.type =ajp3
- worker.host =127.0.0.1
- worker.port =8009
- ## 配置Tomcat
- # docBase改為Apache網站根目錄
- --- conf/server.xml ---
- <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
- <Context path="" docBase="/opt/www/httpd/htdocs" debug="0"/>
- </Host>
- ## 測試
- service httpd start
- catalina.sh start
- # test_page
- --- /opt/www/httpd/htdocs/test.jsp ---
- <%@page language="java" import="java.util.*" %>
- Stay hungry Stay foolish !!!
- Now the time is: <%out.println(new Date());%>
nginx + tomcat
- ## 配置nginx
- --- www.conf ---
- server {
- server_name www.zfl.com ;
- root /opt/www/nginx/html ;
- index index.jsp index.php index.html ;
- location ~* \.php$ {
- fastcgi_pass 127.0.0.1:9000 ;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params ;
- }
- location ~* \.jsp$ {
- proxy_pass http://127.0.0.1:8080 ;
- proxy_redirect off ;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- }
自定義錯誤頁
- --- conf/web.xml ---
- <web-app>
- ...
- <error-page>
- <error-code>404</error-code>
- <location>/404.html</location>
- </error-page>
- ...
- </web-app>
https相關
- ## keytool
- 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"
- ## 啟用https
- --- conf/server.xml ---
- ...
- <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" />
- ...
- ...
- <Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true">
- <SSLHostConfig>
- <Certificate
- certificateKeystoreFile ="/etc/pki/tomcat/tomcat.jks"
- certificateKeyAlias ="tomcat"
- certificateKeystorePassword ="123456"
- type="RSA" />
- </SSLHostConfig>
- </Connector>
- ...
- ...
- <Connector port="8009" protocol="AJP/1.3" redirectPort="443"/>
- ...
- ## 強制跳轉至https
- --- conf/web.xml ---
- <web-app>
- ...
- <security-constraint>
- <web-resource-collection >
- <web-resource-name >SSL</web-resource-name>
- <url-pattern>/*</url-pattern>
- </web-resource-collection>
- <user-data-constraint>
- <transport-guarantee>CONFIDENTIAL</transport-guarantee>
- </user-data-constraint>
- </security-constraint>
- ...
- </web-app>
- ## nginx 代理 tomcat (https)
- nginx與client之間用https通訊 nginx與tomcat之間用http通訊
- --- $nginx/conf.d/www.conf ---
- server {
- ...
- location ... {
- proxy_pass http://127.0.0.1:8888 ;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
- ...
- }
- --- $tomcat/conf/server.xml ---
- ...
- <Host ...>
- ...
- <Valve className="org.apache.catalina.valves.RemoteIpValve"
- remoteIpHeader ="X-Forwarded-For"
- protocolHeader ="X-Forwarded-Proto"
- protocolHeaderHttpsValue ="https"/>
- ...
- </Host>
- ...
-