Read in 6 minutes

last updated 

How to install WordPress with Nginx on CentOS 7

WordPress is the most popular open source blogging and CMS platform worldwide, powering a quarter of all websites on the Internet today. It is based on PHP and MySQL and packs a ton of features that can be extended with free and premium plugins and themes. WordPress is the simplest way to create your online store, website, or blog.

This tutorial describes how to install WordPress on CentOS 7. It is a fairly simple process that takes less than ten minutes to complete.

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

Prerequisites

Ensure the following prerequisites are met before continuing with this tutorial:

  • Have a domain name pointed to your server public IP address. In this tutorial we will use example.com.
  • Logged in as a user with sudo privileges.
  • Nginx installed by following these instructions.
  • You have a SSL certificate installed for your domain. You can generate a free Let’s Encrypt SSL certificate by following these instructions .

Creating MySQL database

WordPress stores its data and configuration in a MySQL database. The first step is to create a MySQL database, MySQL user account and grant access to the database.

If you already don’t have MySQL or MariaDB installed on your Ubuntu server you can install by following one of the instructions below:

Login to the MySQL shell by executing the following command:

mysql -u root -p

From inside the mysql console, run the following SQL statements to create a database named wordpress, user named wordpressuser and to grant all necessary permissions to the user:

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;

Installing PHP 7.2

CentOS 7 ships with PHP version 5.4. The recommended PHP version for WordPress is PHP 7.2.

To install PHP 7.2 on your server you can either follow this tutorial or install PHP 7.2 and all required PHP extensions with the following commands:

sudo yum install epel-release yum-utils
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php72
sudo yum install php-cli php-fpm php-mysql php-json php-opcache php-mbstring php-xml php-gd php-curl

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

By default PHP FPM will run as user apache on port 9000. We’ll change the user to nginx and switch from TCP socket to Unix socket. To do so open the /etc/php-fpm.d/www.conf file edit the lines highlighted in yellow:

/etc/php-fpm.d/www.conf
...
user = nginx
...
group = nginx
...
listen = /run/php-fpm/www.sock
...
listen.owner = nginx
listen.group = nginx

Make sure the /var/lib/php directory has the correct ownership using the following chown command:

chown -R root:nginx /var/lib/php

Once you made the changes, enable and start the PHP FPM service:

sudo systemctl enable php-fpm
sudo systemctl start php-fpm

Downloading Wordpress

Before downloading the Wordpress archive, first create a directory in which we will place the WordPress files:

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

The next step is to download the latest version of WordPress from the WordPress download page using the following wget command:

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

When the download is complete, extract the WordPress archive and move the entire contents into the domain’s document root directory:

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

Set the correct permissions so that the web server can have full access to the site’s files and directories:

sudo chown -R nginx: /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 a new nginx server block:

sudo nano /etc/nginx/conf.d/example.com.conf

Add the following lines:

/etc/nginx/conf.d/example.com.conf
# 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$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    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 .

Before restarting the Nginx service test the configuration to be sure that 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 we can restart Nginx by typing:

sudo systemctl restart nginx

Completing the WordPress Installation

Now that Wordpress is downloaded and the server configuration is complete, you can finish the installation through the web interface.

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

Select the language you would like to use 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 ask you to enter your database connection details. Enter the MySQL user and database details you previously created.

Start the WordPress installation by clicking on the Run the Installation button.

On the next step you’ll need to enter a name for your WordPress site and choose a username (for security purposes do not use “admin” ).

The installer will automatically generate a strong password for you. Do not forget to save this password. You can also set the password by yourself.

Enter your email address and select whether you want to discourage search engines from indexing the site (not recommended).

Click Install WordPress and once the installation is completed you will be taken to a page informing you that WordPress has been installed.

To access your WordPress login page click on the Log in button.

Enter your username and password.

You will be redirected to the WordPress administration dashboard.

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

Conclusion

Congratulations, you have successfully installed WordPress with Nginx on your CentOS 7 server. First Steps With WordPress is a good starting place to learn more about how to make your new WordPress site your own.

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