Install WordPress with Nginx on CentOS 7 / RHEL 7

WordPress is the most widely used open source web blogging and content management software written in php and MySQL. Even ITzGeek uses WordPress.

Here is the small tutorial on setting up WordPress installation with Nginx on CentOS 7.

Prerequisites

Before proceeding, you might want to look at the following.

1: Install EMP stack (Nginx, MariaDB, and PHP) on CentOS 7 / RHEL 7

2: Install phpMyAdmin with Nginx on CentOS 7 / RHEL 7

3: Disable SELinux on CentOS 7 / RHEL 7

Configure Nginx Virtualhost

Let’s create a virtual host for WordPress installation, virtual host configuration files can be found in /etc/nginx/conf.d directory. Normally virtual host file contains a domain name, port number, document root, log location, fast CGI, etc, We have to keep that in mind before creating the file.

Assume the following,

Domain name : wordpress.itzgeek.local
Port No : 80
Document root: /usr/share/nginx/wordpress.itzgeek.local
Logs : /usr/share/nginx/wordpress.itzgeek.local/logs

Create a virtual host.

vi /etc/nginx/conf.d/wordpress.conf

Place the following content.

server {
        listen 80;
        server_name wordpress.itzgeek.local;

        access_log /usr/share/nginx/wordpress.itzgeek.local/logs/access.log;
        error_log /usr/share/nginx/wordpress.itzgeek.local/logs/error.log;

location / {
        root /usr/share/nginx/wordpress.itzgeek.local;
        index index.php index.html index.htm;

if (-f $request_filename) {
        expires 30d;
        break;
}

if (!-e $request_filename) {
        rewrite ^(.+)$ /index.php?q=$1 last;
        }
}

location ~ .php$ {
        fastcgi_pass   localhost:9000;  # port where FastCGI processes were spawned
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   /usr/share/nginx/wordpress.itzgeek.local$fastcgi_script_name;  # same path as above
        fastcgi_param PATH_INFO               $fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        }
}

Create document root and logs directory.

mkdir /usr/share/nginx/wordpress.itzgeek.local
mkdir /usr/share/nginx/wordpress.itzgeek.local/logs

Verify the configuration files.

nginx -t

If you get the following, it means that virtual host entries are correct.

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

Restart the services.

systemctl restart nginx
systemctl restart php-fpm

If you get any errror while restarting the Nginx service, then disable SELinux on your machine.

setenforce 0

Create database

Login into MariaDB.

mysql -u root -p

Create the desired database for WordPress installation.

CREATE DATABASE wordpress;

Create a user.

CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'wppassword';

Grant the permission to the created user to access the newly created WordPress database.

GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';

Exit from MariaDB shell.

exit

Configure WordPress

Download the latest WordPress.

wget http://wordpress.org/latest.tar.gz

Extract it.

tar -zxvf latest.tar.gz

Move it to our document root.

mv wordpress/* /usr/share/nginx/wordpress.itzgeek.local

Copy the wp-sample-config.php file and make it as wp-config.php file.

cp /usr/share/nginx/wordpress.itzgeek.local/wp-config-sample.php /usr/share/nginx/wordpress.itzgeek.local/wp-config.php

Edit the config file and mention the database information.

vi /usr/share/nginx/wordpress.itzgeek.local/wp-config.php

The default configuration will look like below.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
/** MySQL database password */
define('DB_PASSWORD', 'password_here');
/** MySQL hostname */
define('DB_HOST', 'localhost');

Modified entries according to the created database user and database will look like this.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wpuser');
/** MySQL database password */
define('DB_PASSWORD', 'wppassword');
/** MySQL hostname */
define('DB_HOST', 'localhost');

Make nginx user as the owner of WordPress directory.

chown -R nginx:nginx /usr/share/nginx/wordpress.itzgeek.local/

Install WordPress

Open your browser and visit

http://fqdn

OR

http://wordpress.itzgeek.local

Enter the site information and click on Install WordPress.

Install WordPress with Nginx on CentOS 7 – WordPress Setup

You will see the next page like below.

Install WordPress with Nginx on CentOS 7 – WordPress Setup Completed

If you click on continue, you will be asked to enter the password to access the WordPress admin section.

Install WordPress with Nginx on CentOS 7 – WPAdmin Login Page

WordPress Admin Dashboard:

Install WordPress with Nginx on CentOS 7 – WPAdmin DashBoard

That’s All.

centos 7mariadbmysqlnginxphpwordpress
Comments (8)
Add Comment
  • Florian Miesenberger

    Should it mean …
    chown -R nginx:nginx /usr/share/nginx/wordpress.itzgeek.com/

    … instead as:
    chown -R apache:apache /usr/share/nginx/wordpress.itzgeek.com/

    • Raj

      Hi,

      Have updated the article

  • Follower

    This lead me to “nginx: [emerg] open() “/usr/share/nginx/wordpress.tostasqb.com/logs/access.log” failed (13: Permission denied)” when restarting services (before creating databases).

    • Raj

      Please run this
      chown -R nginx:nginx /usr/share/nginx/wordpress.tostasqb.com/

  • Henry

    Make the apache user as the owner to WordPress directory. ???

    Since we use nginx I am curious why this is standing >twice< in your tutorial.
    You should also update twice to nginx:nginx to keep it correct!
    (Else future commenters will follow with issues cause missing updates as needed)

    • Raj

      Dear henry,

      Thanks, have updated the article

  • Jerry

    Is this still up to date? I have to move a wordpress site from an existing windows box to a centOS 7 machine.

    • ITzGeek Web

      its some what old article, it expect that it would be same for all updated version