Linux Nginx installation and deployment

Keywords: Nginx OpenSSL zlib SSL

Linux Nginx installation and deployment

1. Introduction to Nginx

Nginx official website: http://nginx.org/

Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server. The purpose of the initial development is to develop proxy email server roommate: Igor Sysoev. The source code conforms to BSD open source. Its characteristic is that it occupies less memory and has strong concurrency ability. There are many large websites using Nginx: Baidu, Taobao, Tencent and so on.

As an Http server, Nginx has the following basic characteristics:

a. Processing static files, indexing files and automatic indexing, opening file descriptor buffer;
b. Buffer-free reverse proxy acceleration, simple load balancing and fault tolerance;   
c. Modular structure, including gzipping, byte ranges,chunked responses and SSI-filter filters. If FastCGI or other proxy servers handle multiple SSIs in egg juice, this process can run in parallel without waiting for each other.
d. Support for SSL and TLSSNI.

2. Download Source Pack on Official Website

nginx-1.12.1.tar.gz 

Download address: http://nginx.org/en/download.html

3. Preparations

The installation of Nginx depends on three packages

a. SSL functions require OpenSSL libraries, http://www.openssl.org/
b, gzip module needs zlib library, http://www.zlib.net/
c, rewrite module needs PCRE library, http://www.pcre.org/

Install the above software in sequence and skip if installed.

3.1. Source installation zlib

Download address: http://www.zlib.net/

tar -zvxf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install        

3.2. Source installation openssl

Download address: https://www.openssl.org/source/

tar -zvxf openssl-1.1.0f.tar.gz
cd openssl-1.1.0f
./config
make
make install

3.3. Source Installation of pcre

Download address: http://www.pcre.org/

tar -zvxf pcre-8.40.tar.gz
cd pcre-8.40
./configure --prefix=/usr/local/pcre
make
make install

If it's a 64-bit system, you need to create a lib64 directory under the installation directory and copy all the dynamic libraries under lib to lib64.

cd /usr/local/pcre/
mkdir lib64
cd lib
cp -R * /usr/local/pcre/lib64/

4. Install Nginx service and install Nginx in / usr/local/nginx directory

Unzip installation package:

[root@QIANZI-BASE home]# tar -zvxf nginx-1.12.1.tar.gz

[root@QIANZI-BASE home]# cd nginx-1.12.1

Compile and install:

[root@QIANZI-BASE nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=../pcre-8.40 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.0f

[root@QIANZI-BASE nginx-1.12.1]# make

[root@QIANZI-BASE nginx-1.12.1]# make install

Note: The source directories of pcre-8.40, zlib-1.2.11 and openssl-1.1.0f are pcre, zlib and OpenSSL respectively.

5. Verify that Nginx is installed successfully

[root@QIANZI-BASE nginx-1.12.1]# /usr/local/nginx/sbin

[root@QIANZI-BASE sbin]# ./nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

//Input in browser: http://192.168.10.188/

//Output: Welcome to nginx!
     If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
     For online documentation and support please refer to nginx.org.
     Commercial support is available at nginx.com.
     Thank you for using nginx.

//Successful installation!

6. Set Nginx to start automatically

echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local

7. Start nginx service with server command

Create nginx startup script under / etc/init.d:

[root@QIANZI-BASE init.d]# vim nginx
#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig: - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#   proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config: /etc/nginx/nginx.conf 
# config: /etc/sysconfig/nginx 
# pidfile: /var/run/nginx.pid 
# Source function library. 
. /etc/rc.d/init.d/functions 
# Source networking configuration. 
. /etc/sysconfig/network 
# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 
    nginx="/usr/local/nginx/sbin/nginx" 
    prog=$(basename $nginx) 
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
    lockfile=/var/lock/subsys/nginx 

start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
[ $retval -eq 0 ] && touch $lockfile 
    return $retval 
} 

stop() { 
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT 
    retval=$? 
    echo 
[ $retval -eq 0 ] && rm -f $lockfile 
    return $retval 
    killall -9 nginx 
} 

restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    start 
} 

reload() { 
    configtest || return $? 
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP 
    RETVAL=$? 
    echo 
} 

force_reload() { 
    restart 
} 

configtest() { 
    $nginx -t -c $NGINX_CONF_FILE 
} 

rh_status() { 
    status $prog 
} 

rh_status_q() { 
    rh_status >/dev/null 2>&1 
} 

case "$1" in 
    start) 
        rh_status_q && exit 0 
        $1 
    ;; 
    stop) 
        rh_status_q || exit 0 
        $1 
    ;; 
    restart|configtest) 
        $1 
    ;; 
    reload) 
        rh_status_q || exit 7 
        $1 
    ;; 
    force-reload) 
        force_reload 
    ;; 
    status) 
        rh_status 
    ;; 
    condrestart|try-restart) 
        rh_status_q || exit 0 
    ;; 
    *) 
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2 
esac
Modify script permissions

[root@QIANZI-BASE init.d]# chmod 755 nginx

Add script files to chkconfig

[root@QIANZI-BASE init.d]# chkconfig --add nginx

Setting nginx boot up automatically at Level 3 and 5

[root@QIANZI-BASE init.d]# chkconfig --level 35 nginx on

Testing nginx scripts for normal usage

/etc/init.d/nginx restart
/etc/init.d/nginx reload
/etc/init.d/nginx stop 

8. [End]

Posted by Grodo on Wed, 12 Jun 2019 11:29:14 -0700