- 一、Tomcat简介
- 二 、Tomcat帮手 --- JDK
- 安装jdk
- 检查jdk是否安装成功
- 三.安装Tomcat
- Tomcat配置管理页面
- 四、web站点部署
- 五、部署开源站点(jspgou商城)
- 安装数据库
- 六、 jspgou商城上线
- 配置数据库连接
- 七、Tomcat多实例配置
- 复制程序文件
- 浏览器访问
- 八、Tomcat反向代理集群
- nginx负载均衡器
- 配置负载均衡器
一、Tomcat简介
Tomcat的官网: http://tomcat.apache.org
Tomcat是Apache软件基金会项目中的核心项目,由apache、sun和其他一些公司以及个人共同开发完成Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,是开发和调试JSP程序的首选
Tomcat和nginx、apache(httpd)web服务器一样,具有处理HTML页面的功能,不过Tomcat处理静态HTML的能力不如Nginx/Apache服务器
一个tomcat默认的并发是200
tomcat使用方案
建议使用Nginx和Tomcat配合,Nginx处理静态,Tomcat处理动态程序
方案三中后端Tomcat可以运行在单独的主机,也可以是同一台主机上的多实例
二 、Tomcat帮手 — JDK
JDK是Java语言的软件开发工具包,JDK是整个java开发的核心,包含了JAVA的运行环境和JAVA工具
安装jdk
systemctl stop firewalld && setenforce 0
yum -y install wget vim lrzsz
jdk下载界面
[root@localhost ~]# tar xf jdk-8u211-linux-x64.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv jdk1.8.0_211/ java
[root@localhost local]# cd java
[root@localhost java]# vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/java #指定java安装目录
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH #用于指定JAVA系统查找命令的路径
CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar #类的路径,在运行java程序时,如果调用到其他类的时候,在classpath寻找相关的类
[root@localhost java]# source /etc/profile.d/jdk.sh
检查jdk是否安装成功
[root@localhost java]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
三.安装Tomcat
[root@localhost ~]# mkdir /data/application -p
[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.49/bin/apache-tomcat-8.5.49.tar.gz
[root@localhost ~]# tar xf apache-tomcat-8.5.95.tar.gz -C /data/application/
[root@localhost ~]# cd /data/application/
[root@localhost application]# mv apache-tomcat-8.5.95/ tomcat
设置环境变量
[root@localhost tomcat]# vim /etc/profile
export TOMCAT_HOME=/data/application/tomcat #指定tomcat的安装目录
[root@java-tomcat1 application]# source /etc/profile #重新加载环境变量
[root@localhost tomcat]# /data/application/tomcat/bin/version.sh #查看安装的tomcat的信息
Using CATALINA_BASE: /data/application/tomcat
Using CATALINA_HOME: /data/application/tomcat
Using CATALINA_TMPDIR: /data/application/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /data/application/tomcat/bin/bootstrap.jar:/data/application/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Server version: Apache Tomcat/8.5.95
Server built: Oct 12 2023 00:55:53 UTC
Server number: 8.5.95.0
OS Name: Linux
OS Version: 3.10.0-1160.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_211-b12
JVM Vendor: Oracle Corporation
启动tomcat
[root@localhost tomcat]# /data/application/tomcat/bin/startup.sh
检查tomcat是否正常启动
[root@location tomcat]# netstat -lntp |grep java
tcp6 0 0 :::8080 :::* LISTEN 30560/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 30560/java
8005:监听关闭Tomcat的请求,shutdown:向以上端口发送关闭服务器的命令字符串
8080:建立http也就是客户端访问连接用
8009:与其他http服务通信
浏览器访问
日志文件在/data/application/tomcat/logs/catalina.out
Tomcat配置管理页面
修改配置文件
vim /data/application/tomcat/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="user" password="password" roles="tomcat,manager-gui,manager-script,admin-gui,admin-script"/> #写在<tomcat-users>标签里
修改配置如下图
vim /data/application/tomcat/webapps/host-manager/META-INF/context.xml
vim /data/application/tomcat/webapps/manager/META-INF/context.xml
四、web站点部署
方法一 直接将程序目录放在webapps下
方法二 使用开发工具将程序打包成war包,然后上传到webapps目录下
wget http://updates.jenkins-ci.org/download/war/2.129/jenkins.war
cd /data/application/tomcat
cp -r webapps/ /opt/
cd /data/application/tomcat/webapps/
rm -rf *
mkdir ROOT
cd ROOT/
cp /root/jenkins.war .
unzip jenkins.war
五、部署开源站点(jspgou商城)
安装数据库
下载yum源的安装包
[root@mysql ~]# yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
[root@mysql ~]# vim /etc/yum.repos.d/mysql-community.repo
把安装5.7的源打开, 关闭安装8.0的源,如下图
安装mysql
yum -y install mysql mysql-server
启动mysql
systemctl start mysqld
查找密码
[root@mysql ~]# grep "password" /var/log/mysqld.log
2023-10-30T05:11:55.455672Z 1 [Note] A temporary password is generated for root@localhost: !s9MldEIDQz*
#更改密码
mysqladmin -uroot -p'!s9MldEIDQz*' password "Qianfeng@123"
#配置数据库
mysql -pQianfeng@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database jspgou default charset=utf8; #创建数据库
Query OK, 1 row affected (0.00 sec)
六、 jspgou商城上线
上传软件包
rz
解压
[root@localhost DB]# /data/application/tomcat/bin/shutdown.sh
[root@localhost ~]#unzip jspgouV6.1-ROOT.zip
[root@localhost ~]#cp -r ROOT/ /data/application/tomcat/webapps/
[root@localhost ~]#cd /data/application/tomcat/webapps/
[root@localhost webapps]# vim ROOT/WEB-INF/config/jdbc.properties
配置数据库连接
[root@localhost ~]# cd DB
[root@localhost DB]# ls
jspgou.sql
[root@localhost DB]# vim /etc/my.cnf
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUB
explicit_defaults_for_timestamp=1
[root@localhost DB]# mysql -uroot -p'Qianfeng@123' -D jspgou < jspgou.sql
[root@localhost DB]# /data/application/tomcat/bin/startup.sh
七、Tomcat多实例配置
多实例(多进程):同一个程序启动多次,分为两种情况:
第一种:一台机器跑多个站点;
第二种:一个机器跑一个站点多个实例,配合负载均衡
复制程序文件
[root@localhost ~]# cd /data/application
[root@localhost application]# cp -r tomcat/ tomcat2
[root@localhost application]# ls
tomcat tomcat2
[root@localhost application]# sed -i 's#8005#8011#' tomcat/conf/server.xml
[root@localhost application]# sed -i 's#8005#8012#;s#8080#8082#' tomcat2/conf/server.xml
[root@localhost application]# sed -i 's#8009#8019#' tomcat/conf/server.xml
[root@localhost application]# sed -i 's#8009#8029#' tomcat2/conf/server.xml
[root@localhost application]# diff tomcat/conf/server.xml tomcat2/conf/server.xml
22c22
< <Server port="8011" shutdown="SHUTDOWN">
---
> <Server port="8012" shutdown="SHUTDOWN">
67c67
< Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
---
> Define a non-SSL/TLS HTTP/1.1 Connector on port 8082
69c69
< <Connector port="8080" protocol="HTTP/1.1"
---
> <Connector port="8082" protocol="HTTP/1.1"
77c77
< port="8080" protocol="HTTP/1.1"
---
> port="8082" protocol="HTTP/1.1"
122c122
< <!-- Define an AJP 1.3 Connector on port 8019 -->
---
> <!-- Define an AJP 1.3 Connector on port 8029 -->
126c126
< port="8019"
---
> port="8029"
[root@localhost tomcat2]# rm -rf webapps
[root@localhost tomcat2]# cp -r /opt/webapps .
[root@localhost application]# cd /data/application/tomca2t/bin/
[root@localhost bin]# vim catalina.sh
CATALINA_HOME=/data/application/tomcat2
[root@localhost application]# /data/application/tomcat2/bin/startup.sh
检查端口是否启动
[root@localhost bin]# ss -nplt |grep java
LISTEN 0 100 [::]:8080 [::]:* users:(("java",pid=1488,fd=52))
LISTEN 0 100 [::]:8082 [::]:* users:(("java",pid=1765,fd=52))
LISTEN 0 1 [::ffff:127.0.0.1]:8005 [::]:* users:(("java",pid=1488,fd=170))
浏览器访问
八、Tomcat反向代理集群
nginx负载均衡器
[root@localhost ~]# echo '
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1 ' > /etc/yum.repos.d/nginx.repo
[root@localhost ~]# yum install yum-utils -y
[root@localhost ~]# yum install nginx -y
配置负载均衡器
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# rm -f default.conf
[root@localhost conf.d]# vim tomcat.conf
upstream testweb {
server 10.36.192.169:8080 weight=1 max_fails=1 fail_timeout=2s;
server 10.36.192.169:8082 weight=1 max_fails=1 fail_timeout=2s;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://testweb;
}
}
重启nginx
[root@localhost conf.d]# systemctl start nginx
浏览器访问