How to Install WordPress with Nginx on CentOS 6

Introduction

 

WordPress is a free and open source Content Management System, built on PHP and MySQL. It provides over 30,000 plugins for easy customization of appearance and functionality. WordPress is the most popular blogging platform, estimated to be running on 60 million sites and seen by tens of millions of people every day.

This tutorial guides you to install WordPress with Nginx on CentOS 6.

Prerequisites

 

  • This tutorial assumes that you already have LEMP (Linux, Nginx, MySQL and PHP) installed in your server. If you don’t have LEMP stack on your sever, you can find the steps for doing the set up here.
  • The user should have root privileges so as to have administrative capabilities and permissions. You can check out how to grant root privileges for user here.

Now that you have the user with root privileges and mandatory software, let’s get down to installing WordPress.

Download WordPress

 

You can download the WordPress package directly from wordpress.org.

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

Before executing the download, verify whether wget is already installed on your server. You will have the WordPress package in zipped format inside user’s home directory. Unzip it using the command given below.

tar -xzvf latest.tar.gz

 

Create a Database and a User for WordPress

 

Once the WordPress files are unzipped, you can locate them under wordpress directory within home directory.

Now, log into MySQL shell using MySQL root password.

mysql -u root -p

Once logged in, you have to create a wordpress database and a user in that database. You can set a password for the new user. Find the MySQL commands for setting up the database and the user given below.

For creating the database:

CREATE DATABASE wordpress;

Query OK, 1 row affected (0.00 sec)

Note: Here, I have given the name wordpress for the database. You can give any name you wish. However, a unique name is recommended.

 

For creating the user:

CREATE USER [email protected];

Query OK, 0 rows affected (0.00 sec)

 

For setting the password for the user:

SET PASSWORD FOR [email protected]= PASSWORD("yourpassword");

Query OK, 0 rows affected (0.00 sec)

Note: Here, I have given the name wordpressuser1 for the newly created user. You can set password as you prefer.

Now, you can grant all the privileges for the new user. Without proper privileges, you may end up with installation and database permission issues.

GRANT ALL PRIVILEGES ON wordpress.* TO [email protected] IDENTIFIED BY 'yourpassword';

Query OK, 0 rows affected (0.00 sec)

 

Finish up this step by refreshing and exit out of the shell.

FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)
Exit

 

Configure the WordPress Installation

 

Here we are going to set up WordPress and link it to the database that we created for it.

For that, first you need to locate the sample wordpress configuration file from the wordpress directory and copy it to a new file which you can edit later. Thus a new usable wordpress config will be created.

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

Open the wordpress config for editing.

sudo nano ~/wordpress/wp-config.php

Search for the code snippet that contains MySQL settings and update the name of database, username and password correctly.

// ** 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', 'wordpressuser1');

 

/** MySQL database password */

define('DB_PASSWORD', 'yourpassword');

After updating the fields, save and exit.

 

Copy the WordPress Files

 

Create a directory to keep the wordpress files:

sudo mkdir -p /var/www/wordpress

We need to move the unzipped wordpress files to website’s root directory.

sudo cp -r ~/wordpress/* /var/www/wordpress

It is advisable to modify the permissions of /var/www for automatic updating of WordPress plugins and SFTP file editing in the future.

For granting permissions, switch to the web directory:

cd /var/www/

Give ownership of the directory to the Nginx user.

sudo chown nginx:nginx * -R
sudo usermod -a -G nginx username

You should replace the username with the actual user of your server.

Setting Up Nginx Server Blocks

 

In this step, we will set up the WordPress virtual host.

Open up the default configuration host file:

sudo vi /etc/nginx/conf.d/default.conf

Here is a list of changes that needs to be made:

  1. Change the root to /var/www/wordpress
  2. Add index.php to index line.
  3. Replace local host (server_name) to your domain name or IP.
  4. Change the root to access document root – /var/www/wordpress
  5. Uncomment the lines in “location ~ \.php$ {“ section
  6. Change fastcgi_param line so as to help PHP interpreter find php script that was stored in document root.

Once you are done with the changes, the file should look like this:

#
# The default server
#
server {
    listen       80;
    server_name  _;
     #charset koi8-r;
     #access_log  logs/host.access.log  main;
     location / {
        root   /var/www/wordpress;
        index index.php  index.html index.htm;
    }
     error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    } 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/wordpress;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
     # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;    #}
}

After making the changes, save and exit the file.

Restart Nginx to bring in the new changes:

sudo service nginx restart

 

Access and Finalize the WordPress Installation

 

This is the last stage of your WordPress installation. Open your browser and access the page by tailoring your site’s IP address or domain with /wp-admin/install.php. The link may look like this:

www.example.com/wp-admin/install.php

The WordPress online installation page will be opened and it should look similar to this:

Wordpress-install

 

Fill out the required fields in the form and finalize the WordPress installation.

support2 has written 111 articles

Leave a Reply