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.  
  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

热部署

  1.  
    # webapps/ webapps目录
  2.  
    物理目录 访问url
  3.  
    ROOT http://ip
  4.  
  5.  
  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

基于域名的虚拟主机

  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

apache + tomcat

  1.  
    ## 下载tomcat-connectors mod_jk
  2.  
  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

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

自定义错误页

  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

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.  
      ...

猜你喜欢

转载自www.cnblogs.com/yongjin/p/10223429.html