在 Linux 上高效部署 Node.js 应用程序通常涉及以下步骤:
sudo apt update
sudo apt upgrade
git
, vim
, wget
等。sudo apt install git vim wget
apt
):sudo apt install nodejs npm
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install node
mkdir myapp
cd myapp
npm init -y
npm install express
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourusername/yourrepo.git
git push -u origin main
cd /path/to/your/project
npm install
npm install pm2 -g
pm2 start app.js --name "my-app"
sudo apt install nginx
sudo nano /etc/nginx/sites-available/default
在配置文件中添加以下内容:server {
listen 80;
server_name 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 logs my-app
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
构建和运行 Docker 容器:docker build -t my-node-app .
docker run -p 3000:3000 my-node-app
通过以上步骤,你可以在 Linux 上高效地部署 Node.js 应用程序,并确保其高性能和稳定性。