1. Introduction to Nginx
Nginx (pronounced engine x) is specially developed for performance optimization. Its most well-known advantages are its stability, low system resource consumption, and high processing capacity for concurrent connections (a single physical server can support 30,000 to 50,000 concurrent connections) Connection), is a high-performance HTTP and reverse proxy server, and also an IMAP/POP3/SMTP proxy server.
2. Installation preparation
2.1 gcc installation
To install nginx, you need to compile the source code downloaded from the official website. The compilation depends on the gcc environment. If there is no gcc environment, you need to install:
[ root@nginx ~]# yum -y install gcc-c++
2.2 pcre installation
PCRE (Perl Compatible Regular Expressions) is a Perl library, including Perl compatible regular expression libraries. The http module of nginx uses pcre to parse regular expressions, so the pcre library needs to be installed on linux. pcre-devel is a secondary development library developed using pcre. Nginx also needs this library.
[ root@nginx ~]# yum -y install pcre pcre-devel
2.3 zlib installation
The zlib library provides many ways to compress and decompress. nginx uses zlib to gzip the contents of the http package, so you need to install the zlib library on Centos.
[ root@nginx ~]# yum -y install zlib zlib-devel
2.4 OpenSSL installation
OpenSSL is a powerful secure socket layer cryptographic library, including the main cryptographic algorithms, commonly used key and certificate packaging management functions and SSL protocols, and provides a wealth of applications for testing or other purposes.
Nginx not only supports the http protocol, but also supports https (that is, HTTP transmission over the ssl protocol), so you need to install the OpenSSL library in Centos.
[ root@nginx ~]# yum -y install openssl openssl-devel
3. Nginx installation
3.1 Nginx version
Download URL: https://nginx.org/en/download.html
Select the latest stable version nginx-1.12.2
Version description:
Mainline version: Mainline is currently the main version of Nginx, which can be said to be the development version
Stable version: the latest stable version, the recommended version in the production environment
Legacy versions: the stable version of the legacy old version
3.2 Nginx download
Download using wget command
[ root@nginx ~]# wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
Install if there is no wget command:
[ root@nginx ~]# yum -y install wget
3.3 Unzip
[ root@nginx ~]# tar -zxvf nginx-1.12.2.tar.gz
3.4 Installation configuration
3.4.1 Create nginx users and groups
[ root@nginx include]# groupadd nginx
[ root@nginx include]# useradd -g nginx -d /home/nginx nginx
[ root@nginx include]# passwd nginx
3.4.2 Third-party module installation
This article takes the installation of the third-party module sticky as an example, the version is 1.,2.5, download link: https://pan.baidu.com/s/1Zpv6axGNUJkkGcam7EoLaQ Password: 6jaq
Upload and unzip:
[ root@nginx ~]# tar -zxvf nginx-goodies-nginx-sticky-module-ng-08a395c66e42..gz
[ root@nginx ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-1.2.5
3.4.3 installation
[ root@nginx ~]# cd nginx-1.12.2[root@nginx nginx-1.12.2]# ./configure --add-module=/root/nginx-sticky-1.2.5
Specify user, path and module configuration (optional):
. /configure \
- - user=nginx --group=nginx \ #Installed user group
- - prefix=/usr/local/nginx \ #Specify the installation path
- - with-http_stub_status_module \ #To monitor the status of nginx, it needs to be in nginx.conf configuration
- - with-http_ssl_module \ #Support HTTPS
- - with-http_sub_module \ #Support URL redirection
- - with-http_gzip_static_module #Static compression
- - add-module=/root/nginx-sticky-1.2.5 #Install sticky module
3.5 Compile
[ root@nginx nginx-1.12.2]# make && make install
Error:
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:In function'ngx_http_sticky_misc_sha1':/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15:Error:'SHA_DIGEST_LENGTH' not declared(First use in this function)
u_char hash[SHA_DIGEST_LENGTH];^/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15:Note: Each undeclared identifier is only reported once in the function in which it appears/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:10:Error: unused variable'hash'[-Werror=unused-variable]
u_char hash[SHA_DIGEST_LENGTH];^/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:In function'ngx_http_sticky_misc_hmac_sha1':/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:242:15:Error:'SHA_DIGEST_LENGTH' not declared(First use in this function)
u_char hash[SHA_DIGEST_LENGTH];
Solution:
Modify the ngx_http_sticky_misc.c file and add #include<openssl/sha.h> And #include<openssl/md5.h> Module
[ root@nginx nginx-1.12.2]# sed -i '12a #include <openssl/sha.h>'/root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c
[ root@nginx nginx-1.12.2]# sed -i '12a #include <openssl/md5.h>'/root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c
Recompile:
[ root@nginx nginx-1.12.2]# make && make install
3.6 Nginx command global execution settings
[ root@nginx bin]# cd /usr/local/nginx/sbin/[root@nginx sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
4. Nginx related commands
4.1 Version view
[ root@nginx ~]# nginx -v
nginx version: nginx/1.12.2
4.2 View loaded modules
[ root@nginx ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.520150623(Red Hat 4.8.5-28)(GCC)
configure arguments:--add-module=/root/nginx-sticky-1.2.5/
4.3 Start and stop commands
4.3.1 start up
[ root@nginx nginx-1.12.2]# nginx
4.3.2 stop
[ root@nginx nginx-1.12.2]# nginx -s stop
[ root@nginx nginx-1.12.2]# nginx -s quit
4.3.3 Dynamic loading
[ root@nginx nginx-1.12.2]# ngins -s reload
4.3.4 Test the correctness of the configuration file nginx.conf
[ root@nginx ~]# 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
nginx -s quit: The stopping step in this way is to stop when the nginx process finishes processing tasks.
nginx -s stop: This method is equivalent to first finding out the nginx process id and then using the kill command to forcefully kill the process.
nginx -s reload: dynamic loading, when the configuration file nginx.conf changes, execute this command to dynamically load.
4.4 Auto start after boot
Edit the /etc/rc.d/rc.local file and add a new line /usr/local/nginx/sbin/nginx
[ root@nginx rc.d]# cd /etc/rc.d
[ root@nginx rc.d]# sed -i '13a /usr/local/nginx/sbin/nginx'/etc/rc.d/rc.local
[ root@nginx rc.d]# chmod u+x rc.local
5. Change the default port
Edit the configuration file /usr/local/nginx/conf/nginx.conf and modify the default port 80 to 81:
[ root@nginx ~]# view /usr/local/nginx/conf/nginx.conf
Load configuration:
[ root@nginx ~]# nginx -s reload
6. Visit Nginx
6.1 Turn off the firewall
[ root@nginx ~]# firewall-cmd --state
running
[ root@nginx ~]# systemctl stop firewalld.service
[ root@nginx ~]# firewall-cmd --state
not running
6.2 Visit Nginx
http://172.27.9.9:81
Recommended Posts