Tóm Tắt Các Bước Triển Khai GCP VM:
- Vào Console Google Cloud -> Compute Engine -> VM Instances -> Chọn Ubuntu 22.04 LTS (e2-medium).
- Mở cổng Firewall cho HTTP (Port 80) và HTTPS (Port 443).
- SSH vào VM và thực thi từng lệnh bên dưới để cấu hình môi trường Node.js 20, MySQL, Nginx, PM2 & SSL.
# ==============================================================================
# HƯỚNG DẪN BẮT ĐẦU TRIỂN KHAI TRÊN GOOGLE CLOUD VM (UBUNTU 22.04 LTS)
# ==============================================================================
# Bước 1: Cập nhật hệ thống
sudo apt update && sudo apt upgrade -y
# Bước 2: Cài đặt Node.js v20.x LTS, Git, MySQL Server & Nginx
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs build-essential git mysql-server nginx
# Bước 3: Cài đặt Trình quản lý tiến trình PM2 (Process Manager)
sudo npm install -g pm2
pm2 startup systemd
# Bước 4: Khởi tạo & Bảo mật MySQL Database
sudo mysql_secure_installation
sudo mysql -u root -p -e "CREATE DATABASE globaltech_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mysql -u root -p -e "CREATE USER 'gt_user'@'localhost' IDENTIFIED BY 'GlobalTechPass2026!@#';"
sudo mysql -u root -p -e "GRANT ALL PRIVILEGES ON globaltech_db.* TO 'gt_user'@'localhost'; FLUSH PRIVILEGES;"
# Bước 5: Tải mã nguồn ứng dụng & Cài đặt Dependencies
sudo mkdir -p /var/www/globaltech
sudo chown -R $USER:$USER /var/www/globaltech
cd /var/www/globaltech
git clone https://github.com/globaltech/enterprise-cms.git .
npm install --production
# Bước 6: Khởi chạy Node.js Server bằng PM2
pm2 start server.js --name "globaltech-backend"
pm2 save
# Bước 7: Cài đặt SSL Let's Encrypt tự động bảo mật HTTPS
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d globaltech-corp.com -d www.globaltech-corp.com
// server.js - Node.js Express Backend Production Architecture
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const compression = require('compression');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
// Security Middlewares
app.use(helmet());
app.use(cors({ origin: process.env.CORS_ORIGIN || '*' }));
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true }));
app.use(compression());
// Rate Limiting (Phòng chống DDOS)
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 phút
max: 200,
message: { status: 429, message: 'Số lượng yêu cầu quá tải từ IP của bạn!' }
});
app.use('/api/', apiLimiter);
// API Endpoints
app.use('/api/v1/products', require('./routes/productRoutes'));
app.use('/api/v1/services', require('./routes/serviceRoutes'));
app.use('/api/v1/contacts', require('./routes/contactRoutes'));
app.use('/api/v1/auth', require('./routes/authRoutes'));
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server đang chạy tại cổng ${PORT} trên Google Cloud VM...`);
});
-- schema.sql - Schema Cơ Sở Dữ Liệu MySQL
CREATE TABLE IF NOT EXISTS `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`fullname` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) UNIQUE NOT NULL,
`password` VARCHAR(255) NOT NULL,
`role` ENUM('SUPER_ADMIN', 'ADMIN', 'EDITOR') DEFAULT 'EDITOR',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `products` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255) NOT NULL,
`category` VARCHAR(50) NOT NULL,
`price` DECIMAL(15, 2) NOT NULL,
`stock` INT DEFAULT 0,
`image` VARCHAR(255),
`description` TEXT,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `contacts` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`fullname` VARCHAR(100) NOT NULL,
`phone` VARCHAR(20) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`service` VARCHAR(100),
`message` TEXT,
`status` ENUM('NEW', 'PROCESSING', 'COMPLETED') DEFAULT 'NEW',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
# /etc/nginx/sites-available/globaltech.conf
server {
listen 80;
server_name globaltech-corp.com www.globaltech-corp.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name globaltech-corp.com www.globaltech-corp.com;
ssl_certificate /etc/letsencrypt/live/globaltech-corp.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/globaltech-corp.com/privkey.pem;
location / {
proxy_pass http://localhost:5000;
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;
}
}