WordPress powers a large share of all websites, from blogs to WooCommerce shops. Running it on your own VPS or dedicated server with Nginx gives you full control, better performance than typical shared hosting, and the freedom to choose your server location — for example close to visitors in Asia. This guide installs WordPress on a LEMP stack the clean way, with a configuration you can maintain for years.
Prerequisites
- Nginx, MariaDB and PHP-FPM installed — follow installing a LEMP stack first.
- A domain with an A record pointing to the server.
- Shell access as a sudo user.
The examples use example.com, PHP 8.3 and the site directory /var/www/example.com.
Step 1: Create the database
sudo mariadb
CREATE DATABASE wp_example CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wp_example'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL PRIVILEGES ON wp_example.* TO 'wp_example'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Generate a strong password with openssl rand -base64 24.
Step 2: Install WP-CLI
WP-CLI is the official command-line tool for WordPress. It makes installation, updates and maintenance much faster:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Step 3: Download WordPress
sudo mkdir -p /var/www/example.com
sudo chown $USER:www-data /var/www/example.com
cd /var/www/example.com
wp core download --locale=en_US
For a Chinese-language admin, use --locale=zh_CN (Simplified) or --locale=zh_HK (Traditional, Hong Kong).
Step 4: Create wp-config.php
wp config create --dbname=wp_example --dbuser=wp_example --dbpass='a-long-random-password' --dbhost=localhost --dbcharset=utf8mb4
wp config shuffle-salts
Add a few production settings:
wp config set DISALLOW_FILE_EDIT true --raw
wp config set WP_POST_REVISIONS 10 --raw
wp config set WP_AUTO_UPDATE_CORE minor
DISALLOW_FILE_EDIT removes the theme and plugin code editor from the admin area — one less way for an attacker with a stolen password to inject code.
Step 5: Nginx configuration for WordPress
Create /etc/nginx/sites-available/example.com:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.php;
client_max_body_size 64M;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_read_timeout 120s;
}
# Security
location ~ /\.(?!well-known) { deny all; }
location = /xmlrpc.php { deny all; } # remove if you use Jetpack or the mobile app
location ~* /wp-content/uploads/.*\.php$ { deny all; }
location ~* /(wp-config\.php|readme\.html|license\.txt)$ { deny all; }
# Static files
location ~* \.(css|js|jpg|jpeg|png|gif|webp|avif|svg|ico|woff2?)$ {
expires 30d;
access_log off;
try_files $uri =404;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; try_files $uri /index.php?$args; }
}
The try_files … /index.php?$args line makes WordPress “pretty permalinks” work without any rewrite rules.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Step 6: HTTPS
sudo certbot --nginx -d example.com -d www.example.com --redirect
See Nginx and Let’s Encrypt for details.
Step 7: Install WordPress
cd /var/www/example.com
wp core install --url="https://example.com" --title="My Site" \
--admin_user="not-admin" --admin_password="another-long-password" \
--admin_email="you@example.com" --skip-email
Do not use admin as the username — it is the first name bots try. Log in at https://example.com/wp-admin/.
Step 8: Permissions
sudo chown -R $USER:www-data /var/www/example.com
find /var/www/example.com -type d -exec chmod 750 {} \;
find /var/www/example.com -type f -exec chmod 640 {} \;
chmod -R 770 /var/www/example.com/wp-content/uploads
chmod 640 /var/www/example.com/wp-config.php
With this layout, PHP can read everything but write only to uploads. That stops most malware from modifying core files — but it also means plugin and theme updates from the dashboard will ask for FTP details. Two options:
- Recommended: update with WP-CLI as your own user:
wp plugin update --all,wp theme update --all,wp core update. - Convenient: make
wp-contentwritable by the group (chmod -R 770 wp-content) and adddefine('FS_METHOD', 'direct');towp-config.php. Easier, but less protection.
Step 9: First settings
- Settings → Permalinks: choose “Post name” for clean URLs.
- Settings → General: confirm the site URL uses
https://. - Settings → Discussion: turn off comments if you do not need them; spam comments are a constant nuisance.
- Delete the default “Hello world” post, sample page and unused themes (keep one default theme as a fallback).
- Install only plugins you need. Every plugin is code that must be kept updated.
Step 10: Real cron
WordPress runs scheduled tasks (“WP-Cron”) when someone visits the site. On low-traffic sites they run late; on busy sites they run too often. Use the system cron instead:
wp config set DISABLE_WP_CRON true --raw
crontab -e
*/5 * * * * cd /var/www/example.com && /usr/local/bin/wp cron event run --due-now >/dev/null 2>&1
Security hardening
- Strong unique passwords and two-factor authentication for every admin (several free plugins add 2FA).
- Limit login attempts with Fail2ban and Nginx rate limiting.
- Keep core, plugins and themes updated weekly:
wp core update && wp plugin update --all && wp theme update --all. - Back up files and database off the server: restic and MySQL backups.
Performance
A default WordPress install is fast enough for small sites. For more traffic, add page caching, Redis object caching and image optimisation — all covered in WordPress speed optimisation. If many of your visitors are in mainland China, also read speeding up a website for China — themes often load Google Fonts, which can stall pages there.
Troubleshooting
| Problem | Fix |
|---|---|
| 404 on every page except home | try_files $uri $uri/ /index.php?$args; missing |
| “Error establishing a database connection” | Check wp config get DB_PASSWORD, test with mariadb -u wp_example -p |
| Redirect loop after enabling HTTPS | Site URL mismatch; behind Cloudflare, use Full (strict) SSL mode |
| Upload fails for large files | Raise client_max_body_size, upload_max_filesize, post_max_size |
| White screen | Enable WP_DEBUG_LOG and read wp-content/debug.log |