How to install Wordpress using Nginx

To Install WordPress using Nginx Web Server

In this article we will learn how to install WordPress using Nginx in CentOS. Nginx is compiled as a package in repositories and can be installed with apt-get Package Utility which supports Virtual Hosts like Apache and PHP files.

To Install Nginx Web Server

For Redhat Based
Run the following command to download the rpm package of nginx and then install it.

[root@linuxhelp~]#  wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
--2016-07-30 15:12:05--  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Resolving nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4680 (4.6K) [application/x-redhat-package-manager]
Saving to: ‘ nginx-release-centos-7-0.el7.ngx.noarch.rpm’ 

100%[=============================================================> ] 4,680       --.-K/s   in 0.004s  

2016-07-30 15:12:07 (1.16 MB/s) - ‘ nginx-release-centos-7-0.el7.ngx.noarch.rpm’  saved [4680/4680]

Run the following command to add the rpm packages.

[root@linuxhelp~]# rpm -Uvh nginx-release-centos-7-0.el7.ngx.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]

Now install the Nginx packages.

[root@linuxhelp~]# yum install nginx
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.dhakacom.com
 * extras: centos-hcm.viettelidc.com.vn
 * updates: centos-hcm.viettelidc.com.vn
updates/7/x86_64/primary_db                                                     | 5.7 MB  00:00:07     
Resolving Dependencies
-->  Running transaction check
--->  Package nginx.x86_64 1:1.10.1-1.el7.ngx will be installed
.
.
.
 Installing : 1:nginx-1.10.1-1.el7.ngx.x86_64                                                     1/1 
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
  Verifying  : 1:nginx-1.10.1-1.el7.ngx.x86_64                                                     1/1 

Installed:
  nginx.x86_64 1:1.10.1-1.el7.ngx                                                                      
Complete!

For Ubuntu based
Run the following command to install Nginx in Ubuntu.

root@linuxhelp~$ sudo apt-get install nginx

To install database

Run the following command to install the necessary packages for WordPress.

For Redhat based

[root@linuxhelp~]# yum install php-fpm php-mysql mariadb-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.dhakacom.com
 * extras: centos-hcm.viettelidc.com.vn
 * updates: centos-hcm.viettelidc.com.vn
Resolving Dependencies
-->  Running transaction check
--->  Package mariadb-server.x86_64 1:5.5.35-3.el7 will be updated
--->  Package mariadb-server.x86_64 1:5.5.47-1.el7_2 will be an update
--->  Package php-pdo.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
-->  Running transaction check
--->  Package libzip.x86_64 0:0.10.1-8.el7 will be installed
.
.
.
Installed:
  php-fpm.x86_64 0:5.4.16-36.1.el7_2.1              php-mysql.x86_64 0:5.4.16-36.1.el7_2.1             

Updated:
  mariadb-server.x86_64 1:5.5.47-1.el7_2                                                               
Complete!

For Ubuntu based
Utilise the following command to install the php5 packages in Ubuntu.

root@linuxhelp~$ apt-get install php5 php5-fpm mysql-server php5-mysql

To configure WordPress

Open the config file and add the following contents in location category.

[root@linuxhelp~]# vim /etc/nginx/conf.d/default.conf
location / {
        try_files $uri $uri/ /index.php$is_args$args 
        }

The final config file appear as follows.

server {
    listen       80 
    server_name  localhost 
        root   /usr/share/nginx/html 
        index  index.html index.htm index.php 
location / {
try_files $uri $uri/ /index.php$is_args$args 
}
    error_page   500 502 503 504  /50x.html 
    location = /50x.html {
        root   /usr/share/nginx/html 
    }
   location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000 
        fastcgi_index  index.php 
        fastcgi_param  SCRIPT_FILENAME  $request_filename 
        include        fastcgi_params 
    }
}

In php config file, change the username and group name from ‘ apache‘ to ‘ nginx‘ .

[root@linuxhelp~]# vim /etc/php-fpm.d/www.conf 
.
user = nginx
.
group = nginx
.

To create a Database

Create the database for wordpress by using the following commands.

[root@linuxhelp~]# mysql
Welcome to the MariaDB monitor.  Commands end with   or g.
Your MariaDB connection id is 2
Server version: 5.5.47-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type ' help '  or ' h'  for help. Type ' c'  to clear the current input statement.

MariaDB [(none)]>  show databases 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.10 sec)

MariaDB [(none)]>  CREATE DATABASE linuxhelp 
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]>  show databases 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linuxhelp          |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]>  CREATE USER linux IDENTIFIED BY ' 12345'  
Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]>  GRANT ALL PRIVILEGES ON linuxhelp.* TO linux IDENTIFIED BY ' 12345'  
Query OK, 0 rows affected (0.05 sec)

MariaDB [(none)]>  FLUSH PRIVILEGES 
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>  exit
Bye

Here the database name is ‘ linuxhelp’ , user as ‘ linux’ and the password is ‘ 12345’ .

After completing the configuration, start and enable all the services.

[root@linuxhelp~]# systemctl start nginx
[root@linuxhelp~]# systemctl start php-fpm 
[root@linuxhelp~]# systemctl start mariadb
[root@linuxhelp~]# systemctl enable nginx php-fpm mariadb
ln -s ' /usr/lib/systemd/system/nginx.service'  ' /etc/systemd/system/multi-user.target.wants/nginx.service' 
ln -s ' /usr/lib/systemd/system/php-fpm.service'  ' /etc/systemd/system/multi-user.target.wants/php-fpm.service' 
ln -s ' /usr/lib/systemd/system/mariadb.service'  ' /etc/systemd/system/multi-user.target.wants/mariadb.service' 

To install Nginx

Remove the files in /usr/share/nginx/html/ and then download the WordPress zip file.

[root@linuxhelp~]#  cd /usr/share/nginx/html
[root@linuxhelp html]# rm -rf *
[root@linuxhelp html]# wget http://wordpress.org/latest.zip
--2016-07-30 15:46:48--  http://wordpress.org/latest.zip
Resolving wordpress.org (wordpress.org)... 66.155.40.249, 66.155.40.250
Connecting to wordpress.org (wordpress.org)|66.155.40.249|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://wordpress.org/latest.zip [following]
--2016-07-30 15:46:48--  https://wordpress.org/latest.zip
Connecting to wordpress.org (wordpress.org)|66.155.40.249|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8424250 (8.0M) [application/zip]
Saving to: ‘ latest.zip’ 

100%[=============================================================> ] 8,424,250    190KB/s   in 46s    

2016-07-30 15:47:35 (181 KB/s) - ‘ latest.zip’  saved [8424250/8424250]

Unzip the downloaded file.

[root@linuxhelp~]# unzip latest.zip

Go to newly generated directory and move all the contents as shown below.

[root@linuxhelp html]# cd wordpress
[root@linuxhelp wordpress]# mv * ../

Remove the ‘ latest.zip’ file and ‘ wordpress’ directory and copy the wp-config-sample.php file to wp-config.php, then add the DB details.

[root@linuxhelp html]# cp wp-config-sample.php wp-config.php
[root@linuxhelp html]# vim wp-config.php

Edit the contents of this file as shown below.

define(' DB_NAME' , ' linuxhelp' ) 
define(' DB_USER' , ' linux' ) 
define(' DB_PASSWORD' , ' 12345' ) 
define(' DB_HOST' , ' localhost' ) 
define(' DB_CHARSET' , ' utf8' ) 
define(' DB_COLLATE' , ' ' ) 

Restart all the services by using the following command.

[root@linuxhelp~]# systemctl restartnginx
[root@linuxhelp~]# systemctl restart mariadb
[root@linuxhelp~]# systemctl restart php-fpm

Open the browser and call the URL http://< IP_address>


Enter the Database details and click Submit.

Verify whether the config file is updated or not and click Run the install.

Pick the Site Title, Username and password to login. Click Install WordPress.

Login into the WordPress with the user credentials.


The home page of WordPress appears.

To activate or install plugins, Click Plugins menu and add it.

WordPress is sucessfully installed with nginx web server.

Tag : Wordpress
Comment
jagan
Oct 05 2017
try to change wordpress folder and files permissions into 755 and set ownership as nginx. then restart nginx service and retry .
garybraniff
Oct 05 2017
Hello, I followed these instructions exactly but when I try and go to my IP I get a 403 forbidden error. shouldn't there be permissions set on the wordpress folder/files?
Add a comment
FAQ
Q
How to install the php5 packages in Ubuntu?
A
install the php5 packages in Ubuntu by below command
# apt-get install php5 php5-fpm mysql-server php5-mysql
Q
how to create a Database of mysql in linux?
A
create a Database of mysql in linux by following steps
# mysql
# CREATE DATABASE linuxhelp
Q
where is the configure file for WordPress is located in linux?
A
the configure file for WordPress is located in linux by using the below command
# vim /etc/nginx/conf.d/default.conf
Q
what are all the necessary packages to install WordPress?
A
the necessary packages to install WordPress are
php-fpm
php-mysql
mariadb-server
Q
which command to download the rpm package of nginx in linux?
A
Download the rpm package of nginx by following command
# wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm