Thursday, January 9, 2014

Nginx installation on linux

Nginx is a high-performance, open source web server and reverse proxy, extremly popular for handling and serving static contents.

Here I will explain how to install nginx webserver under debian based system.

First install the compliers and dependencies,

apt-get install gcc g++ make zlib1g-dev zlib-dev

mkdir /opt/source

cd /opt/source

Now download the latest nginx software,

wget http://nginx.org/download/nginx-1.4.4.tar.gz

For 3rd party headers more module support,

wget --no-check-certificate https://codeload.github.com/agentzh/headers-more-nginx-module/zip/master

mv master master.zip

unzip master.zip

For latest openssl support for nginx, download and extract.

wget http://www.openssl.org/source/openssl-1.0.1e.tar.gz

tar -zxvf openssl-1.0.1e.tar.gz

Download and unzip pcre,

wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34.zip

unzip pcre-8.34.zip

Now we will start installing nginx,

tar -zxvf nginx-1.4.4.tar.gz
cd nginx-1.4.4

./configure --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_ssl_module --with-http_gzip_static_module --with-pcre=/opt/source/pcre-8.34 --add-module=/opt/source/headers-more-nginx-module --with-openssl=/opt/source/openssl-1.0.1e --with-debug --without-http_autoindex_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_scgi_module

make
make install

Create a startup script,

vim /etc/init.d/nginx

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Modify according to your configuration.
DAEMON=/usr/local/nginx/sbin/nginx
PIDDIR=/var/run

NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile $PIDDIR/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile $PIDDIR/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  restart)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        $PIDDIR/$NAME.pid --exec $DAEMON || true
    sleep 1
    start-stop-daemon --start --quiet --pidfile \
        $PIDDIR/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDDIR/$NAME.pid \
          --exec $DAEMON || true
      echo "$NAME."
      ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload}"
    exit 1
    ;;
esac

exit 0


chmod +x /etc/init.d/nginx

Now we can start nginx by issuing the below command,

/etc/init.d/nginx start


Sample configs ..

## Simple configuration ##
server {
         listen       80;
         server_name  localhost;
         access_log  logs/localhost.access.log  main;

         location / {
            root   /var/www/html;
            index  index.html index.htm;
         }
       }

## Redirection of all clients to https ##
 server {

        listen 80;
        server_name _;
        rewrite ^(.*) https://$host$1 permanent;

}

 ## Access denied to this uri ##
location ~ ^(/private/.*) {
        deny all;
        error_page   403        https://www.test.com/;
}

## For https sites ##
  listen 443;
        ssl on;
        ssl_certificate /etc/httpd/conf/certs/test.crt;
        ssl_certificate_key  /etc/httpd/conf/certs/test.key;
        ssl_ciphers  -all:RC4-SHA;
        ssl_prefer_server_ciphers   on;
        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;


## For passing requests to apache listening on 8080 ##
location / {

        proxy_pass   https://127.0.0.1:8080;

        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}


## Caching denied for this uri ##
location ~ ^/new/(.*)$ {
        add_header Cache-Control .no-cache,no-store,must-revalidate.;
        add_header Pragma .no-cache.;
}

speed-up-nginx

php-support

Thanks for reading,

Cheers/-

No comments:

Post a Comment