关于node全栈项目打包发布linux项目问题总集

1.用pm2部署nest

说明:如果一开始将nest直接打包放到linux服务器上用pm2执行则会报错,这是因为tsconfig.build.tsbuildinfo文件的路径以及相关依赖问题。

报错会为:什么东西找不到....

所以建议以下为步骤一步一步配置

  1. 将整个nest添加压缩包直接传到的root中的任何一个目录中,在linux中解压
  2. 进入nest的项目文件用npm run build进行打包
  3. 此时需要下载好pm2进程管理器
  4. 直接pm2 start main.js运行后端项目
  5. 此时为了验证后端项目是否正常启动成功 可以在win上的浏览器缩编输入一个后端的get接口来验证

2.安装nginx

因为某些linux服务器为了兼容一些特定的项目使用的老的版本的linux系统,比如我此时用的就是centOS7,因为centOS7在2024年的6月份就把此linux版本的yum源镜像归档了,所以会导致我们在使用yum源来下载一些工具的时候会出现yum报错的现象。此时别慌,可以按照以下步骤操作!

报错信息如:

Loading mirror speeds from cached hostfile Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7\&arch=x86_64\&repo=os\&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"

解决方法

  • 进入/etc/yum.repos.d目录下找到 CentOS-Base.repo
  • cd /etc/yum.repos.d

阿里云镜像源

以下两个命令都可以,执行完成后进入/etc/yum.repos.d

然后执行:

  • cat CentOS-Base.repo

解析nginx

  • yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  • 此时来到你安装nginx的目录
  • ./configure --prefix=/usr/local/nginx --with-http_ssl_module
  • make&&make install

此时去/usr/local/nginx目录中去找配置好的nginx(此时就可以看到nginx执行文件了)

配置nginx.conf文件

(两个,一个为一开始下载的nginx文件,另一个为解析到/usr/local/nginx之后的文件)

复制代码
http {
    include   mime.types;
    default_type  application/octet-stream;
    server {
        listen 443 ssl;
        server_name 自己解析的域名;
        #你解析域名的密钥文件两个(假如你都放在了/etc/Assl目录下)
        ssl_certificate /etc/Assl/XXXXX.pem;
        ssl_certificate_key /etc/Assl/XXXXX.key;

        location / {
            #访问域名就可以看到的前端项目(假如你都放在了/etc/lesaosao_lunxun_ui目录下)
            root  /etc/lesaosao_lunxun_ui/dist_pc;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        location /api/ {
            #访问后端项目的端口(ip+端口号)
            proxy_pass http://xxx.xx.xx.xx:3000/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /images/uploadimg/ {
            #访问后端存放前端上传的图片目录
            alias /root/usr/lesaosao_lunxun/uploadimg/;
        }

        # SSL configurations
        proxy_ssl_session_reuse off;
        proxy_ssl_protocols TLSv1.2 TLSv1.3;
        proxy_ssl_ciphers HIGH:!aNULL:!MD5;
    }

    server {
        #第二个前端项目(假如不需要使用域名访问)
        listen 8100;

        location / {
             #访问8100端口就可以看到的前端项目(假如你都放在了/etc/lesaosao_lunxun_ui目录下)
            root /etc/lesaosao_lunxun_ui/dist_web;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        location /api/ {
            #访问后端项目的端口(ip+端口号)
            proxy_pass http://xxx.xx.xx.xx:3000/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        #因为该前端不涉及上传图片等文件所以该目录可忽略
        location /images/uploadimg/ {
            alias /images/uploadimg/;
        }

        # SSL configurations
        proxy_ssl_session_reuse off;
        proxy_ssl_protocols TLSv1.2 TLSv1.3;
        proxy_ssl_ciphers HIGH:!aNULL:!MD5;
    }
}
events {
    	worker_connections  1024;
	}

重启nginx服务!

发现访问域名项目即可被访问到!(特殊linux需配置防火墙以及开放端口等操作)

相关推荐
Zach_yuan18 分钟前
深入浅出 JSONCpp
linux·服务器·网络·c++
AtoposのCX33025 分钟前
Docker运行hello-world镜像失败或超时
运维·docker
马猴烧酒.35 分钟前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
北京迅为1 小时前
《【北京迅为】itop-3568开发板NPU使用手册》- 第 7章 使用RKNN-Toolkit-lite2
linux·人工智能·嵌入式·npu
Dragon~Snow1 小时前
Linux Centos9 安装 Elasticsearch
linux·elasticsearch·jenkins
熊延1 小时前
麒麟V10系统安装部署elasticsearch
linux·运维·服务器·elasticsearch·搜索引擎·全文检索
Jia ming2 小时前
跟踪器与事件使用举例
linux·事件·跟踪器
生活很暖很治愈2 小时前
Linux——基础IO&软硬链接
linux·ubuntu
2401_858936882 小时前
【Linux C 编程】标准 IO 详解与实战:从基础接口到文件操作实战
linux·c语言
Roc.Chang2 小时前
Ubuntu 下 VLC 无法启动(Segmentation fault)终极解决方案
linux·ubuntu·vlc·媒体播放