Wednesday, 7 December 2016

Nginx installation and configuration linux

Nginx Server Installation and configuration Centos


Operating System : centos 6.x 64bit 

1. Installation through RPM.

# wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

2. Installation through epel


## RHEL/CentOS 6 64-Bit ##
# wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm


3. After any one of above step completed. Please proceed with you install.

# yum install nginx

4.  Belo are the default location for Nginx configuration.

Default configuration Directory : /etc/nginx
Default Vhost Directory         : /etc/nginx/conf.d/( in this directory we need to add the configuration for domain in virtual.conf or any *.conf in this directory

Default log file directory: /var/log/nginx/

Default document root directory: /usr/share/nginx/html

Default configuration file: /etc/nginx/nginx.conf(in this configuration file main setting are there)

Worker_processes :-

worker_processes  2;  this line in nginx.conf file we need to set the worker_processes  according to the number of CPUs on server for better perfomance

5. Sample Domain configuration :-

#cd /etc/nginx/conf.d/
# vi virtual.conf  ( open the file and remove existing configurations and add like below configuration)

Note :  line must end with';'

server {
    listen       80;
    listen       10.180.20.161:80;
server_name  trailinfos.com;
access_log   /var/log/nginx/trailinfos.com.access.log;
error_log    /var/log/nginx/trailinfos.com.error.log;

}


Above one is the basic configuration for domain we need to include rewrite and location between starting and ending "{"

server {
 _____
___________

}

Loading Balancing or proxy Balancing :-

upstream rails_application  {
server  10.180.20.160:80;
server  10.180.20.161:80;
}

To difine the balancing servers should like above for proxy balancing and for preferences need to set the weight like below.

upstream rails_application   {
        server srv1.example.com weight=3;
        server srv2.example.com;
        server srv3.example.com;
    }

from above example in place of 'rails_application' you can give any name and define the upstream name in domain configuration syntax should be like


upstream rails_application  {
server  10.180.20.160:80;
server  10.180.20.161:80;
}

server {
 _____
_________
location / {
   proxy_pass  http://rails_application;
   }
__

}


Define the Custome home directory and index page.
server {
 _____
_________
location / {
      root   /home/html;
       index  index.php;
   }
}


PHP Installation with Nginx :-

1. Installation of required packages through yum.

yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

2.APC installation

yum install php-pecl-apc 

3. open the /etc/php.ini and set cgi.fix_pathinfo=0:

4. set date.timezone in /etc/php.ini

[...]
[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = "Europe/Berlin"
[...]

[root@server1 nginx]# cat /etc/sysconfig/clock
ZONE="Europe/Berlin"
[root@server1 nginx]# 


5. After above steps completed open the mime.types file and add the 'text/php                             php php5;' line at the end of line .

vi /etc/nginx/mime.types

text/php                             php php5;

6. Reload the php-fpm  service :

service php-fpm reload

7. Add the below configuration in domain configuration for enable the php.

server {
 _____
_________


location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

8. Add the rewrite rules :

server {
 _____
_________
location ~ \.(gif|jpg|png|txt) {
        return http://trailinfos.com/index.php;
    }
}

From above rewrite rule if you click on http://trailinfos.com/any.gif or http://trailinfos.com/any.jpg ..etc like it redirect to http://trailinfos.com/index.php

9. Restart the Nginx service

#service nginx restart

10. Please find below details configuration file. 

upstream rails_application  {
server  10.180.20.160:80;
server  10.180.20.161:80;
}
server {
    listen       80;
    listen       10.180.20.161:80;
server_name  trailinfos.com;
access_log   /var/log/nginx/trailinfos.com.access.log;
error_log    /var/log/nginx/trailinfos.com.error.log;
 location / {
   proxy_pass  http://rails_application;
   }

#location / {
#       root   html;
#       index  index.php;
#   }
location ~ \.(gif|jpg|png|txt) {
        return http://trailinfos.com/index.php;
    }
location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

you can add the another domain same like don't include "upstream " it's one time defenition 

server {
    listen       80;
    listen       10.180.20.161:80;
server_name  test.trailinfos.com;
access_log   /var/log/nginx/test.trailinfos.com.access.log;
error_log    /var/log/nginx/test.trailinfos.com.error.log;
 location / {
   proxy_pass  http://rails_application;
   }

#location / {
#       root   html;
#       index  index.php;
#   }
location ~ \.(gif|jpg|png|txt) {
        return http://test.trailinfos.com/index.php;
    }
location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

No comments:

Post a Comment