安装jdk
[root@cent-8 ~]$ dnf list *JDK*
[root@cent-8 ~]$ dnf -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
[root@cent-8 ~]$ java -version
[root@cent-8 ~]$ which java
/usr/bin/java
[root@cent-8 ~]$ ll /usr/bin/java
lrwxrwxrwx 1 root root 22 Nov 17 02:34 /usr/bin/java -> /etc/alternatives/java
[root@cent-8 ~]$ ll /etc/alternatives/java
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
[root@cent-8 ~]$ vim hello.java
class Hello{
public static void main(String[] args)
{
System.out.println("hello, world");
}
}
[root@cent-8 ~]$ javac hello.java
[root@cent-8 ~]$ java Hello
hello, world
安装tomcat
[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
[root@cent-8 ~]$ tar xf apache-tomcat-8.5.72.tar.gz -C /usr/local/
[root@cent-8 ~]$ cd /usr/local/
[root@cent-8 /usr/local]$ ln -s apache-tomcat-8.5.72/ tomcat
[root@cent-8 /usr/local]$ echo 'PATH=/usr/local/tomcat/bin:$PATH' > /etc/profile.d/tomcat.sh
[root@cent-8 /usr/local]$ . /etc/profile.d/tomcat.sh
[root@cent-8 /usr/local]$ echo $PATH
/usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@cent-8 /usr/local]$ ls tomcat/bin/
bootstrap.jar catalina-tasks.xml commons-daemon.jar configtest.sh digest.sh shutdown.bat startup.sh tool-wrapper.bat version.sh
catalina.bat ciphers.bat commons-daemon-native.tar.gz daemon.sh setclasspath.bat shutdown.sh tomcat-juli.jar tool-wrapper.sh
catalina.sh ciphers.sh configtest.bat digest.bat setclasspath.sh startup.bat tomcat-native.tar.gz version.ba
[root@cent-8 ~]$ catalina.sh version
[root@cent-8 ~]$ startup.sh
[root@cent-8 ~]$ ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
访问页面http://10.0.0.18:8080/
[root@cent-8 ~]$ shutdown.sh
[root@cent-8 ~]$ catalina.sh start
[root@cent-8 ~]$ catalina.sh stop
tomcat自启动service文件
#为jdk路径创建软连接
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
[root@cent-8 ~]$ useradd -r -s /sbin/nologin tomcat
[root@cent-8 ~]$ chown -R tomcat.tomcat /usr/local/tomcat/
[root@cent-8 ~]$ vim /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/usr/local/jre
#JRE_HOME=/usr/local/jdk/jre
[root@cent-8 ~]$ vim /lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
[root@cent-8 ~]$ systemctl daemon-reload
[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 "%r" %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作用
还可以添加日志等独立的配置
<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无关联(无粘性)关系
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.