Installing WordPress with Nginx and PHP-FPM on Ubuntu 22.04

Contents

Introduction

WordPress is the world’s most popular content management system (CMS), powering over 40% of all websites
source. Pairing it with the performance-focused
Nginx web server and PHP-FPM on Ubuntu 22.04 ensures a fast, scalable, and secure environment.
This guide will walk you through every step, from server preparation to final WordPress configuration.

Prerequisites

  • Ubuntu 22.04 LTS server (bare-metal or VPS) with sudo privileges.
  • A registered domain name (e.g., example.com) pointed via an A record to your server’s public IP.
  • Basic command-line familiarity and understanding of Linux directory structure.
  • Firewall (ufw) access to open ports 80 and 443.

Overview of Components

Component Version on Ubuntu 22.04 Purpose
Nginx 1.18.x High-performance HTTP server/reverse proxy.
PHP-FPM 8.1 Process manager for PHP enabling better concurrency.
MariaDB 10.6 Relational database for storing WordPress data.
WordPress 6.x Open-source CMS.

1. System Update Base Packages

Always begin by synchronizing your package index and upgrading existing packages:

sudo apt update
sudo apt -y upgrade

Then install essential tools:

sudo apt install -y curl gnupg2 ca-certificates lsb-release

2. Installing Nginx

Ubuntu’s repositories include a stable Nginx build. Install and enable it:

sudo apt install -y nginx
sudo systemctl enable –now nginx

Verify by pointing your browser to http://your_server_ip/. You should see the default Nginx welcome page.

3. Secure MariaDB (or MySQL)

Install MariaDB and run the built-in security script:

sudo apt install -y mariadb-server
sudo mysql_secure_installation
  • Set a strong root password.
  • Remove anonymous users.
  • Disallow remote root login.
  • Remove test database.
  • Reload privilege tables.

More details: MariaDB Secure Installation

4. Creating WordPress Database User

sudo mysql -u root -p

Inside the MariaDB shell, run:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
CREATE USER wpuser@localhost IDENTIFIED BY strong_password
GRANT ALL PRIVILEGES ON wordpress. TO wpuser@localhost
FLUSH PRIVILEGES
EXIT

5. Installing PHP PHP-FPM

Add the Ondřej Surý PPA for up-to-date PHP if needed, or install directly:

sudo apt install -y php8.1-fpm php8.1-mysql php8.1-xml php8.1-curl php8.1-gd php8.1-mbstring php8.1-zip

Confirm PHP-FPM is running:

sudo systemctl status php8.1-fpm

PHP-FPM listens on /run/php/php8.1-fpm.sock by default and integrates seamlessly with Nginx.

6. Configuring Nginx Server Block

Create a new server block file:

sudo nano /etc/nginx/sites-available/example.com

Populate with:

server {
listen 80
listen [::]:80
server_name example.com www.example.com

root /var/www/wordpress
index index.php index.html index.htm

access_log /var/log/nginx/wordpress.access.log
error_log /var/log/nginx/wordpress.error.log

location / {
try_files uri uri/ /index.phpargs
}

location ~ .php {
include snippets/fastcgi-php.conf
fastcgi_pass unix:/run/php/php8.1-fpm.sock
}

location ~ /.ht {
deny all
}
}

Enable and test:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

7. Downloading Configuring WordPress

Fetch the latest WordPress package:

cd /tmp
curl -O https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz

Move files, set permissions, and create the configuration:

sudo mkdir -p /var/www/wordpress
sudo cp -a /tmp/wordpress/. /var/www/wordpress
sudo chown -R www-data:www-data /var/www/wordpress
sudo find /var/www/wordpress/ -type d -exec chmod 750 {}
sudo find /var/www/wordpress/ -type f -exec chmod 640 {}

Configure wp-config.php:

cd /var/www/wordpress
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
  • Set DB_NAME, DB_USER, DB_PASSWORD.
  • Generate unique Authentication Keys and Salts from
    WordPress.org.

8. Finalizing Installation via Web Interface

  1. Open your browser at http://example.com.
  2. Select language, click “Continue.”
  3. Enter Site Title, Admin Username, Password, Email.
  4. Complete installation log in to /wp-admin.

9. Enabling HTTPS (Optional but Recommended)

Secure your site with a free TLS certificate from Let’s Encrypt:

sudo apt install -y certbot python3-certbot-nginx
sudo certbot –nginx -d example.com -d www.example.com

Certbot will automatically configure HTTPS and set up renewal.
Learn more: Let’s Encrypt Docs

10. Performance Security Recommendations

  • Enable caching with plugins like WP Super Cache or W3 Total Cache.
  • Use Nginx caching (fastcgi_cache) and Gzip compression in your server block.
  • Keep all software updated: sudo apt update sudo apt upgrade.
  • Limit PHP memory and execution time in /etc/php/8.1/fpm/php.ini.
  • Install a Web Application Firewall (WAF) such as ModSecurity.

Conclusion

You now have a robust, high-performance WordPress setup running on Nginx and PHP-FPM under Ubuntu 22.04.
This environment is scalable, secure, and optimized for modern web traffic. For further tuning or troubleshooting, consult:



Acepto donaciones de BAT's mediante el navegador Brave 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *