Installing WordPress with LEMP on Ubuntu 22.04: A Step-by-Step Guide

Introduction:

LEMP stack, consisting of Linux, Nginx, MySQL, and PHP, is a popular combination for hosting dynamic websites. WordPress, a widely-used content management system (CMS), can be installed on Ubuntu 22.04 using the LEMP stack. In this article, we will walk you through the process of installing WordPress with LEMP on Ubuntu 22.04.

Step 1: Update System Packages

Before starting the installation, it’s essential to update the system packages to their latest versions. Open a terminal and execute the following commands:

sudo apt update
sudo apt upgrade

Step 2: Install Nginx

Nginx is a high-performance web server that will serve our WordPress site. Install Nginx by running the following command:

sudo apt install nginx

Once the installation is complete, start Nginx and enable it to start on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 3: Install MySQL

MySQL is a relational database management system that will store the data for our WordPress site. Install MySQL by running the following command:

sudo apt install mysql-server

During the installation process, you will be prompted to set a root password for MySQL. Make sure to choose a strong password and remember it for future use.

After installation, secure your MySQL installation by running the security script:

sudo mysql_secure_installation

Follow the on-screen instructions to configure the security settings.

Step 4: Install PHP and Required Modules

WordPress requires PHP to run. Install PHP and the necessary PHP extensions by running the following command:

sudo apt install php-fpm php-mysql php-common php-gd php-json php-cli php-mbstring php-opcache php-readline php-xml php-curl

Step 5: Configure Nginx for WordPress

To configure Nginx for WordPress, we need to create an Nginx server block. Open a new server block configuration file in the Nginx sites-available directory:

sudo nano /etc/nginx/sites-available/wordpress

Copy and paste the following configuration into the file:

server {
    listen 80;
    server_name your_domain_or_ip;
    root /var/www/wordpress;
    index index.php index.html index.htm;
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
    
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; }
    
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
    
    location ~ /\.ht {
        deny all;
    }
}

Replace your_domain_or_ip with your actual domain name or server IP address.

Save the changes and exit the editor.

Step 6: Enable the Nginx Server Block

Enable the Nginx server block by creating a symbolic link from the sites-available directory to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/

Remove the default Nginx configuration file:

sudo rm /etc/nginx/sites-enabled/default

Test the Nginx configuration for any syntax errors:

sudo nginx -t

If no errors are displayed, restart Nginx:

sudo systemctl restart nginx

Step 7: Create a MySQL Database and User

To create a MySQL database and user for WordPress, log in to the MySQL shell:

sudo mysql -u root -p

Enter your MySQL root password when prompted.

Inside the MySQL shell, run the following commands to create a database, user, and grant privileges:

CREATE DATABASE wordpress;
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL ON wordpress.* TO 'wordpress_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace 'your_password' with a strong password of your choice.

Step 8: Download and Configure WordPress

Download the latest version of WordPress to the Nginx document root directory:

cd /var/www/
sudo wget -c http://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo chown -R www-data:www-data wordpress/

Rename the default WordPress configuration file:

sudo mv /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php

Open the WordPress configuration file for editing:

sudo nano /var/www/wordpress/wp-config.php

Locate the following lines and update them with your MySQL database details:

define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress_user');
define('DB_PASSWORD', 'your_password');
define('DB_HOST', 'localhost');

Save the changes and exit the editor.

Step 9: Complete WordPress Installation

In your web browser, navigate to your server’s domain name or IP address:

http://your_domain_or_ip

Follow the on-screen instructions to complete the WordPress installation. Provide the requested information, including the site title, admin username, password, and email address.

Once the installation is complete, you can log in to the WordPress admin dashboard and start customizing your website.

Conclusion:

You have successfully installed WordPress with the LEMP stack on Ubuntu 22.04. By following the steps outlined in this guide, you now have a robust platform for creating and managing your website. Enjoy exploring the vast possibilities of WordPress and unleash your creativity to build a remarkable online presence.


Leave a Reply

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