CentOS7部署Node项目

更新系统和安装 git、vim、curl
yum update -y
yum install curl git -y
通过 nvm 安装 Node.js

安装 nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash

验证安装是否成功

source ~/.bashrc
nvm --version

看到输出版本信息 0.33.5 表示安装成功

安装 MySQL 5.7

下载安装

yum install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm -y
yum install mysql-community-server -y

启动 mysql

systemctl start mysqld
systemctl enable mysqld

查找 root 的初始密码

cat /var/log/mysqld.log | grep password

更改密码

mysql -uroot -p 

回车后输入查找到的密码,登录成功
在这里插入图片描述

部署Node项目

下载项目的源码

mkdir /var/www
cd /var/www
git clone https://gitee.com/icjs-cc

全局安装 ThinkJS 命令

npm install -g think-cli
thinkjs -V

安装依赖

cd /var/www/test
npm install 

创建数据库并导入数据

 mysql -uroot -p -e "create database test character set utf8mb4"
 mysql -uroot -p nideshop < /var/www/test/test.sql

修改项目数据库配置

vim src/common/config/database.js

修改后

const mysql = require('think-model-mysql');

module.exports = {
    handle: mysql,
    database: 'test',
    prefix: 'test_',
    encoding: 'utf8mb4',
    host: '127.0.0.1',
    port: '3306',
    user: 'root',
    password: '你的密码',
    dateStrings: true
};

注意 encoding,prefix 的值

编译项目

npm run compile
node production.js

打开另一个终端验证是否启动成功

curl -I http://127.0.0.1:8080/

输出 HTTP/1.1 200 OK,则表示成功,Ctrl + C 停止运行

使用 PM2 管理服务

安装配置 pm2

npm install -g pm2

修改项目根目录下的 pm2.json 为:

vim pm2.json

修改后内容:

{
  "apps": [{
    "name": "test",
    "script": "production.js",
    "cwd": "/var/www/test",
    "exec_mode": "fork",
    "max_memory_restart": "256M",
    "autorestart": true,
    "node_args": [],
    "args": [],
    "env": {

    }
  }]
}

如果服务器配置较高,可适当调整 max_memory_restart 和instances的值

启动pm2

pm2 start pm2.json

验证是否可以访问

curl -I http://127.0.0.1:8080/
使用 nginx 做反向代理
yum install nginx -y
systemctl start nginx.service
systemctl enable nginx.service

测试本地是否可以正常访问

curl -I localhost 

修改nginx配置

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
vim /etc/nginx/nginx.conf

内容如下(只需更改 server 里面的内容)

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
        server_name nideshop.com www.test.com; # 改成你自己的域名
        root /var/www/test/www;
        set $node_port 8080;

        index index.js index.html index.htm;
        if ( -f $request_filename/index.html ){
            rewrite (.*) $1/index.html break;
        }
        if ( !-f $request_filename ){
            rewrite (.*) /index.js;
        }
        location = /index.js {
            proxy_http_version 1.1;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_pass http://127.0.0.1:$node_port$request_uri;
            proxy_redirect off;
        }

        location ~ /static/ {
            etag         on;
            expires      max;
        }
    }
}

重新启动nginx并验证nginx是否还可以正常访问

nginx -t 
systemctl restart nginx.service
curl  http://127.0.0.1/

如果返回接口数据则代表运行成功

注:阿里云默认外网不能访问80/443端口,请更改实例的安全组配置,配置教程

配置https访问

安装certbot

yum install epel-release -y
yum install certbot-nginx -y
certbot --nginx

如果 certbot -nginx 这步出错,则执行

pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3

重新执行 certbot --nginx,详情文档

certbot renew --dry-run

测试浏览器使用https形式访问是否成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值