How to install Nginx with SPDY in centos linux from source code

How to installing Nginx from source code?

 

Nginx is very popular web server. With spdy we can make nginx sever much faster and secure.  SPDY main goal is to reduce load latency by compression, multiplexing and prioritization . SPDY only works in SSL ( https) . In this blog, we will learn how to install nginx with spdy. Setting and configuring nginx with spdy will be covered in seperate blog. 

 

Step 1: Get the latest Nginx from nginx.org

 $ wget http://nginx.org/download/nginx-1.7.3.tar.gz
 $ tar -xzf nginx-1.7.3.tar.gz

Step 2: Configure Nginx  with SPDY.

Along with spdy i am adding few more options to nginx which is generaly required in web servers. But based on own requirement we can add or reduce these extra parameters.  So go to nginx folder as above and do the following:

 

$ cd nginx-1.7.3
 $  ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log  --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock  --http-client-body-temp-path=/var/lib/nginx/body  --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --with-http_ssl_module  --with-http_spdy_module --with-http_realip_module  --with-http_gunzip_module   --with-http_gzip_static_module  --with-http_stub_status_module  --user=nginx group =nginx
$ make
 $ make install

 

Above will install Nginx with spdy.

Step 3: Add Nginx as user ( you can set different users as you want):

 

$ useradd -M -r --shell /sbin/nologin --home-dir /opt/nginx nginx

 

Step 4: Create Nginx demon:

$ wget -qO /etc/init.d/nginx https://gist.github.com/sairam/5892520/raw/b8195a71e944d46271c8a49f2717f70bcd04bf1a/etc-init.d-nginx
$ chmod +x /etc/init.d/nginx

Set chkconfig run:

chkconfig --add nginx
chkconfig --level 345 nginx on

Step 5: Nginx is now setup so start nginx server:

service nginx start

Setting domains config:

Step 1: I prefer to put all my domains config file in seperate folder so that it will be easy to manage. To do that lets create the folder called vhosts in /etc/nginx/vhosts.

$ cd /etc/nginx
$ mkdir vhosts

Step 2: Create a domain config file for example domain.com and put your domain setting there.

Example of basic nginx domain config file with spdy enable:

 server {
 error_log /var/log/domain.com_error_log warn;
 listen 198.168.0.1:443 ssl spdy;
 server_name 198.168.0.1 www.domain.com;
 access_log /var/log/domain.com-bytes_log bytes_log;
 access_log /var/log/domain.com_access_log combined;
 root /home/domain/public_html;
ssl_certificate /etc/nginx/ssl/domain.com.chained.crt;
 ssl_certificate_key /etc/nginx/ssl/myserver.key;
 ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
 ssl_ciphers RC4:HIGH:!aNULL:!MD5;
 ssl_prefer_server_ciphers on;
 keepalive_timeout 60;
 ssl_session_cache shared:SSL:10m;
 ssl_session_timeout 10m;
  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;    }     # deny access to apache .htaccess files    location ~ /\.ht    {        deny all;    }}
Above is just an example, configuring nginx server will be covered in seperate blog.

Step 3: Nginx server configuration. This will be seperate topic but for the basic setup you can look below to get the idea.

Nignx config file will be in /etc/nginx/nginx.config

Example of nginx config:
# run as user nginx
user nginx;

# number of workers, general its number of cpu in your server.
 worker_processes 2;

# log critical errors in given location
 error_log /var/log/nginx/error.log crit;
worker_rlimit_nofile 20480;
 events {
 worker_connections 5120; # increase for busier servers
 use epoll; # you should use epoll here for Linux kernels 2.6.x
 }
http {
 open_file_cache max=2000 inactive=20s;
 open_file_cache_valid 30s;
 open_file_cache_min_uses 2;
 open_file_cache_errors on;
 # access_log off;
 server_name_in_redirect off;
 proxy_headers_hash_max_size 1024;
 proxy_headers_hash_bucket_size 1024;
 server_names_hash_max_size 10240;
 server_names_hash_bucket_size 1024;
server_names_hash_max_size 10240;
 server_names_hash_bucket_size 1024;
 include mime.types;
 default_type application/octet-stream;
 server_tokens off;
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout 5;
 gzip on;
 gzip_vary on;
 gzip_disable "MSIE [1-6]\.";
 gzip_proxied any;
 gzip_http_version 1.1;
 gzip_min_length 1000;
 gzip_comp_level 6;
 gzip_buffers 16 8k;
 # You can remove image/png image/x-icon image/gif image/jpeg if you have slow CPU
 gzip_types text/plain text/xml text/less text/css application/x-javascript application/xml application/xml+rss text/javascript application/atom+xml image/png image/x-icon image/gif image/jpeg;
ignore_invalid_headers on;
 client_header_timeout 3m;
 client_body_timeout 3m;
 send_timeout 3m;
 reset_timedout_connection on;
 connection_pool_size 256;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 client_max_body_size 200M;
 client_body_buffer_size 128k;

request_pool_size 32k;
 output_buffers 4 32k;
 postpone_output 1460;
 proxy_temp_path /tmp/nginx_proxy/;
 client_body_in_file_only on;
 log_format bytes_log "$msec $bytes_sent .";

# load all nginx domain config file in vhosts folder.
include "/etc/nginx/vhosts/*";
}

Adding Nginx plugin to munin in Cpanel servers

In munin different plugins have different permission or settings to fetch the data. Easiest way to get that information is by running command

cd /etc/munin/plugins
munin-run PLUGINNAME

To setup the nginx modules, we need to give url to the munin from where it can fetch data: 

setting up url just for munin as location host: 

open and edit the nginx config file, ( you can also create seperate vhost files for this configuration)

for nginx main config file you can edit nginx.conf inside /etc/nginx/ by command line vi /etc/nginx/nginx.conf

server {
       listen 127.0.0.1;
       server_name localhost;
       location /nginx_status {
               stub_status on;
               access_log   off;
               allow 127.0.0.1;
               deny all;
       }
 }


To create a seperate vhost file:
 you can create the vhost file eg: nginx-example inside /etc/nginx/vhost/
and copy and past above data. 

Setting munin to point to the file: 
Open the cpanel.conf inside /etc/munin/plugin-conf.d and add or edit the line 

[nginx plugin name ]
env.url http://localhost/nginx_status

restart the nginx server by : 
service nginx restart

and also restart the munin node 
service munin-node restartwait 10 to 15 min for muinin to gather data.