Node.js VPS Hosting: Triển Khai Ứng Dụng Node
Node.js là nền tảng server-side JavaScript phổ biến nhất thế giới, được sử dụng bởi Netflix, LinkedIn và PayPal. Triển khai ứng dụng Node.js trên VPS offshore của AnubizHost cho bạn toàn quyền kiểm soát môi trường runtime, không bị giới hạn bởi shared hosting và không bị gián đoạn vì tài nguyên dùng chung. Hướng dẫn này bao gồm mọi bước từ cài đặt Node đến production deployment với PM2 và Nginx.
Need this done for your project?
We implement, you ship. Async, documented, done in days.
Cài Đặt Node.js trên VPS
Sử dụng NodeSource repository để cài đặt Node.js phiên bản LTS mới nhất:
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
apt install -y nodejs
node --version
npm --versionCài đặt PM2 để quản lý process Node.js trong production:
npm install -g pm2PM2 tự động khởi động lại ứng dụng khi bị crash và khi server reboot. Đây là công cụ thiết yếu cho môi trường production.
Deploy Ứng Dụng Node.js với PM2
Clone code và cài đặt dependencies:
git clone https://github.com/youruser/yourapp.git /var/www/myapp
cd /var/www/myapp
npm install --productionKhởi động ứng dụng với PM2:
pm2 start src/index.js --name "myapp"
pm2 save
pm2 startupChạy lệnh được PM2 gợi ý để cài đặt systemd service. Kiểm tra trạng thái:
pm2 status
pm2 logs myapp --lines 50Để deploy code mới không có downtime:
git pull origin main
npm install --production
pm2 reload myappCấu Hình Nginx Làm Reverse Proxy cho Node.js
Cài đặt Nginx và cấu hình reverse proxy:
apt install -y nginx
nano /etc/nginx/sites-available/myappNội dung cấu hình:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}Kích hoạt cấu hình:
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginxThêm SSL HTTPS với Certbot
Cài đặt SSL miễn phí từ Let's Encrypt:
apt install -y certbot python3-certbot-nginx
certbot --nginx -d yourdomain.com -d www.yourdomain.comCertbot tự động cập nhật cấu hình Nginx để redirect HTTP sang HTTPS. Chứng chỉ tự động gia hạn mỗi 90 ngày qua cron job. Kiểm tra tự động gia hạn:
certbot renew --dry-runỨng dụng Node.js của bạn giờ có thể truy cập qua HTTPS với SSL hợp lệ. VPS offshore của AnubizHost không có giới hạn băng thông ảnh hưởng đến hiệu suất ứng dụng.
Related Services
Why Anubiz Host
Ready to get started?
Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.