Vì sao chọn Nginx cho VPS Ubuntu?
Nhẹ, xử lý tĩnh siêu nhanh, ít tốn RAM.
Dễ tách layer: Nginx làm reverse proxy, app thì PHP-FPM hoặc Node chạy riêng.
Cộng đồng lớn, tài liệu rõ. Nhiều “best practice” sẵn dùng.
Chuẩn bị nhanh (5 phút)
VPS Ubuntu 22.04+ đã SSH được.
Quyền sudo.
Tên miền trỏ được về IP VPS (có thể trỏ sau, nhưng trỏ trước sẽ mượt bước SSL).
Lệnh update hệ thống:
sudo apt update && sudo apt upgrade -y
Cài Nginx và mở tường lửa UFW
sudo apt install -y nginx
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status
Kiểm tra Nginx:
systemctl status nginx
Mở trình duyệt tới IP VPS để thấy trang mặc định Nginx.
Tài liệu tham khảo: Nginx Docs · Ubuntu Server Docs
Cài Database (tùy dự án)
Mình thường chọn MariaDB vì nhẹ, tương thích MySQL.
sudo apt install -y mariadb-server mariadb-client
sudo mysql_secure_installation
Gợi ý bảo mật:
Set root password nếu không dùng socket auth.
Xóa anonymous users, chặn root remote, xóa test DB, reload privileges.
Tạo database và user:
sudo mysql -u root -p
CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'password-manh';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
Tài liệu: MariaDB Knowledge Base
Cài PHP 8.3 (nếu chạy PHP-FPM)
Thêm PPA (khi cần phiên bản mới):
sudo apt install -y software-properties-common ca-certificates lsb-release apt-transport-https curl
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
Cài PHP-FPM và extensions phổ biến:
sudo apt install -y \\
php8.3 php8.3-fpm php8.3-cli php8.3-mysql php8.3-curl php8.3-mbstring \\
php8.3-xml php8.3-bcmath php8.3-intl php8.3-sqlite3 php8.3-zip \\
php8.3-gd php8.3-redis php-imagick php8.3-opcache php8.3-apcu
Gợi ý tối ưu prod (php.ini):
memory_limit = 256M
post_max_size = 64M
upload_max_filesize = 64M
max_execution_time = 120
cgi.fix_pathinfo = 0
; Opcache
opcache.enable=1
opcache.validate_timestamps=0
opcache.max_accelerated_files=20000
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
Reload dịch vụ:
sudo systemctl reload php8.3-fpm
Tài liệu: PHP Manual
Cài Node.js (nếu build frontend/SSR)
Cách nhanh qua nvm:
curl -o- <https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh> | bash
source ~/.bashrc
nvm install --lts
node -v && npm -v
Tài liệu: Node.js Docs
Tạo thư mục website và file test
Ví dụ domain example.com
sudo mkdir -p /var/www/example.com/public
sudo chown -R $USER:$USER /var/www/example.com
cat << 'PHP' | sudo tee /var/www/example.com/public/info.php
<?php phpinfo();
PHP
Cấu hình Nginx cho website (PHP-FPM)
Tạo file cấu hình:
sudo nano /etc/nginx/sites-available/example.com
Nội dung mẫu tối ưu cơ bản:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/public;
index index.php index.html;
# Security headers cơ bản
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
# Gzip tĩnh
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \\\\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_read_timeout 120s;
}
location ~ /\\\\.ht {
deny all;
}
}
Kích hoạt site và tắt site mặc định:
sudo ln -s /etc/nginx/sites-available/example.com/etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx
Tài liệu: Nginx Docs - Using PHP with PHP-FPM
Trỏ domain về VPS
Trong DNS domain, thêm bản ghi A:
@ → IP VPS
www → IP VPS
Kiểm tra propagation:
dig +short A example.com
Tài liệu: Cloudflare Docs hoặc nhà cung cấp DNS của bạn.
Cài chứng chỉ SSL (Let’s Encrypt)
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
Kiểm tra gia hạn tự động:
sudo systemctl status certbot.timer
sudo certbot renew --dry-run
Tài liệu: Certbot Docs
Kiểm tra cuối và tối ưu nhanh
Mở https://example.com thấy trang web chạy OK.
Bật HTTP/2 mặc định trên Nginx mới. Nếu chưa, thêm
listen 443 ssl http2;
trong server HTTPS.Tăng giới hạn upload khi cần:
client_max_body_size 64m;
Log rotate: đã có sẵn trên Ubuntu, xem
/etc/logrotate.d/nginx
.
Mẫu cấu hình thêm
A) Laravel / Livewire / Filament
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \\\\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}
B) Next.js SSR (Node server)
upstream next_app { server 127.0.0.1:3000; keepalive 64; }
map $http_upgrade $connection_upgrade { default upgrade; '' close; }
server {
listen 80;
server_name example.com www.example.com;
location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_pass http://next_app; }
}
C) React SPA hoặc Next static export
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/out; # thư mục build tĩnh
location / { try_files $uri /index.html; }
}
D) NestJS API
upstream nest_app { server 127.0.0.1:4000; keepalive 64; }
server {
listen 80;
server_name api.example.com;
location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://nest_app; }
}
Lỗi thường gặp (và cách mình hay xử)
502 Bad Gateway: sai socket PHP-FPM hoặc service chưa chạy → kiểm tra
php8.3-fpm
và đường dẫnfastcgi_pass
.404 khi dùng Laravel: thiếu
try_files
→ dùng đúng snippet ở trên.SSL fail với Certbot: domain chưa trỏ hoặc port 80 bị chặn → mở
Nginx Full
trên UFW, chờ DNS propagate.Tải file lớn bị fail: thêm
client_max_body_size
và reload Nginx.
Kết luận
Nếu bạn muốn website chạy nhanh, ổn định và dễ mở rộng, setup website với Nginx trên VPS Ubuntu là một lựa chọn “đãi cát thấy vàng”. Cứ theo checklist ở trên, bạn sẽ có trang web sẵn sàng nhận traffic thật. Cần nâng cấp thêm? Hãy tối ưu PHP-FPM, bật cache, hoặc chuyển sang kiến trúc reverse proxy cho microservices.