• Installing the LEMP Stack on your server

    This is the second article in the series Installing WordPress on your Server, with PHP 8.4 and Cloudflare SSL. After spinning up a new server, the next step is to install the required environment for WordPress: LEMP stack.

    What is LEMP stack

    LEMP stack is a collection of open-source software components used together to host dynamic websites and web applications, such as WordPress. The acronym stands for:

    • L – Linux: the kernel that serves as the foundation, or operating system (in this tutorial we’ll install Ubuntu operating system).
    • E – Nginx (pronounced ‘Engine-X’): the web server that handles requests and delivers web pages to users.
    • M – MySQL or MariaDB: the database system that stores website content and data.
    • P – PHP: the scripting language that processes dynamic content (like WordPress code) and communicates with the database.

    Specifically, we’ll install the latest stable version of the components, which are Ubuntu 24.04, PHP 8.4 and MariaDB.

    Installing LEMP stack

    Updating system packages

    Start by connecting to your server via SSH, then proceed to update your packages with two commands:

    sudo apt update -y
    sudo apt upgrade -y
    

    Installing MariaDB

    Install MariaDB as the database:

    sudo apt install mariadb-server -y

    Start MariaDB and enable it to run automatically after a server reboot:

    sudo systemctl start mariadb
    sudo systemctl enable mariadb
    

    Then, use this command to run a wizard that secure your database:

    sudo mysql_secure_installation

    You will be prompted with several questions:

    • Enter current password for root (enter for none) → Press Enter.
    • Set root password? [Y/n] → Press Y and type your password.
    • Re-enter new password → Type your password again.
    • Remove anonymous users? [Y/n] → Press Y.
    • Disallow root login remotely? [Y/n] → Press Y.
    • Remove test database and access to it? [Y/n] → Press Y.
    • Reload privilege tables now? [Y/n] → Press Y.

    Installing PHP 8.4

    Ubuntu’s default package sources may not include PHP 8.4. So to install PHP 8.4, we can use the widely trusted ppa:ondrej/php repo, which offers maintained versions and related extensions.

    sudo add-apt-repository ppa:ondrej/php
    sudo apt update
    
    sudo apt install php8.4 -y
    
    sudo apt install php8.4-fpm php8.4-mysql php8.4-cli php8.4-curl php8.4-mbstring php8.4-xml php8.4-zip php8.4-gd php8.4-intl -y
    
    sudo systemctl start php8.4-fpm
    sudo systemctl enable php8.4-fpm
    

    The above commands will:

    • Add the new repo that contains PHP 8.4.
    • Refresh the package list.
    • Install PHP 8.4.
    • Install several extensions that commonly required by PHP and WordPress.
    • Start the PHP 8.4 and enable it to run automatically after a server reboot.

    Installing Nginx

    Next, we will install the Nginx web server and use Cloudflare SSL to secure the server connection.

    Type these commands to install, start and enable Nginx to run automatically after a server reboot.

    sudo apt install nginx -y
    
    sudo systemctl start nginx
    sudo systemctl enable nginx
    

    Create a directory for your WordPress website (e.g. taihoang):

    sudo mkdir /var/www/taihoang
    sudo chown -R $USER:$USER /var/www/taihoang
    Code language: Shell Session (shell)

    Open the Nginx config file:

    sudo nano /etc/nginx/sites-available/defaultCode language: Shell Session (shell)

    Then add this content to that file (change your domain at the server_name):

    server {
        
        listen 443 ssl;
        server_name taihoang.com www.taihoang.com;
        root /var/www/taihoang/wordpress;
        index index.html index.htm index.php;
        
        ssl_certificate /etc/ssl/certs/cloudflare.pem;
        ssl_certificate_key /etc/ssl/private/cloudflare.key;
    
        gzip on;
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.4-fpm.sock;
        }
    
        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }
    }
    Code language: Nginx (nginx)

    Activate the above configuration by linking that config file to the sites-enabled directory:

    sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/Code language: Shell Session (shell)

    In the above config file, there are two lines specify the SSL certificate:

    ssl_certificate /etc/ssl/certs/cloudflare.pem;
    ssl_certificate_key /etc/ssl/private/cloudflare.key;Code language: Nginx (nginx)

    In order to have those files, follow the below guide to set everything up before continuing to this article.

    Creating the Database

    From the terminal, type this command to enter the interface of MariaDB:

    sudo mysql

    Create a new Database:

    CREATE DATABASE wordpress_database DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;Code language: SQL (Structured Query Language) (sql)

    Create a new User for Database (change the password from db_password to a stronger password):

    CREATE USER 'db_admin'@'localhost' IDENTIFIED BY 'db_password';Code language: SQL (Structured Query Language) (sql)

    Grant permission for the user db_admin:

    GRANT ALL ON wordpress_database.* TO 'db_admin'@'localhost';Code language: SQL (Structured Query Language) (sql)

    At this point, you have successfully installed LEMP stack on your server. Next step: Installing WordPress.

    Done.


    Next in the series: