Install Nginx under Centos

Posted by bassguru on Sun, 17 May 2020 21:15:28 +0200

Our goal today is to set up a Nginx service on Centos to facilitate subsequent testing and learning.This is also the first step in learning about other Internet components.Next, I will describe the source code compilation process step by step, with some instructions.

1. Download the latest source code on the official website and unzip it

The current official latest version (as of December 12, 2019) is 1.17.6, so we go directly to the official website to find the download address.Direct use of yum installation is not recommended because there are two issues:

  • The version is too old. For example, CentOS, the version installed by direct yum is version 1.12.2, which is out of date.

  • It is not possible to customize the installation module, installation directory, etc., which is not convenient for subsequent experiments.


[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@localhost ~]# cd /usr/local/src
# download nginx Installation package[root@localhost src]# wget https://www.linuxprobe.com/Software/nginx-1.6.0.tar.gz 
# Unzip installation package[root@localhost src]# tar -xzf nginx-1.6.0.tar.gz

2. Pre-install dependent Libraries

Nginx was developed entirely in C, so there must be a C compilation environment. CentOS 7.6 environments often have a gcc compiler pre-installed, so no additional installation is required, and you can install it without using yum.In addition, using Nginx's compression capabilities, regular expression capabilities, and so on, we need to install some additional dependency libraries, which must be done or errors will occur during the compilation phase.The following three libraries are used to compare the libraries that most commonly used modules in Nginx rely on. Please install them in advance when performing Nginx source compilation.

Download dependent library packages required for Nginx installation

[root@localhost ~]# cd /usr/local/src
[root@localhost src]# ls
Python-3.7.0.tar.xz
[root@localhost src]# wget https://www.linuxprobe.com/Software/zlib-1.2.8.tar.gz
--2020-05-17 20:35:36--  https://www.linuxprobe.com/Software/zlib-1.2.8.tar.gz
//Resolving host www.linuxprobe.com (www.linuxprobe.com)... 183.240.224.121, 183.240.224.114, 183.240.224.116,...
//Connecting to www.linuxprobe.com (www.linuxprobe.com)|183.240.224.121|:443... Connected.
//HTTP request sent, waiting for response... 200 OK
//Length: 571091 (558K) [application/octet-stream]
//Save to:'zlib-1.2.8.tar.gz'
100%[==============================================================================================================================>] 571,091     1.16MB/s Time-consuming 0.5s   
2020-05-17 20:35:37 (1.16 MB/s) - Saved "zlib-1.2.8.tar.gz" [571091/571091])
[root@localhost src]# wget https://www.linuxprobe.com/Software/pcre-8.35.tar.gz
--2020-05-17 20:36:12--  https://www.linuxprobe.com/Software/pcre-8.35.tar.gz
//Resolving host www.linuxprobe.com (www.linuxprobe.com)... 183.240.224.115, 183.240.224.119, 183.240.224.120,...
//Connecting to www.linuxprobe.com (www.linuxprobe.com)|183.240.224.115|:443... Connected.
//HTTP request sent, waiting for response... 200 OK
//Length: 1996552 (1.9M) [application/octet-stream]
//Saving to:'pcre-8.35.tar.gz'
100%[==============================================================================================================================>] 1,996,552   2.94MB/s Time-consuming 0.6s   
2020-05-17 20:36:13 (2.94 MB/s) - Saved "pcre-8.35.tar.gz" [1996552/1996552])
[root@localhost src]# wget https://www.linuxprobe.com/Software/openssl-1.0.1h.tar.gz
--2020-05-17 20:36:54--  https://www.linuxprobe.com/Software/openssl-1.0.1h.tar.gz
//Resolving HostWww.linuxprobe.com(www.linuxprobe.com)...183.240.224.120,183.240.224.116,183.240.224.117,...
//ConnectingWww.linuxprobe.com(www.linuxprobe.com)|183.240.224.120|:443... connected.
//HTTP request sent, waiting for response... 200 OK
//Length: 4475692 (4.3M(application/octet-stream]
//Saving to:'openssl-1.0.1h.tar.gz"
100%[==============================================================================================================================>] 4,475,692   3.93MB/s Time-consuming 1.1s   
2020-05-17 20:36:57 (3.93 MB/s) - Saved "openssl-1.0.1h.tar.gz" [4475692/4475692])
[root@localhost src]# ls
openssl-1.0.1h.tar.gz  pcre-8.35.tar.gz  Python-3.7.0.tar.xz  zlib-1.2.8.tar.gz
[root@localhost src]#

Before the Nginx service program can be officially installed, we need to address software dependencies for it, such as the package pcre, which provides a Perl language-compatible regular expression library, which is an essential dependency package for the Nginx service program to implement pseudo-static functionality.Following are the source files for unzipping, compiling, generating, and installing the Nginx service program:

[root@localhost ~]# cd /usr/local/src

[root@localhost src]# ls
openssl-1.0.1h.tar.gz  pcre-8.35.tar.gz  Python-3.7.0.tar.xz  zlib-1.2.8.tar.gz
[root@localhost src]# tar xzvf pcre-8.35.tar.gz 
[root@localhost src]# cd pcre-8.35
[root@localhost pcre-8.35]# ./configure --prefix=/usr/local/pcre
[root@localhost pcre-8.35]# make
[root@localhost pcre-8.35]# make install

The openssl package is a program file used to provide a Web site encryption certificate service. When installing the program, you need to customize the installation directory of the service program to make it more controllable when you call them later.

[root@localhost pcre-8.35]# cd /usr/local/src
[root@localhost src]# ls
openssl-1.0.1h.tar.gz  pcre-8.35  pcre-8.35.tar.gz  Python-3.7.0.tar.xz  zlib-1.2.8.tar.gz
[root@localhost src]# tar xzvf openssl-1.0.1h.tar.gz
[root@localhost src]# cd openssl-1.0.1h
[root@localhost openssl-1.0.1h]# ./config --prefix=/usr/local/openssl
[root@localhost openssl-1.0.1h]# make
[root@localhost openssl-1.0.1h]# make install

The OpenSSL package will provide many available commands in the / usr/local/openssl/bin directory by default after installation. We need to add this directory to the PATH environment variable, write it to the configuration file, and execute the source command to make the new PATH environment variable content take effect immediately, as before:

[root@linuxprobe pcre-8.35]# vim /etc/profile
..................Omit some output information.........
 64 
 65 for i in /etc/profile.d/*.sh ; do
 66 if [ -r "$i" ]; then
 67 if [ "${-#*i}" != "$-" ]; then
 68 . "$i"
 69 else
 70 . "$i" >/dev/null
 71 fi
 72 fi
 73 done
 74 export PATH=$PATH:/usr/local/mysql/bin:/usr/local/openssl/bin
 75 unset i
 76 unset -f pathmunge
[root@linuxprobe pcre-8.35]# source /etc/profile

The zlib package is a function library file that provides compression capabilities.In fact, these service programs invoked by the Nginx service program do not need to be deeply understood, as long as they have a general understanding of their role is sufficient:

[root@localhost openssl-1.0.1h]# cd /usr/local/src
[root@localhost src]# ls
openssl-1.0.1h  openssl-1.0.1h.tar.gz  pcre-8.35  pcre-8.35.tar.gz  Python-3.7.0.tar.xz  zlib-1.2.8.tar.gz
[root@localhost src]# tar xzvf zlib-1.2.8.tar.gz
[root@localhost src]# cd zlib-1.2.8
[root@localhost zlib-1.2.8]# ./configure --prefix=/usr/local/zlib
[root@localhost zlib-1.2.8]# make
[root@localhost zlib-1.2.8]# make install

Restart the system after the dependent libraries are installed

[root@localhost nginx-1.17.6]# reboot

Compile and install Nginx

[root@localhost mysql-5.6.19]# cd /usr/local/src
[root@localhost src]# useradd www -s /sbin/nologin
[root@localhost nginx-1.6.0]# make install

Especially in Nginx, you can specify to install some modules or not. The default installed modules are only suitable for simple scenarios, and often in slightly more complex cases, additional modules, or third-party and custom modules are required.This high scalability is one of the highlights of Nginx.To see the optional parameters for configure, use the --help option:

$ ./configure --help

Of all the optional parameters, two are most commonly used:

  • - prefix=PATH: Configure the root directory of the Nginx installation deployment.Similar to installing software under Windows, we specify the installation directory;

  • - with-xxx_module: - without-xxx_module where XXX represents the name of a module of Nginx, for example:

    • With-http_ssl_module->Supports SSL/TLS, or HTTPS

    • With-http_v2_module ->Supports HTTP/2

    • Without-http_fastcgi_module ->Do not use fastcgi

To improve the functionality of subsequent tests, here are as many modules as possible when compiling:


To start the Nginx service program and add it to the startup entry, you also need a script file.Unfortunately, there is no script file available by default after installing the Nginx package, so Mr. Liu Rui has prepared an available startup script file for your readers. All you need to do is create a script file in the / etc/rc.d/init.d directory and copy the following script directly (I believe that after you have mastered the contents of Chapter 4, you should be able to read it smoothly).Understand this script file).

[root@linuxprobe nginx-1.6.0]# vim /etc/rc.d/init.d/nginx#!/bin/bash
# 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: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
        if [ -z "`grep $user /etc/passwd`" ]; then
                useradd -M -s /bin/nologin $user
        fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
        if [ `echo $opt | grep '.*-temp-path'` ]; then
                value=`echo $opt | cut -d "=" -f 2`
                if [ ! -d "$value" ]; then
                        # echo "creating" $value
                        mkdir -p $value && chown -R $user $value
                fi
        fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
}
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

After saving the script file, remember to give it 755 permissions so that it can execute.The script is then executed in absolute paths, the Nginx service program is restarted with the restart parameter, and the Nginx service program is added to the boot entry using the chkconfig command.Be accomplished!

[root@linuxprobe nginx-1.6.0]# chmod 755 /etc/rc.d/init.d/nginx
[root@linuxprobe nginx-1.6.0]# /etc/rc.d/init.d/nginx restart
Restarting nginx (via systemctl):                          [  OK  ]
[root@linuxprobe nginx-1.6.0]# chkconfig nginx on

After starting Nginx, first use ps-ef | grep nginx to see if the Nginx process has started. Based on the default configuration, we will see two Nginx start processes: master process and worker process.

This is the Master-Worker mechanism of Nginx that we talked about earlier, which will be explained in more detail later.In addition, we can use the command netstat-anltp | grep 80 to see that port 80 is already being listened on CentOS, and this listening service is Nginx.Finally, you can check the Nginx service directly on the CentOS machine with a browser or curl command:


[root@localhost ~]# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]# ps -ef |grep nginx
root     37071     1  0 22:46 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www      37073 37071  0 22:46 ?        00:00:00 nginx: worker process
root     37105  2397  0 22:49 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]#

When "Welcome to Nginx!" appears, our Nginx is working properly.


Topics: Linux Nginx OpenSSL zlib CentOS