How to install WordPress with Nginx on Ubuntu 18.04

s2 0

WordPress is by far the most popular open source blogging and CMS platform, powering over a quarter of the world’s websites. It’s based on PHP and MySQL and offers a ton of features that can be expanded with free and premium plugins and themes. WordPress is the easiest way to build your online store, website or blog.

In this tutorial, we are going to show you how to install WordPress on an Ubuntu 18.04 machine. It’s a pretty straightforward process that takes less than ten minutes.

We use a LEMP stack with Nginx as the web server, SSL certificate, the latest PHP 7.2 and MySQL / MariaDB as the database server.

requirements

Make sure you meet the following requirements before proceeding with this tutorial:

  • They have a domain name that points to the public IP of your server. We will use example.com.
  • You are logged in as a user with sudo rights.
  • You installed Nginx by following these instructions.
  • An SSL certificate installed for your domain. You can install a free Let’s Encrypt SSL certificate by following these instructions.

Update the package index and system packages to the latest versions:

sudo apt updatesudo apt upgrade

Create MySQL database

WordPress uses the MySQL database to store all of its data. Our first step is to create a MySQL database and a MySQL user account and grant access to the database.

If you haven’t already installed MySQL or MariaDB on your Ubuntu server, you can install it using one of the following instructions:

  • Install MySQL on Ubuntu 18.04
  • Install MariaDB on Ubuntu 18.04

Log into the MySQL shell by entering the following command and entering the password when prompted:

mysql -u root -p

In the MySQL Shell, run the following SQL statements to create a database named. to create wordpress, User name wordpressuser and to grant the user all necessary authorizations:

CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'change-with-strong-password';FLUSH PRIVILEGES;EXIT;

After executing the commands above, you will have a new MySQL database and user account that will be used by your WordPress instance.

Install PHP

PHP 7.2, the standard PHP version in Ubuntu 18.04, is fully supported and recommended for WordPress.

To install PHP and any required PHP extensions, run the following command:

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

We installed PHP-FPM because we will be using Nginx as a web server.

The PHP-FPM service will start automatically after the installation process is complete.

Download WordPress

Before you download the WordPress archive, first create a directory that contains our WordPress files:

sudo mkdir -p /var/www/html/example.com

Our next step is to get the latest version of WordPress from the. to download WordPress download page
with the following wget command:

cd /tmpwget https://wordpress.org/latest.tar.gz

As soon as the download is complete, unzip the WordPress archive and move the extracted files to the domain’s document root directory:

tar xf latest.tar.gzsudo mv /tmp/wordpress/* /var/www/html/example.com/

Finally, we need to set the correct permissions so that the web server has full access to the site’s files and directories.

Since both Nginx and PHP run as www-data User and group to set proper ownership run the following chown command:

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

Configuring Nginx

By now you should already have Nginx with SSL certificate installed on your system, if not, check the prerequisites for this tutorial.

To create a new server block for our WordPress instance, we will use the Nginx recipe
from the official Nginx site.

Open your text editor and create the following file:

/etc/nginx/sites-available/example.com
# Redirect HTTP -> HTTPS
server {
    listen 80;
    server_name www.example.com example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}

# Redirect WWW -> NON WWW
server {
    listen 443 ssl http2;
    server_name www.example.com;

    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;
    include snippets/ssl.conf;

    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;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

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

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

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

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

    location ~* .(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires max;
        log_not_found off;
    }

}
Don’t forget to replace example.com with your WordPress domain and set the correct path to the SSL certificate files. The snippets used in this configuration are created in this guide.

Activate the server block by creating a symbolic link to the sites-enabled Directory:

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

Before restarting the Nginx service, run a test to make sure there are no syntax errors:

sudo nginx -t

If there are no errors, the output should look like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

and you can restart Nginx by typing:

sudo systemctl restart nginx

Completion of the WordPress installation

After WordPress is downloaded and the server configuration is complete, we can complete the installation via the web interface.

Open your browser, enter your domain and a screen similar to the following will appear:

Select the language you want and click on the Continue Button.

Next you will see the following information page, click on the Let's go! Button.

On the next screen, the setup wizard will prompt you for your database connection details. Enter the MySQL user and database details you created earlier.

Start the installation by clicking on the Run the Installation Button.

In the next step you have to enter a name for your WordPress site and choose a username (do not enter “admin” for security reasons).

The installer will automatically generate a strong password for you. Don’t forget to save this password. You can also set the password yourself.

Enter your email address and choose whether to stop search engines from indexing the site (not recommended).

click Install WordPress and once the installation is complete, you will be taken to a page informing you that WordPress has been installed. To access your WordPress login form, click the Log in Button.

Enter your username and password and click on that Log in Button.

Once you are signed in, you will be redirected to the WordPress administration dashboard.

From here you can start customizing your WordPress installation by installing new themes and plugins.

diploma

Congratulations, you have successfully installed WordPress with Nginx on your Ubuntu 18.04 server. Getting started with WordPress
is a good starting point to learn more about getting started with WordPress.

If you have any questions, feel free to leave a comment below.