WordPress LEMP Installation: Ubuntu 开发环境 24.04 + Nginx 性能调优 + PHP 8.3 + MySQL 8.0 Complete Tutorial
Most WordPress installation guides either target Ubuntu 20.04 (outdated) or skip the critical config differences that will break your site at 2AM. After installing LEMP on two different VPS 配置避坑 providers and hitting every pitfall, I documented exactly what works on Ubuntu 24.04 LTS with PHP 8.3.
Why LEMP Instead of cPanel,aaPanel, or 1Panel
cPanel and Chinese alternatives like 1Panel are fine for quick deployments. But if you want:
- Actual understanding of what's running (not clicking buttons)
- Production stability without panel lock-in
- Saving ¥30-80/month in panel fees
LEMP is the cleaner choice. WordPress officially recommends LEMP for production, and I've run it for 18 months across 3 sites without a single crash from the stack itself.
Prerequisites
- VPS running Ubuntu 24.04 LTS (not 22.04 — PHP versions differ)
- SSH access with a sudo-enabled non-root user
- Domain with A record pointing to your VPS IP
Step 1 — Server Initialization
Update package lists and upgrade:
sudo apt update && sudo apt upgrade -y
Create a dedicated WordPress user (don't run services as root):
sudo adduser wpuser
sudo usermod -aG www-data wpuser
The www-data group gives Nginx proper file access later.
Step 2 — Install Nginx
sudo apt install nginx -y
Nginx starts automatically. Verify:
sudo systemctl status nginx
Should show Active: active (running).
Configure UFW firewall if enabled:
sudo ufw allow 'Nginx Full'
Visit http://your_server_ip in a browser — you should see the Nginx welcome page.
⚠️ Trap 1: UFW enabled but Nginx HTTP not allowed
If UFW shows Status: inactive, no issue. If it's active but you didn't allow Nginx HTTP, external requests won't reach Nginx.
Step 3 — Install MySQL 8.0
sudo apt install mysql-server -y
Run secure initialization:
sudo mysql_secure_installation
Interactive prompts:
- Enable password validation (VALIDATE PASSWORD): recommend **No** for dev/staging
- Set root password: enter and confirm
- Remove anonymous users: **Yes**
- Disallow root login remotely: **Yes**
- Remove test database: **Yes**
- Reload privilege tables: **Yes**
Create a dedicated WordPress database and user (skipping this and using root is a security risk):
sudo mysql
At the MySQL prompt:
CREATE DATABASE wordpress_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpadmin'@'localhost' IDENTIFIED BY 'your_own_strong_password_here';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wpadmin'@'localhost';
FLUSH PRIVILEGES;
EXIT;
⚠️ Trap 2: Wrong database charset breaks Chinese characters
WordPress stores Emojis and Chinese comments as utf8mb4. If you use the default utf8 charset, Chinese comments become garbled. The command above explicitly uses utf8mb4.
Step 4 — Install PHP 8.3-FPM and Required Extensions
Ubuntu 24.04 ships PHP 8.3 by default (not 8.1 as many guides still show). Install:
sudo apt install php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-xmlrpc php8.3-zip php8.3-bcmath php8.3-intl -y
These extensions cover WordPress core + most popular plugins:
- php8.3-mysql: database connection
- php8.3-gd: image processing (required by plugins like Imagify)
- php8.3-bcmath: high-precision math (required by Stripe payment plugins)
- php8.3-intl: internationalization (required by multilingual plugins)
⚠️ Trap 3: Ubuntu 24.04 socket path changed
Many tutorials write /var/run/php/php8.1-fpm.sock. On Ubuntu 24.04, it's /run/php/php8.3-fpm.sock. Wrong path = 502 Bad Gateway when accessing PHP files.
Verify PHP:
php -v
# Output should show: PHP 8.3.x
Step 5 — Configure Nginx Site
Create site config:
sudo nano /etc/nginx/sites-available/wordpress
Paste this content (note the correct socket path):
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/wordpress;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
}
Enable site and reload:
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
⚠️ Trap 4: Editing sites-available without re-symlinking
If you modify sites-available, you must re-create the symlink in sites-enabled. Deleting the symlink takes the site offline immediately.
Create WordPress directory:
sudo mkdir -p /var/www/wordpress
sudo chown -R www-data:www-data /var/www/wordpress
Step 6 — Install WordPress (CLI, no browser needed)
Download WordPress:
cd /var/www/wordpress
sudo -u www-data curl -O https://wordpress.org/latest.tar.gz
sudo -u www-data tar -xzf latest.tar.gz --strip-components=1
sudo rm latest.tar.gz
⚠️ Trap 5: Permission errors when installing plugins
If directory is owned by root, WordPress admin can't write files. The chown -R www-data:www-data above is critical — many tutorials miss it.
Configure wp-config.php:
sudo -u www-data nano /var/www/wordpress/wp-config.php
Add database credentials (replace with values from Step 3):
define('DB_NAME', 'wordpress_db');
define('DB_USER', 'wpadmin');
define('DB_PASSWORD', 'your_password');
define('FS_METHOD', 'direct');
⚠️ Key: Insufficient DB user privileges cause WordPress installer errors
If you see "Error establishing database connection" at http://your_domain, go back to Step 3 and verify credentials. Test connection:
mysql -u wpadmin -p
# Enter password, should connect successfully
If MySQL connects but WordPress still fails, add to wp-config.php:
define('DB_HOST', 'localhost');
Step 7 — Configure SSL (Let's Encrypt Free Certificate)
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Follow prompts: enter email (for expiry notices), accept ToS, certbot handles validation and certificate installation automatically.
Certbot modifies Nginx config automatically, adding HTTPS settings and a 301 redirect from HTTP to HTTPS.
Test auto-renewal:
sudo certbot renew --dry-run
Output should include Congratulations, all renewals succeeded. If it does, your certificates will auto-renew before expiry.
Verify Installation
Visit https://your_domain — you should see the WordPress setup wizard. Fill in site title, admin username, and password to complete installation.
Quick functionality checks:
- Upload an image, verify Media Library works
- Install a plugin, verify write access works
- Visit `https://your_domain/wp-admin/`, verify admin panel loads
LEMP vs Docker vs Panel Solutions: Which to Choose
| Approach | Best For | Pros | Cons |
|---|---|---|---|
| LEMP direct install | Learning, tech-savvy users | Low resource usage, full control | Manual maintenance |
| Docker | Container-experienced users | Isolation, one-command deploy | Higher learning curve |
| Panel (cPanel/1Panel) | Fast deployment, budget available | GUI, beginner-friendly | Lock-in, monthly fees |
LEMP is the most resource-efficient. My 3 WordPress sites run on LEMP with 1GB RAM VPS, PHP-FPM process management is more efficient than Docker.
Troubleshooting
**502 Bad Gateway**: Verify PHP-FPM socket path (/run/php/php8.3-fpm.sock on Ubuntu 24.04)
Database connection failed: Confirm Step 3 password matches Step 6 wp-config.php
**Plugin upload fails**: Check /var/www/wordpress ownership is www-data:www-data
SSL certificate fails: Verify domain DNS A record points to server IP (DNS propagation can take 10 min to 48 hours)
Performance Optimization (Optional but Recommended)
After LEMP is running, WordPress needs additional tuning to be fast. At minimum:
1. **PHP opcode cache**: apt install php8.3-opcache, then enable opcache.enable=1 in php.ini
2. **Nginx caching**: configure fastcgi_cache or proxy_cache
3. **PHP-FPM process tuning**: edit /etc/php/8.3/fpm/pool.d/www.conf, adjust pm.max_children based on available RAM
I'll cover WordPress performance optimization in a dedicated article (WordPress speed optimization 2026 series).
Verified Information
🔗 Related Tech Articles
Deep dive into related technical topics: