当前位置:首页 > CN2资讯 > 正文内容

云服务器搭建方案 如何搭建云服务器环境

1天前CN2资讯



云服务环境搭建

  • 简介
  • 一、云服务器开通
  • 1 二级菜单1
  • 2 二级菜单2
  • 二、FinalShell工具介绍
  • 三、云服务器基本环境搭建
  • 1、安装 nvm
  • 2、yum 介绍
  • 四、CentOS Node.js环境搭建
  • 五、CentOS Nginx环境搭建
  • 1、安装 Nginx 依赖
  • 2、编译源码
  • 3、制作软连接
  • 4、制作个性化配置
  • 六、CentOS git部署 + 免密更新
  • 1、git 安装
  • 2、设置免密更新
  • 七、CentOS MySQL环境安装
  • 八、前端项目构建与部署方法
  • 九、后端部署方法
  • 手动部署
  • 自动部署
  • 编写自动化脚本
  • 总结



简介

 本文主要介绍云服务器的开通以及云服务环境搭建,然后将项目打包,将打包后的代码部署到云服务环境中

一、云服务器开通

记得加上安全组

1 二级菜单1

2 二级菜单2

二、FinalShell工具介绍

后续再补!!!

三、云服务器基本环境搭建

1、安装 nvm

  • 在命令行中输入如下地址进行安装
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/ | bash // or wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/ | 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
  • 打开 .bash_profile 文件,出现如下内容表示并没有写入 nvm 的内容,nvm 这个指令没有生成
[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
  • 打开 .bashrc 文件显示如下,表示 nvm 指令写入到 .bashrc 文件中了
[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
  • 可以使用如下命令将环境变量生效,然后就可以使用 nvm 指令
[root@VM-0-2-centos ~]# source .bashrc
  • 验证 nvm 指令,显示版本号表示成功
[root@VM-0-2-centos ~]# nvm -v 0.38.0

2、yum 介绍

  • yum 是 CentOS 自带的软件,主要用来管理 CentOS 的软件仓库
  • yum -y 表示选择同意
[root@VM-0-2-centos ~]# yum -y

四、CentOS Node.js环境搭建

  • 安装最新的 node 版本
[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 ~]#
  • 安装 cnpm,cnpm 可以更快下载 npm 安装包
npm install -g cnpm --registry=https://registry.npm.taobao.org
  • 查看 cnpm 的安装路径
[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 [email protected] (/root/.nvm/versions/node/v16.7.0/lib/node_modules/cnpm/lib/parse_argv.js) [email protected] (/root/.nvm/versions/node/v16.7.0/lib/node_modules/cnpm/node_modules/npm/lib/npm.js) [email protected] (/root/.nvm/versions/node/v16.7.0/bin/node) [email protected] (/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:// [root@VM-0-2-centos ~]#

五、CentOS Nginx环境搭建

1、安装 Nginx 依赖

  • 安装 Nginx 依赖
// 安装 pcre 的依赖 yum -y install pcre* // 安装 openssl 的依赖 yum -y install openssl*
  • 两个依赖安装完成之后就可以安装 Nginx 服务器
  • 新建一个目录,将 nginx 源码放到这下面
[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 目录,下载 nginx 源码
// 进入 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]#
  • rwxr-xr-x 中的 x 表示可执行
  • 在 CentOS 上执行文件使用 ./可执行文件 或 sh 可执行文件
// 执行 configure 文件 ./configure

2、编译源码

  • 使用 make 进行编译
[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
  • 解决问题:执行如下命令(只能使用 vi 不能使用 vim),
[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
  • 重新进入 nginx 源码目录下,重新编译
[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
  • 目前还不能使用,可以通过 -t 读取配置文件
[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]#

3、制作软连接

  • 软连接可以理解为手动创建快捷方式
[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]#
  • 就可以执行 nginx 指令
[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 服务器,并通过 云服务器 IP 进行访问
// 启动 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

4、制作个性化配置

  • 进入 /root/nginx 根目录下,创建一个 nginx.conf 文件
[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]#
  • 进入主配置文件中修改 nginx.conf 文件中的配置信息
// 指定 user 为 root # 指定 user 为 root user root; worker_processes 1; // 主配置文件改为监听 9000 端口 server { listen 9000; server_name localhost;
  • 在配置文件末尾加 include /root/nginx/*.conf; 表示 nginx 会将该目录下的所有以 .conf 结尾的配置文件融合到主配置文件中
# } #} include /root/nginx/*.conf; } -- 插入 --
  • 然后在 /root/nginx/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 *; } }
  • 在 front-resources 目录下添加一个 index.html 文件并写入:
<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]#
  • 在查看文件内容时如果想显示行号,可以按下 shift + 冒号键,然后输入: set nu 即可显示行号

六、CentOS git部署 + 免密更新

1、git 安装

// git 安装 yum -y install git // 查看版本 git --version
  • 如果 git 版本较低,可以对 git 版本进行升级
  • 首先 remove 掉 yum 源上的 git
yum remove git
  • 然后用 yum 源安装 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 指令下载 git 最新的版本
wget https:///git/git/archive/v2.9.2.tar.gz
  • 解压
tar -zxvf v2.9.2.tar.gz
  • 进入源码目录下
cd git-2.9.2
  • 使用 make 编译源码
// 编译时指定安装后的可执行文件位于哪 make prefix=/usr/local/git all
  • 编译完成后进行安装
make prefix=/usr/local/git install
  • 执行 git 指令不成功,进入 /usr/local/git/bin 查看
  • 制作软连接
// 进入 /usr/bin cd /usr/bin // 制作软连接 ln -s /usr/local/git/bin/git git
  • 然后就可以执行 git 指令
git --version
  • 在 /root 目录下新建一个目录,用于存放 后端源码
mkdir back-resources

2、设置免密更新

  • 通过 ssh 做免密登录
// 通过 ssh-keygen 生成免密登录的密钥 // 通过 -C 属性去填写 git 仓库的用户名 ssh-keygen -t rsa -C “248342961@” // 点击三次回车将密钥生成 // 将密钥打印在终端上面 cat ~/.ssh/id_rsa.pub // 复制密钥后进入 Gitee 仓库 “我的设置”添加密钥 // 将密钥粘贴到密钥内容上
  • 将密钥添加到用户设置的好处:对该用户的所有项目都可以免密 clone,不用在每个项目上点“仓库设置”单独设置
  • 进入刚才创建的 back-resources 目录下再来 git clone 下载代码
git clone [email protected]:lgk2021/xiaomudushu_back-management.git
  • 更新代码
// 进入要更新源码的目录下,执行 git pull

七、CentOS MySQL环境安装

  • yum 安装 mysql-server
// 注意:我们要安装的是 MySQL 服务端 // 如果输入的是 mysql,则只会安装客户端 yum install mysql-server
  • 安装完成后,输入 mysql -u root -p 显示 MySQL 没有启动
完毕! [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 ~]#
  • 检查 MySQL 状态
[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)
  • 启动 MySQL
[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>
  • 如果 MySQL 在初始化的时候给了默认密码,可以到 log 日志中去找
cat /var/log/mysqld.log |grep password
  • 复制默认密码后,重新登录
mysql -u root -p
  • 查看数据库,如果显示:You must reset your password using ALTER USER …
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)
  • 新建数据库:book 是数据库名
create database book;
  • 删除数据库
drop database db_name;
  • 使用数据库
use book;
  • 查看数据库表
show tables;
  • 创建数据库表:test
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;
  • 在使用 Navicat 连接测试数据库时,可能会报错:Test Failed 1130 - Host ‘服务器IP’ is not allowed to connect to this MySQL server
  • 可以使用命令行的方式(登录状态)输入下列命令
// 使用数据库 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;

八、前端项目构建与部署方法

九、后端部署方法

手动部署

  • 手动部署就是在本地手动通过 FinalShell 等文件传输工具将源代码上传到 /root/back-resources 目录下

自动部署

  • 自动部署是:本地将源代码上传到 git 仓库后,采用 git 免密登录的方式,将代码 git pull 到云服务端本地
  • git pull 的操作可以编写自动化脚本执行
编写自动化脚本
  • 首先在 /root/back-resources 目录下创建脚本文件
// 切换目录 cd /root/back-resources // 创建脚本文件 touch update.sh
  • vim 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 &

总结


    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/31851.html

    分享给朋友:

    “云服务器搭建方案 如何搭建云服务器环境” 的相关文章

    **解析cn2线路的美国vps:为什么它是你的最佳选择?**

    ---##cn2线路的美国vps,为什么它如此受欢迎?在数字化时代,VPS(虚拟私人服务器)已经成为企业和个人用户不可或缺的工具。无论是用于网站托管、游戏服务器搭建,还是作为企业数据中转站,VPS的功能和性能直接影响用户体验。在众多VPS服务中,"cn2线路的美国vps"凭借其独特的优势,迅速成为市...

    Windows SSH Keygen 无法连接问题解决指南

    在现代网络环境中,SSH(Secure Shell)协议扮演着至关重要的角色,确保了远程登录的安全性与可靠性。在Windows操作系统中,了解SSH的基本知识是非常必要的。SSH不仅提供了加密的网络服务,还为我们在远程管理服务器时提供了安全的通道。 当我们谈到SSH的时候,首先想到的就是它的密钥认证...

    芝加哥时区详解:如何有效应对中部标准时间的挑战与机遇

    芝加哥位于美国伊利诺伊州的东北部,是美国重要的城市之一。对于身处这座城市的人们,了解芝加哥时区无疑是日常生活中的一部分。我自己在这里生活的时候,时区的变化让我对时间更加敏感。芝加哥时区,其实就是中部标准时间(Central Standard Time,CST), UTC-6。这种时间定义不仅影响着我...

    Faconhost: 多功能网络托管服务平台的全面评测与用户体验

    什么是 Faconhost 遇到 Faconhost,我的第一印象是它是一个多功能的网络托管服务平台。它为用户提供了灵活、高效的网络解决方案,帮助个人和企业轻松管理他们的网站和在线业务。在互联网不断发展的今天,选择一个合适的托管服务显得至关重要,而 Faconhost 就是这样一个目标明确的选择。...

    如何购买域名:推荐域名选择与注册指南

    在我们生活的这个数字化时代,域名的角色显得尤为重要。简单来说,域名就是一个网站在互联网上的地址,类似于我们个人的住址。在没有域名的情况下,寻找一个特定的网站将变得异常困难。通过域名,用户可以轻松访问到他们所需的信息、资源或服务。而域名不仅仅是一个简单的地址,它承载着品牌的形象和企业的文化。 域名对品...

    Claw VPS:高效灵活的虚拟专用服务器解决方案

    1. Claw VPS 介绍 说到虚拟专用服务器,Claw VPS绝对值得一提。它是一种高效、灵活的云服务器解决方案,专为满足不同用户需求而设计。Claw VPS以其出色的性能和易用性,吸引了不少企业和个人用户。通过虚拟化技术,Claw VPS能够在一台物理服务器上创建多个独立的虚拟服务器。这样,用...