阿里云服务器Centos7搭建java web环境
1.版本信息
JDK >= 1.8 (推荐1.8版本)
nginx >= 1.21.0
Mysql >= 5.7 (推荐5.7版本)
2.jdk安装
2.1 使用yum安装jdk8
yum install java-1.8.0-openjdk-devel.x86_64
2.2 添加到环境变量
vi /etc/profile
# 在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL下添加
# 此处的java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64可能与我不一致需要到对应目录下查找一下
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.352.b08-2.el7_9.x86_64
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$PATH:$JAVA_HOME/bin
source /etc/profile # 变量立即生效
3.nginx安装
3.1 安装依赖包
yum -y install gcc gcc-c++ # nginx 编译时依赖 gcc 环境
yum -y install pcre pcre-devel # 让 nginx 支持重写功能
$yum -y install zlib zlib-devel # zlib 库提供了很多压缩和解压缩的方式,nginx 使用 zlib 对 http 包内容进行 gzip 压缩
yum -y install openssl openssl-devel# 安全套接字层密码库,用于通信加密
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
3.2 下载并解压安装包
#创建一个文件夹
mkdir -p /www/server/nginx
#切换目录
[root@localhost ~]# cd /www/server/nginx
#下载
[root@localhost nginx]# wget http://nginx.org/download/nginx-1.21.0.tar.gz
#解压
[root@localhost nginx]# tar -xvf nginx-1.21.0.tar.gz
3.3 安装nginx
#进入nginx目录
[root@localhost nginx]# cd /www/server/nginx/nginx-1.21.0
#执行命令
[root@localhost nginx-1.21.0]# ./configure --prefix=/usr/local/nginx
#执行make命令
[root@localhost nginx-1.21.0]# make
#执行make install命令
[root@localhost nginx-1.21.0]# make install
3.4 配置nginx.conf
此处我没有修改任何东西
#备份Nginx配置文件
[root@localhost conf]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
#打开配置文件
[root@localhost conf]# vi /usr/local/nginx/conf/nginx.conf
3.5 启动nginx与nginx常用命令
安装完成一般常用命令
cd /usr/local/nginx/sbin 首先进入 sbin 目录 ./nginx 启动 Nginx ./nginx -s stop 停止 Nginx ./nginx -s reload 重新加载 Nginx ./nginx -v 查看 Nginx 版本
[root@localhost nginx]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx
查看nginx是否启动成功
[root@localhost /]# ps -ef | grep nginx root 18642 1 0 11:10 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 18643 18642 0 11:10 ? 00:00:00 nginx: worker process root 19168 1500 0 11:21 pts/0 00:00:00 grep --color=auto nginx
3.6 配置文件修改后,需要指定配置文件进行重启
3.6.1 检测配置文件是否正确
#检测文件是否配置正确
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3.6.2 指定配置文件进行重启
#定配置文件进行重启
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
3.7 创建一个软链接启动nginx
简化3.5的操作
#建立软链接
[root@localhost sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
#启动
[root@localhost sbin]# nginx
#查看nginx版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.21.0
3.8 配置nginx服务并且开机自启动
3.8.1配置服务
#进入目录
[root@localhost system]# cd /usr/lib/systemd/system/
#创建文件
[root@localhost system]# touch nginx.service
#赋予可执行的权限
[root@localhost system]# chmod +x /usr/lib/systemd/system/nginx.service
###编辑内容
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile= /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true[Install]
WantedBy=multi-user.target
内容解释
[Unit] #对服务的说明 Description=nginx - high performance web server #描述服务 After=network.target remote-fs.target nss-lookup.target #描述服务类别[Service] #服务的一些具体运行参数的设置 Type=forking #后台运行的形式 PIDFile= /usr/local/nginx/logs/nginx.pid #PID文件的路径 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf #启动准备 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #启动命令 ExecReload=/usr/local/nginx/sbin/nginx -s reload #重启命令 ExecStop=/usr/local/nginx/sbin/nginx -s stop #停止命令 ExecQuit=/usr/local/nginx/sbin/nginx -s quit #快速停止 PrivateTmp=true #给服务分配临时空间[Install] WantedBy=multi-user.target #服务用户的模式
3.8.2 启动服务
#在启动服务之前,需要先重载systemctl命令
[root@localhost ~]# systemctl daemon-reload
#启动服务或者使用systemctl start nginx
[root@localhost ~]# systemctl start nginx.service
#运行以下命令设置Nginx服务开机自启动
[root@localhost ~]# systemctl enable nginx
3.8.3服务命令
systemctl status nginx #状态
systemctl start nginx #启动
systemctl stop nginx #停止
systemctl restart nginx #重启
4.mysql安装
4.1下载下载 MySQL yum包
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
4.2安装MySQL源
rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
4.3 获取公钥
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
4.4 安装MySQL服务端,需要等待一些时间
yum install -y mysql-community-server
如果没有4.3步骤就会出现一下错误
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hZrkJvu5-1674811800936)(C:\Users\zr\AppData\Roaming\Typora\typora-user-images\image-20230127121355199.png)]
4.5 启动MySQL
systemctl start mysqld.service
4.6 检查是否启动成功
systemctl status mysqld.service
4.7 获取临时密码,MySQL5.7为root用户随机生成了一个密码
grep 'temporary password' /var/log/mysqld.log
通过临时密码登录MySQL,进行修改密码操作
mysql -uroot -p
#因为MySQL的密码规则需要很复杂,我们一般自己设置的不会设置成这样,所以我们全局修改一下
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
#这时候我们就可以自己设置想要的密码了
ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';
#授权其他机器远程登录
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;#刷新配置
FLUSH PRIVILEGES;
4.8 开启开机自启动
systemctl enable mysqld
systemctl daemon-reload
4.9 设置默认字符集为utf8,使其支持中文解决乱码问题,8.0版本默认utf8不用修改
vim /etc/my.cnf
改成如下,然后保存
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html[mysql]
default-character-set=utf8[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
default-storage-engine=INNODB
character_set_server=utf8sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTIONsymbolic-links=0log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
重启使配置生效
service mysqld restart
4.10 卸载MySQL仓库
#一开始的时候我们安装的yum,每次yum操作都会更新一次,耗费时间,我们把他卸载掉
rpm -qa | grep mysql
yum -y remove mysql57-community-release-el7-10.noarch