Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Install WordPress with LEMP on Ubuntu 18.04

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [How to Install WordPress with LEMP on Ubuntu](#how-to-install-wordpress-with-lemp-on-ubuntu
    – [Create a MySQL Database and User for WordPress](#create-a-mysql-database-and-user-for-wordpress
    – [Install Additional PHP Extensions](#install-additional-php-extensions
    – [Configure Nginx](#configure-nginx
    – [Download WordPress](#download-wordpress
    – [Setup the WordPress Configuration File](#setup-the-wordpress-configuration-file
    – [Complete WordPress Installation through a Web Interface](#complete-wordpress-installation-through-a-web-interface
    – [Conclusion](#conclusion

    ## Introduction

    In this guide, we will explain to you how to install WordPress with LEMP on Ubuntu 18.04.

    [WordPress](https://wordpress.com/ is a free and open-source Content Management System (CMS. It was designed to manage blogs. It is the most popular website management system with serving more than 60 million websites as of 2018. It allows you to develop a website and manage your content without coding. It can also be used to develop an operational website.

    WordPress is a theme based platform which provides you with free and premium themes that can be integrated easily without any programming and designing language. It has a fantastic feature called plugins which can be used to add required modules and extend its functionality. It is multilingual and allows you to translate content in your language.

    [LEMP](https://lemp.io/ is an acronym for Linux, Nginx, MySQL, and PHP. The LEMP software stack is a group of software used for developing and deploying web pages and web applications. The MySQL database management system stores all the backend data and PHP handles the dynamic processes.

    ## Prerequisites

    You should have access to an Ubuntu 18.04 server. Create a non-root user with sudo privileges by following our guide, [Initial server setup with Ubuntu 18.04](https://systemongrid.com/support/guides/how-to-do-initial-server-setup-with-ubuntu-18.04. You need to install the LEMP stack as WordPress requires a web server, a database, and PHP to function correctly. You can do this by following our guide, Install LEMP stack on Ubuntu 18.04.

    ## How To Install WordPress with LEMP on Ubuntu

    ## Create a MySQL Database and User for WordPress

    WordPress uses MySQL, a data management system, to store and manage your website data. Assuming that you have installed MySQL following the guides in prerequisites.

    Now, you need to create a MySQL database and User for WordPress. To do so, log into your MySQL root account using sudo if it is configured to use auth_socket authentication plugin.

    “`
    $ sudo mysql
    “`
    Use the below command if you have configured MySQL to use a password authentication method.

    “`
    $ mysql -u root -p
    “`
    You will be asked to enter the password for your MySQL root account. Type it and press ENTER.

    Now, you need to create a database that WordPress can control. In this guide, we use ‘wpdatabase’ for the database, and you can replace it as your wish.

    Create a database using the below command.

    “`
    mysql> CREATE DATABASE wpdatabase DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    “`

    Then, you need to create a separate MySQL user to operate on your new database, wpdatabase. In this guide, we use ‘mysqluser’, you can replace it as your wish.

    Create a user, set a password and give access to the new database using the below command.

    “`
    mysql> CREATE USER ‘mysqluser’@’localhost’ IDENTIFIED BY ‘password’;
    “`

    Give permissions to the user on the database using the below command.

    “`
    mysql> GRANT ALL ON wpdatabase.* TO ‘mysqluser’@’localhost’ IDENTIFIED BY ‘password’;
    “`

    Now, run the below command to make your changes effective.

    “`
    mysql> FLUSH PRIVILEGES;
    “`

    If you are okay with the changes, you can exit the MySQL shell using the below command.

    “`
    mysql> exit
    “`

    ## Install Additional PHP Extensions

    After creating a MySQL database and user for WordPress, you need to install additional PHP extensions. To do so, use the below command.

    “`
    $ sudo apt update
    “`

    “`
    $ sudo apt install php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip
    “`

    Now, restart the PHP-FPM process to make the changes effective using the below command.

    “`
    $ sudo systemctl restart php7.2-fpm
    “`

    ## Configure Nginx

    You need to make a few changes to your Nginx server block files. If you have followed the prerequisites guide, you must have a configuration file for your site in the directory, /etc/nginx/sites-available/ configured to your server’s IP address or domain name and secured by an SSL/TLS certificate.

    Then, copy the text files from /etc/nginx/sites-available/default to /etc/nginx/sites-available/wordpress using the below command. (In this guide, as an example, we use /etc/nginx/sites-available/wordpress. You must replace the path to your configuration file.

    “`
    $ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wordpress
    “`

    Additionally, we use /var/www/html/wordpress as the root directory of your WordPress install. Replace it with the web root mentioned in your configuration.

    Open server block file of your site using the below command.

    “`
    $ sudo vi /etc/nginx/sites-available/wordpress
    “`

    In this guide, we use a regular expression location to match requests for static files. You can modify the static files list to add other file extensions that your website may use.

    “`
    /etc/nginx/sites-available/wordpress

    server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index index.php index.html index.htm;
    server_name example.com www.example.com;

    client_max_body_size 100M;

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

    location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    }
    “`

    Then, save and close the file.

    Now, delete the default nginx server block using the below command to avoid conflicting server name error.

    “`
    $ sudo rm -rf /etc/nginx/sites-enabled
    “`

    Now, enable the virtual host by running the below command.

    “`
    $ sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
    “`

    And, run the below commands to reload Nginx web server and PHP-FPM settings.

    “`
    $ sudo systemctl restart nginx.service
    “`

    “`
    $ sudo systemctl restart php7.2-fpm.service
    “`

    Now, check the syntax of your configuration edits with the following command.

    “`
    $ sudo nginx -t
    “`

    If you get no errors, reload Nginx for the new configuration

    “`
    $ sudo service nginx reload
    “`

    ## Download WordPress

    Now, as your server software is configured, you can download and setup WordPress. It is recommended to download the latest version of WordPress from their official website for security reasons.

    Change it into a writable directory and download the compressed version using the below commands.

    “`
    $ cd /tmp
    “`

    “`
    $ curl -LO https://wordpress.org/latest.tar.gz
    “`

    Now, extract the compressed file to create the WordPress directory structure using the below command.

    “`
    $ tar xzvf latest.tar.gz
    “`

    Now, copy the sample configuration file to the file which WordPress reads. Do it using the below command.

    “`
    $ cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
    “`

    Now, create a directory within /var/www/html and copy the contents of the directory into your document root using the below commands.

    “`
    $ sudo mkdir -p /var/www/html/wordpress
    “`

    “`
    $ sudo cp -a /tmp/wordpress/. /var/www/wordpress
    “`

    Now, all your files are in one place. Assign their ownership to the www-data user and group using the below command.

    “`
    $ sudo chown -R www-data:www-data /var/www/wordpress
    “`

    Then, give read and write permissions to your WordPress website.

    “`
    $ sudo chmod -R 755 /var/www/html/
    “`

    ## Setup the WordPress Configuration File

    Then, you need to edit the main WordPress configuration file.

    You need to adjust some secret keys for the secure installation. WordPress provides a security key generation, grab secure values from it using the below command.

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

    You will get a few unique values like the following.

    “`
    define(‘AUTH_KEY’, ‘1jl/vqfs