Skip to main content

Business tech · Intermediate · 30 min

How to Install WordPress on Ubuntu 26.04 with Nginx and PHP 8.5

By Padding · Updated 16 Nov 2026
Ubuntu 26.04.1 LTS · WordPress 7.1.4 · Nginx 1.30 · PHP 8.5 · MySQL 8.4 · WP-CLI 2.12.0

You will install WordPress 7.1 on Ubuntu 26.04 with Nginx 1.30, PHP-FPM 8.5 and MySQL 8.4, using WP-CLI so no password goes on a command line, a dedicated PHP user, the official salt keys, hardened file permissions and the risky XML-RPC and uploads-PHP paths closed.

On this page

WordPress runs a large share of the web: blogs, company sites and shops. Installing it is simple to get nearly right and dangerously wrong. A typical shortcut leaves the database password on a command line, lets the web server own and rewrite all the code, and leaves old entry points open that attackers probe all day.

This tutorial builds the stack on your own Ubuntu 26.04 server: Nginx faces the internet, PHP-FPM 8.5 runs WordPress's PHP code as a dedicated user, and MySQL 8.4, from the Ubuntu archive, stores the content. You install and configure WordPress with WP-CLI, its command-line tool, which lets you do each step with a command you can repeat, and lets you keep passwords out of your shell history. The result is a site you can log in to, with permissions that follow WordPress's own hardening guide, https://developer.wordpress.org/advanced-administration/security/hardening/.

Nginx is not installed here. Follow that tutorial first, which also gives you the nginx user used below. For the database layer in more depth, see the MySQL tutorial; WordPress's hosting handbook lists the supported database versions (https://make.wordpress.org/hosting/handbook/server-environment/): MySQL 9.7 LTS and 8.4 LTS, and MariaDB 12.3, 11.8, 11.4 and 10.11 LTS. The MySQL 8.4 in Ubuntu's archive is on that list.

Step 1 · Install PHP-FPM and MySQL

WordPress needs PHP with a handful of extensions: mysql to talk to the database, curl for updates and remote requests, gd for resizing images, and mbstring, xml, zip and intl for text, feeds and archives. PHP-FPM is the process manager that runs it for Nginx. Install all of them with MySQL:

Bash
sudo apt update
sudo apt install -y php8.5-fpm php8.5-mysql php8.5-curl php8.5-gd php8.5-intl php8.5-mbstring php8.5-xml php8.5-zip mysql-server curl unzip

The packages start MySQL and PHP-FPM for you. The Ubuntu MySQL package listens on the loopback address only. Confirm that it does, because a database reachable from the internet is a common cause of break-ins:

Bash
ss -tln | grep 3306
php-fpm8.5 --version | head -n 1

The MySQL line shows 127.0.0.1:3306 (and perhaps 127.0.0.1:33060, the X protocol), never 0.0.0.0 or *. The PHP line starts with PHP 8.5..

Step 2 · Create the database and its user

WordPress gets its own database and a user that can reach nothing else. Generate a long random password into a shell variable, and send the SQL to mysql on standard input, so the password never appears in the process list or in ~/.mysql_history. The mysql command here runs as the MySQL root account, which on Ubuntu you reach with sudo and no password:

Bash
DB_PW="$(openssl rand -hex 16)"
sudo mysql <<SQL
CREATE DATABASE wordpress CHARACTER SET utf8mb4;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY '${DB_PW}';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
SQL

Keep this terminal open until you finish the configuration step, because DB_PW lives only in it. The next steps hand it to WordPress through a prompt. If you lose it before then, set a new one with ALTER USER 'wpuser'@'localhost' IDENTIFIED BY '...' and repeat.

Check it worked

Bash
sudo mysql -e "SHOW GRANTS FOR 'wpuser'@'localhost'"

The grants show ALL PRIVILEGES ON \`wordpress\`.* and nothing else apart from the default usage line.

Step 3 · Install WP-CLI

WP-CLI is a single PHP archive (a .phar file). Download the 2.12.0 release and its checksum from the project's GitHub release page, check it, and install it as the command wp:

Bash
cd "$(mktemp -d)"
BASE="https://github.com/wp-cli/wp-cli/releases/download/v2.12.0"
curl -fsSLO "${BASE}/wp-cli-2.12.0.phar"
curl -fsSLO "${BASE}/wp-cli-2.12.0.phar.sha512"
echo "$(cat wp-cli-2.12.0.phar.sha512)  wp-cli-2.12.0.phar" | sha512sum -c -
sudo install -o root -g root -m 0755 wp-cli-2.12.0.phar /usr/local/bin/wp
wp --version

The checksum line ends in OK, and wp --version prints WP-CLI 2.12.0. WP-CLI is not an apt package here, so apt upgrade will never update it. The upgrade steps are in the maintenance step below.

Step 4 · Create the PHP user and download WordPress

PHP-FPM will run WordPress as its own user, wordpress, with no password and no login shell. Its home folder is /var/lib/wordpress, outside the web root, because WP-CLI keeps a download cache in the home folder and that cache must not be served to visitors. That user also owns the files, which is what lets WordPress update itself, and you run WP-CLI as that user, so that the files it creates have the right owner:

Bash
sudo useradd --system --user-group --create-home --home-dir /var/lib/wordpress --shell /usr/sbin/nologin wordpress
sudo install -d -o wordpress -g wordpress -m 755 /var/www/wordpress
cd /var/www/wordpress
sudo -u wordpress wp core download

WP-CLI downloads the newest release, checks it against the checksum that wordpress.org publishes, and unpacks it. It ends with Success: WordPress downloaded.

Check it worked

Bash
cd /var/www/wordpress
sudo -u wordpress wp core version
ls
sudo ls -A /var/lib/wordpress

The version starts with 7.1. (7.1.4 when this was written), and the first listing has index.php, wp-admin, wp-content, wp-includes and the other core files. The second shows .wp-cli, the cache, and a few starter files for the shell. Neither is under the web root.

Step 5 · Create wp-config.php with your secrets

wp-config.php holds the database login and a set of secret keys, called salts, that WordPress uses to sign login cookies. wp config create writes the file, and fetches fresh random salts from WordPress's own generator, https://api.wordpress.org/secret-key/1.1/salt/, so you never copy keys by hand.

Two details keep it safe. --prompt=dbpass makes WP-CLI read the database password from standard input instead of taking it as an option, so it stays out of the process list. And umask 077 makes the new file private from the start, readable only by its owner. The command feeds it from the DB_PW variable you made earlier, so you do not retype it:

Bash
cd /var/www/wordpress
printf '%s\n' "$DB_PW" | (umask 077; sudo -u wordpress wp config create --dbname=wordpress --dbuser=wpuser --dbcharset=utf8mb4 --prompt=dbpass)

WP-CLI echoes the full command it runs, including the password, to your terminal. That stays on your screen only, but clear the screen afterwards and do not paste the output anywhere. The command ends with Success: Generated 'wp-config.php' file.

Turn off the file editors in the WordPress admin area. With them on, an attacker who steals an admin login can edit PHP code from the browser. DISALLOW_FILE_EDIT removes that option, as the hardening guide recommends:

Bash
cd /var/www/wordpress
sudo -u wordpress wp config set DISALLOW_FILE_EDIT true --raw

Check it worked

Bash
cd /var/www/wordpress
sudo ls -l wp-config.php
sudo -u wordpress wp config get DB_USER
sudo -u wordpress wp config has AUTH_KEY && echo "AUTH_KEY is set"
sudo -u wordpress wp config get AUTH_KEY | grep -qv 'put your unique phrase here' && echo "AUTH_KEY is not the sample text"

The file shows -rw------- with owner and group wordpress. The user is wpuser, AUTH_KEY is set confirms the salt exists, and AUTH_KEY is not the sample text confirms it was generated. To replace the salts later, which signs everybody out (for example after a suspected leak), run sudo -u wordpress wp config shuffle-salts.

Step 6 · Set the file permissions

The hardening guide says directories should be 755 and files 644, writable only by their owner, with a stricter mode for wp-config.php. WP-CLI already created the files that way, and these two commands set the same modes explicitly, so you can run them again any time a deploy or an upload leaves the wrong ones. Neither touches wp-config.php's 600:

Bash
sudo find /var/www/wordpress -type d -exec chmod 755 {} +
sudo find /var/www/wordpress -type f ! -name wp-config.php -exec chmod 644 {} +

This model means the PHP user owns the code, so WordPress can install updates and plugins from its admin area. The price is that an attacker who gets PHP code running on the site can rewrite that code too. A stricter setup leaves the code owned by an admin user, makes only wp-content/uploads writable by PHP, and applies updates with WP-CLI as that admin. If you do that, add define( 'DISALLOW_FILE_MODS', true ); to wp-config.php so the dashboard does not offer updates it cannot apply. The guide describes both models.

Step 7 · Configure PHP-FPM for WordPress

Give the site its own PHP-FPM pool. It runs as wordpress, listens on a Unix socket that only the nginx group can reach, and sets upload limits for media. The packaged default pool is not needed, so the second command renames it, because PHP-FPM only loads files that end in .conf:

Bash
sudo tee /etc/php/8.5/fpm/pool.d/wordpress.conf > /dev/null <<'INI'
[wordpress]
user = wordpress
group = wordpress

listen = /run/php/wordpress.sock
listen.owner = wordpress
listen.group = nginx
listen.mode = 0660

pm = dynamic
pm.max_children = 8
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

php_admin_value[upload_max_filesize] = 64M
php_admin_value[post_max_size] = 64M
php_admin_value[memory_limit] = 256M
INI
sudo mv /etc/php/8.5/fpm/pool.d/www.conf /etc/php/8.5/fpm/pool.d/www.conf.disabled
sudo php-fpm8.5 --test

The test ends with configuration file /etc/php/8.5/fpm/php-fpm.conf test is successful. pm.max_children = 8 caps the PHP workers. A WordPress worker commonly uses 50 to 100 MB, so multiply what you measure by this number and keep it well below your free memory. Restart PHP-FPM to load the pool:

Bash
sudo systemctl restart php8.5-fpm
sudo systemctl enable php8.5-fpm

Check it worked

Bash
ls -l /run/php/wordpress.sock

The socket exists, with permissions srw-rw----, owner wordpress and group nginx.

Step 8 · Configure Nginx

Write the site. Replace example.com with your domain. The try_files line sends every address that is not a real file to WordPress's index.php, which is what makes pretty links work. The last blocks close paths that WordPress sites get attacked through:

Bash
sudo tee /etc/nginx/conf.d/wordpress.conf > /dev/null <<'NGINX'
server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/wordpress;
    index index.php;
    client_max_body_size 64m;

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

    # Old remote-publishing API; a favourite for password guessing and abuse.
    # Remove this block only if a tool you use needs XML-RPC.
    location = /xmlrpc.php {
        return 403;
    }

    # Uploaded files must never run as PHP. This block must come before the
    # PHP block: Nginx uses the first matching regular expression.
    location ~* ^/wp-content/uploads/.*\.php$ {
        return 403;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/wordpress.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }

    # Hidden files such as .git or .env, but not /.well-known for HTTPS certificates.
    location ~ /\.(?!well-known) {
        deny all;
    }
}
NGINX
sudo nginx -t

nginx -t says syntax is ok and test is successful. The three security blocks do this:

  • xmlrpc.php is WordPress's legacy remote API. Mobile apps and some plugins used it, and the WordPress REST API replaced it. It lets a login be tried many times in one request. Most sites are better off with it closed.
  • Files under uploads can come from visitors. If a PHP file ever lands there, serving it as code would hand an attacker control of the site, so Nginx refuses. The order matters: Nginx tries regular-expression locations from top to bottom and uses the first that matches, so this block has to sit above the general .php block. Placed below it, the rule would never run.
  • fastcgi_hide_header removes the X-Powered-By header, which tells visitors your PHP version.

One more thing to remove. The Nginx package from nginx.org ships a welcome site, /etc/nginx/conf.d/default.conf, that listens on port 80. A request whose name matches no server_name, such as one made to the server's bare IP address, goes to the first server Nginx loads, which is that welcome site, not WordPress. If you test by IP address, or want unknown names to fail, remove it. This deletes only the sample page:

Bash
sudo rm -f /etc/nginx/conf.d/default.conf
sudo nginx -t

Apply it:

Bash
sudo systemctl reload nginx

Step 9 · Run the WordPress installer with WP-CLI

Now install the site: it creates the tables and the first administrator. --url must match how visitors reach the site. Use your real domain, http:// for now, because HTTPS comes in a later step. You do not pass --admin_password: when it is missing, WP-CLI generates a strong password and prints it once at the end, so no password is on the command line. Replace the title and email with your own:

Bash
cd /var/www/wordpress
sudo -u wordpress wp core install --url=http://example.com --title="My Site" --admin_user=siteadmin --admin_email=you@example.com --skip-email

The last lines are Admin password: followed by a generated password, and Success: WordPress installed successfully. Copy the password to your password manager now: it is not shown again. Choose a username that is not admin, because that is the name guessers try first. To replace the generated password with your own, run the prompt below, which reads it without showing it and without putting it on the command line:

Bash
sudo -u wordpress wp user update siteadmin --prompt=user_pass

Turn on readable permalinks (/my-post-title/). This stores the setting in the database. Nginx needs nothing more because of the try_files line:

Bash
cd /var/www/wordpress
sudo -u wordpress wp rewrite structure '/%postname%/'

Check it worked

Ask WP-CLI, then ask Nginx. If your domain does not point at the server yet, send the Host header yourself, as below:

Bash
cd /var/www/wordpress
sudo -u wordpress wp core is-installed && echo "WordPress is installed"
curl -s -H 'Host: example.com' http://127.0.0.1/ | grep -o '<title>[^<]*</title>'
curl -s -o /dev/null -w 'login page: %{http_code}\n' -H 'Host: example.com' http://127.0.0.1/wp-login.php
curl -s -o /dev/null -w 'xmlrpc: %{http_code}\n' -H 'Host: example.com' http://127.0.0.1/xmlrpc.php
curl -s -H 'Host: example.com' http://127.0.0.1/wp-json/ | grep -o '"name":"My Site"'

You see WordPress is installed, <title>My Site</title>, login page: 200, xmlrpc: 403, and "name":"My Site" from the REST API, which proves the pretty-link routing works. In a browser, open http://example.com/wp-login.php and log in as siteadmin.

Troubleshooting

  • 502 Bad Gateway: Nginx cannot reach PHP-FPM. Check that /run/php/wordpress.sock exists, that its group is nginx, and that PHP-FPM is running with sudo systemctl status php8.5-fpm.
  • Error establishing a database connection: the password or user in wp-config.php does not match MySQL. Test with sudo -u wordpress wp db check.
  • A blank page or 403 from PHP: file ownership is wrong. Run sudo chown -R wordpress:wordpress /var/www/wordpress and the permission commands again.
  • The browser redirects to another address: siteurl and home do not match the domain you use. Fix them with sudo -u wordpress wp option update siteurl 'http://example.com' and the same for home.
  • Uploads fail: client_max_body_size in Nginx and upload_max_filesize and post_max_size in the pool must all be large enough.

Step 10 · Add HTTPS

Passwords and login cookies must not travel over plain HTTP. Get a free certificate with Certbot as in free HTTPS with Let's Encrypt and Nginx, and open ports 80 and 443 in the firewall. Then tell WordPress its new address, and force the admin area onto HTTPS. Replace example.com:

Bash
cd /var/www/wordpress
sudo -u wordpress wp option update home 'https://example.com'
sudo -u wordpress wp option update siteurl 'https://example.com'
sudo -u wordpress wp config set FORCE_SSL_ADMIN true --raw

If you also have posts that link to the old http:// address, wp search-replace 'http://example.com' 'https://example.com' --dry-run shows what would change before you run it without --dry-run. Take a backup first: back up and restore a WordPress site with WP-CLI.

Step 11 · Tidy up and keep it updated

WordPress ships with two plugins, Hello Dolly (a sample) and Akismet (spam filtering), both switched off. Remove what you do not use, since every installed plugin is code that needs patching:

Bash
cd /var/www/wordpress
sudo -u wordpress wp plugin list --fields=name,status,update
sudo -u wordpress wp plugin delete hello akismet

The list shows the two bundled plugins as inactive, and the delete command ends with Success: Deleted 2 of 2 plugins. Keep Akismet if you want its spam filtering.

WordPress applies minor releases of itself automatically by default. Everything else you update. Plugins and themes are the usual way in, so update them often. Check what is waiting, and apply it, with these commands. Take a backup before updating:

Bash
cd /var/www/wordpress
sudo -u wordpress wp core check-update
sudo -u wordpress wp plugin list --update=available

On a fresh install the first command says Success: WordPress is at the latest version. and the second prints nothing, because no plugin update is waiting.

Bash
sudo -u wordpress wp core update
sudo -u wordpress wp plugin update --all
sudo -u wordpress wp theme update --all

WP-CLI itself, as a hand-installed program, is not covered by apt upgrade. To upgrade, download the newer release and checksum from https://github.com/wp-cli/wp-cli/releases, check it as in the install step, and sudo install it over /usr/local/bin/wp. wp cli check-update tells you whether a newer version exists. The MySQL and PHP packages do update through apt, so run sudo apt update && sudo apt upgrade regularly, or set up automatic security updates.

Two more things to do for a public site. Limit login attempts: put a rate limit on /wp-login.php in Nginx, as in rate limit an API with Nginx, and consider two-factor login for administrators. And make sure the database is backed up, which the next tutorial covers.

Step 12 · Remove everything

If this was a trial, remove the site. These commands delete the WordPress files, the database and its user, the web server and PHP-FPM configuration, and the WordPress user. The database deletion cannot be undone:

Bash
sudo rm /etc/nginx/conf.d/wordpress.conf
sudo systemctl reload nginx
sudo rm /etc/php/8.5/fpm/pool.d/wordpress.conf
sudo mv /etc/php/8.5/fpm/pool.d/www.conf.disabled /etc/php/8.5/fpm/pool.d/www.conf
sudo systemctl restart php8.5-fpm
sudo mysql -e "DROP DATABASE wordpress; DROP USER 'wpuser'@'localhost';"
sudo rm -rf /var/www/wordpress
sudo userdel --remove wordpress
sudo rm /usr/local/bin/wp

If you added HTTPS and firewall rules, remove those too. certbot delete removes the certificate you got for the site, and the numbered firewall list shows the rules to remove. Replace <number> with the number of the rule you opened for this site, and check the list again after each deletion, because the numbers shift:

Bash
sudo certbot delete --cert-name example.com
sudo ufw status numbered
sudo ufw delete <number>

The Nginx, PHP and MySQL packages stay installed. If you will not use them, remove them with apt purge.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.