Grid Guides

Explore How System On Grid Can Work For You

How to Install WordPress with LEMP on Ubuntu 18.04


Table of Contents

Introduction

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

WordPress 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 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. 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<XhdXoAPz9 DO NOT COPY THESE VALUES c_j{iwqD^<+c9.k<[email protected]');
define('SECURE_AUTH_KEY',  'E2N-h2]Dcvp+aS/p7X DO NOT COPY THESE VALUES {Ka(f;rv?Pxf})CgLi-3');
define('LOGGED_IN_KEY',    'W(50,{W^,OPB%PB<JF DO NOT COPY THESE VALUES 2;y&,2m%3]R6DUth[;88');
define('NONCE_KEY',        'll,4UC)7ua+8<!4VM+ DO NOT COPY THESE VALUES #`DXF+[$atzM7 o^-C7g');
define('AUTH_SALT',        'koMrurzOA+|L_lG}kf DO NOT COPY THESE VALUES  07VC*Lj*lD&?3w!BT#-');
define('SECURE_AUTH_SALT', 'p32*p,]z%LZ+pAu:VY DO NOT COPY THESE VALUES C-?y+K0DK_+F|0h{!_xY');
define('LOGGED_IN_SALT',   'i^/G2W7!-1H2OQ+t$3 DO NOT COPY THESE VALUES t6**bRVFSD[Hi])-qS`|');
define('NONCE_SALT',       'Q6]U:K?j4L%Z]}h^q7 DO NOT COPY THESE VALUES 1% ^qUswWgn+6&xqHN&%');

These are the configuration lines to set secure keys. Copy and paste them in your configuration file. To do so, open the WordPress configuration file using the below command.

$ sudo nano /var/www/wordpress/wp-config.php

Find the dummy values for the settings that looks like the following.

. . .
define('AUTH_KEY',                 'put your unique phrase here');
define('SECURE_AUTH_KEY',          'put your unique phrase here');
define('LOGGED_IN_KEY',            'put your unique phrase here');
define('NONCE_KEY',                'put your unique phrase here');
define('AUTH_SALT',                'put your unique phrase here');
define('SECURE_AUTH_SALT',         'put your unique phrase here');
define('LOGGED_IN_SALT',           'put your unique phrase here');
define('NONCE_SALT',               'put your unique phrase here');
. . .

Delete them and paste the values copied from the command line as shown below.

Now, you need to change the database connection settings, adjusting the database name, the database user, and the password we configured within the MySQL. The one more change you need to make is setting the filesystem method to “direct”. All these settings can be added anywhere in the file.

. . .

define('DB_NAME', 'wpdatabase');

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

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

. . .

define('FS_METHOD', 'direct');

Then, save and close the file.

Complete WordPress Installation through a Web Interface

Now, you have completed the server configuration and its time to complete WordPress installation through a web browser.

Copy the URL highlighted in the screenshot, http://your_IP_address and run it on your web browser.

Then, you will be directed to the WordPress admin page. Select a language of your choice and click on ‘Continue’.

Then, you will see WordPress welcome screen with some required fields, site title, username, password, your email and search engine visibility. Fill them with the appropriate details and click on ‘Install WordPress’.

Then, you will get a screen with the success board with your username and your chosen password.

Conclusion

Now you have successfully installed WordPress with LEMP stack on Ubuntu 18.04.