目录
一.云服务器
1.购买云服务器
这里以阿里云为例:轻量应用服务器_web服务器_个人建站_弹性计算-阿里云
因为我们只是简单的部署前后端和数据库,所以这里我们选择2核2G的云服务器就完全够用了。
一般类似阿里,百度,华为,腾讯都是有云服务器的,可以看一看有一些是有新用户优惠的,非常便宜。
对云服务器进行配置后进行购买,支付完成后,系统会自动配置属于我们的云服务器。
对于新的服务器要重置一下密码:
开放服务器端口:
云服务器的基本信息:
二.使用Xshell和Xftp连接云服务器
1.安装Xshell并连接云服务器
点击上面的官网连接,然后下载Xshell:
注册个人用户:
首次进入Xshell需要创建一个新的会话:
创建完成后:
点击刚才创建好的会话:
这里输入在云服务器上创建好的用户名就可以,如果没有就先进入root用户然后在root用户下创建一个新的用户完成配置。
有的时候在选定用户后会直接连接失败,此时只需要重新选择一下刚才创建好的会话就好了:
如果刚才修改的密码:
连接成功
2.安装Xftp
此时就可以通过拖动实现本地主机和远程云端服务器文件传输了。
三.安装MySQL
1.查找安装包
apt list | grep "mysql-serve"
2.安装MySQL
sudo apt install mysql-server
root@hcss-ecs-0bb1:~# apt list |grep "mysql-server" #查找mysql的安装包
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
default-mysql-server-core/focal,focal 1.0.5ubuntu2 all
default-mysql-server/focal,focal 1.0.5ubuntu2 all
mysql-server-8.0/focal-updates,focal-security 8.0.39-0ubuntu0.20.04.1 amd64
mysql-server-8.0/focal-updates,focal-security 8.0.39-0ubuntu0.20.04.1 i386
mysql-server-core-8.0/focal-updates,focal-security 8.0.39-0ubuntu0.20.04.1
amd64
mysql-server-core-8.0/focal-updates,focal-security 8.0.39-0ubuntu0.20.04.1 i386
mysql-server/focal-updates,focal-updates,focal-security,focal-security 8.0.39-
0ubuntu0.20.04.1 all
root@hcss-ecs-0bb1:~# sudo apt install mysql-server #输⼊安装命令
Reading package lists... Done
Building dependency tree
Reading state information... Done
//...
Need to get 36.8 MB of archives.
After this operation, 318 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y #输⼊Y确认
Get:1 http://repo.huaweicloud.com/ubuntu focal/main amd64 mysql-common all
5.8+1.0.5ubuntu2 [7,496 B]
Get:2 http://repo.huaweicloud.com/ubuntu focal-updates/main amd64 mysql-clientcore-8.0 amd64 8.0.39-0ubuntu0.20.04.1 [5,088 kB]
Get:3 http://repo.huaweicloud.com/ubuntu focal-updates/main amd64 mysql-client-
8.0 amd64 8.0.39-0ubuntu0.20.04.1 [22.0 kB]
//...
emitting matrix : 100% |###########################################|
done!
1
3.查看MySQL状态
安装好后,检查数据库的运行状态:
sudo systemctl status mysql
4.MySQL安装安全设置
默认的MySQL设置是不安全的,MySQL安装提供了一个安全脚本,用于解决不太安全的默认选项。
sudo mysql_secure_installation
root@hcss-ecs-0bb1:~# sudo mysql_secure_installation #安装安全设置
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: Y #是否设置验证密码组件
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary
file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 #设置密码强度
Skipping password set for root as authentication with auth_socket is used by
default.
If you would like to use password authentication instead, this can be done
with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-userpassword-management for more information.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #默认情
况下,MySQL安装有⼀个匿名⽤⼾, 允许任何⼈登录MySQL. 是否删除匿名⽤⼾?
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #仅
应允许root从'localhost'连接
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for
No) : Y #默认情况下, MySQL带有⼀个test数据库, 是否删除?
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是
否现在加载配置, 使刚才的修改⽣效?
Success.
All done!
5.连接数据库
没有设置过密码的新数据库可以这样连接
sudo mysql
如果数据库被设置密码了系统会报错
6.给数据库设置密码
进入数据库后:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
7.将准备好的sql文件部署到数据库中
这里使用Navicat是方式
在服务器中创建一个data路径
进入这个文件夹中,使用xftp将刚才准备好sql文件导入到服务器中:
然后在data中使用mysql -uroot -p 输入密码进入数据库,在数据库中创建所需要的数据库,注意Linux中是严格区分大小写的,在后端中设置的是大写的数据库就创建大写的,设置是小写的就创建小写的。
创建好数据库后,进入数据库,执行对于的数据库文件:
source /data/XXX.sql;
执行完成后在服务器中就将数据库部署好了。
四.在云服务器上搭建Java部署环境并部署后端项目
1.更新软件数据包
sudo apt-get update
2.安装软件包
首先检查系统是否已经下载好了,项目所需要的jdk
apt list | grep "jdk"
如果没有就安装所需要的jdk,这里安装的是OpenJDK,OpenJDK是一个开源的JDK版本,和Oracle官方的JDK略有区别,此处就试用OpenJDK就可以。安装Oracle会非常麻烦。
sudo apt install openjdk-11-jdk
安装完成后检查jdk是否安装成功了:
java --version
3.将后端项目打包
检查后端项目配置文件的配置是否正确:
成功后会在target文件中得到被打包项目的包
然后将这个jar包放到刚才创建好的data文件夹中:
4.启动后端项目
启动后端项目:
nohup java -jar projectmgt.jar > logs.out 2>&1 &
查看日志:
tail -f logs.out
五.在云服务器上搭建前端运行环境并且部署前端项目
1.将前端项目打包,
这里使用的前端编辑器是WebStorm:
2. 将打包好的包放到data文件夹中:
3. 安装nginx:
# 更新软件包索引
sudo apt update
# 安装 Nginx
sudo apt install -y nginx
# 启动 Nginx
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 查看运行状态
sudo systemctl status nginx
4.配置nginx:
vim /etc/nginx/nginx.conf
在http中配置如下:
server {
listen 8090;
server_name localhost;
root /data/projectManager;
index index.html;
# 前端 vue 路由处理
location / {
try_files $uri $uri/ /index.html;
}
# 反向代理后端接口
location /projectmgt/ {
proxy_pass http://127.0.0.1:19010/projectmgt/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
然后前端项目就部署成功了。