当前位置: 首页 > news >正文

NextJS工程部署到阿里云linux Ecs

nextjs项目有多种部署方式,本文介绍最简单的一种方式,将源码上传到云服务器,编译后使用pm2后台运行nextjs工程。

检查node、npm是否安装

查看npm版本,如果版本较低先升级npm版本

npm -v

卸载

yum remove nodejs npm -y

安装新版本

sudo yum install https://rpm.nodesource.com/pub_21.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y 
sudo yum install nodejs -y --setopt=nodesource-nodejs.module_hotfixes=1

部署和编译

  1. 拷贝工程源码目录到服务器,其中node_modules、.git不需要上传

  2. 编译

npm run build

使用pm2后台运行nextjs程序

  1. 安装pm2
npm install -g pm2
  1. 运行
#注意当前是在工程目录下,命令中的名称可以任意命名,pm2管理中起到标识作用,实际运行的程序是当前目录下
pm2 start --name 自定义名称 npm -- start
  1. 查看端口占用,如工程中使用的3000,端口列表中也出现了,说明后台运行成功了
netstat -nultp
  1. 如果服务器开放了对应端口,可以加端口浏览器访问项目,如果只能开放80,继续nginx

安装配置nginx

  1. 安装nginx
yum install nginx -y
  1. nginx配置文件(/etc/nginx/etc/nginx.conf)
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/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;autoindex on;autoindex_exact_size off;autoindex_localtime on;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 default_server;listen       [::]:80 default_server;server_name  _;root         /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}include /etc/nginx/vhosts/*.conf;
}
  1. 多工程配置
    创建 /etc/nginx/vhosts/工程名.conf,自行修改域名,确保域名映射已设置好
server {listen       80;#443;server_name  xxx.xxxx.com;root html;index index.html index.htm;location / {proxy_pass http://127.0.0.1:3000;#设置主机头和客户端真实地址,以便服务器获取客户端真实IPproxy_set_header Host $host;proxy_set_header X-Real-Ip $remote_addr;proxy_set_header X-Forwarded-For $remote_addr;}
}
  1. 启动nginx
# 启动
systemctl start nginx
# 停止
systemctl stop nginx
# 重启
systemctl restart nginx 
# 重新加载配置文件
nginx -s reload
  1. 使用域名访问测试

pm2命令介绍

# 查看任务
pm2 list/# 重启
pm2 restart app_name# 停止
pm2 stop app_name|app_id# 停止所有
pm2 stop all# 删除
pm2 delete app_name|app_id# 删除所有
pm2 delete all# 日志
pm2 logs app_name|app_id# 查看所有日志
pm2 logs
http://www.lryc.cn/news/231464.html

相关文章:

  • 汽车以太网IOP测试新利器
  • 高防IP是什么?如何隐藏源站IP?如何进行防护?
  • ElasticSearch---查询es集群状态、分片、索引
  • Angular 使用教程——基本语法和双向数据绑定
  • 【ASP.NET】Hello World
  • AI创作系统ChatGPT网站源码+支持最新GPT-Turbo模型+支持DALL-E3文生图/AI绘画源码
  • C#_查找图片(按键精灵找图)
  • C#中.NET Framework4.8 控制台应用通过EF访问新建数据库
  • 无防御香港服务器如何防CC
  • MyBatis的插件能在哪些地方进行拦截?
  • 【BUG库】 记录自己学习工作中遇到的程序BUG
  • 卡尔曼家族从零解剖-(07) 高斯分布积分为1,高斯分布线性变换依旧为高斯分布,两高斯函数乘积仍为高斯。
  • 设计模式-访问者模式(Visitor)
  • C++二分查找算法:132 模式解法二枚举2
  • JavaWeb-HTML
  • 新外卖霸王餐小程序、H5、微信公众号版外卖系统源码
  • LeetCode - #89 格雷编码
  • 11.3SpringMVC
  • c语言从入门到实战——数组指针与函数指针
  • Rust图形界面编程:egui平直布局
  • Android13 wifi adb 串口开启
  • 关于一个屏幕取词程序,AI给的创建思路及指导
  • MySql跨库跨表触发器
  • NextJS开发:shadcn/ui中Button组件扩展增加图标
  • Go 语言
  • 【计算机网络笔记】DHCP协议
  • 21 Linux 自带的LED驱动
  • 神通MPP数据库的跨库查询
  • JavaWeb-WEB请求过程
  • 《QT从基础到进阶·二十一》QGraphicsView、QGraphicsScene和QGraphicsItem坐标关系和应用