1、挂载云盘
由于加购了一块40G SSD云盘,所以需要先挂载
1.1、连接服务器
本次使用的SecureCRT
1.2、查看云盘
fdisk -l
Dist /dev/vdb即需要挂载的硬盘
1.3、格式化云盘
sudo mkfs.ext4 /dev/vdb
1.4、挂载云盘
mount /dev/vdb /home
1.5、查看挂载状态
mount
1.6、配置开机自动挂载
vim /etc/fstab
增加配置,保存
/dev/vdb /home ext4 defaults 0 0
1.7、重启检查
重启系统
init 6
查看硬盘
df -h
/dev/vdb已成功挂载
2、安装Nginx
2.1、添加Nginx源
cd /etc/yum.repos.d/
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.2、安装Nginx
安装依赖
yum install -y gcc pcre-devel openssl-devel zlib-devel
安装Nginx
yum install -y nginx
修改监听端口
vim /etc/nginx/conf.d/default.conf
3、安装MySQL v8.0
3.1、添加MySQL源
cd /etc/yum.repos.d/
rpm -ivh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
3.2、安装MySQL
yum -y install mysql-community-server
3.3、查询安装情况
mysql -V
3.4、启动MySQL服务
systemctl start mysqld
3.5、设置开机启动
systemctl enable mysqld
systemctl daemon-reload
3.6、查看数据库密码
cat /var/log/mysqld.log | grep password
3.7、登录MySQL
mysql -uroot -p
3.8、修改登录密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
exit;
3.9、创建用户
create user '用户名'@'%' identified with mysql_native_password by '密码';
grant all privileges on *.* to '用户名'@'%' with grant option;
flush privileges;
exit;
3.10、修改默认编码
vim /etc/my.cnf
在[mysqld]下添加编码配置:
character_set_server=utf8
init_connect=‘SET NAMES utf8’
3.11、重启MySQL服务
systemctl restart mysqld
3.12、导入数据
阿里云开放3306端口,连接MySQL,创建数据库
4、部署前端项目
前端vue项目导入/usr/share/nginx/html目录
cd /usr/share/nginx/html
启动服务:
systemctl start nginx.service
关闭服务:
systemctl stop nginx.service
重启服务:
systemctl restart nginx.service
查看服务:
ps aux|grep nginx
5、部署后端接口
SpringBoot项目打成jar包,上传至服务器
修改application.yml配置并上传,与jar包同级
创建服务脚本
case "$1" in
start)
nohup java -jar test-api.jar >test-api.txt &
echo $! > /var/run/test-api.pid
;;
stop)
if [ ! -f /var/run/test-api.pid ]; then
echo "test-api: not running"
exit 1
fi
kill -TERM `cat /var/run/test-api.pid`
;;
reload)
if [ ! -f /var/run/test-api.pid ]; then
echo "test-api: not running"
exit 1
fi
kill -HUP `cat /var/run/test-api.pid`
;;
*)
echo "Usage: /etc/init.d {start|stop|reload}"
exit 1
;;
esac
exit 0
启动服务:
sh test.sh start
关闭服务:
sh test.sh stop
重启服务:
sh test.sh reload
6、配置反向代理
6.1、阿里云控制台配置域名解析
ps:@表示顶级域名(Nginx重定向至www),www是前端模板域名(考虑用户兼容性问题,未使用vue),cms是前端vue域名,cms-api是后端接口域名
6.2、Nginx配置反向代理
vim /etc/nginx/conf.d/default.conf
ps:cms-api为api接口域名,涉及跨域请求,接口程序/Nginx需任选一处进行跨域处理,本示例在程序中进行了处理
upstream cms-api {
server 127.0.0.1:8081 max_fails=1 fail_timeout=60;
}
upstream www {
server 127.0.0.1:8082 max_fails=1 fail_timeout=60;
}
server {
listen 80;
if ($host = xxx.cn) {
rewrite /(.*) http://www.xxx.cn/$1 permanent;
}
}
server {
listen 80;
server_name cms.xxx.cn;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server {
listen 80;
server_name cms-api.xxx.cn;
location / {
proxy_pass http://cms-api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name www.xxx.cn;
location / {
proxy_pass http://www;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}