4 steps to Install NGINX on CentOS 7.0

Q. How to install NGINX on centOS 7.0 ?

A. 4 easy steps to install and run NGINX on CentOS 7.0 :

1. Add Nginx Repository :

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2. Install with yum command :

[root@localhost ~]# yum install nginx -y

3. Start NGINX service :

[root@localhost ~]# systemctl start nginx.service

4. Enable NGINX at boot :

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

How to Fix “/var/run/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1”

Issue :

I received the following error in nginx error log. Happened on nginx/1.6.2 and PHP 5.4.32 (fpm-fcgi).

2014/09/17 00:17:30 [crit] 11909#0: *34 connect() to unix:/var/run/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: www.ehowstuff.com, request: "GET /feed/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm.sock:", host: "www.ehowstuff.com"

Solution :

Edit /etc/php-fpm.d/www.conf . Uncomment “listen.mode = 0666”

How to Install Nginx With PHP5 (and PHP-FPM) and MySQL (LEMP) On Ubuntu 14.04

NGINX, pronounced “Engine X” is an alternate web server for Apache. NGINX is an open source web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols. If you plan to run a very busy and high concurrency websites but with low memory usage, i would suggest you to setup NGINX as a web server on your Virtual Private Server (VPS) or dedicated server.

1. Install nginx, PHP and MySQL :

ehowstuff@ubuntu14:~$ sudo apt-get install nginx php5 php5-fpm php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl mysql-server-5.6 -y

During this installation, you will require to set MySQL’s root password :
1

2

2. Open /etc/php5/fpm/php.ini and set cgi.fix_pathinfo=0:

ehowstuff@ubuntu14:~$ sudo vi /etc/php5/fpm/php.ini
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

; FastCGI under IIS (on WINNT based OS) supports the ability to impersonate
; securi

3. Set date.timezone in /etc/php5/fpm/php.ini :

ehowstuff@ubuntu14:~$ sudo vi /etc/php5/fpm/php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Asia/Kuala_Lumpur"

; http://php.net/date.default-latitude
;date.default_latitude = 31.7667

4. By default PHP-FPM is listening on the socket /var/run/php5-fpm.sock. We have an option to make PHP-FPM use a TCP connection. Open and add “listen = 127.0.0.1:9000” and comment out “listen = /var/run/php5-fpm.sock”

ehowstuff@ubuntu14:~$ sudo vim /etc/php5/fpm/pool.d/www.conf

Change as below :

..
..
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
..
..

5. Restart PHP-FPM :

ehowstuff@ubuntu14:~$ sudo service php5-fpm restart
stop: Unknown instance:
php5-fpm start/running, process 7314

6. Backup NGINX config file :

ehowstuff@ubuntu14:~$ sudo cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

7. Check Number CPU on your VPS server. This will will require to adjust NGINX Worker Processes & Connections. NGINX workers is equal to the number of processors :

ehowstuff@ubuntu14:~$ lscpu | grep '^CPU(s)'
CPU(s):                1

8. Configure worker_processes on nginx.conf as below :

..
..
worker_processes 1;
..
..

Full nginx configuration should like below :

user www-data;
worker_processes 1;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

9. Under your home directory, create public_html and change owner to www-data. This will be your document root directory :

ehowstuff@ubuntu14:~$ sudo mkdir public_html/
ehowstuff@ubuntu14:~$ sudo chown -R www-data:www-data public_html/

10. Create index.php under document root directory :

ehowstuff@ubuntu14:~$ sudo vim public_html/index.php

Insert the following :

<?php
phpinfo();
?>

11. Now it’s turn to setup your NGINX. Please modify the default site.

ehowstuff@ubuntu14:~$ sudo vim /etc/nginx/sites-available/default

Kindly update the root directory and comment out the required lines as below :

# You may add here your
# server {
#	...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

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

	root /home/ehowstuff/public_html;
	index index.html index.htm index.php;

	# Make site accessible from http://localhost/
	server_name localhost;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
		# Uncomment to enable naxsi on this location
		# include /etc/nginx/naxsi.rules
	}

	# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
	#location /RequestDenied {
	#	proxy_pass http://127.0.0.1:8080;    
	#}

        error_page 404 /404.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;
	}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
	#	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
	#
	#	# With php5-cgi alone:
		fastcgi_pass 127.0.0.1:9000;
	#	# With php5-fpm:
	#	fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#	listen 8000;
#	listen somename:8080;
#	server_name somename alias another.alias;
#	root html;
#	index index.html index.htm;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}


# HTTPS server
#
#server {
#	listen 443;
#	server_name localhost;
#
#	root html;
#	index index.html index.htm;
#
#	ssl on;
#	ssl_certificate cert.pem;
#	ssl_certificate_key cert.key;
#
#	ssl_session_timeout 5m;
#
#	ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#	ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
#	ssl_prefer_server_ciphers on;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

12. Restart your nginx and php5-fpm service to take effect all changes :

ehowstuff@ubuntu14:~$ sudo service nginx restart
ehowstuff@ubuntu14:~$ sudo service php5-fpm restart

13. Test your NGINX site by visit http://IP-Adress :
ubuntu-lemp

Done!!!

How to Fix 403 forbidden error at Nginx

Nginx is a alternate web server for Apache and capable to handle large traffic of the websites on your virtual private server (VPS) or on your dedicated server. Nginx work well with PHP-FPM on CentOS 5.x and also CentOS 6.x. But most of the Nginx newbie struggling to get it run perfectly and hit by “403 forbidden error”. This “403 forbidden error” means that the webpage that you trying to access is forbidden or you don’t have permission to access certain part of the website. This article is meant for the linux server administrator who is supposed to have some basic knowledge on administering the linux server and also may useful for those who start to have VPS or dedicated server.

Sample error :

2014/04/18 23:18:34 [error] 11933#0: *5 directory index of "/usr/share/nginx/html/" is forbidden, client: 192.168.0.1, server: www.ehowstuff.local, request: "GET / HTTP/1.1", host: "www.ehowstuff.local"

Possible reasons and common errors :

a) Directory index is not properly defined
b) Permissions are not set correctly
c) php-fpm Permissions not configured correctly

One of the most popular reason for “403 forbidden error” using Nginx is that the folder directory permissions are not set and also directory index are not the server block.

Solution :
1. Verify the document root was set 755 permissions to your directory accordingly
2. Add index index.php; In the server block.
3. makesure “fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;” as been added :

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

How to Setup WordPress on Nginx, PHP-FPM and MySQL

WordPress website can be served using Apache or NGINX. Apache is the most popular Web server and most widely used for WordPress blogging platform. Apache is a great option and has served many of the world’s largest Web sites. Alternate web server for Apache is Nginx, pronounced “Engine X”. Nginx is an open source web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols. Many websites and the web developer have moved to NGINX because it’s scalable, low resources, can handle many users concurrency and good website performance. For largest and busiest website, i would suggest you to host you websites and blog on Virtual Private Server (VPS) or dedicated server and run NGINX as a web server. Assumed that MySQL has been prepared and configured for WordPress and was tested on CentOS 6.5.

1. Prepared Nginx Repository :

[root@vps ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2. Install NGINX, PHP, php-fpm amd MySQL server :

[root@vps ~]# yum install nginx php php-cli php-mysql php-gd php-xml php-fpm mysql mysql-server -y

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

[root@vps ~]# vi /etc/php.ini
..
..
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
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 = "Asia/Kuala_Lumpur"

5. This server configuration was setup for PHP-FPM use a UNIX Socket.

[root@vps ~]# vim /etc/php-fpm.d/www.conf

Specify .sock path :

..
listen = /var/run/php-fpm.sock
..

Change user to run php-fpm :

..
user = nginx
..
..
group = nginx
..

6. Backup NGINX config file :

[root@vps ~]# cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

7. Create sites-available folder under /etc/nginx :

[root@vps ~]# mkdir /etc/nginx/sites-available

8. Adjust NGINX Worker Processes & Connections. NGINX workers equal the number of processors :

Check Number CPU on your VPS server :

[root@vps ~]# lscpu | grep '^CPU(s)'
CPU(s):                1

or

[root@vps ~]# cat /proc/cpuinfo | grep processor
processor       : 0
..
worker_processes  1;
..
..
worker_connections  1024;
..

Configure nginx.conf as below :

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;
    gzip_types text/css text/x-component application/x-javascript application/javascript  text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;

    
    include /etc/nginx/sites-available/*.conf;
}

9. Create common.conf and wordpress.conf under /etc/nginx/conf.d/ folder :

[root@vps ~]# vim /etc/nginx/conf.d/common.conf
[root@vps ~]# vim /etc/nginx/conf.d/wordpress.conf
[root@vps ~]# vim /etc/nginx/conf.d/common.conf

Add the following :

# Global configuration file.
# ESSENTIAL : Configure Nginx Listening Port
listen 80;
# ESSENTIAL : Default file to serve. If the first file isn't found,
index index.php index.html index.htm;
# ESSENTIAL : no favicon logs
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
# ESSENTIAL : robots.txt
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}
# ESSENTIAL : Configure 404 Pages
error_page 404 /404.html;
# ESSENTIAL : Configure 50x Pages
error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
# SECURITY : Deny all attempts to access hidden files .abcde
location ~ /\. {
    deny all;
}
# PERFORMANCE : Set expires headers for static files and turn off logging.
location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
    access_log off; log_not_found off; expires 30d;

Explanation :

listen 80; 

-Specifies the listening port of the server.

index index.php index.html index.htm;

-Specifies the default file to serve (WordPress index.php). For HTML sites, please include index.html & index.htm;.

location = /robots.txt {allow all;} 

-Allows the access to robots.txt

Create wordpress.conf :

[root@vps ~]# vim /etc/nginx/conf.d/wordpress.conf

Add the following :

# WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact
location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

# SECURITY : Deny all attempts to access PHP Files in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}
# REQUIREMENTS : Enable PHP Support
location ~ \.php$ {
    # SECURITY : Zero day Exploit Protection
    try_files $uri =404;
    # ENABLE : Enable PHP, listen fpm sock
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# PLUGINS : Enable Rewrite Rules for Yoast SEO SiteMap
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

Explanation :

try_files $uri $uri/ /index.php?q=$uri&$args 

-rewrite rule required to allow you to choose your custom permalink structure on WordPress.

location ~* /(?:uploads|files)/.*\.php$ {deny all;} 

-this will prevent malicious code from being uploaded and executed from the WordPress media directory.

location ~ \.php$ {...}

-since WordPress is a php site, we need to tell NGINX how to a pass our php scripts to PHP5.

try_files $uri =404; 

-this is a security rule, you only want to either serve a determined php file or go to a 404 error.

10. Create a virtual server under /etc/nginx/sites-available/ directory :

[root@vps ~]# vi /etc/nginx/sites-available/ehowstuff.local.conf

Assumed that you want to configure a WordPress site with www.ehowstuff.local domain, please setup as below :

server {
    server_name ehowstuff.local;
    rewrite ^/(.*)$ http://www.ehowstuff.local/$1 permanent;
}

server {
        server_name www.ehowstuff.local;
        root /var/www/html/wordpress;
        access_log /var/log/nginx/www.ehowstuff.local.access.log;
        error_log /var/log/nginx/www.ehowstuff.local.error.log;
        include conf.d/common.conf;
        include conf.d/wordpress.conf;
}

Please change the following for virtual server ehowstuff.local.conf :

server_name: Define the server block for the URL.
root: Where you keep your website file
access log & error log: Paths for your logs

11. Start php-fpm and nginx :

[root@vps ~]# /etc/init.d/php-fpm start
Starting php-fpm:                                          [  OK  ]
[root@vps ~]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]

12. Make php-fpm and nginx start at boot :

[root@vps ~]# chkconfig php-fpm on
[root@vps ~]# chkconfig nginx on

13. Verify that the required port already present.

[root@vps ~]# netstat -plunt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1042/rpcbind
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3174/nginx
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1096/sshd
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      3394/mysqld
tcp        0      0 :::111                      :::*                        LISTEN      1042/rpcbind
tcp        0      0 :::22                       :::*                        LISTEN      1096/sshd
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               1042/rpcbind
udp        0      0 0.0.0.0:793                 0.0.0.0:*                               1042/rpcbind
udp        0      0 :::111                      :::*                                    1042/rpcbind
udp        0      0 :::793                      :::*                                    1042/rpcbind

How to Setup Nginx With PHP-FastCGI on CentOS 6.2/CentOS 6.3 VPS Server

At the previous post, i have setup Nginx as reverse proxy to apache web server. This post will covers the steps how to setup nginx web server to use PHP-FastCGI for dynamic content. Nginx (pronounced “Engine-X”) is a free, open-source HTTP Web server and one of the alternative to Apache http server. It’s a high performance edge web server with the lowest memory footprint and the key features to build modern and efficient web infrastructure. FastCGI is a language independent, scalable, open extension to CGI that provides high performance without the limitations of server specific APIs. FastCGI provides better scalability and performance.

1. Prepared yum repository for nginx :

[root@centos63 ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2. Install Nginx :

[root@centos63 ~]# yum install nginx -y

3. Install required php packages :

[root@centos63 ~]# yum install php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy -y

Examples of Software Installed and the Dependency:

Installed:
  php-eaccelerator.i686 1:0.9.6.1-1.el6   php-imap.i686 0:5.3.3-14.el6_3      php-ldap.i686 0:5.3.3-14.el6_3
  php-magickwand.i686 0:1.0.9-1.el6       php-magpierss.noarch 0:0.72-6.el6   php-mssql.i686 0:5.3.3-1.el6
  php-odbc.i686 0:5.3.3-14.el6_3          php-pear.noarch 1:1.9.4-4.el6       php-shout.i686 0:0.9.2-6.el6
  php-snmp.i686 0:5.3.3-14.el6_3          php-soap.i686 0:5.3.3-14.el6_3      php-tidy.i686 0:5.3.3-14.el6_3
  php-xml.i686 0:5.3.3-14.el6_3           php-xmlrpc.i686 0:5.3.3-14.el6_3

Dependency Installed:
  ConsoleKit.i686 0:0.4.1-3.el6                        ConsoleKit-libs.i686 0:0.4.1-3.el6
  GConf2.i686 0:2.28.0-6.el6                           ImageMagick.i686 0:6.5.4.7-6.el6_2
  ORBit2.i686 0:2.14.17-3.1.el6                        atk.i686 0:1.28.0-2.el6
  avahi-libs.i686 0:0.6.25-11.el6                      cairo.i686 0:1.8.8-3.1.el6
  cups-libs.i686 1:1.4.2-48.el6_3.1                    dbus.i686 1:1.2.24-7.el6_3
  eggdbus.i686 0:0.6-3.el6                             fontconfig.i686 0:2.8.0-3.el6
  freetds.i686 0:0.91-2.el6                            ghostscript.i686 0:8.70-14.el6_3.1
  ghostscript-fonts.noarch 0:5.50-23.1.el6             gtk2.i686 0:2.18.9-10.el6
  hicolor-icon-theme.noarch 0:0.11-1.1.el6             jasper-libs.i686 0:1.900.1-15.el6_1.1
  lcms-libs.i686 0:1.19-1.el6                          libICE.i686 0:1.0.6-1.el6
  libIDL.i686 0:0.8.13-2.1.el6                         libSM.i686 0:1.1.0-7.1.el6
  libXcomposite.i686 0:0.4.1-2.el6                     libXcursor.i686 0:1.1.10-2.el6
  libXdamage.i686 0:1.1.2-1.el6                        libXext.i686 0:1.1-3.el6
  libXfixes.i686 0:4.0.4-1.el6                         libXfont.i686 0:1.4.1-2.el6_1
  libXft.i686 0:2.1.13-4.1.el6                         libXi.i686 0:1.3-3.el6
  libXinerama.i686 0:1.1-1.el6                         libXrandr.i686 0:1.3.0-4.el6
  libXrender.i686 0:0.9.5-1.el6                        libXt.i686 0:1.0.7-1.el6
  libc-client.i686 0:2007e-11.el6                      libcroco.i686 0:0.6.2-5.el6
  libfontenc.i686 0:1.0.5-2.el6                        libgsf.i686 0:1.14.15-5.el6
  libogg.i686 2:1.1.4-2.1.el6                          librsvg2.i686 0:2.26.0-5.el6_1.1.0.1.centos
  libshout.i686 0:2.2.2-5.1.el6                        libthai.i686 0:0.1.12-3.el6
  libtheora.i686 1:1.1.0-2.el6                         libtidy.i686 0:0.99.0-19.20070615.1.el6
  libtiff.i686 0:3.9.4-6.el6_3                         libvorbis.i686 1:1.2.3-4.el6_2.1
  libwmf-lite.i686 0:0.2.8.4-22.el6.centos             lm_sensors-libs.i686 0:3.1.1-10.el6
  net-snmp.i686 1:5.5-41.el6_3.1                       net-snmp-libs.i686 1:5.5-41.el6_3.1
  pango.i686 0:1.28.1-3.el6_0.5.1.centos               pixman.i686 0:0.18.4-1.el6_0.1
  polkit.i686 0:0.96-2.el6_0.1                         sgml-common.noarch 0:0.6.3-32.el6
  speex.i686 0:1.2-0.12.rc1.1.el6                      unixODBC.i686 0:2.2.14-11.el6
  urw-fonts.noarch 0:2.4-10.el6                        xorg-x11-font-utils.i686 1:7.2-11.el6

Dependency Updated:
  dbus-libs.i686 1:1.2.24-7.el6_3

Complete!

4. Configure EPEL Repository on CentOS 6.3 and install spawn-fcgi :

[root@centos63 ~]# yum install spawn-fcgi -y

5. Start configure nginx. It is better to backup original nginx config file :

[root@centos63 ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

6. Create nginx DocumentRoot for ehowstuff.local virtual host :

[root@centos63 ~]# mkdir -p /var/www/html/ehowstuff.local
[root@centos63 ~]# mkdir -p /var/www/html/ehowstuff.local/public_html
[root@centos63 ~]# chown -R nginx:nginx /var/www/html/ehowstuff.local/public_html

7. Create folder where to store access.log and error.log :

[root@centos63 ~]# mkdir -p /var/log/nginx/ehowstuff.local
[root@centos63 ~]# chown -R nginx:nginx /var/log/nginx/ehowstuff.local

8. Modify default nginx config file :

[root@centos63 ~]# vi /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


9. Add the host configuration file under /etc/nginx/conf.d/. Nginx config file will load all *.conf files under conf.d folder :

As example, the website domain is ehowstuff.local. So virtual server ehowstuff.local will be created and named as ehowstuff.local.conf :

[root@centos63 ~]# vi /etc/nginx/conf.d/ehowstuff.local.conf
server {
    server_name www.ehowstuff.local ehowstuff.local;
    access_log /var/log/nginx/ehowstuff.local/access.log;
    error_log /var/log/nginx/ehowstuff.local/error.log;
    root /var/www/html/ehowstuff.local/public_html;

    location / {
        index index.html index.htm index.php;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html/ehowstuff.local/public_html$fastcgi_script_name;
    }
}

10. Check your nginx version :

[root@centos63 ~]# nginx -v
nginx version: nginx/1.2.4

11. Configure php-fastcgi :

[root@centos63 ~]# cd /opt
[root@centos63 opt]# wget -O php-fastcgi-rpm.sh http://library.linode.com/assets/696-php-fastcgi-rpm.sh
[root@centos63 opt]# mv php-fastcgi-rpm.sh /usr/bin/php-fastcgi
[root@centos63 opt]# chmod +x /usr/bin/php-fastcgi
[root@centos63 opt]# wget -O php-fastcgi-init-rpm.sh http://library.linode.com/assets/697-php-fastcgi-init-rpm.sh
[root@centos63 opt]# mv php-fastcgi-init-rpm.sh /etc/rc.d/init.d/php-fastcgi
[root@centos63 opt]# chmod +x /etc/rc.d/init.d/php-fastcgi

12. Start php-fastcgi to control spawn-fcgi :

[root@centos63 ~]# /etc/init.d/php-fastcgi start
Starting php-cgi: spawn-fcgi: child spawned successfully: PID: 3380
                                                           [  OK  ]

13. Start Nginx service :

[root@centos63 ~]# service nginx start

14. Make Nginx and php-fastcgi start at boot :

[root@centos63 opt]# chkconfig --add nginx
[root@centos63 opt]# chkconfig nginx on
[root@centos63 opt]# chkconfig --add php-fastcgi
[root@centos63 opt]# chkconfig php-fastcgi on

Nginx Reverse Proxy Setup for Linux Server

Nginx Reverse ProxyNginx also pronounced “Engine-X” is a free, open-source HTTP Web server and one of the best alternative to Apache http server. It is a high-performance edge web server with the lowest memory footprint and the key features to build modern and efficient web infrastructure. Nginx also provides a combination of Nginx web servers, Nginx reverse proxy and Nginx load balancing solution to the websites that running on high traffic and just wants to be consistently efficient. Nginx has the lowest memory footprint possible and optimizes CPU usage while delivering maximum performance even on a very cheap server hardware. More importantly, Nginx is able to continuously take more connections while maintaining low memory usage.

What is Nginx Reverse Proxy ?

When Nginx reverse proxy received request, it sends a request to the specified proxied server. In this case the specified proxied server is Apache web server. When Nginx reverse proxy fetches the response from Apache web server, It will sends it back to the client. In other words, Nginx reverse proxy serve as front-end server for Apache web service.

How to Setup Nginx Reverse Proxy for Linux

This article will show you how to install and configure Nginx reverse proxy for Apache web server. It was assumed that Apache web server has been running at 192.168.1.55 and nginx will be install at another server with Ip address, 192.168.1.54. This has been tested and working fine at CentOS 6 / CentOS 7 / RHEL 7 / Oracle Linux 7.

Nginx Reverse Proxy Server : 192.168.1.54
Apache Web server : 192.168.1.55

1. Prepared yum repository for nginx :

# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2. Perform yum install for nginx :

# yum install nginx -y

3. Backup original nginx config file :

# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

4. Modify default nginx config file :

# vi /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {

    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    charset   utf-8;
    keepalive_timeout  65;
    server_tokens       off;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         off;

    server {
          listen 80;
          server_name  _;
          root   /usr/share/nginx/html;
          index  index.html index.htm;     }

    include  conf.d/*.conf;
}

5. Add the host configuration file under /etc/nginx/conf.d/. Nginx config file will load all *.conf files under conf.d folder :

As example, the website domain is ehowstuff.local. So virtual server ehowstuff.local will be created and named as ehowstuff.local.conf :

# vi /etc/nginx/conf.d/ehowstuff.local.conf
server {
      listen 80;
      server_name  ehowstuff.local www.ehowstuff.local;

 access_log  off;
 error_log off;

location / {
  proxy_pass              http://192.168.1.55:80/;
  proxy_set_header        X-Real-IP       $remote_addr;
  proxy_set_header        Host  $host;
  proxy_redirect          off;
  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_connect_timeout   90;
  proxy_send_timeout      90;
  proxy_read_timeout      90;
  client_max_body_size    10m;
  client_body_buffer_size 128k;
  proxy_buffer_size       4k;
  proxy_buffers           4 32k;
  proxy_busy_buffers_size 64k;
}
}

On above configuration file, all the traffic to ehowstuff.local port 80 will be redirected to the Apache web server that hosted at 192.168.1.55. Nginx reverse proxy serve as front-end server for Apache web service.

6. Verify the configuration file :

# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

7. Start nginx server :

# service nginx start

8. Configure nginx start at boot :

# chkconfig nginx on

 

How to Install Nginx Web Server on CentOS 6.3

Nginx or “engine x” is a free, open-source HTTP server and one of the alternative to Apache http server. It’s a high performance edge web server with the lowest memory footprint and the key features to build modern and efficient web infrastructure. This post will show the quick step to install nginx on linux CentOS 6.3 server.

1. Prepared yum repository for nginx :

[root@centos63 ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2. Perform yum install for nginx :

[root@centos63 ~]# yum install nginx -y

Examples :

[root@centos63 ~]# yum install nginx -y
Loaded plugins: fastestmirror, presto
Loading mirror speeds from cached hostfile
 * base: mirrors.hostemo.com
 * extras: mirrors.hostemo.com
 * updates: mirrors.hostemo.com
nginx                                                                        | 1.3 kB     00:00
nginx/primary                                                                | 3.9 kB     00:00
nginx                                                                                         25/25
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nginx.i386 0:1.2.2-1.el6.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================
 Package              Arch                Version                        Repository            Size
====================================================================================================
Installing:
 nginx                i386                1.2.2-1.el6.ngx                nginx                308 k

Transaction Summary
====================================================================================================
Install       1 Package(s)

Total download size: 308 k
Installed size: 623 k
Downloading Packages:
Setting up and reading Presto delta metadata
Processing delta metadata
Package(s) data still to download: 308 k
nginx-1.2.2-1.el6.ngx.i386.rpm                                               | 308 kB     00:02
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : nginx-1.2.2-1.el6.ngx.i386                                                       1/1
----------------------------------------------------------------------

Thanks for using NGINX!

Check out our community web site:
* http://nginx.org/en/support.html

If you have questions about commercial support for NGINX please visit:
* http://www.nginx.com/support.html

----------------------------------------------------------------------
  Verifying  : nginx-1.2.2-1.el6.ngx.i386                                                       1/1

Installed:
  nginx.i386 0:1.2.2-1.el6.ngx

Complete!

3. Start nginx :

[root@centos63 ~]# service nginx start
Starting nginx:                                            [  OK  ]

4. Make nginx auto start at boot :

[root@centos63 ~]# chkconfig nginx on

How to Install and Configure Nginx Web Server on CentOS 6.2

Nginx or “engine x” is a free, open-source HTTP server and one of the alternative to Apache http server. It’s a high performance edge web server with the lowest memory footprint and the key features to build modern and efficient web infrastructure. In this post, i will guide you through the basic steps to install and configure Nginx web server on linux CentOS 6.2 server.

To add nginx yum repository, create a file named /etc/yum.repos.d/nginx.repo.

[root@centos62 ~]# vi /etc/yum.repos.d/nginx.repo

Paste one of the configurations below:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Install nginx using yum command :

[root@centos62 ~]# yum install nginx -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.ipserverone.com
 * epel: ftp.yz.yamagata-u.ac.jp
 * extras: centos.ipserverone.com
 * rpmforge: apt.sw.be
 * updates: centos.ipserverone.com
nginx                                                                        | 1.3 kB     00:00
nginx/primary                                                                | 3.0 kB     00:00
nginx                                                                                         17/17
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nginx.i686 0:1.0.12-1.el6 will be updated
---> Package nginx.i386 0:1.0.14-1.el6.ngx will be an update
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================
 Package             Arch               Version                           Repository           Size
====================================================================================================
Updating:
 nginx               i386               1.0.14-1.el6.ngx                  nginx               294 k

Transaction Summary
====================================================================================================
Upgrade       1 Package(s)

Total download size: 294 k
Downloading Packages:
nginx-1.0.14-1.el6.ngx.i386.rpm                                              | 294 kB     00:43
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating   : nginx-1.0.14-1.el6.ngx.i386                                                      1/2
  Cleanup    : nginx-1.0.12-1.el6.i686                                                          2/2

Updated:
  nginx.i386 0:1.0.14-1.el6.ngx

Complete!

Modify default website config file :

[root@centos62 ~]# vi /etc/nginx/conf.d/default.conf

Original

 location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

Edit as below :

 location / {
        #root   /usr/share/nginx/html;
        root   /var/www/html/nginxweb;
        index  index.html index.htm;

Create index.html :

[root@centos62 ~]# vi /var/www/html/nginxweb/index.html

Add this html code:

<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Test Page for nginx web server
</div>
</body>
</html>

Start nginx web server :

[root@centos62 ~]# service nginx start