Window 10 系统下阿里云服务器部署 node 环境

110 阅读7分钟

在这里插入图片描述

建议安装一个 xshell。这样的话,文章当中的命令,就可以直接复制粘贴上去了。

(如果你不闲麻烦,那当我没说) 在这里插入图片描述

使用 NVM 安装多版本(v12.21.0)

  1. 使用git将源码克隆到本地的~/.nvm目录下,并检查最新版本

    yum install git
    git clone https://github.com/cnpm/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
    
  2. 激活NVM

    echo ". ~/.nvm/nvm.sh" >> /etc/profile
    source /etc/profile
    
  3. 列出Node.js的所有版本

    nvm list-remote
    
  4. 安装多个Node.js版本

    nvm install v7.4.0
    nvm install v12.21.0
    
  5. 运行nvm ls查看已安装的Node.js版本。本示例使用的版本为v12.21.0。返回结果如下所示。

    [root@iZXXXXZ .nvm]# nvm ls
             v12.21.0
    ->       v7.4.0
             system
    stable -> 7.4 (-> v7.4.0) (default)
    unstable -> 12.21 (-> v12.21.0) (default)
    
  6. 运行nvm use <版本号>可以切换Node.js版本。

    例如,切换Node.js版本至v12.21.0。返回结果如下所示。

    [root@iZXXXXZ .nvm]# nvm use v12.21.0
    Now using node v12.21.0
    

专有网络需要安装 nginx

  1. yum安装

    // 添加CentOS 7 Nginx yum资源库
    rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    
    
    // 安装
    yum install -y nginx
    
  2. 检查nginx是否安装成功

    nginx -t  // 有下面提示代表安装成功
    
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
  3. 启动nginx

    systemctl start nginx.service  // 启动nginx
    systemctl stop nginx.service   // 停止nginx
    systemctl restart nginx.service // 重启nginx
    systemctl enable nginx.service // 设置开机启动
    

    如果报错:

    Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
    

    解决办法:杀死 nginx 的所有进程,重新启动nginx

    查看进程

    netstat -tpln
    

    杀死进程

    kill -9 PID
    
  4. nginx 配置

    nginx配置文件为 /etc/nginx/nginx.conf

    (如果你发现的 nginx.conf 文件没有 server 字段。那这个字段应该添加到 http 中,才会生效)

    server {
        listen 80;
        location / {
            proxy_pass http://127.0.0.1:3000; # 本地node启动的端口为3000
        }
    }
    

    重启 nginx

    systemctl restart nginx.service 
    

    如果报错:Failed to restart nginx.servic.service: Unit not found.

    原因:是因为nginx没有将添加到系统服务,手动添加这个服务即可解决

  5. vim /etc/init.d/nginx

  6. 插入以下代码

    #!/bin/sh
    # nginx - this script starts and stops the nginx daemin
    #
    # chkconfig:   - 85 15
    
    # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
    #               proxy and IMAP/POP3 proxy server
    
    # processname: nginx
    # config:      /usr/local/nginx/conf/nginx.conf
    # pidfile:     /usr/local/nginx/logs/nginx.pid
    
    # Source function library.
    
    . /etc/rc.d/init.d/functions
    
    # Source networking configuration.
    
    . /etc/sysconfig/network
    
    # Check that networking is up.
    
    [ "$NETWORKING" = "no" ] && exit 0
    
    nginx="/usr/local/nginx/sbin/nginx"
    
    prog=$(basename $nginx)
    
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
    
    lockfile=/var/lock/subsys/nginx
    
    start() {
    
        [ -x $nginx ] || exit 5
    
        [ -f $NGINX_CONF_FILE ] || exit 6
    
        echo -n $"Starting $prog: "
    
        daemon $nginx -c $NGINX_CONF_FILE
    
        retval=$?
    
        echo
    
        [ $retval -eq 0 ] && touch $lockfile
    
        return $retval
    
    }
    
    
    stop() {
    
        echo -n $"Stopping $prog: "
    
        killproc $prog -QUIT
    
        retval=$?
    
        echo
    
        [ $retval -eq 0 ] && rm -f $lockfile
    
        return $retval
    
    }
    
    
    
    restart() {
    
        configtest || return $?
    
        stop
    
        start
    
    }
    
    
    reload() {
    
        configtest || return $?
    
        echo -n $"Reloading $prog: "
    
        killproc $nginx -HUP
    
        RETVAL=$?
    
        echo
    
    }
    
    force_reload() {
    
        restart
    
    }
    
    
    configtest() {
    
      $nginx -t -c $NGINX_CONF_FILE
    
    }
    
    
    
    rh_status() {
    
        status $prog
    
    }
    
    
    rh_status_q() {
    
        rh_status >/dev/null 2>&1
    
    }
    
    case "$1" in
    
        start)
    
            rh_status_q && exit 0
            $1
            ;;
    
        stop)
    
    
            rh_status_q || exit 0
            $1
            ;;
    
        restart|configtest)
            $1
            ;;
    
        reload)
            rh_status_q || exit 7
            $1
            ;;
    
    
        force-reload)
            force_reload
            ;;
        status)
            rh_status
            ;;
    
    
        condrestart|try-restart)
    
            rh_status_q || exit 0
                ;;
    
        *)
    
            echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
            exit 2
    
    esac
    
  7. cd /etc/init.d

  8. 依此执行以下命令

    chmod 755 /etc/init.d/nginx
    chkconfig --add nginx
    
  9. 开启nginx

    service nginx start
    

    如果一切顺利,你会看到

    [root@iZuf6gk9a5p415wnahjxc4Z init.d]# service nginx start
    Starting nginx (via systemctl):                            [  OK  ]
    

    防火墙

  10. 服务器防火墙

    查看所有信息,看添加的端口是否存在(注:ports后面的就是开放的对应端口)

    systemctl start firewalld
    firewall-cmd --list-all
    

    可以看到如下信息:

    public
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: dhcpv6-client ssh
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    

    若是ports为空,则自行添加所需要开放的端口( 例如添加3000端口 )

    // 添加需要开放的端口号
    firewall-cmd --zone=public --add-port=3000/tcp --permanent
    

    重启防火墙

    firewall-cmd --reload
    

    再次检查检查端口是否打开

    firewall-cmd --list-all
    
  11. 配置安全组

在这里插入图片描述

不出意外的话,访问 ip+端口,可以看到 Hello World,两个大字。那么恭喜你,你成功了!!! 在这里插入图片描述 总结一下✔️:

想要放开一个端口,必须要去阿里云的官网上进行安全组配置,放开防火墙端口,nginx 中写入端口号。

配置 git

  1. 安装 git

    yum install git
    

    查看版本

    git --version
    

    生成 ssh 公钥

    ssh-keygen -t rsa -C "1*********62@qq.com"
    
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:m0OxHwUdbdxkFRRfyLCX2uqrzfIixoidrK0RO1v7Pe8 1514535562@qq.com
    The key's randomart image is:
    +---[RSA 2048]----+
    |          ..o*.BO|
    |           ...*o+|
    |        .   o.o .|
    |         o . +   |
    |    .   S . . .  |
    |     o . + . .   |
    |    ++.++ . .    |
    |    .*=.+o++     |
    |    +ooo..oOE.   |
    +----[SHA256]-----+
    

    进入 .ssh 文件夹

    cd .ssh
    

    查看 id_rsa.pub

    ls 
    

    ==> authorized_keys id_rsa id_rsa.pub known_hosts

    输入命令 more id id_rsa.pub

    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjzz+Qr35mu94Hlse9Ml89yzkPzw4A1gP2ibulEpLPGR1ojuQJgp7HhOB/2TkZ
    4F8jWwjehbwekdwkdqkwbd31293u1291bkwabcscgas7q76hjsbckasc asc0ascsauc82yge32ebqwbsabxadgqROONG
    Gt2ZTX8g4VSlceFyqr5c3248y12ihdnqkwkdqdwqdnwqidRj3kUDyI+Q+K4IocNZ1kf/E3u5h4RSvVBgM9ZMc4c+vPA2eHhxQiQik+/La
    F7RqzuMQVDxTs/RR6gzZTmFw6OkHsZCIhIwm8RSNzQv3wb7PcpM8DEBOZYNUkXl+Ja4Rcpmxjz7x7BI5 1365125855562@qq.com
    

    复制粘贴到你的 gitee 的 ssh 中。这样就可以在服务器端使用 git 了。

安装 pm2

  1. pm2

    npm install pm2 -g
    

启动项目

  1. 找一个文件夹,存放项目代码

    cd /home

    git clone git@gitee.com:suiboyu/readbook-express.git
    

    像之前一样 cd readbook-expressnpm install

    使用 pm2 开启服务

    pm2 start app.js
    

    看到这个,证明你成功开启了。

在这里插入图片描述

不出意外的话,访问 ip+端口,根据项目中的接口路由,在浏览器中进行访问。

恭喜我,我成功了!!! 在这里插入图片描述

一般来说,脚手架搭好的框架中,都会有 script 命令。

pm2 start npm -- run <scriptname>

MySQL 数据库8.0

(建议安装高版本,不然后面导入数据的时候,可能会出现版本冲突,导入失败✔️)

  1. 确保服务器系统处于最新状态
yum -y update
  1. 首先检查是否已经安装,如果已经安装先删除以前版本,以免安装不成功
rpm -qa | grep mysql
// 或者
yum list installed | grep mysql

​ 卸载命令

rpm -e mysql57-community-release

如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

rpm -e --nodeps mysql-libs-5.1.73-8.el6_8.x86_64
  1. 下载MySql安装包
rpm -ivh http://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
  1. 安装MySql
yum install -y mysql-server
  1. 设置开机启动mysql
systemctl enable mysqld.service
  1. 检查是否已经安装了开机自动启动
systemctl list-unit-files | grep mysqld

​ 出现这个,标志完成

mysqld.service                                enabled 
mysqld@.service                               disabled
  1. 设置开启服务
systemctl start mysqld.service
  1. 查看 MySQL 默认密码
cd /var/log/
cat mysqld.log

在这里插入图片描述

  1. 登录 MySQL
mysql -uroot -p
  1. 修改默认密码
set global validate_password_policy=0;
set global validate_password_length=1;
alter user 'root'@'localhost' identified by '123456';
use mysql;
select host,user from user;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '406103' WITH GRANT OPTION;
flush privileges;

​ 验证是否成功

select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
4 rows in set (0.00 sec)

现在数据库已经配置完成,但是之前开发的数据都在本地。我猜你一定不想重新再写一份,那怎么办呢?把本地的数据迁移到云服务器上。 在这里插入图片描述

  1. sqlyog 迁移数据 在这里插入图片描述

云服务器上新建一个同名的数据库名称。在本地数据库上进行迁移操作。

在这里插入图片描述

在这里插入图片描述

如果顺利的话,你会在云服务器的数据库上,看见新创建的数据库中,成功导入了表。

在这里插入图片描述

在这里插入图片描述

配置 Redis

  1. 下载 redis

    wget http://download.redis.io/releases/redis-stable.tar.gz
    
  2. 编译

    解压

    tar zxvf redis-stable.tar.gz
    

    移动

    mv redis-stable /usr/local/redis
    

    进入到 redis 的 src 目录下

    cd /usr/local/redis/src
    

    编译

    make
    

    可能会报错

    server.c:3318:16: error: ‘struct redisServer‘ has no member named ‘loading‘

    解决办法:

    升级gcc

    yum -y install centos-release-scl
    
    yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
    
    scl enable devtoolset-9 bash
    
  3. 编译检查

    make test
    
  4. 安装

    make install
    
  5. 启动

    redis-server
    

    报错:

    -bash: redis-server: command not found

    解决办法:

    //     安装目录
    ls -s /usr/local/redis /usr/bin/redis-server
    

    重新启动

    redis-server
    
    1739:C 19 May 2021 13:55:54.805 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    1739:C 19 May 2021 13:55:54.805 # Redis version=6.0.5, bits=64, commit=00000000, modified=0, pid=1739, just started
    1739:C 19 May 2021 13:55:54.805 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                    _._                                                  
               _.-``__ ''-._                                             
          _.-``    `.  `_.  ''-._           Redis 6.0.5 (00000000/0) 64 bit
      .-`` .-```.  ```\/    _.,_ ''-._                                   
     (    '      ,       .-`  | `,    )     Running in standalone mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
     |    `-._   `._    /     _.-'    |     PID: 1739
      `-._    `-._  `-./  _.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |           http://redis.io        
      `-._    `-._`-.__.-'_.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-._`-.__.-'_.-'    _.-'                                   
          `-._    `-.__.-'    _.-'                                       
              `-._        _.-'                                           
                  `-.__.-'                                               
    
    1739:M 19 May 2021 13:55:54.807 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    1739:M 19 May 2021 13:55:54.807 # Server initialized
    
  6. 配置后台运行

    redis 目录下

    [root@iZuf6gk9a5p415wnahjxc4Z redis]# ls
    00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-moduleapi  src     utils
    BUGS             deps     MANIFESTO  runtest          runtest-sentinel   tests
    CONTRIBUTING     INSTALL  README.md  runtest-cluster  sentinel.conf      TLS.md
    

    编辑 redis.conf 文件

    vim redis.conf
    

    daemonize: no ===> daemonize: yes

  7. 运行 redis

    redis-cli
    
    127.0.0.1:6379> set name nick
    OK
    127.0.0.1:6379> get name
    "nick"
    127.0.0.1:6379> 
    

    耶 (^-^)V,成功了。奖励自己一朵小红花

在这里插入图片描述

万一要是失败,那就祝你好运了

在这里插入图片描述

参考文章:

www.jianshu.com/p/7aad651bd…

help.aliyun.com/document_de…

www.cnblogs.com/ansibee/p/8…

blog.csdn.net/weixin_4332…

blog.csdn.net/qq_45441466…

blog.csdn.net/babybabyup/…

blog.csdn.net/q258523454/…

blog.csdn.net/qq_45069833…