在CentOS上部署Node.js项目可以通过多种方法实现,以下是几种常见的方法:
Dockerfile
的文件,内容如下:# 使用官方的 Node.js 镜像作为基础镜像
FROM node:14-alpine
# 设置工作目录
WORKDIR /app
# 复制 package.json 和 package-lock.json 到工作目录
COPY package*.json .
# 安装项目依赖
RUN npm install
# 复制项目代码到工作目录
COPY . .
# 构建生产环境的应用程序
RUN npm run build
# 暴露应用程序的端口
EXPOSE 3000
# 启动应用程序
CMD ["node", "dist/main.js"]
docker build -t <镜像名称>:<标签> .
docker run -p 3000:3000 <镜像名称>:<标签>
curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
sudo yum install -y nodejs
sudo npm install -g pm2
sudo yum install -y epel-release
sudo yum install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
/etc/nginx/conf.d/
目录下),添加以下内容:server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host host;
proxy_cache_bypass http_upgrade;
}
}
sudo systemctl restart nginx
pm2 start app.js
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
nvm install 14
cd /path/to/your/project
npm install
pm2 start app.js
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:CentOS上Node.js项目怎么部署