How to Install WordPress with LEMP Stack on Ubuntu 18.04

How to Install WordPress with LEMP Stack on Ubuntu 18.04
How to Install WordPress with LEMP Stack on Ubuntu 18.04

Install WordPress with LEMP Stack on Ubuntu 18.04

WordPress is the most popular and widely used content management system or blogging platform in the world. WordPress is popularly used as a Blog, Portfolio Website, eCommerce platform etc. At the backend, WordPress uses a MySQL database for storage and works on PHP processing. In this tutorial, you are going to learn how to install WordPress with LEMP (Linux, Nginx, MySQL, and PHP) stack on Ubuntu 18.04.

Prerequisites

Before you start to install WordPress with LEMP (Linux, Nginx, MySQL, and PHP) stack on Ubuntu. You must have a non-root user account on your server with sudo privileges. Also, you should have:

  • Nginx Installed on your Ubuntu system using this tutorial.
  • Your Domain must be pointed to your Ubuntu server on which you are going to install WordPress.
  • You should also have installed LetsEncrypt SSL certificate by using this tutorial.
  • Install MySQL or MariaDB for backend database.

Create MySQL Database and Grant Permissions

You should have MySQL installed on your system if it is not installed you can install it by this tutorial.

First, log in to your MySQL database using below command:

mysql -u root -p

The above command will ask you to enter the password for the root user. Enter the password to go ahead.

Create the MySQL database for your WordPress typing below command:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Now create a MySQL user and grant permissions for it using below command:

GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'enter-password-here';

Now flush the database so the current MySQL version will know about these changes:

FLUSH PRIVILEGES;

Exit MySQL using below command:

EXIT;

Install PHP on Ubuntu

At the time of writing this tutorial PHP 7.2 is the latest stable version available for PHP language. You can also check the latest version for installation on PHP official website.

First, update the apt package manager index and upgrade the packages by typing below command:

sudo apt update && sudo apt upgrade

Now to install PHP and all the required modules run below command in the terminal:

sudo apt install php7.2-fpm php7.2-json php7.2-opcache php7.2-mysql php7.2-xml php7.2-mbstring php7.2-cli php7.2-fpm php7.2-gd php7.2-curl

You can confirm PHP installation by typing below command:

php -v

Now you should restart the Nginx service by running below command:

sudo systemctl restart nginx

Download and Setup WordPress

First, navigate to /tmp directory typing below command:

cd /tmp

Download the latest WordPress setup using below wget command:

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

You should extract the downloaded file using below command:

tar xzvf latest.tar.gz

Now create the configuration file for WordPress using below command:

cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

Then copy all the files to example.com directory using below command:

sudo cp -a /tmp/wordpress/. /var/www/example.com

Now change the ownership of example.com directory using below command:

sudo chown -R www-data:www-data /var/www/example.com

To set up the WordPress configuration file you should generate some configuration files for it. Do so by running below command:

curl -s https://api.wordpress.org/secret-key/1.1/salt/

The generated output keys should be:

define('AUTH_KEY',         'm=w)!7{-EEc&[email protected]/?!>_hcF*BmQ+S2Do!QP>>O-|OI21');
define('SECURE_AUTH_KEY',  'S?lk-{RG 5K~sd*1$N<aZ18jy|^0n#[email protected]#dJy2M-|jUruu[T+ cYfJ^@2-'); 
define('LOGGED_IN_KEY', '>i*8?IA#h/[email protected]?6MezjmoBWm&&b+h1YP?T.]Y=&*^h9[Bm`ThdbJ5zepb824LUd;-');
define('NONCE_KEY',        'cPim1L6}H1rQLtLj|FrN1DO:LZVsh`rr}5 `}k,f~%u)papX4|_J^Q%PKJ44uF[l');
define('AUTH_SALT',        ',+Aa_iZ/%yj5?-0F.O>Ogd6jCLU+2_2M$+1Zo-hUog70lLa$)[email protected]<~v!Acd');
define('SECURE_AUTH_SALT', '-9sQ8iLS}1-iEX)b<A6(JNuPIGv2SV5ZiHV])[email protected]$4{[email protected]*fj8[ *Uc-'); 
define('LOGGED_IN_SALT', 'K$i5b^g?TK4M|w;mqlh>m9ZJ5eVAq0X;we}jvw:JNkKm-O|-=GdH-{I><`J(ZgKB'); 
define('NONCE_SALT', 'c_VY?z=E}2r0A&r!F/qk*rtM3>K-Id+z*qG*^2g#4/-sR2%GP>b|{<97nL4uP8K/');

Now open the /var/www/example.com/wp-config.php file,

sudo nano /var/www/example.com/wp-config.php

Find below the section and replace it with the above data:

How to install WordPress - Configuration
How to install WordPress – Configuration

Now you should also update configuration file as given below for database connection by replacing password-you-provided with the password you entered in above instructions:

define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password-you-provided');

define('FS_METHOD', 'direct');

Configure Nginx

Now open /etc/nginx/sites-available/example.com.conf file:

sudo nano /etc/nginx/sites-available/example.com.conf

And paste the following code in the above file example.com.conf file:

server {
    listen 80;
    server_name example.com;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    root /var/www/html/example.com;
    index index.php;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    # log files
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

}

Next, to create symlink from directory sites-available to sites-enabled:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Restart Nginx by using following command:

sudo systemctl restart nginx

Test WordPress

Now open the browser and navigate to your websites URL.

It will ask you for selecting language:

Now it will also ask you for database configuration and authentication setup. Enter the proper inputs and you will be welcome to WordPress:

How to install WordPress - WordPress Home
How to install WordPress – WordPress Home

Conclusion

You have successfully learned how to install WordPress with LEMP stack on Ubuntu 18.04. If you have any queries please don’t forget to comment out.

LEAVE A REPLY

Please enter your comment!
Please enter your name here