DEV Community

shiva kumar
shiva kumar

Posted on • Edited on

2

Welcome To Nginx! (Part-I)

What is Nginx?

According to Wiki, Nginx is an open source web server which can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. The software was created by Igor Sysoev and first publicly released in 2004. Nginx claims to run 60% of the busiest website on the internet.
We use Nginx mostly as a web server(with the passenger for Rails or golang application) and as a load balancer.

How to install Nginx in Ubuntu?

Two ways to install Nginx one is from the package and other is from source(recommended)

  1. Installing from the package is very simple. This will install the latest stable version

    $ sudo apt-get install nginx
    
    $ sudo nginx -v
      nginx version: nginx/1.10.2
    
  2. Installing Nginx from source. There are few dependencies we need to install before installing Nginx

    $ sudo apt-get install libpcre3-dev build-essential libssl-dev
    
    1. libpcre3-dev -> This is basically a regular expression library
    2. libssl-dev -> This package is part of OpenSSL implementation of the SSL and TLS for a secure connection
    3. build-essentail -> This package includes GNU C++ compiler, GNU C compiler and other libraries

You can install on any preferred location, mostly it's installed in /opt or /etc

    $ cd /etc/

    $ sudo wget http://nginx.org/download/nginx-1.10.2.tar.gz
Enter fullscreen mode Exit fullscreen mode

Unzipping the tar file

    $ sudo tar -zxvf nginx-1.10.2.tar.gz

    $ cd /etc/nginx-1.10.2
Enter fullscreen mode Exit fullscreen mode

Configure the build

    $ sudo ./configure --prefix=/etc/nginx --user=nginx --group=nginx --with-http_ssl_module

    Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/etc/nginx"
  nginx binary file: "/etc/nginx/sbin/nginx"
  nginx modules path: "/etc/nginx/modules"
  nginx configuration prefix: "/etc/nginx/conf"
  nginx configuration file: "/etc/nginx/conf/nginx.conf"
  nginx pid file: "/etc/nginx/logs/nginx.pid"
  nginx error log file: "/etc/nginx/logs/error.log"
  nginx http access log file: "/etc/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
Enter fullscreen mode Exit fullscreen mode

Let's build and install with the above configuration

    $ sudo make 
    $ sudo make install
Enter fullscreen mode Exit fullscreen mode

We need to create a separate user and group for Nginx

    $ sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx 
Enter fullscreen mode Exit fullscreen mode

Installing from source does not include init file for starting and stop the Nginx during the server reboots

    $ sudo vi /etc/init.d/nginx

#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/etc/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/etc/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /etc/nginx/logs/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /etc/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/etc/nginx/logs/$NAME.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/etc/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /etc/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
exit 1
;;
esac
exit 0
view raw nginx hosted with ❤ by GitHub
Enter fullscreen mode Exit fullscreen mode

Make the above file executable and add it to default run levels

    $ sudo chmod +x /etc/init.d/nginx
    $ sudo /usr/sbin/update-rc.d -f nginx defaults
Enter fullscreen mode Exit fullscreen mode

Now we can start, stop, restart and check status using the below command

    $ sudo service nginx <command>
Enter fullscreen mode Exit fullscreen mode

command -> start, stop, restart, status

You can check if nginx is running using the below command or by visiting the ip address where you have installed

    $ ps aux | grep nginx
Enter fullscreen mode Exit fullscreen mode

nginx

browser

Congratulation! You have successfully installed Nginx.

Part II will be about, what different files inside the conf folder does, configuring Nginx to serve an application and what's the role of each block inside nginx.conf file.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay