资源池
本章资源:点击这里 提取码:upbf
tomcat官网:https://tomcat.apache.org/
jpress博客系统安装包下载地址(不是官网):https://gitee.com/fuhai/jpress/blob/alpha/wars/jpress-web-newest.war
1、Tomcat简介
- Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成;
- Tomcat 属于轻量级应用服务器;其技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器;Tomcat处理静态HTML的能力不如Nginx/Apache服务器.
- 另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。
- Tomcat是开发和调试JSP程序的首选.
2、Tomcat安装
第一步:下载并上传Tomcat安装包
╭─root@localhost.localdomain ~
╰─➤ rz
╭─root@localhost.localdomain ~
╰─➤ unzip apache-tomcat-9.0.20.zip
╭─root@localhost.localdomain ~
╰─➤ mv apache-tomcat-9.0.20 /usr/local/tomcat #移动并改名
╭─root@localhost.localdomain ~
╰─➤ chmod -R a+x /usr/local/tomcat
第二步:下载Java环境
注意:选择yum安装tomcat需要jdk,就不用再手动配置环境变量
╭─root@localhost.localdomain ~
╰─➤ yum install java -y
第三步:启动Tomcat
╭─root@localhost.localdomain ~
╰─➤ cd /usr/local/tomcat/bin
╭─root@localhost.localdomain /usr/local/tomcat/bin
╰─➤ bash startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
╭─root@localhost.localdomain /usr/local/tomcat/bin
╰─➤ ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 :::8009 :::*
第四步:浏览器访问
3、Tomcat目录讲解
╭─root@localhost.localdomain /usr/local/tomcat/bin
╰─➤ cd /usr/local/tomcat
╭─root@localhost.localdomain /usr/local/tomcat
╰─➤ tree -L 1
.
├── bin #用以启动、关闭Tomcat或者其它功能的脚本(.bat文件和.sh文件)
├── BUILDING.txt
├── conf #用以配置Tomcat的XML及DTD文件
├── CONTRIBUTING.md
├── lib #存放web应用能访问的JAR包
├── LICENSE
├── logs #Catalina和其它Web应用程序的日志文件
├── NOTICE
├── README.md
├── RELEASE-NOTES
├── RUNNING.txt
├── temp #临时文件
├── webapps #Web应用程序根目录
└── work #用以产生有JSP编译出的Servlet的.java和.class文件
7 directories, 7 files
╭─root@localhost.localdomain /usr/local/tomcat
╰─➤ cd webapps
╭─root@localhost.localdomain /usr/local/tomcat/webapps
╰─➤ ll
total 4.0K
drwxr-xr-x 14 root root 4.0K May 3 23:27 docs #tomcat帮助文档
drwxr-xr-x 6 root root 83 May 3 23:27 examples #web应用实例
drwxr-xr-x 5 root root 87 May 3 23:27 host-manager #管理
drwxr-xr-x 5 root root 103 May 3 23:27 manager #管理
drwxr-xr-x 3 root root 283 May 3 23:27 ROOT #默认网站根目录
4、Tomcat主配置文件server.xml讲解
╭─root@localhost.localdomain /usr/local/tomcat
╰─➤ cd /usr/local/tomcat/conf/
╭─root@localhost.localdomain /usr/local/tomcat/conf
╰─➤ vim server.xml
...
<Server port="8005" shutdown="SHUTDOWN">
#8005: 接收shutdown指令
<Connector port="8080" protocol="HTTP/1.1"
#8080: 监听的端口
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
#8009: AJP协议
...
4、tomcat同机多服务部署(三台)
第一步:复制两份tomcat文件
╭─root@localhost.localdomain ~
╰─➤ cd /usr/local/
╭─root@localhost.localdomain /usr/local
╰─➤ ls
bin etc games include lib lib64 libexec sbin share src tomcat
╭─root@localhost.localdomain /usr/local
╰─➤ cp -a tomcat tomcat1
╭─root@localhost.localdomain /usr/local
╰─➤ cp -a tomcat tomcat2
╭─root@localhost.localdomain /usr/local
╰─➤ ls
bin etc games include lib lib64 libexec sbin share src tomcat tomcat1 tomcat2
第二步:修改配置文件
╭─root@localhost.localdomain /usr/local
╰─➤ vim tomcat1/conf/server.xml #tomcat1
...
<Server port="8006" shutdown="SHUTDOWN"> #8005-->8006
...
<Connector port="8081" protocol="HTTP/1.1" #8080-->8081
...
╭─root@localhost.localdomain /usr/local
╰─➤ vim tomcat2/conf/server.xml #tomcat2
...
<Server port="8007" shutdown="SHUTDOWN"> #8005-->8007
...
<Connector port="8082" protocol="HTTP/1.1" #8080-->8082
...
第三步:分别启动三个tomcat服务
╭─root@localhost.localdomain /usr/local
╰─➤ tomcat/bin/startup.sh #tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
╭─root@localhost.localdomain /usr/local
╰─➤ tomcat1/bin/startup.sh #tomcat1
Using CATALINA_BASE: /usr/local/tomcat1
Using CATALINA_HOME: /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.
╭─root@localhost.localdomain /usr/local
╰─➤ tomcat2/bin/startup.sh #tomcat2
Using CATALINA_BASE: /usr/local/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.
第四步:查看端口
╭─root@localhost.localdomain /usr/local
╰─➤ ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 100 :::8081 :::*
LISTEN 0 100 :::8082 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8006 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8007 :::*
LISTEN 0 100 :::8009 :::*
第五步:为每个tomcat服务写首页文件
╭─root@localhost.localdomain /usr/local
╰─➤ echo "hello tomcat" >> tomcat/webapps/ROOT/index.html
╭─root@localhost.localdomain /usr/local
╰─➤ echo "hi tomcat1" >> tomcat1/webapps/ROOT/index.html
╭─root@localhost.localdomain /usr/local
╰─➤ echo "morning tomcat2" >> tomcat2/webapps/ROOT/index.html
第五步:浏览器访问
tomcat:
tomcat1:
tomcat2:
5、tomcat集群部署
nginx服务器:192.168.80.4
tomcat服务器:192.168.80.3
第一步:部署tomcat后端节点
- 参考:4、tomcat同机多服务部署(三台)
第二步:编辑nginx配置文件
╭─root@localhost.localdomain ~
╰─➤ yum install nginx -y
╭─root@localhost.localdomain ~
╰─➤ vim /etc/nginx/nginx.conf
...
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream du {
server 192.168.80.3:8080 weight=3 max_fails=2 fail_timeout=2;
server 192.168.80.3:8081 weight=5 max_fails=2 fail_timeout=2;
server 192.168.80.3:8082 weight=1 max_fails=2 fail_timeout=2;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://du;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
...
第三步:检测nginx配置并启动
╭─root@localhost.localdomain ~
╰─➤ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
╭─root@localhost.localdomain ~
╰─➤ systemctl restart nginx
第四步:浏览器访问
6、tomcat上线jpress博客系统
第一步:下载jpress
- 本章章首有资源池
第二步:上传软件包并解压
╭─root@localhost.localdomain ~
╰─➤ cd /usr/local/tomcat/webapps/ROOT
╭─root@localhost.localdomain /usr/local/tomcat/webapps/ROOT
╰─➤ rm -rf ./*
zsh: sure you want to delete all the files in /usr/local/tomcat/webapps/ROOT/. [yn]? y
╭─root@localhost.localdomain /usr/local/tomcat/webapps/ROOT
╰─➤ rz
╭─root@localhost.localdomain /usr/local/tomcat/webapps/ROOT
╰─➤ unzip fuhai-jpress-alpha.zip
第三步:解压相关文件
╭─root@localhost.localdomain /usr/local/tomcat/webapps/ROOT
╰─➤ cp jpress/wars/jpress-web-newest.war .
╭─root@localhost.localdomain /usr/local/tomcat/webapps/ROOT
╰─➤ unzip jpress-web-newest.war
╭─root@localhost.localdomain /usr/local/tomcat/webapps/ROOT
╰─➤ ls
fuhai-jpress-alpha.zip jpress jpress-web-newest.war META-INF robots.txt static templates WEB-INF
第四步:下载数据库并使用
╭─root@localhost.localdomain /usr/local/tomcat/webapps/ROOT
╰─➤ yum install mariadb-server -y
╭─root@localhost.localdomain /usr/local/tomcat/webapps/ROOT
╰─➤ systemctl restart mariadb
╭─root@localhost.localdomain /usr/local/tomcat/webapps/ROOT
╰─➤ mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]> create database du;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> grant all on *.* to du@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
第五步、重启tomcat
╭─root@localhost.localdomain ~
╰─➤ cd /usr/local/tomcat/bin
╭─root@localhost.localdomain /usr/local/tomcat/bin
╰─➤ ./shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
╭─root@localhost.localdomain /usr/local/tomcat/bin
╰─➤ ./startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
第六步:浏览器访问