setup nginx server in ubuntu

Theory:-

Nginx (pronounced “engine x”) is a web server. It can act as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAPprotocols, as well as a load balancer and an HTTP cache.

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

NGINX is one of a handful of servers written to address the C10K problem. Unlike traditional servers, NGINX doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load. Even if you don’t expect to handle thousands of simultaneous requests, you can still benefit from NGINX’s high-performance and small memory footprint. NGINX scales in all directions: from the smallest VPS all the way up to large clusters of servers.

NGINX powers several high-visibility sites, such as Netflix, Hulu, Pinterest, CloudFlare, Airbnb, WordPress.com, GitHub,SoundCloud, Zynga, Eventbrite, Zappos, Media Temple, Heroku, RightScale, Engine Yard, MaxCDN and many others.

Apache is like Microsoft Word, it has a million options but you only need six. NGINX does those six things, and it does five of them 50 times faster than Apache.

Answer To why Nginx :-

Click Here – Link

————————————————————————-+
Installation:-

#sudo add-apt-repository ppa:nginx/stable                                          or
#sudo apt-get update
#sudo apt-get install nginx

#cd /var/www/html/
#sudo mkdir rupinddns.net
#cd rupinddns.net/
#sudo vim index.html
copy paste below

<html>
<body>
<h1>It is rupinp.ddns.net</h1>
</body>
</html>

 

save it – esc and :wq

#cd /etc/nginx/sites-available/
#cp default default.backup
#vim default

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don’t use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html/rupinddns.net;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

  server_name rupinp.ddns.net;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

save it – esc + :wq

#sudo service nginx stop
#sudo service nginx start

Check the syntax and configuration by using following :-

#sudo nginx -t
o/p:-

Test the portal :-

#curl rupinp.ddns.net
o/p:-

or in browser :-

But browser or curl using http://rupinp.ddns.net it will give error

Add ip to hosts file

#sudo vim /etc/hosts
127.0.0.1              localhost  rupinp.ddns.net
or
<ip addr of your m/c>      rupinp.ddns.net

save – esc +  :wq

o/p:-

————————————————————————+

2)Self signed SSL certificates :-

A self-signed certificate will not validate the identity of your server for your users since it is not signed by one of their web browser’s trusted certificate authorities, but it will allow you to encrypt communications with your web clients.

#sudo mkdir /etc/nginx/ssl
#sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

#cd /etc/nginx/sites-available
#sudo cp default default.backup

#sudo vim default
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
 listen 443 ssl default_server;
       ssl_certificate /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;      
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don’t use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html/rupinddns.net;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name rupinp.ddns.net;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;

}

}

save it – esc+ :wq
#sudo nginx -t

#sudo service nginx restart

Test it :

—————————————————————–+

3) Redirect HTTP (port 80 ) to HTTPS  ( port 443 )

Edit the file –
#cd /etc/nginx/sites-available/
#vim default
…………
…………
add below line outside of the original server block (that is listening on HTTPS, port 443)
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name rupinp.ddns.net;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#       include snippets/fastcgi-php.conf;
#
#       # With php7.0-cgi alone:
#       fastcgi_pass 127.0.0.1:9000;
#       # With php7.0-fpm:
#       fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
#       deny all;
#}
}
server {
    listen 80;
    server_name rupinp.ddns.net http://www.rupinp.ddns.net;
    return 301 https://rupinp.ddns.net;
}

# Virtual Host configuration for example.com

# Virtual Host configuration for example.com

#sudo nginx -t #sudo service nginx reload
————————————————————————-+

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s