Thursday, 7 January 2016

WordPress installation in Nginx

Nginx Installation.

first need to update
          sudo apt-get update
Install the Nginx
        apt-get install nginx
  Start the Nginx server
        Service Nginx restart
after starting nginx server enter IP in web browser  you can get nginx default page  

Install MySQL to Manage Site Data

Install mysql-server

 sudo apt-get install mysql-server
 sudo mysql_install_db

Secure installation for the Root password reset
sudo mysql_secure_installation

Install PHP for Processing

Install php5-fpm
sudo apt-get install php5-fpm php5-mysql

Edit the Php.ini file
sudo vim  /etc/php5/fpm/php.ini

in this file chanage 1 to 0
cgi.fix_pathinfo=0
need to restart our PHP processor
sudo service php5-fpm restart

Configure Nginx to Use our PHP Processor


sudo nano /etc/nginx/sites-available/default
 
Currently, with the comments removed, the Nginx default server block file looks like this:

server {
   listen 80 default_server;
   listen [::]:80 default_server ipv6only=on;

   root /usr/share/nginx/html;
   index index.php index.html index.htm;

   server_name server_domain_name_or_IP;

   location / {
       try_files $uri $uri/ =404;
   }

   error_page 404 /404.html;
   error_page 500 502 503 504 /50x.html;
   location = /50x.html {
       root /usr/share/nginx/html;
   }

   location ~ \.php$ {
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }
}

Restart Nginx to make the necessary changes:

sudo service nginx restart

Create a PHP File to Test Configuration

sudo nano /usr/share/nginx/html/info.php

We can type this into the new file. This is valid PHP code that will return formatted information about our server


<?php
phpinfo();
?>
Now, you can visit this page in your web browser by visiting your server's domain name or public IP address followed by /info.php


Create a MySQL Database and User for WordPress

Login To Mysql server
mysql -u root -p

Create database for the Wordpress
CREATE DATABASE wordpress;

Create User for the Wordpress database in mysql
CREATE USER wordpressuser@localhost IDENTIFIED BY 'password';

Grant privileges for the wordpress user
GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost;

Everything should be configured correctly now. We need to flush the privileges to disk so that our current instance of MySQL knows about the privilege changes we have made:
FLUSH PRIVILEGES;
Now, exit the MySQL prompt:
exit;

Download WordPress to your Server

Download the wordpress source code

tar xzvf latest.tar.gz

Install the wordpress required php packages
sudo apt-get install php5-gd libssh2-php

Configure WordPress

Go to extracted wordpress folder
cd ~/wordpress
copy the wp-config-sample.php to wp-config.php
cp wp-config-sample.php wp-config.php

edit wp -config.php file and add the Username,password,Database name
nano wp-config.php

Copy the Files to the Document Root

Create directory on /var/
sudo mkdir -p /var/www/html

copy all the wordpress files to /var/www/html
sudo cp -rf  ~/wordpress/ /var/www/html/

Modify Nginx Server Blocks

Copy the default config file to wordpress conf file
sudo cp /etc/nginx/sites-available/default   /etc/nginx/sites-available/wordpress

Open the new file we made so that we can make some changes.
sudo vim  /etc/nginx/sites-available/wordpress
  • We will want to make the following changes.

server {
       listen 80 default_server;
       listen [::]:80 default_server ipv6only=on;

       root /var/www/html;
       index index.php index.html index.htm;

       server_name your_domain.com;

       location / {
               # try_files $uri $uri/ =404;
               try_files $uri $uri/ /index.php?q=$uri&$args;
       }

       error_page 404 /404.html;

       error_page 500 502 503 504 /50x.html;
       location = /50x.html {
               root /usr/share/nginx/html;
       }

       location ~ \.php$ {
               try_files $uri =404;
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               fastcgi_pass unix:/var/run/php5-fpm.sock;
               fastcgi_index index.php;
               include fastcgi_params;
       }
}
  • link the wordpress config file to sites-enabled.
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
  • The file we just linked conflicts with our old default file, since it borrowed so much from it. We need to disable the old file.
sudo rm /etc/nginx/sites-enabled/default
  • Now, restart the web server and PHP processor to enable our changes.
sudo service nginx restart
sudo service php5-fpm restart

Complete the Installation through the Web Interface

http://your_domain.com