在Linux上部署JavaWeb应用
# 环境准备
# JDK
# 安装
准备安装包:jdk-8u221-linux-x64.rpm
# 查看是否已经安装jdk
rpm -qa | grep jdk
# 卸载默认安装的jdk
rpm -e --nodeps jdk名称
rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.261-2.6.22.2.el7_8.x86_64
rpm -e --nodeps copy-jdk-configs-3.3-10.el7_5.noarch
rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.262.b10-1.el7.x86_64
rpm -e --nodeps java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64
rpm -e --nodeps java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64
# 将安装包jdk-8u221-linux-x64.rpm上传至Linux
rpm -ivh jdk-8u221-linux-x64.rpm
2
3
4
5
6
7
8
9
10
11
12
13
# 配置
配置环境变量
vi /etc/profile
添加以下内容
JAVA_HOME=/usr/java/jdk1.8.0_221-amd64
CLASSPATH=%JAVA_HOME%/lib:%JAVA_HOME%/jre/lib
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export PATH CLASSPATH JAVA_HOME
2
3
4
使环境变量生效
source /etc/profile
查看jdk版本
java -version
# Tomcat
# 安装
准备安装包:apache-tomcat-9.0.34.tar.gz
# 解压
tar -zxvf apache-tomcat-9.0.34.tar.gz
2
# 配置
<Server port="8005" shutdown="SHUTDOWN">
<Service name="Catalina">
<!-- 修改默认端口号 -->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- 配置虚拟目录 -->
<Context path="/files" docBase="/opt/myweb/upload" reloadable="true" debug="0" crossContext="true"/>
</Host>
</Engine>
</Service>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
同一台机器部署多个Tomcat
1、复制多个tomcat
# 将 apache-tomcat-9.0.34 解压目录复制多份
cp -r apache-tomcat-9.0.34 目标位置/目标文件夹名
cp -r apache-tomcat-9.0.34 tomcat-8081
cp -r apache-tomcat-9.0.34 tomcat-8082
...
2
3
4
5
2、配置环境变量
vi /etc/profile
添加以下内容
# 第一个Tomcat,端口号:8081
export CATALINA_BASE_8081=/usr/local/tomcat/tomcat-8081
export CATALINA_HOME_8081=/usr/local/tomcat/tomcat-8081
export TOMCAT_HOME_8081=/usr/local/tomcat/tomcat-8081
# 第二个Tomcat,端口号:8082
export CATALINA_BASE_8082=/usr/local/tomcat/tomcat-8082
export CATALINA_HOME_8082=/usr/local/tomcat/tomcat-8082
export TOMCAT_HOME_8082=/usr/local/tomcat/tomcat-8082
# 第N个
...
2
3
4
5
6
7
8
9
10
11
12
使环境变量生效
source /etc/profile
3、对每个tomcat都修改这三个文件bin/startup.sh、bin/shutdown.sh 、bin/catalina.sh
JAVA_HOME=/usr/java/jdk1.8.0_221-amd64
CLASSPATH=%JAVA_HOME%/lib:%JAVA_HOME%/jre/lib
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export PATH CLASSPATH JAVA_HOME
export CATALINA_BASE=$CATALINA_BASE_8081 # 按需修改
export CATALINA_HOME=$CATALINA_HOME_8081 # 按需修改
2
3
4
5
6
# 使用
cd bin
# 启动
./startup.sh
# 关闭
./shutdown.sh
cd logs
# 查看日志
tail -f catalina.out
2
3
4
5
6
7
8
9
10
11
12
# MySQL
# 安装
准备安装包:mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar
1、查看是否有mariadb,如果有则卸载自带的 mariadb
# 查看是否有mariadb
rpm -qa | grep mariadb
# 卸载默认的mariadb
rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
2
3
4
5
2、解压
# 将文件解压到指定目录
tar -xvf mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar -C /opt/mysql
2
3、安装 common、libs、client、server
cd /opt/mysql
rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-client-8.0.20-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm --nodeps --force
2
3
4
5
4、初始化MySQL
mysqld --initialize
# 解决:mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
yum install -y libaio
2
3
4
5、授权防火墙
chown mysql:mysql /var/lib/mysql -R;
systemctl start mysqld.service;
systemctl enable mysqld;
2
3
6、查看数据库的初始化密码
cat /var/log/mysqld.log | grep password
7、登录MySQL
mysql -u root -p
8、修改初始化密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
9、开启远程访问
create user 'root'@'%' identified with mysql_native_password by 'root';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
2
3
10、MySQL 安装默认使用美国的时区,北京时间比美国晚 8 小时
set global time_zone='+8:00';
11、开放3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
systemctl restart firewalld.service
firewall-cmd --reload
2
3
12、重新登录MySQL
# Redis
# 安装
准备安装包:redis-6.0.9.tar.gz
# 解压文件
# -z或--gzip或--ungzip 通过gzip指令处理备份文件。
# -x或--extract或--get 从备份文件中还原文件。
# -v或--verbose 显示指令执行过程。
# -f<备份文件>或--file=<备份文件> 指定备份文件。
tar -zxvf redis-6.0.9.tar.gz -C /opt/redis
# 进入Redis解压目录
cd redis-6.0.9
# 编译
make
# 解决:“cc:命令未找到” 报错。yum方式安装需要能上网
yum -y install gcc
yum -y install gcc-c++
# 解决:jemalloc/jemalloc.h:没有那个文件或目录”
make MALLOC=libc
# 解决:‘struct redisServer’没有名为‘maxmemory’的成员
# 在阿里云,centos-release-scl 可能安装不成功,解决方法见后
yum install centos-release-scl -y
yum install devtoolset-9-gcc
scl enable devtoolset-9 bash
# 安装
make install
# 查看安装位置
whereis redis-cli
whereis redis-server
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
在阿里云上,运行 yum install centos-release-scl,出现 No package centos-release-scl available.解决:
rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl-rh/2/3.el7.centos/noarch/centos-release-scl-rh-2-3.el7.centos.noarch.rpm
rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl/2/3.el7.centos/noarch/centos-release-scl-2-3.el7.centos.noarch.rpm
2
3
# 配置
cp redis.conf /opt/redis/redis.conf
cd /opt/redis
vi redis.conf
2
3
以守护进程方式启动Redis
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
2
3
# 使用
# 指定配置文件启动Redis服务器
/usr/local/bin/redis-server /opt/redis/redis.conf
# 进入Redis客户端
/usr/local/bin/redis-cli -p 6379
# 退出Redis客户端
127.0.0.1:6379> shutdown
not connected> exit
2
3
4
5
6
7
8
9
keys *
set key value
get key
del key
# 查看 key 还有多少秒过期,-1表示永不过期
ttl key
# 清空当前库
flushdb
# 清空所有库
flushall
2
3
4
5
6
7
8
9
10
11
12
13
14
# Nginx
# 安装
准备安装包:
pcre-8.37.tar.gz
nginx-1.12.2.tar.gz
1、安装pcre
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。
tar -xvf pcre-8.37.tar.gz -C /opt/pcre
./configure
make
make install
pcre-config --version
2
3
4
5
2、安装 openssl 、zlib 、 gcc 依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
3、安装Nginx
tar -xvf nginx-1.12.2.tar.gz -C /opt/nginx
./configure
make
# 解决:cc1: all warnings being treated as errors
cd objs
vim Makefile
# 将 CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused -Werror -g
# 的 -Werror -g 去掉,保存退出,重新编译
make install
# 安装路径
/usr/local/nginx
2
3
4
5
6
7
8
9
10
11
12
13
14
# 配置
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 192.168.66.131;
# 反向代理,防止java代码获取的是127.0.0.1
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# 解决:中文乱码
charset utf-8;
# 反向代理-文件系统
location ~ /files/ {
proxy_pass http://127.0.0.1:8081;
}
# 反向代理-HRAPI
location ~ /hrapi/ {
proxy_pass http://127.0.0.1:8082;
}
# 反向代理-UUAP
location ~ /uuap/ {
proxy_pass http://127.0.0.1:8083;
}
# 动静分离-UUAP(需要验证)
# location ~ /uuap/*.*\.(css|js|gif|jpg|png)$ {
# root /opt/myweb/uuap/static;
# }
# 动静分离-UUAP(需要验证)
location ~ /uuap/*.*\.(css|js|gif|jpg|png)$ {
root html/uuap/static;
}
# 静态资源
location / {
# 自己的静态网站直接放在 /usr/local/nginx/html 目录下,访问时通过目录名访问
root html;
index index.html index.htm;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 使用
# 启动
./nginx
# 停止
./nginx -s stop
# 修改配置后重加载
./nginx -s reload
2
3
4
5
6
7
8
如果出现403错误,vi nginx.conf,将user nobody 改为 root
# 应用部署
# jar启动SpringBoot应用,后台运行,并制定日志输出位置
nohup java -jar uuap.jar --spring.profiles.active=uat >> uuap.txt &
nohup java -jar hr-api.jar --spring.profiles.active=uat >> hr-api.txt &
# 查找应用进程
ps -ef|grep uuap
# 杀死应用进程
kill -9 19144
2
3
4
5
6
7
8
9
idea下springboot打jar、war包及部署外部tomcat (opens new window)