vi

LEMP Stack VPS: Linux Nginx MySQL PHP Offshore

LEMP Stack (Linux, Nginx, MySQL, PHP) là nền tảng server phổ biến nhất cho PHP web applications. Khác với LAMP truyền thống dùng Apache, LEMP sử dụng Nginx giúp tăng đáng kể hiệu suất, đặc biệt cho concurrent connections cao. Hướng dẫn này cài đặt toàn bộ LEMP stack trên VPS offshore AnubizHost từ đầu đến khi sẵn sàng host website production.

Need this done for your project?

We implement, you ship. Async, documented, done in days.

Start a Brief

Cài Đặt Nginx

Bắt đầu với cập nhật hệ thống và cài đặt Nginx:

apt update && apt upgrade -y
apt install -y nginx
systemctl enable nginx
systemctl start nginx

Cấu hình firewall:

ufw allow OpenSSH
ufw allow 'Nginx Full'
ufw enable
ufw status

Test Nginx bằng cách truy cập IP của VPS trong trình duyệt - bạn sẽ thấy trang chào mừng mặc định. Kiểm tra version:

nginx -v

Cài Đặt MySQL

apt install -y mysql-server
systemctl enable mysql
mysql_secure_installation

Trong quá trình mysql_secure_installation:

  • Thiết lập VALIDATE PASSWORD plugin - chọn MEDIUM hoặc STRONG
  • Đặt password root mạnh
  • Remove anonymous users: YES
  • Disallow root login remotely: YES
  • Remove test database: YES
  • Reload privilege tables: YES

Tạo database và user cho application:

mysql -u root -p
CREATE DATABASE webapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'Password@123';
GRANT ALL PRIVILEGES ON webapp.* TO 'webuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Cài Đặt PHP-FPM

apt install -y software-properties-common
add-apt-repository ppa:ondrej/php -y
apt update
apt install -y php8.3-fpm php8.3-mysql php8.3-xml     php8.3-mbstring php8.3-curl php8.3-zip php8.3-gd     php8.3-intl php8.3-bcmath php8.3-imagick
systemctl enable php8.3-fpm
systemctl start php8.3-fpm

Tối ưu php.ini cho production:

nano /etc/php/8.3/fpm/php.ini
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
memory_limit = 256M
expose_php = Off
systemctl restart php8.3-fpm

Cấu Hình Nginx Virtual Host với PHP

Tạo server block cho website PHP:

mkdir -p /var/www/mysite/public
nano /etc/nginx/sites-available/mysite
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/mysite/public;
    index index.php index.html;

    client_max_body_size 64M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.ht {
        deny all;
    }
}
ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx

Test PHP-FPM hoạt động:

echo "" > /var/www/mysite/public/info.php

Truy cập http://yourdomain.com/info.php để xác nhận PHP chạy đúng. Xóa file này sau khi test xong vì lý do bảo mật.

Why Anubiz Host

100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included

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.

Anubiz Chat AI

Online