Step 1: Install Nginx, PHP-FPM, and MySQL
sudo apt update
sudo apt install nginx mysql-server php-fpm php-mysql php-curl php-gd php-intl php-mbstring php-xml php-zip
sudo mysql_secure_installation
Install php-fpm, not the php package, which can pull in Apache and its PHP module. On Debian, use mariadb-server in place of mysql-server and run sudo mariadb-secure-installation; MySQL itself is covered in how to install MySQL on Ubuntu. Open ports 80 and 443 in the firewall. The examples use PHP 8.3 (Ubuntu 24.04); check yours with php -v and replace 8.3 with your version.
Step 2: Create the Database and User
sudo mysql
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
EXIT;
Enter the real password in the SQL shell, never on a shell command line. The user can reach only the wordpress database.
Step 3: Download and Verify WordPress
cd /tmp
curl -LO https://wordpress.org/latest.tar.gz
curl -LO https://wordpress.org/latest.tar.gz.sha1
echo "$(cat latest.tar.gz.sha1) latest.tar.gz" | sha1sum -c -
The last command must print latest.tar.gz: OK. If it does not, download again and do not install that file. Then unpack and move it:
tar -xzf latest.tar.gz
sudo mv wordpress /var/www/example.com
Step 4: Ownership and Permissions
sudo chown -R www-data:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} \;
sudo find /var/www/example.com -type f -exec chmod 644 {} \;
www-data is the user PHP-FPM runs as on Debian and Ubuntu, so WordPress can write uploads. Never use 777. More in chown recursive.
Step 5: Create wp-config.php
cd /var/www/example.com
sudo -u www-data cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
Set DB_NAME to wordpress, DB_USER to wp_user, DB_PASSWORD to your password, and leave DB_HOST as localhost. Below them are eight lines of keys and salts with placeholder text. Open the official WordPress secret key generator, whose address is in the comment above those lines, and replace all eight lines with the ones it produces. Then restrict the file:
sudo chmod 640 wp-config.php
Step 6: The Server Block
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 index.html;
client_max_body_size 64M;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~* ^/wp-content/uploads/.*\.php$ {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
- try_files: serves an existing file or folder and hands every other address to index.php, which WordPress permalinks need.
- Uploads rule: refuses to run PHP files in the uploads folder. It must come before the general PHP location, because Nginx uses the first regular expression that matches.
- client_max_body_size: larger uploads get a 413 error; see how to increase the PHP upload limit.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
On AlmaLinux and Rocky Linux, the file goes in /etc/nginx/conf.d/example.com.conf, the socket is /run/php-fpm/www.sock, and the files belong to the PHP-FPM pool user (apache by default). There is no snippets folder: in the PHP location, write include fastcgi_params; and fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; instead. With SELinux enforcing, let PHP write only to wp-content:
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/example.com/wp-content(/.*)?"
sudo restorecon -Rv /var/www/example.com
Step 7: HTTPS, Then the Installer
sudo certbot --nginx -d example.com -d www.example.com
Get the certificate before you open the installer, so the admin password is never sent over plain HTTP; see Certbot Nginx. Then open https://example.com, choose a site title, an admin user name other than admin, and a strong password.
Troubleshooting
- The browser downloads a PHP file: the PHP location is missing or the socket path is wrong; compare it with
ls /run/php/. - 502 Bad Gateway: PHP-FPM is not running; check
systemctl status php8.3-fpm. - Error establishing a database connection: the values in wp-config.php do not match the database user.
- 403 Forbidden: see 403 Forbidden in Nginx.