UbuntuUbuntu Based

How To Install WordPress with LEMP Stack on Ubuntu 22.04 LTS

Install WordPress with LEMP Stack on Ubuntu 22.04

In this tutorial, we will show you how to install WordPress with LEMP Stack on Ubuntu 22.04 LTS. For those of you who didn’t know, WordPress is a free and open-source content management system mainly used to publish blogs on the internet. It is written in PHP and used MariaDB or MySQL as a database backend. WordPress CMS has many customization tools, such as an administrative dashboard with a user-friendly interface to create new web pages, add media, and more. WordPress is one of the most used CMS in the market today.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of WordPress with LEMP Stack on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install WordPress with LEMP Stack on Ubuntu 22.04 LTS Jammy Jellyfish

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade
sudo apt install software-properties-common dirmngr

Step 2. Installing the LEMP Stack.

Before starting this tutorial, the LEMP server must be installed on your server. If you do not have LEMP Stack installed, you can follow our guide here.

Step 3. Installing WordPress on Ubuntu 22.04.

By default, WordPress is not available on Ubuntu 22.04 base repository. Now run the following command below to download the latest stable version of WordPress to your Ubuntu system:

cd /var/www/html
curl -O https://wordpress.org/latest.tar.gz

After WordPress is downloaded, extract the downloaded file:

tar -zxvf latest.tar.gz

We will need to change some folders permissions:

chown -R www-data:www-data /var/www/html/wordpress
chmod -R 755 /var/www/html/wordpress

Step 4. Configuring MariaDB for WordPress.

By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation script. you should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB:

mysql_secure_installation

Configure it like this:

- Set root password? [Y/n] y
- Remove anonymous users? [Y/n] y
- Disallow root login remotely? [Y/n] y
- Remove test database and access to it? [Y/n] y
- Reload privilege tables now? [Y/n] y

Next, we will need to log in to the MariaDB console and create a database for WordPress. Run the following command:

mysql -u root -p

This will prompt you for a password, so enter your MariaDB root password and hit Enter. Once you are logged in to your database server you need to create a database for WordPress installation:

MariaDB [(none)]> CREATE DATABASE wordpress_db;
MariaDB [(none)]> CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'your-strong-password';
MariaDB [(none)]> GRANT ALL ON wordpress_db.* TO 'wordpress_user'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Step 5. Configure WordPress.

First, we rename the WordPress sample configuration file using the following command:

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

Then, edit the WordPress configuration file:

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

Add the following configuration:

define( 'DB_NAME', 'wordpress_db' );

/** Database username */
define( 'DB_USER', 'wordpress_user' );

/** Database password */
define( 'DB_PASSWORD', 'your-strong-password' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

Step 6. Configuring Nginx Virtual Host for WordPress.

Now create an Nginx virtual host configuration file:

nano /etc/nginx/conf.d/wordpress.conf

Add the following file:

server {
    listen 80;
    root /var/www/html/wordpress;
    index  index.php index.html index.htm;
    server_name  your-domian.com;

    client_max_body_size 500M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
	
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }	

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

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

Save and close the file then, restart the Nginx web server so that the changes take place:

nginx -t
sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm

Step 6. Enable HTTPS on WordPress.

First, we install Certbot on Ubuntu 22.04 using the following command:

sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

After complete install Certbot, now we set up Certbot for Nginx using the following command:

sudo apt install python3-certbot-nginx

Next, run the following command to start the creation of your certificate:

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email admin@your-domain.com -d www.your-domain.com

Output:

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://domain.com and https://www.your-domain.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=your-domain.com
https://www.ssllabs.com/ssltest/analyze.html?d=www.your-domain.com
-------------------------------------------------------------------------------

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/your-domain.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/your-domain.comm/privkey.pem
Your cert will expire on 2022-05-20. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

Let’s Encrypt certificates have 90 days of validity, and it is highly advisable to renew the certificates before they expire. To test that this renewal process is working correctly, you can run:

sudo certbot renew --dry-run

Output:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/your-domain.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for your-domain.com and www.your-domain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded: 
  /etc/letsencrypt/live/domain.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Step 7. Configure Firewall.

Ubuntu 22.04 has ufw a firewall running by default. Enable connection through ports 80 HTTP and 443 HTTPS:

sudo ufw allow 'Nginx FULL'
sudo ufw enable
sudo ufw status

Step 8. Accessing WordPress Web Interface.

Once successfully installed, open your web browser and access the WordPress installation wizard using the URL https://your-domain.com. You will be redirected to the following page:

Install WordPress with LEMP Stack on Ubuntu 22.04 LTS Jammy Jellyfish

Congratulations! You have successfully installed WordPress. Thanks for using this tutorial for installing WordPress with LEMP on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the WordPress website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button