Centos 7在线安装JDK1.8+Tomcat+MySQL8.0+Nginx
一、安装JDK
注: 以下命令环境在Xshell中进行。
温馨提示: 在线安装方式需要有网速的前提,有的软件下载需要大量时间。
1、查询出系统自带的OpenJDK及版本
rpm -qa | grep jdk
2、如果显示已安装openjdk则对其进行卸载
#卸载
rpm -e --nodeps + ‘要卸载的jdk全名’。
#检查系统自带的OpenJDK是否卸载成功
rpm -qa | grep jdk
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208104658835-1123149964.png)
3、检测可安装JDK版本
yum search java | grep jdk
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208104719184-1376761973.png)
4、安装JDK
#选择一款JDK版本(以1.8为例)
yum install -y java-1.8.0-openjdk.x86_64
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208104734847-377389847.png)
5、配置环境变量
java默认安装路径/usr/lib/jvm/
;
加入环境变量配置,在/etc/profile
配置文件中加入 java 环境变量:
#进入/etc/profile 配置文件
vim /etc/profile
#java 环境变量内容:
#java环境变量
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin
#JAVA_HOME后面内容根据实际情况确定,每个人的最后的版本不同
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208104803913-1586268461.png)
6、使配置文件生效
source /etc/profile
7、验证是否安装成功
java -version
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208104820947-1033382310.png)
二、安装Tomcat
1、查看安装包信息
yum info tomcat
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208104836569-1397954123.png)
2、安装
yum -y install tomcat
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208104853442-466337495.png)
3、查看是否安装成功
rpm -q tomcat
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208104908735-244278611.png)
4、配置环境变量
tomcat 默认安装路径/usr/share/tomcat/
;
加入环境变量配置,在/etc/profile
配置文件中加入 Tomcat 环境变量:
#进入/etc/profile 配置文件
vim /etc/profile
#Tomcat 环境变量内容:
#tomcat环境变量
COMCAT_BASE=/usr/share/tomcat
COMCAT_HOME=/usr/share/tomcat
export JAVA_HOME PATH CLASSPATH COMCAT_BASE COMCAT_HOME
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208104956950-2093889118.png)
5、使配置文件生效
source /etc/profile
6、查看是否生效
echo $COMCAT_BASE
echo $COMCAT_HOME
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105014980-1787954013.png)
7、启动 tomcat
systemctl start tomcat.service
8、查看是否启动
systemctl status tomcat
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105031860-1980690840.png)
如果想运行一个 tomcat 应用程序,可以将它放在 /usr/share/tomcat/webapps
的目录,配置 tomcat,并重新启动 tomcat 服务。
9、配置端口
如果是在VM虚拟机模拟,那么得输入命令手动开启8080端口了,因为Centos系统默认开放的端口只有:21和22。需要将8080端口号加入配置,执行以下代码可解决:
#查看防火墙状态
systemctl status firewalld
#执行命令来开启8080端口
firewall-cmd --permanent --zone=public --add-port=8080/tcp
#重启防火墙
firewall-cmd --reload
#验证开启的8080端口是否生效
firewall-cmd --zone=public --query-port=8080/tcp
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105048466-1912188535.png)
10、解决FirewallD警告问题
vi /etc/firewalld/firewalld.conf
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105107866-1273024085.png)
重启tomcat,在检查启动状态。
#重启tomcat:
systemctl restart tomcat
#查看tomcat状态:
systemctl status tomcat
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105131009-936362382.png)
11、尝试解决打不开8080的问题
- 下载依赖包
#下载依赖包
yum install tomcat-webapps tomcat-admin-webapps
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105208374-1810412151.png)
- 同时关闭防火墙
#关闭防火墙
systemctl stop firewalld
#查看防火墙状态
systemctl status firewalld
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105245900-571254613.png)
- 再次尝试打开8080,ip: +8080
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105305246-248557451.png)
12、最后配置Tomcat的自启动
#Tomcat的自启动
systemctl enable tomcat.service
#系统重启
reboot
#浏览器打开
ip: +8080
三、安装MySQL
1、检查是否安装过MySQL
#检查是否安装过
rpm -qa | grep -i mysql
#卸载之前的安装
rpm -e --nodeps 软件名 //强力删除,对相关依赖的文件也进行强力删除
2、卸载系统自带的Mariadb
#查询出已安装的mariadb
rpm -qa| grep mariadb
#卸载 , 文件名为使用rpm -qa|grep mariadb 命令查出的所有文件
rpm -e --nodeps 文件名
3、查看masql的版本
yum repolist all | grep mysql
4、安装MySQL8.0
CentOS7 开始官方库把默认的mysql换成了开源的mariadb了。如果要用yum方式安装mysql,必须先下载安装MySQL源才行。
#下载mysql8.0的rpm仓库源
wget https://repo.mysql.com/mysql80-community-release-el7.rpm
#安装mysql8.0源
yum -y install ./mysql80-community-release-el7.rpm
#生成yum索引缓存,并查看目录大小
yum makecache fast
#查看正在使用的MySQL repo
yum repolist all|grep mysql
#安装MySQL8.0
yum install -y mysql-community-common mysql-community-libs mysql-community-libs-compat mysql-community-client mysql-community-server
#设置 mysql 开机启动
systemctl enable mysqld
#启动mysql
systemctl start mysqld
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105330228-979921629.png)
5、查看初始化随机生成的root密码
cat /var/log/mysqld.log | grep password
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105352629-1985713341.png)
6、安全设置
mysql_secure_installation
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105411574-1224216359.png)
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105432735-325701551.png)
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105452126-1417430830.png)
7、mysql登录
#随机密码登录
mysql -uroot -p
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105512218-10815032.png)
8、重置密码(Mysql8.0+有变化)
#先把root的旧密码置空
use mysql;
#备注:Mysql5.7+ password字段 已改成 authentication_string字段
update user set authentication_string='' where user='root';
#重置成新密码
#备注:Mysql8.0修改密码方式已有变化(此处是个坑,需要注意)
#Mysql8.0之前;
update user set password=password('root') where user='root';
#Mysql8.0之后;
alter user 'root'@'localhost' identified by 'root';
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105535294-167242703.png)
9、修改密码策略配置
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 错误原因,版本8.0 默认设置密码不能相同于用户名。
validate_password.check_user_name | ON // 校验密码不能等同于用户名
validate_password.policy | LOW // 设置低级别策略
validate_password.length | 4 // 设置最低长度为4
#显示密码策略
SHOW VARIABLES LIKE 'validate_password%';
#校验密码不能等同于用户名
set global validate_password.check_user_name=OFF;
#设置最低长度为4
set global validate_password.length=4;
#校验设置低级别策略
set global validate_password.policy=LOW;
#显示密码策略
SHOW VARIABLES LIKE 'validate_password%';
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105601080-60471940.png)
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105626370-621261568.png)
10、重新修改密码
#设置完密码策略后重新输入修改命令,更改后的密码为123456
alter user 'root'@'localhost' identified by '123456';
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105645496-675592858.png)
11、退出后使用新密码再登录mysql
#退出命令行
mysql> quit;
Bye
#新密码登录
mysql -uroot -p123456
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105703431-1683797167.png)
12、如何停止、重启和查看mysql服务
#停止服务
systemctl stop mysqld
#重启服务
systemctl restart mysqld
#查看mysql服务
systemctl status mysqld
四、安装Nginx
1、安装必备组件
yum install yum-utils
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105721671-597380432.png)
2、设置 yum 存储库
#新建nginx.repo
vim /etc/yum.repos.d/nginx.repo
#nginx.repo内容
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
3、使用主线nginx包
yum-config-manager --enable nginx-mainline
4、安装 nginx
yum install -y nginx
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105743595-314274022.png)
5、启动 nginx
systemctl start nginx.service
6、查看nginx是否启动
systemctl status nginx
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105800969-749675272.png)
7、开启80端口号
如果是在VM虚拟机模拟,那么得输入命令手动开启80端口了,因为Centos系统默认开放的端口只有:21和22。需要将80端口号加入配置,执行以下代码可解决:
#查看防火墙状态
systemctl status firewalld
#执行命令来开启80端口
firewall-cmd --permanent --zone=public --add-port=80/tcp
#重启防火墙
firewall-cmd --reload
#验证开启的8080端口是否生效
firewall-cmd --zone=public --query-port=80/tcp
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105818181-370478919.png)
最后,浏览器中直接输入服务器的ip地址即可。
![](https://img2023.cnblogs.com/blog/2301705/202302/2301705-20230208105834135-1009593247.png)
8、设置 nginx 开机自启动
systemctl enable nginx.service
9、nginx 配置信息
网站文件存放默认位置(Welcome to nginx 页面) | /usr/share/nginx/html |
网站默认站点配置 | /etc/nginx/conf.d/default.conf |
自定义 nginx 站点配置文件存放目录 | /etc/nginx/conf.d/ |
nginx 全局配置文件 | /etc/nginx/nginx.conf |
启动 nginx | service nginx start |
关闭 nginx | service nginx stop |
重启 nginx | service nginx reload |