本文主要介绍云服务器的开通以及云服务环境搭建,然后将项目打包,将打包后的代码部署到云服务环境中
记得加上安全组
后续再补!!!
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
// or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
[root@VM-0-2-centos ~]# ll .nvm/
总用量 140
-rw-r--r-- 1 root root 2201 8月 22 01:16 bash_completion
-rwxr-xr-x 1 root root 344 8月 22 01:19 nvm-exec
-rw-r--r-- 1 root root 134284 8月 22 01:16 nvm.sh
[root@VM-0-2-centos ~]#
[root@VM-0-2-centos ~]# ll .bash_profile
-rw-r--r--. 1 root root 176 5月 11 2019 .bash_profile
[root@VM-0-2-centos ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
~
".bash_profile" 12L, 176C
[root@VM-0-2-centos ~]# vim .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
".bashrc" 16L, 373C
[root@VM-0-2-centos ~]# source .bashrc
[root@VM-0-2-centos ~]# nvm -v
0.38.0
[root@VM-0-2-centos ~]# yum -y
[root@VM-0-2-centos ~]# nvm install node
Downloading and installing node v16.7.0...
Downloading https://nodejs.org/dist/v16.7.0/node-v16.7.0-linux-x64.tar.xz...
################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.7.0 (npm v7.20.3)
Creating default alias: default -> node (-> v16.7.0)
[root@VM-0-2-centos ~]#
[root@VM-0-2-centos ~]# node -v
v16.7.0
[root@VM-0-2-centos ~]# npm -v
7.20.3
[root@VM-0-2-centos ~]#
npm install -g cnpm --registry=https://registry.npm.taobao.org
[root@VM-0-2-centos ~]# which cnpm
/root/.nvm/versions/node/v16.7.0/bin/cnpm
[root@VM-0-2-centos ~]#
[root@VM-0-2-centos ~]# cnpm -v
cnpm@7.0.0 (/root/.nvm/versions/node/v16.7.0/lib/node_modules/cnpm/lib/parse_argv.js)
npm@6.14.14 (/root/.nvm/versions/node/v16.7.0/lib/node_modules/cnpm/node_modules/npm/lib/npm.js)
node@16.7.0 (/root/.nvm/versions/node/v16.7.0/bin/node)
npminstall@5.0.2 (/root/.nvm/versions/node/v16.7.0/lib/node_modules/cnpm/node_modules/npminstall/lib/index.js)
prefix=/root/.nvm/versions/node/v16.7.0
linux x64 4.18.0-305.3.1.el8.x86_64
registry=https://registry.nlark.com
[root@VM-0-2-centos ~]#
// 安装 pcre 的依赖
yum -y install pcre*
// 安装 openssl 的依赖
yum -y install openssl*
[root@VM-0-2-centos ~]# mkdir nginx
[root@VM-0-2-centos ~]# ll
总用量 4
drwxr-xr-x 2 root root 4096 8月 22 16:11 nginx
[root@VM-0-2-centos ~]# cd /
[root@VM-0-2-centos /]# cd /root
[root@VM-0-2-centos ~]# ll
总用量 4
drwxr-xr-x 2 root root 4096 8月 22 16:11 nginx
[root@VM-0-2-centos ~]#
// 进入 nginx 目录
cd nginx
// 下载 nginx 源码
wget http://nginx.org/download/nginx-1.12.2.tar.gz
::: warning
源码大多都是 c 语言编写的,在做 c 的编译时还要下载 gcc、make等工具
:::
- 解压 nginx 源码
[root@VM-0-2-centos nginx]# ls
nginx-1.12.2.tar.gz
[root@VM-0-2-centos nginx]# tar -zxvf nginx-1.12.2.tar.gz
[root@VM-0-2-centos nginx]# ll
总用量 964
drwxr-xr-x 8 1001 1001 4096 10月 17 2017 nginx-1.12.2
-rw-r--r-- 1 root root 981687 10月 17 2017 nginx-1.12.2.tar.gz
[root@VM-0-2-centos nginx]#
[root@VM-0-2-centos nginx]# cd nginx-1.12.2/
[root@VM-0-2-centos nginx-1.12.2]# ll
总用量 724
drwxr-xr-x 6 1001 1001 4096 8月 22 16:29 auto
-rw-r--r-- 1 1001 1001 278202 10月 17 2017 CHANGES
-rw-r--r-- 1 1001 1001 423948 10月 17 2017 CHANGES.ru
drwxr-xr-x 2 1001 1001 4096 8月 22 16:29 conf
-rwxr-xr-x 1 1001 1001 2481 10月 17 2017 configure
drwxr-xr-x 4 1001 1001 4096 8月 22 16:29 contrib
drwxr-xr-x 2 1001 1001 4096 8月 22 16:29 html
-rw-r--r-- 1 1001 1001 1397 10月 17 2017 LICENSE
drwxr-xr-x 2 1001 1001 4096 8月 22 16:29 man
-rw-r--r-- 1 1001 1001 49 10月 17 2017 README
drwxr-xr-x 9 1001 1001 4096 8月 22 16:29 src
[root@VM-0-2-centos nginx-1.12.2]#
// 执行 configure 文件
./configure
[root@VM-0-2-centos nginx-1.12.2]# make -j4
make[1]: *** [objs/Makefile:777:objs/src/os/unix/ngx_user.o] 错误 1
make[1]: *** 正在等待未完成的任务....
make[1]: 离开目录“/root/nginx/nginx-1.12.2”
make: *** [Makefile:8:build] 错误 2
[root@VM-0-2-centos nginx-1.12.2]# cd src/os/unix
[root@VM-0-2-centos unix]# vi ngx_user.c
#ifdef __GLIBC__
/* work around the glibc bug */
/* cd.current_salt[0] = ~salt[0]; */
#endif
[root@VM-0-2-centos unix]# vi ngx_user.c
[root@VM-0-2-centos unix]# cd ..
[root@VM-0-2-centos os]# cd /root
[root@VM-0-2-centos ~]# cd nginx/
[root@VM-0-2-centos nginx]# ls
nginx-1.12.2 nginx-1.12.2.tar.gz
[root@VM-0-2-centos nginx]# cd nginx-1.12.2/
[root@VM-0-2-centos nginx-1.12.2]# make -j4
-ldl -lpthread -lcrypt -lpcre -lz \
-Wl,-E
make[1]: 离开目录“/root/nginx/nginx-1.12.2”
[root@VM-0-2-centos nginx-1.12.2]#
make install
[root@VM-0-2-centos nginx-1.12.2]# nginx
-bash: nginx: 未找到命令
[root@VM-0-2-centos nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t
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
[root@VM-0-2-centos nginx-1.12.2]#
[root@VM-0-2-centos nginx-1.12.2]# nginx
-bash: nginx: 未找到命令
// 进入 /usr/bin/ 目录下
[root@VM-0-2-centos nginx-1.12.2]# cd /usr/bin/
[root@VM-0-2-centos bin]#
// 制作软连接:前面是实际路径,后面是链接
[root@VM-0-2-centos bin]# ln -s /usr/local/nginx/sbin/nginx nginx
// 链接指向实际路径
[root@VM-0-2-centos bin]# ll nginx
lrwxrwxrwx 1 root root 27 8月 22 17:34 nginx -> /usr/local/nginx/sbin/nginx
[root@VM-0-2-centos bin]#
[root@VM-0-2-centos bin]# cd
[root@VM-0-2-centos ~]# pwd
/root
[root@VM-0-2-centos ~]# nginx
[root@VM-0-2-centos ~]#
[root@VM-0-2-centos ~]# ps -ef|grep nginx
root 3880652 1 0 17:41 ? 00:00:00 nginx: master process nginx
nobody 3880653 3880652 0 17:41 ? 00:00:00 nginx: worker process
root 3986465 558468 0 17:45 pts/0 00:00:00 grep --color=auto nginx
[root@VM-0-2-centos ~]# nginx -s stop
[root@VM-0-2-centos ~]# ps -ef|grep nginx
root 4020953 558468 0 17:46 pts/0 00:00:00 grep --color=auto nginx
[root@VM-0-2-centos ~]#
[root@VM-0-2-centos conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@VM-0-2-centos conf]# vim nginx.conf
// 启动 nginx
[root@VM-0-2-centos front-resources]# nginx
// 停止 nginx
[root@VM-0-2-centos front-resources]# nginx -s stop
[root@VM-0-2-centos front-resources]# nginx
// 对启动中的 nginx 进行重启
[root@VM-0-2-centos front-resources]# nginx -s reload
[root@VM-0-2-centos nginx]# cd /root
[root@VM-0-2-centos ~]# ls
nginx
[root@VM-0-2-centos ~]# cd nginx/
[root@VM-0-2-centos nginx]# touch nginx.conf
[root@VM-0-2-centos nginx]# cd /usr/local/nginx/conf/
[root@VM-0-2-centos conf]# vim nginx.conf
[root@VM-0-2-centos conf]#
// 指定 user 为 root
# 指定 user 为 root
user root;
worker_processes 1;
// 主配置文件改为监听 9000 端口
server {
listen 9000;
server_name localhost;
# }
#}
include /root/nginx/*.conf;
}
-- 插入 --
server {
listen 80; // 监听 80 端口
server_name localhost; // 服务名是主机IP
// 用 139.155.40.16/ 只能访问到 front-resources 目录下的文件
root /root/nginx/front-resources;
# autoindex on; // 是否开启 indexOf
// 用来给 http 的 header 中添加不清理缓存的配置
add_header Cache-Control "no-cache, must-revalidate";
// 匹配所有通过 / 路由 添加跨域的支持
location / {
add_header Access-Control-Allow-Origin *;
}
}
<h1>Hello world</h1>
[root@VM-0-2-centos front-resources]# nginx -t
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
[root@VM-0-2-centos front-resources]#
// git 安装
yum -y install git
// 查看版本
git --version
yum remove git
yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel
// 通过源码编译的方式再安装 git
yum install - y gcc perl-ExtUtils-MakeMaker
yum install -y tcl build-essential tk gettext
wget https://github.com/git/git/archive/v2.9.2.tar.gz
tar -zxvf v2.9.2.tar.gz
cd git-2.9.2
// 编译时指定安装后的可执行文件位于哪
make prefix=/usr/local/git all
make prefix=/usr/local/git install
// 进入 /usr/bin
cd /usr/bin
// 制作软连接
ln -s /usr/local/git/bin/git git
git --version
mkdir back-resources
// 通过 ssh-keygen 生成免密登录的密钥
// 通过 -C 属性去填写 git 仓库的用户名
ssh-keygen -t rsa -C “248342961@qq.com”
// 点击三次回车将密钥生成
// 将密钥打印在终端上面
cat ~/.ssh/id_rsa.pub
// 复制密钥后进入 Gitee 仓库 “我的设置”添加密钥
// 将密钥粘贴到密钥内容上
git clone git@gitee.com:lgk2021/xiaomudushu_back-management.git
// 进入要更新源码的目录下,执行
git pull
// 注意:我们要安装的是 MySQL 服务端
// 如果输入的是 mysql,则只会安装客户端
yum install mysql-server
完毕!
[root@VM-0-2-centos ~]# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@VM-0-2-centos ~]#
[root@VM-0-2-centos ~]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL 8.0 database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@VM-0-2-centos ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
[root@VM-0-2-centos ~]#
[root@VM-0-2-centos ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21 Source distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
cat /var/log/mysqld.log |grep password
mysql -u root -p
show databases;
alter user 'root'@'localhost' identified by '示例:lgk123123.';
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
create database book;
drop database db_name;
use book;
show tables;
CREATE TABLE tb_test(
id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(60) NOT NULL,
score TINYINT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY(id)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS tb_name;
// 使用数据库
use mysql;
// % 是通配符,表示匹配所有IP
// 使用 mysql_native_password 插件进行验证(与 v8.0 之前的不同了)
// 密码可以使用原来的,也可以设置简单的 123456
create user 'root'@'%' identified with mysql_native_password by '123456';
// 赋予所有权限给 'root'@'%'
grant all privileges on *.* to 'root'@'%';
// 使设置生效
flush privileges;
// 切换目录
cd /root/back-resources
// 创建脚本文件
touch update.sh
echo "开始更新「小慕读书」小程序服务端"
cd /root/back-resources/node-imooc-demo
echo "正在更新代码..."
git pull
echo "正在重启服务..."
kill -9 `ps -ef|grep node|grep app.js|awk '{print $2}'`
node app.js &
echo "「小慕读书」小程序服务端启动成功"
echo "开始更新「小慕读书」后台服务端"
cd /root/back-resources/admin-imooc-node
echo "正在更新代码..."
git pull
echo "正在重启服务..."
node app.js &
echo "「小慕读书」后台服务端启动成功"
echo "正在启动「小慕读书」H5服务端"
cd /root/node-imooc-demo
node app.js &