centos7 + nginx + nodejs 部署

2023-02-18 15:43:28 浏览数 (1)

centos7 安装 nodejs 环境

https://www.cnblogs.com/fozero/p/10967154.html

yum 安装 nodejs:

代码语言:javascript复制
yum install -y nodejs

安装 淘宝 镜像 cnpm:

代码语言:javascript复制
npm install -g cnpm --registry=https://registry.npm.taobao.org

然后使用 cnpm 安装 forever:

代码语言:javascript复制
cnpm install -g forever

nodejs 代码及服务启动

前端代码:

https://xieboke.net/article/361/

启动 nodejs 本地服务:

代码语言:javascript复制
[root@izj6c5nf8wccxwegz vue_project]# vim app.js 
[root@izj6c5nf8wccxwegz vue_project]# forever stop app.js 

info:    Forever stopped process:
    uid  command       script forever pid   id logfile                 uptime       
[0] pZSq /usr/bin/node app.js 21352   21358    /root/.forever/pZSq.log 0:0:0:31.665 
[root@izj6c5nf8wccxwegz vue_project]# 
[root@izj6c5nf8wccxwegz vue_project]# forever start app.js 
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: app.js

nginx 配置

主配置文件:

代码语言:javascript复制
#user  nobody;
user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    include vhost/*.conf; # 子配置文件的目录   
     
}

子配置文件:

代码语言:javascript复制
upstream hello {
    server 127.0.0.1:9999;  # nodesjs 本地启动的端口
}

server {
    listen 80;  # 监听外网的端口
    server_name zhuoyue.zone;  # 外网域名

    location / {
        proxy_set_header Host  $http_host;
        proxy_set_header X-Real-IP  $remote_addr;  
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Nginx-proxy true;
        proxy_pass http://hello;  # 代理到 upstream hello
        proxy_redirect off;
    }
}

检查配置文件和重启 nginx

代码语言:javascript复制
nginx -t
nginx -s reload

参考文档

https://github.com/wmui/web-deploy/blob/master/zi-dong-hua-bu-shu.md

0 人点赞