部署腾讯云轻量应用服务器

部署腾讯云轻量应用服务器

买了新的腾讯云轻量应用服务器,记录一下部署服务器的过程。

部署系统

我选择了部署OpenCloudOS系统。

切换shell

bash
1
2
3
4
5
6
7
8
9
10
11
12
# 查看当前shell
echo $SHELL

# 列出所有的shell
cat /etc/shells

# 查看shell安装路径
which bash
which zsh

# 切换shell
chsh -s /usr/bin/zsh

最后重启终端即可。

安装tldr

安装tldr方便查询命令。

bash
1
sudo yum install -y tldr

安装neofetch

安装neofetch查看系统信息。

bash
1
sudo yum install -y neofetch

安装bat

安装bat替代cat:

bash
1
2
3
wget -c http://repo.openfusion.net/centos7-x86_64/bat-0.7.0-1.of.el7.x86_64.rpm

yum install -y bat-0.7.0-1.of.el7.x86_64.rpm

安装Git

bash
1
sudo yum install -y git

安装nvm

使用国内源安装nvm:https://gitee.com/RubyMetric/nvm-cn/

bash
1
2
3
4
curl -o- https://gitee.com/RubyMetric/nvm-cn/raw/main/install.sh | bash

chmod +x ~/.nvm/nvm.sh
source ~/.bashrc

npm换源:https://gitee.com/RubyMetric/chsrc

nvm安装nodejs

nvm命令:

bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 查看已安装的nodejs
nvm list
nvm ls

# 查看远端可以安装的nodejs
nvm ls-remote

# 安装nodejs
nvm install [version_name]

# 切换nodejs版本
nvm use system
nvm use [version_name]

# 更新nvm
nvm-update

这里安装了20的LTS版本:

bash
1
2
3
nvm install v20.13.1 

node -v

安装cnpm:https://npmmirror.com/

bash
1
2
3
npm install -g cnpm --registry=https://registry.npmmirror.com

cnpm install [package]

安装PM2

PM2: https://pm2.keymetrics.io/

bash
1
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
45
46
47
48
49
50
51
52
53
54
55
56
57
# 安装PM2
npm install pm2 -g

# 启动一个进程
pm2 start app.js

# 启动多个进程,进程个数应设置为CPU核心数
pm2 start app.js -i 4

# 启动一个进程,并命名该进程
pm2 start app.js --name app_name

# 监控已启动的进程
pm2 monit

# 查看进程的日志
pm2 logs app.js

# 查看所有进程的日志
pm2 logs

# 查看进程的详情
pm2 show app.js

# 列出所有的进程
pm2 list

# 停止进程
pm2 stop app.js

# 停止所有进程
pm2 stop all

# 杀掉所有进程
pm2 kill

# 重启进程
pm2 restart app.js

# 重启所有进程
pm2 restart all

# 重载进程
pm2 reload app.js

# 重载所有进程
pm2 reload all

# 删除进程
pm2 delete app.js

# 删除所有进程
pm2 delete all

# 开机启动
pm2 startup
pm2 save

安装python

bash
1
2
# 安装3.9
sudo yum install -y python39

安装nginx

bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 安装nginx
sudo yum install -y nginx

# 查看nginx状态
sudo systemctl status nginx

# 启动nginx
sudo systemctl start nginx

# 停止nginx
sudo systemctl stop nginx

# 重启nginx
sudo systemctl restart nginx

# 系统启动时启动nginx
sudo systemctl enable nginx

编辑nginx配置文件:/etc/nginx/nginx.conf

conf
1
2
3
4
5
6
7
8
9
10
server {
listen 80; # 端口
server_name www.baidu.com; # 域名或者ip
root /www/test; # 代码路径
include /etc/nginx/default.d/*.conf;
location / {
root /www/test;
index index.jsp index.html index.htm;
}
}

修改完配置文件后需要重新载入配置文件:

bash
1
sudo systemctl reload nginx

然后浏览器打开ip就发现nginx服务启动成功了。

最后再将前端项目通过SCP、SFTP等工具上传到/www/test下即可通过ip访问到页面。

bash
1
scp -r /path/to/your/local/vue-app/dist username@your_server_ip:/path/to/your/server/vue-app/

实际开发中一般通过CI/CD将代码部署到服务器上。

安装caddy

bash
1
2
3
4
5
# 安装
sudo yum install -y caddy

# 验证
caddy version

安装htop

bash
1
sudo yum install -y htop

配置防火墙规则

这里配置了常用的规则。

配置防火墙规则

然后设置服务器实例套用规则。

安装mysql-server

bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 安装server
sudo yum install -y mysql-server

# 启动server
sudo systemctl start mysqld

# 停止server
sudo systemctl stop mysqld

# 重启server
sudo systemctl restart mysqld

# 启动系统时自动启动mysql-server
sudo systemctl enable mysqld

# 运行安全安装向导
mysql_secure_installation

# 登录mysql
mysql -u root -p

安全安装向导:

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 #选择密码强度 ##上面选择不安装密码强度验证组件布显示这个
Please set the password for root here.

New password:   #设置root密码

Re-enter new password:  #确认root密码

Estimated strength of the password: 90 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y  #是否确定使用上面的密码
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  #是否删除匿名用户?
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) : n #是否禁止root远程登录

... skipping.
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 #是否删除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! 

mysql基本操作:

mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
# 列出所有的数据库
SHOW DATABASES;

# 选择数据库
USE database_name;

# 列出数据库中所有的表
SHOW TABLES;

# 退出
EXIT;
QUIT;
CTRL+D

安装java

bash
1
2
3
4
5
6
7
8
9
10
# 安装jdk
sudo yum install -y java-11-openjdk

# 验证
java --version

# 配置环境变量
echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.22.0.7-2.oc8.x86_64' >> ~/.bashrc
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> ~/.bashrc
source ~/.bashrc

安装Docker

bash
1
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
# 卸载已存在的docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

# 安装yum-utils
sudo yum install -y yum-utils

# 配置yum源
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 安装docker相关包
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

# 启动docker
sudo systemctl start docker

# 开机启动
sudo systemctl enable docker

# 运行容器
sudo docker run hello-world

购买域名

购买域名后添加DNS解析记录:

typecasting.icu域名解析

域名备案要提交的材料越来越多,各种实名认证、签署各种承诺书、还需要录制承诺视频。

妈的实在是太恶心了。

评论