安装jdk

  1. [root@cent-8 ~]$ dnf list *JDK*
  2. [root@cent-8 ~]$ dnf -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
  3. [root@cent-8 ~]$ java -version
  4. [root@cent-8 ~]$ which java
  5. /usr/bin/java
  6. [root@cent-8 ~]$ ll /usr/bin/java
  7. lrwxrwxrwx 1 root root 22 Nov 17 02:34 /usr/bin/java -> /etc/alternatives/java
  8. [root@cent-8 ~]$ ll /etc/alternatives/java
  9. lrwxrwxrwx 1 root root 73 Nov 17 02:34 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el8_4.x86_64/jre/bin/java
  10. [root@cent-8 ~]$ vim hello.java
  11. class Hello{
  12. public static void main(String[] args)
  13. {
  14. System.out.println("hello, world");
  15. }
  16. }
  17. [root@cent-8 ~]$ javac hello.java
  18. [root@cent-8 ~]$ java Hello
  19. hello, world

安装tomcat

  1. [root@cent-8 ~]$ wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.72/bin/apache-tomcat-8.5.72.tar.gz
  2. [root@cent-8 ~]$ tar xf apache-tomcat-8.5.72.tar.gz -C /usr/local/
  3. [root@cent-8 ~]$ cd /usr/local/
  4. [root@cent-8 /usr/local]$ ln -s apache-tomcat-8.5.72/ tomcat
  5. [root@cent-8 /usr/local]$ echo 'PATH=/usr/local/tomcat/bin:$PATH' > /etc/profile.d/tomcat.sh
  6. [root@cent-8 /usr/local]$ . /etc/profile.d/tomcat.sh
  7. [root@cent-8 /usr/local]$ echo $PATH
  8. /usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  9. [root@cent-8 /usr/local]$ ls tomcat/bin/
  10. bootstrap.jar catalina-tasks.xml commons-daemon.jar configtest.sh digest.sh shutdown.bat startup.sh tool-wrapper.bat version.sh
  11. catalina.bat ciphers.bat commons-daemon-native.tar.gz daemon.sh setclasspath.bat shutdown.sh tomcat-juli.jar tool-wrapper.sh
  12. catalina.sh ciphers.sh configtest.bat digest.bat setclasspath.sh startup.bat tomcat-native.tar.gz version.ba
  13. [root@cent-8 ~]$ catalina.sh version
  14. [root@cent-8 ~]$ startup.sh
  15. [root@cent-8 ~]$ ss -ntl
  16. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  17. LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
  18. LISTEN 0 100 *:8080 *:*
  19. 访问页面http://10.0.0.18:8080/
  20. [root@cent-8 ~]$ shutdown.sh
  21. [root@cent-8 ~]$ catalina.sh start
  22. [root@cent-8 ~]$ catalina.sh stop

tomcat自启动service文件

  1. #为jdk路径创建软连接
  2. ln -s /usr/share/applications/java-1.8.0-openjdk-1.8.0.312.b07-1.el8_4.x86_64-policytool.desktop /usr/local/jdk
  3. [root@cent-8 ~]$ useradd -r -s /sbin/nologin tomcat
  4. [root@cent-8 ~]$ chown -R tomcat.tomcat /usr/local/tomcat/
  5. [root@cent-8 ~]$ vim /usr/local/tomcat/conf/tomcat.conf
  6. JAVA_HOME=/usr/local/jre
  7. #JRE_HOME=/usr/local/jdk/jre
  8. [root@cent-8 ~]$ vim /lib/systemd/system/tomcat.service
  9. [Unit]
  10. Description=Tomcat
  11. After=syslog.target network.target
  12. [Service]
  13. Type=forking
  14. EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
  15. ExecStart=/usr/local/tomcat/bin/startup.sh
  16. ExecStop=/usr/local/tomcat/bin/shutdown.sh
  17. PrivateTmp=true
  18. User=tomcat
  19. Group=tomcat
  20. [Install]
  21. WantedBy=multi-user.target
  22. [root@cent-8 ~]$ systemctl daemon-reload
  23. [root@cent-8 ~]$ systemctl enable --now tomcat

案例——部署JPress

[root@cent-8 /usr/local/tomcat]$ ls
bin  BUILDING.txt  conf  CONTRIBUTING.md  lib  LICENSE  logs  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  temp  webapps  work

[root@cent-8 /usr/local/tomcat]$ ls conf/ (主配置目录)
Catalina   catalina.properties    jaspic-providers.xml  logging.properties  tomcat.conf   catalina.policy    
tomcat-users.xsd   jaspic-providers.xsd  server.xml   web.xml tomcat-users.xml   context.xml

[root@cent-8 /usr/local/tomcat]$ ls webapps/ (app目录)
docs  examples  host-manager  manager  ROOT

[root@cent-8 /usr/local/tomcat]$ ls webapps/ROOT/ 
asf-logo-wide.svg  bg-button.png  bg-middle.png  bg-nav.png  bg-upper.png  favicon.ico  index.jsp  RELEASE-NOTES.txt  tomcat.css  tomcat.svg  WEB-INF
yum install mysql-server -y
ystemctl enable --now mysqld
mysql> create database ipress;
mysql> create user jpress@"10.0.0.%" identified by "jpress";
mysql> grant all on jpress.* to jpress@"10.0.0.%";

[root@cent-8 /usr/local/tomcat/webapps]$ ln -s jpress-v4.1.4 jpress
[root@cent-8 /usr/local/tomcat/webapps]$ ls
docs  examples  host-manager  jpress  jpress-v4.1.4  jpress-v4.1.4.war  manager  ROOT

后端页面:http://10.0.0.18:8080/jpress/admin/root
前端页面:http://10.0.0.18:8080/jpress/

[root@cent-8 /usr/local/tomcat/webapps]$ tree jpress
jpress
├── attachment
│   └── 20211118
│       └── 3b189c7725904e4da939840091469de8.jpg
├── META-INF
│   ├── MANIFEST.MF

常见配置详解

WEB方式的管理页面

状态页Server status、Manager APP

#基于WEB的管理Server status和Manager APP实现应用部署(默认的管理页面被禁用)

①此文件指定授权用户信息,指定用户和密码(#修改全局配置文件需要重启服务生效)

vim conf/tomcat-users.xml         
<role rolename="manager-gui"/>
<user username="admin" password="123456" roles="manager-gui"/>

②限制访问的网段10.0.x.x(#修改WebApp的配置无需重启服务即可生效)
vim webapps/manager/META-INF/context.xml
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|10\.0\.\d+\.\d+"

虚拟主机管理Host Manager

①此文件指定授权用户信息,指定用户和密码
vim conf/tomcat-users.xml 
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>        #添加新的role
<user username="admin" password="12345678" roles="manager-gui,admin-gui"/>        #添加新的role
systemctl restart tomcat

②限制访问的网段10.0.0.x
vim webapps/host-manager/META-INF/context.xml 
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|10\.0\.0\.\d+" />

端口8005安全配置

8005是Tomcat的管理端口,默认监听在127.0.0.1上。无需验证就可发送SHUTDOWN (大小写敏感)这个字符串,tomcat接收到后就会关闭此Server。此行不能被注释,否则无法启动tomcat服务

此管理功能建议禁用,可将SHUTDOWN改为一串猜不出的字符串实现,或者port修改成 0, 会使用随机端口,如:36913
port设为-1等无效端口,将关闭此功能

显示指定的http服务器版本信息

默认不显示tomcat的http的Server头信息, 可以指定tomcat的http的Server头信息为相应的值

vim /usr/local/tomcat/conf/server.xml 
<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" Server="ZyyServer"/>

多虚拟主机

1.域名解析配置
2.vim conf/server.xml 
<Host name="web1.zyy.com"  appBase="webapp1">
</Host>
<Host name="web2.zyy.com"  appBase="webapp2">
</Host>

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="web1_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />    #可虚拟主机专有访问日志

mkdir webapp{1,2}/ROOT
chown -R tomcat.tomcat webapp{1,2}
echo web1.zyy.com > webapp1/ROOT/index.html
echo web2.zyy.com > webapp2/ROOT/index.html

测试:http://web1.zyy.com:8080/
测试:http://web2.zyy.com:8080/

Context 配置

Context作用

  • 路径映射:将url映射至指定路径,而非使用appBase下的物理目录,实现虚拟目录功能
  • 应用独立配置,例如单独配置应用日志、单独配置应用访问控制 ```bash

    映射指定路径

    映射站点的根目录

还可以添加日志等独立的配置

<a name="mJoti"></a>
###   其它配置  
```bash
conf/server.xml中可以配置service,connector, Engine,Host等

一、一般情况下,一个Server实例配置一个Service,name属性相当于该Service的ID。
<Service name="Catalina">            

二、redirectPort,如果访问HTTPS协议,自动转向这个连接器。但大多数时候,Tomcat并不会开启HTTPS,因为Tomcat往往部署在内部,HTTPS性能较差
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

三、引擎配置
<Engine name="Catalina" defaultHost="localhost">

四、defaultHost指向内部定义某虚拟主机。缺省虚拟主机可以改动,默认localhost。
<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">

(unpackWARs 是否自动解压war格式   autoDeploy 热部署,自动加载并运行应用)

结合反向代理实现tomcat部署

实现会话保持

session sticky会话黏性


Session 复制集群


session 共享服务器(msm)

msm(memcached session manager)提供将Tomcat的session保持到memcached或redis的程序,可以实现高可用。
sticky模式:前端tomcat和后端memcached有关联(粘性)关系
non-sticky模式:前端tomcat和后端memcached无关联(无粘性)关系

image.png

memcached

安装memcached

连接memcached

①与memcached通信的不同语言的连接器。libmemcached提供了C库和命令行工具。
②python 语言连接

基于Repcached实现双主数据同步


Tomcat 性能优化


遇到的问题

tomcat.conf文件中JAVA_HOME配置错误,使 tomcat.service: Failed with result ‘exit-code’.

Q:JAVA_HOME变量没有设置好,其中指定的jdk路径并不存在,需要指定准确
[root@cent-8 ~]$ systemctl status tomcat
● tomcat.service - tomcat
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2021-11-17 15:04:46 CST; 6s ago
  Process: 1953 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 1961 (code=exited, status=126)

Nov 17 15:04:46 cent-8 systemd[1]: Starting tomcat...
Nov 17 15:04:46 cent-8 systemd[1]: Started tomcat.
Nov 17 15:04:46 cent-8 systemd[1]: tomcat.service: Main process exited, code=exited, status=126/n/a
Nov 17 15:04:46 cent-8 systemd[1]: tomcat.service: Failed with result 'exit-code'.

A:为JAVA_HOME变量中的路径不正确,为jre路径创建软连接并写入正确路径:
ln -s /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el8_4.x86_64/jre /usr/local/jre
[root@cent-8 ~]$ vim /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/usr/local/jre

[root@cent-8 ~]$ systemctl status tomcat
● tomcat.service - tomcat
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-11-17 15:08:26 CST; 18s ago
  Process: 2004 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 2012 (java)
    Tasks: 20 (limit: 17768)
   Memory: 95.0M
   CGroup: /system.slice/tomcat.service
           └─2012 /usr/local/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogMana>

Nov 17 15:08:26 cent-8 systemd[1]: Starting tomcat...
Nov 17 15:08:26 cent-8 systemd[1]: Started tomcat.