Install and Configure WordPress on Ubuntu 24.04 with LEMP

WordPress is an open-source content management system (CMS) developed with PHP and supported by MySQL or MariaDB. Originally a blogging tool, it has evolved into one of the most popular website platforms, powering more than 22% of the top one million websites as of December 2024.

This guide details the process of installing and setting up WordPress on an Ubuntu 24.04 server with the LEMP stack (Linux, Nginx, MySQL, PHP). You will install the required software, secure your setup, and configure WordPress to serve dynamic sites from your own server.

Prerequisites

Before proceeding, ensure you meet the following requirements:

  • Access to an Ubuntu 24.04 server using a non-root user with sudo privileges.
  • A domain A record pointing to your server IP (for example, www.example.com) if using a custom domain.

Install Nginx Web Server

First, install Nginx on your system:

$ sudo apt install nginx -y

Start the Nginx service:

$ sudo systemctl start nginx

Enable Nginx to launch at system boot:

$ sudo systemctl enable nginx

Check the service status to confirm Nginx is running:

$ sudo systemctl status nginx

Sample Output:

● nginx.service - nginx - high performance web server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled)
     Active: active (running) since Sat 2025-04-26 13:18:09 WAT; 9min ago
       Docs: https://nginx.org/en/docs/
   Main PID: 1629 (nginx)
...

Configure the Firewall

Next, adjust the firewall settings with UFW (Uncomplicated Firewall) to allow HTTP and HTTPS traffic.

Check the current firewall status:

Allow HTTP and HTTPS using the Nginx Full profile:

$ sudo ufw allow "Nginx Full"

Reload UFW to apply changes:

Verify the updated firewall rules:

Expected Output:

To                         Action      From
--                         ------      ----
Nginx Full                 ALLOW       Anywhere
Nginx Full (v6)            ALLOW       Anywhere (v6)

Install MySQL Database

Next, install and configure the MySQL database server for WordPress.

Install the MySQL server package:

$ sudo apt install mysql-server -y

Check that MySQL was installed properly:

Sample Output:

mysql  Ver 8.0.36 for Linux on x86_64 (MySQL Community Server - GPL)

Start the MySQL service:

$ sudo systemctl start mysql

Enable MySQL to launch at startup:

$ sudo systemctl enable mysql

Verify that the service is active and running:

$ sudo systemctl status mysql

Sample Output:

● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled)
     Active: active (running) since Sat 2025-04-26 13:42:56 WAT; 2min ago
   Main PID: 43442 (mysqld)
...

For security, run the built-in script to strengthen your MySQL installation:

$ sudo mysql_secure_installation

Create a WordPress Database and User

WordPress needs a separate MySQL database to store content, settings, and user data. Follow these steps to create one:

Log into the MySQL shell as the root user:

Create the database for WordPress:

mysql> CREATE DATABASE wordpressdb;

Create a dedicated MySQL user with a strong password:

mysql> CREATE USER 'wordpressdbuser'@'localhost' IDENTIFIED BY 'strongpassword';

Grant all privileges on the WordPress database to this user:

mysql> GRANT ALL PRIVILEGES ON wordpressdb.* TO 'wordpressdbuser'@'localhost';

Apply the new privileges:

Verify the database creation:

Sample Output:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpressdb        |
+--------------------+

Check that the user was created:

mysql> SELECT User, Host FROM mysql.user WHERE User = 'wordpressdbuser';

Sample Output:

+-----------------+-----------+
| User            | Host      |
+-----------------+-----------+
| wordpressdbuser | localhost |
+-----------------+-----------+

Confirm the privileges assigned to the user:

mysql> SHOW GRANTS FOR 'wordpressdbuser'@'localhost';

Sample Output:

+--------------------------------------------------------------------------+
| Grants for wordpressdbuser@localhost                                     |
+--------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `wordpressdbuser`@`localhost`                      |
| GRANT ALL PRIVILEGES ON `wordpressdb`.* TO `wordpressdbuser`@`localhost` |
+--------------------------------------------------------------------------+

Exit the MySQL shell:

Install PHP and Extensions

WordPress requires PHP to generate dynamic content and connect with the MySQL database. Begin by installing PHP and the necessary modules:

Install PHP and required packages:

$ sudo apt install php php-cli php-common php-imap php-fpm php-snmp php-xml php-zip php-mbstring php-curl php-mysqli php-gd php-intl -y

Check the installed PHP version:

Sample Output:

PHP 8.3.X (cli) (built: ...)

Confirm php-fpm is active and running:

$ sudo systemctl status php8.3-fpm

Download and Install WordPress

Now, download and prepare WordPress:

Download the latest WordPress release:

$ wget http://wordpress.org/latest.tar.gz

Extract the downloaded archive:

$ sudo tar -xvzf latest.tar.gz

Move WordPress files to the Nginx web root directory:

$ sudo mv wordpress/* /var/www/html/

Set file ownership to the Nginx user:

$ sudo chown -R www-data:www-data /var/www/html

Switch to the web root directory:

Remove default Nginx placeholder files:

$ sudo rm index.html index.nginx-debian.html

Rename the sample WordPress configuration file:

$ sudo mv wp-config-sample.php wp-config.php

Verify WordPress files are in place:

Ensure files such as wp-config.php, wp-login.php, and the wp-admin folder are present.

Configure the wp-config.php File

Update the WordPress configuration file with your database credentials so it can connect to MySQL.

Open wp-config.php with a text editor:

Update the following lines with the database information:

// The name of the database for WordPress 
define( 'DB_NAME', 'wordpressdb' );

// MySQL database username 
define( 'DB_USER', 'wordpressdbuser' );

// MySQL database password 
define( 'DB_PASSWORD', 'strongpassword' );

// MySQL hostname 
define( 'DB_HOST', 'localhost' );

Save and close the file. Ensure the values match those set earlier when creating the database and user.

Create an Nginx Server Block for WordPress

Configure Nginx with a server block to properly handle WordPress traffic and PHP requests.

Locate the active PHP-FPM socket:

Sample Output:

php8.3-fpm.pid  php8.3-fpm.sock  php-fpm.sock

Note the correct socket filename, e.g., php8.3-fpm.sock.

Edit the default Nginx configuration file:

$ sudo nano /etc/nginx/sites-available/default

Replace its content with the following configuration (adjust domain and socket path as needed):

server {
    listen 80;
    server_name www.example.com;

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

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; # adjust if your PHP version differs
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location = /favicon.ico {
        access_log off;
        log_not_found off;
        expires max;
    }

    location = /robots.txt {
        access_log off;
        log_not_found off;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location /wp-content/uploads/ {
        location ~ \.php$ {
            deny all;
        }
    }
}

Check the configuration syntax:

Sample Output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx to apply changes:

$ sudo systemctl restart nginx

Access the WordPress Dashboard

Open a browser and navigate to http://www.example.com. You should see the WordPress installation page.

Select your preferred language and click Continue. Fill in your Site Title, Username, Password, and Email, then click Install WordPress. Once completed, log in with the credentials you set to reach the WordPress dashboard.

Conclusion

In this tutorial, you installed and configured WordPress on Ubuntu 24.04 using the LEMP stack (Linux, Nginx, MySQL, PHP). You set up Nginx, created a secure MySQL database, configured PHP with the necessary extensions, and deployed WordPress for production use.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: