Knowledgebase:
How To Install WordPress On CentOS 7 With NGINX
Posted by Daniel Naval on 11 May 2019 06:32 PM

In this article, we will show you how to install WordPress on CentOS 7 running NGINX. You will be needing to follow the commands shown below to install and successfully publish your wordpress site on the web.

Requirements:
Domain name that is pointing to your server’s public IP address
NGINX installed on the server
MySQL or MariaDB installed on the server
Sudo or root privilege on the server
PHP 7.1 or 7.2 installed on the server (in this articles we will use PHP 7.2)

Firstly, assuming you have your MySQL and MariaDB installed on your server and login to it. We will create a database named “wordpress” and database user named “wordpressuser”, then we will grant all required permissions to the user. See commands below:

mysql -u root -p
mysql> CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
mysql> GRANT ALL ON wordpress.* TO ‘wordpressuser’@’localhost’ IDENTIFIED BY ‘change-with-strong-password’;
mysql> SET PASSWORD FOR 'wordpressuser'@'hostname' = PASSWORD('new-password');
mysql> FLUSH PRIVILEGES;
mysql> EXIT;

Installing PHP 7.2
Next, we will be installing the PHP 7.2 as the CentOS 7 comes with PHP version 5.4. For WordPress, it is recommended to use PHP 7.2. To install the PHP 7.2, you may follow the commands below:

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

The PHP-FPM is required as we will be using NGINX as our web server and by default it will run as user “apache” on port “9000”. So, we will be changing the configuration for user to “nginx” and switch from TCP socket to UNIX socket. The configuration file to update is “/etc/php-fpm.d/www.conf”. See commands below:

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

Then change the ownership of “/var/lib/php” directory. See below:

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

Once done, you may now enable and start the PHP-FPM service.

systemctl enable php-fpm
systemctl start php-fpm

Installing WordPress
To install wordpress on your server, you need to download it first and store on the /tmp directory using “wget” command.

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

Once the download is completed, just extract the “latest.tar.gz” and move the contents to the domain’s root directory which we will be creating below.

mkdir -p /usr/share/nginx/example.com/

Make sure you are on the “/tmp” directory by typing in “pwd” or just cd /tmp again. Then you can now extract and move the wordpress archive.

tar xf latest.tar.gz
mv /tmp/wordpress/* /usr/share/nginx/example.com/

You need to set the correct permissions after moving the contents for the web server to have full access on the files and directories.

chown -R nginx: /usr/share/nginx/example.com/

Configuring NGINX
Since you already have NGINX installed on your system, we will now create a server block for your wordpress instance. You can use “vi” or “nano” in creating the configuration file.

vi /etc/nginx/conf.d/example.com.conf

Then add the following lines:

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

}

After you have created the configuration file, test it first using the command below:

nginx -t

This should show as a result:

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

Then you can now restart the NGINX Service.

systemctl restart nginx

WordPress Installation
After the wordpress has been downloaded, extracted and moved to your domain root directory, you can now finish the installation.
Now, open you browser and enter your domain name and you should see the information page. Just click on “Let’s go!”.

Then you will be redirected to the setup page for your database connection. Enter your MySQL user and database details you have created previously. Click submit once done.

Next, click on the “Run the installation” to start the wordpress installation.

On the next step, you will be setting up you wordpress site’s name and username to login to your wordpress admin. A password will be automatically generated by the system but you still can choose your own.
Enter your email address on the field and choose if you wish to discourage search engines form indexing your wordpress site.

Once completed you can click on the “Install WordPress” button and you will be redirected to a page that shows the wordpress has been installed. Click on Login to continue.

Enter your username and password once you are on the login page.

You will then see your wordpress admin dashboard.

 

Congratulations! You have successfully installed and configured your wordpress on CentOS 7 with NGINX!