How to Install WordPress with LEMP [Nginx] stack on Ubuntu 18.04/18.10

Due to Nginx’s structure WordPress works really well. It is more faster and doesn’t break easily on high traffic as compared to Apache. If you are not using Nginx for WordPress then you should consider using it.

Previously, on this guide, I showed how to install Nginx on Ubuntu 18.04/18.10. It was a little trick and tech-savvy, But don’t worry. In this article, I will tell you two ways to install WordPress, one for normal, non-sysadmin users and One for Experts and advanced users.

If you want to install WordPress faster, or you don’t have that much time, then you can go with the first, the easy way.

Easy Way

To install WordPress in 2 minutes, (I’m nor kidding) just follow the below steps. But remember SSL will not work if you have your domain on Cloudflare or any Proxy server. But if you can figure out a way let me know in the comments…

Login to your server via SSH. If you don’t know how to Login to your server via SSH then refer this article.

Since this is your initial session on your newly created server we will first update the pre-installed packages and then proceed to install WordPress.

sudo apt update

Now, let’s apply the magic. But before proceeding please note that you should be the root user, If you are using server provided by AWS or Lightsail then enter sudo su before proceeding.

wget -qO ee rt.cx/ee4 && sudo bash ee

And now you are partially done. Finish the above installation and then to create a new WordPress site just paste the below code.

sudo ee site create example.com --type=wp --cache --ssl=le

replace the example.com place holder with your own domain. But if you are using cloudflare, then ssl install will fail, so I don’t recommend using Cloudflare. But if you still want to use cloudflare then use this code, but it will not use let’s encrypt.

sudo ee site create example.com --type=wp --cache

Note: Don’t forget to change the Cloudflare SSL settings to flexible or your website may not be accessible.

Refer to the Easy engine Handbook and Commands, to learn more about this approach. You can now log in to your WordPress site using the given login details.

Hard Way

If you are crazy or really interested, to install WordPress your way or to learn more about Linux commands then you should follow this. Or it is really hard for a beginner to start, After 50 tries, I successfully was able to install WordPress on Nginx.

prequisites:

  1. Install LEMP stack: WordPress will need a web server, a database, and PHP in order to correctly function. Setting up a LEMP stack (Linux, Nginx, MySQL, and PHP) fulfills all of these requirements. Follow this guide to install and configure this software.

1. Creating a MySQL Database and user:

Now, that you have insatlled all the required things we will now create a new MySQL database and user for WordPress.

Run the below command to login to the database server. When prompted for a password, type the root password you created above.

sudo mysql -u root -p

Now, create a database, by using the below command

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Note: Every MySQL statement must end in a semi-colon (;). Check to make sure this is present if you are running into any issues.

Here WordPress is the name of our database, if you want to change the name of your database then replace it with anything you want.

Now, create a database user I will use tropyl as my user, you can use anything.

CREATE USER 'tropyl'@'localhost' IDENTIFIED BY 'your_password_here';

Now, I am going to grant the new user full access to our database, so that we don’t face any issues later.

GRANT ALL ON wordpress.* TO 'tropyl'@'localhost' IDENTIFIED BY 'your_password_here';

Now, you should flush and initiate your new MySQL instance by this command

FLUSH PRIVILEGES;

Exist out of the MySQL client, by typing

EXIT;

2. Configuring Nginx:

Next, I will be making a few minor adjustments to our Nginx server block files. Based on the prerequisite tutorials, you should have a configuration file for your site in the /etc/nginx/sites-available/ directory configured to respond to your server’s domain name or IP address.

Open your main server block with sudo privileges to begin:

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

Within the main server block, we need to add a few location blocks.

server {
    . . .

    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;
    }
    . . .
}

Inside of the existing location / block, we need to adjust the try_files list so that instead of returning a 404 error as the default option, control is passed to the index.php file with the request arguments.

This should look something like this:

server {
    . . .
    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
    }
    . . .
}

When you are finished, save and close the file.

Now, we can check our configuration for syntax errors by typing:

sudo nginx -t

If no errors were reported, reload Nginx by typing:

sudo systemctl reload nginx

The next steps is to download and install WordPress

3. Downloading WordPress:

Now, our server is absolutely ready to handle and server WordPress. For security reasons, it is highly recommended that you download WordPress from their official website.

To do so we will change our current directory to a moveable one and then download the compressed release by typing:

cd /tmp
curl -LO https://wordpress.org/latest.tar.gz

Now, that your have downloaded the official compressed release of WordPress, it’s time to extract it.

tar xzvf latest.tar.gz

Now, everything is fairly, done but to see and access our WordPress install we need to move it to the root directory of our server. Before we do that, we can copy over the sample configuration file to the filename that WordPress actually reads:

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

Now, we can copy the entire contents of the directory into our document root. We are using the -a flag to make sure our permissions are maintained. We are using a dot at the end of our source directory to indicate that everything within the directory should be copied, including any hidden files:

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

Now, all our files are in place, the only thing we need to do is to assign ownership them to the www-data user and group. This is the user and group that Nginx runs as, and Nginx will need to be able to read and write WordPress files in order to serve the website and perform automatic updates.

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

Our files are now in our server’s document root and have the correct ownership, but we still need to complete some more configuration.

4. WordPress Configuration File:

Now, to make our WordPress install secure and properly connect it to our database we need to do some changes in the WordPress Configuration file.

First, we will add WordPress secret keys, which are also known as salts to our configuration file.

To grab secure values from the WordPress secret key generator, type:

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

You will get back unique values that look something like this:

define('AUTH_KEY',         '!uoV/|_@Va,=kwn8 yTrbA{oTZN%_%iKs=-Lw$<NIV@MO&RZ.6WQ<!R{e+V%E$Ut');
define('SECURE_AUTH_KEY',  '-7r0%<`$sXS~TDXvZ?37-fvXH(nUE]Ea5[Jjivnqd^14!SK3Mn|~3EYR+rSZa|%>');
define('LOGGED_IN_KEY',    'G6-Q~^3w80^i/-rwspc0itZf!0yV3g{6Bqxvj@KWp>N ,$C-L0|:M/1<wo60*Dyd');
define('NONCE_KEY',        'jntC!|i0/[4~%/5o:KU})F1e:pqzm(!4JczX|wSbk6B?J/ji0G %yy*~Z38|0=/D');
define('AUTH_SALT',        '+Cy0dLMe/EP)b[ +S8Eh2Dpg,RtT>8e1VRv ,pl{&!0A=j2p+}rK@tVNa0biKzYu');
define('SECURE_AUTH_SALT', '$<c*NJ}pX-(+p^Ic._{-}2:pf2i[T|!6Hmc(Gay9d`Zo>v-%j@V;d7iD3Ew5^/`~');
define('LOGGED_IN_SALT',   'xy4C]+Bg>N^$#Htt+pgd)|1*A%1?Ac2&-e)I?;JG)|Y.a6rQT+SxCr$hzSs9&tkM');
define('NONCE_SALT',       '8KNp&d;V!LL22e`r|V5Y,Dq{L+bNK5R< k?,~Ac.:N,~x{:i@LY|=!oynN.+#Uka');

These are configuration lines that we can paste directly in our configuration file to set secure keys. But don’t copy my keys, because your key must be unique, instead copy the keys you got.

Now, we need to paste the keys in the configuration file. to open the file, we need to type this:

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

Find the section that contains the dummy values for those settings. It will look something like this:

. . .

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 those lines and paste in the values you copied from the command line.

Next, we need to connect our WordPress install to the database. To do so, we need to change some settings in the beginning of the WordPress Configuration file.

First, navigate to the top of the file where you will see some thing like this:

. . .

define('DB_NAME', 'wordpress');

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

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

. . .

Now, save and close the file.

5. Completing the WordPress Installation

Now that the server configuration is complete, we can finish up the installation through the web interface.

In your web browser, navigate to your server’s domain name or public IP address:

http://server_domain_or_IP

I think you can complete the next steps your self…

Conclusion:

I think this tutorial has helped you a lot in installing WordPress. If you got stuck anywhere just tell in comments, I will surely help you out. Some common next steps are to choose the permalinks setting for your posts (can be found in Settings > Permalinks) or to select a new theme (in Appearance > Themes). If this is your first time using WordPress, explore the interface a bit to get acquainted with your new CMS.

Leave a Comment

Your email address will not be published. Required fields are marked *