Operations 10 min read

Master Nginx: From Installation to Load Balancing and Service Scripts

This guide walks through testing Nginx configuration, starting the server, understanding its load‑balancing architecture, installing from source with required dependencies, handling common build errors, reviewing the final configuration summary, and creating a Linux init script for reliable service management.

21CTO
21CTO
21CTO
Master Nginx: From Installation to Load Balancing and Service Scripts

1. Test Nginx Configuration

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

2. Start Nginx

/usr/local/nginx/sbin/nginx

3. Nginx Load Balancing

Nginx architecture and load‑balancing concepts are illustrated below.

Load‑balancing diagram:

Nginx upstream currently supports five distribution methods:

Round Robin (default) : requests are assigned sequentially; failed back‑ends are automatically removed.

Weight : assign a weight to each server; higher weight receives proportionally more traffic.

ip_hash : hash the client IP so a client consistently reaches the same back‑end, useful for session persistence.

fair (third‑party) : prefers servers with shorter response times.

url_hash (third‑party) : hashes the request URL, directing the same URL to the same back‑end, beneficial for caching.

4. Install and Configure Nginx

(1) Build from source

# Change to source directory
cd /usr/local/src/
# Download source tarball
wget http://syslab.comsenz.com/downloads/linux/nginx-0.9.6.tar.gz
# Extract and enter directory
tar zxvf nginx-0.9.6.tar.gz
cd nginx-0.9.6
# Configure with desired options
./configure \
  --prefix=/usr/local/nginx \
  --sbin-path=/usr/local/nginx/sbin/nginx \
  --conf-path=/usr/local/nginx/conf/nginx.conf \
  --error-log-path=/usr/local/nginx/logs/error.log \
  --http-log-path=/usr/local/nginx/logs/access.log \
  --pid-path=/usr/local/nginx/var/nginx.pid \
  --lock-path=/usr/local/nginx/var/nginx.lock \
  --http-client-body-temp-path=/dev/shm/nginx_temp/client_body \
  --http-proxy-temp-path=/dev/shm/nginx_temp/proxy \
  --http-fastcgi-temp-path=/dev/shm/nginx_temp/fastcgi \
  --user=www --group=www \
  --with-cpu-opt=pentium4F \
  --without-select_module \
  --without-poll_module \
  --with-http_realip_module \
  --with-http_sub_module \
  --with-http_gzip_static_module \
  --with-http_stub_status_module \
  --without-http_ssi_module \
  --without-http_userid_module \
  --without-http_geo_module \
  --without-http_memcached_module \
  --without-http_map_module \
  --without-mail_pop3_module \
  --without-mail_imap_module \
  --without-mail_smtp_module \
  --with-pcre=/usr/local/src/pcre-8.32/ \
  --with-zlib=/usr/local/zlib
# Build and install
make && make install
# Create temporary directory for fastcgi buffers
mkdir /dev/shm/nginx_temp

Some Nginx versions may fail to compile PCRE; ensure the PCRE source is extracted to /usr/local/src/pcre-8.32 and use --with-pcre=/usr/local/src/pcre-8.32 without rebuilding PCRE.

(2) Install required dependencies

# Install build tools
apt-get --install-suggests install gcc g++ make
# Download additional libraries
wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
wget http://zlib.net/zlib-1.2.8.tar.gz
wget http://www.openssl.org/source/openssl-1.0.1g.tar.gz
wget http://www.canonware.com/download/jemalloc/jemalloc-3.6.0.tar.bz2
wget http://tengine.taobao.org/download/tengine-2.0.2.tar.gz
# Build and install PCRE
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/usr/local/pcre-8.35
make && make install
# Build and install Zlib
cd ..
tar zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib-1.2.8
make && make install

If you encounter the error "the HTTP gzip module requires the zlib library", either disable the module with --without-http_gzip_module or provide the Zlib library via --with-zlib=<path>. Another common error is missing a C++ compiler for PCRE; install a suitable C++ compiler.

After successful installation, the configuration summary looks like:

[Nginx Configuration Summary]
+ using PCRE library: /usr/local/src/pcre-8.32
+ OpenSSL library is not used
+ using builtin md5 code
+ sha1 library is not found
+ using zlib library: /usr/local/zlib
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/var/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "/dev/shm/nginx_temp/client_body"
nginx http proxy temporary files: "/dev/shm/nginx_temp/proxy"
nginx http fastcgi temporary files: "/dev/shm/nginx_temp/fastcgi"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

(2) Create an init script for Nginx

#!/bin/bash
# Startup script for the Nginx HTTP Server
NGINX_PATH="/usr/local/nginx/"
OPTIONS="-c ${NGINX_PATH}conf/nginx.conf"
prog=nginx
nginx=${NGINX_PATH}sbin/nginx
pidfile=/var/run/nginx.pid
. /etc/rc.d/init.d/functions

start() {
  echo -n "Starting $prog: "
  daemon --pidfile=${pidfile} $nginx $OPTIONS
  RETVAL=$?
  echo
  return $RETVAL
}

stop() {
  echo -n "Stopping $prog: "
  killproc -p ${pidfile} $nginx
  RETVAL=$?
  echo
}

reload() {
  echo -n "Reloading $prog: "
  killproc -p ${pidfile} $nginx -HUP
  RETVAL=$?
  echo
}

case "$1" in
  start)   start   ;;
  stop)    stop    ;;
  restart) stop; start ;;
  reload)  reload  ;;
  status)  status $prog; RETVAL=$? ;;
  *) echo "Usage: $prog {start|stop|restart|reload|status}"; RETVAL=3 ;;
esac
exit $RETVAL

Make the script executable and enable it at boot:

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
load balancingConfigurationNginxInstallationinit script
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.