How To Install WordPress on Localhost (LEMP Stack)

Installing WordPress locally on a Linux system with a LEMP stack provides a practical environment for development, testing, and learning without publishing the website to the internet. This guide walks you through installing Nginx, MariaDB, PHP-FPM, the required PHP extensions, and WordPress.

The following steps explain how to install WordPress locally on a Linux system such as Debian using a LEMP stack (Linux, Nginx, MySQL/MariaDB, PHP-FPM).

1. Install the LEMP Stack and Required Extensions

Update your local repository and install Nginx, MariaDB, PHP-FPM, and essential PHP modules required by WordPress:

sudo apt update

sudo apt install nginx mariadb-server php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-soap php-intl php-zip

The installed packages provide the main components required to run WordPress with Nginx:

  • nginx: The high-performance HTTP web server and reverse proxy that delivers your website content to visitors.
  • mariadb-server: The SQL database management system used to store and manage your website's data (posts, users, and settings).
  • php-fpm: The FastCGI Process Manager that processes dynamic PHP code for Nginx.
  • php-mysql: The module that enables PHP to communicate with your MariaDB database.
  • php-curl: The extension that allows PHP to make HTTP requests and connect to external APIs or remote servers.
  • php-gd: The graphics library used by PHP to process, crop, resize, and compress images.
  • php-mbstring: The multi-byte string extension that handles non-ASCII characters and special language scripts safely.
  • php-xml: The parser module used by PHP to read, transform, and output XML documents and RSS feeds.
  • php-soap (Optional): The extension used by PHP to interact with web services using the Simple Object Access Protocol.
  • php-intl: The Internationalization module that handles regional formatting for numbers, dates, currencies, and locale-aware sorting.
  • php-zip: The extension that allows PHP to read, create, and extract compressed .zip archives.

Verify the PHP-FPM Service

Verify that the PHP-FPM service is running. Adjust php8.4-fpm or your specific PHP version if needed:

sudo systemctl status php8.4-fpm

2. Create a MySQL/MariaDB Database and User

WordPress requires a database to store site data. Log in to the MySQL/MariaDB command line:

sudo mysql -u root

Execute the following commands to set up the database and user. Replace wordpress, wp_user, and password as needed:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';

FLUSH PRIVILEGES;

EXIT;

3. Download and Extract WordPress

Navigate to your web root, download the WordPress archive, and extract it:

cd /var/www/html

sudo wget https://wordpress.org/latest.tar.gz

sudo tar -xzvf latest.tar.gz

Optional: Change the WordPress Folder Name

To change the WordPress folder name, replace mysitename with your desired name, and adjust the command path as needed:

sudo mv wordpress mysitename

Optional: Remove the Downloaded Archive

To remove the downloaded archive and free up disk space:

sudo rm latest.tar.gz

Assign WordPress File Permissions

Assign permissions so the Nginx web server user (www-data) can write to the directory:

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

sudo chmod -R 755 /var/www/html/wordpress

4. Configure wp-config.php

Copy the sample configuration file:

cd /var/www/html/wordpress

sudo cp wp-config-sample.php wp-config.php

Edit the file:

sudo nano wp-config.php

Update the database credentials using the details set up in Step 2:

define( 'DB_NAME', 'wordpress' );

define( 'DB_USER', 'wp_user' );

define( 'DB_PASSWORD', 'password' );

define( 'DB_HOST', 'localhost' );

Save and exit by pressing Ctrl + O, then Enter, followed by Ctrl + X.

5. Create an Nginx Server Block

Nginx requires a block configuration to process PHP and handle WordPress permalinks properly. Create a new site configuration file:

cd ~

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

Paste the following configuration. Verify your PHP-FPM socket path under fastcgi_pass, and update 8.4 to your installed PHP version if necessary:

server {

    listen 80;

    server_name localhost;

    root /var/www/html/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:/run/php/php8.3-fpm.sock; # Adjust PHP version as needed

    }

    location ~ /\.ht {

        deny all;

    }

}

The fastcgi_pass value must correspond to the PHP-FPM socket available on your system. For example, if you have a different PHP version installed, adjust the socket path accordingly.

Enable the Site Configuration

Enable the WordPress site configuration and verify the Nginx configuration before restarting the service:

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

sudo nginx -t

sudo systemctl restart nginx

6. Run the WordPress Web Installer

Open your web browser and navigate to:

http://localhost

Select your preferred language and click Continue.

Enter your Site Title, administrative Username, Password, and Email.

Click Install WordPress.

7. Log In to WordPress

Once the installation is completed, log in at:

http://localhost/wp-admin

You can now start using your local LEMP WordPress installation for development, testing, and other local WordPress tasks.

By combining Nginx, MariaDB, and PHP-FPM, the LEMP stack provides the components required to run WordPress locally on a Linux system. After the Nginx server block and WordPress configuration are in place, the site can be accessed through your local browser.

https://youtu.be/MbWgAdKaYqk

Post a Comment

Previous Post Next Post

Contact Form