Introduction, installation and deployment of Nginx based on Linux Environment

Posted by pugg09 on Tue, 11 Jan 2022 12:03:12 +0100

catalogue

1, Introduction to Nginx

2, Manual installation and deployment

1, Introduction to Nginx

The even version of Nginx is a stable version; Odd is the development test version

Nginx high-performance, safe and stable WEB server software is installed by default and only one set of websites are published. In the enterprise production environment, multiple websites are usually published based on Nginx WEB, and the method of publishing multiple websites is called virtual host. If you configure multiple websites manually, it is difficult and cumbersome. SHELL programming automation can be introduced.

Build Nginx WEB platform based on SHELL programming, and realize automatic management and configuration of virtual host (multiple websites). The key points of programming are as follows:

1. Deploy Nginx WEB platform in advance to check whether the service is deployed;

2. If the Nginx WEB service has been installed and installed wirelessly, you can directly configure the virtual host;

3. Modify the main configuration file of Nginx WEB and add virtual host configuration information;

4. The virtual host mainly adopts multi domain mode, with the same 80 port, IP address and different access domain name;

5. Support the addition of a single virtual host or multiple virtual hosts at the same time;

echo $? If equal to 0, precompiling is OK

make

make install

cd

ll /usr/local/nginx/

/usr/sbin/groupadd -f www Create group /usr/sbin/useradd -g www www Create user

netstat -tnlp |grep 80 View listening port

firewall-cmd --add-port=80/tcp --permanent Open firewall port 80

Main configuration file: cd /usr/local/nginx/conf

Get into the habit of backing up configuration files CP nginx conf nginx. conf.bak

Many # comments or spaces can be deleted

Delete method 1:

awk '/#/' nginx.conf / the first slash indicates matching, the middle is matching content / the last indicates matching is completed

awk '!/#/' nginx.conf ! Plus exclamation mark, which refers to mismatch

awk '!/#/' nginx.conf | awk '!/^$/' ^ Indicates what starts with ^ abc starts with abc ^ $blank line

Delete method 2:

sed '/#/d' nginx.conf |sed '/^$/d'

Delete method 3:

grep -aiv "#" nginx.conf| grep -v "^$"

Copy the displayed, and then vim opens nginx The conf configuration file ggdG can be deleted and replaced

A server is a virtual host

server_name access domain name

root html/v2.lxh.net publishing directory screenshot shows the relative path HTML/

The original web page file directory is stored on the screenshot of / usr/local/nginx/html as the relative path html/

/usr/local/nginx/sbin/nginx -s reload restart

Now there will be no nginx at work Conf is configured with server, but the following method is adopted

Directory to publish: domains directory

cd /usr/local/nginx/conf

mkdir domains establish domains folder

touch v1.lxh.net

vim v1.lxh.net

If you want to configure a virtual host, copy v1.0 in the domains folder lxh. Net to modify the configuration

/usr/local/nginx/sbin/nginx -s reload
events { 
worker_connections 1024; 
} 
http { 
include mime.types; 
default_type application/octet-stream; 
sendfile on; 
keepalive_timeout 65; 
server { 
listen 80; 
server_name localhost; 
location / { 
root html; 
index index.html index.htm; 
} 
error_page 500 502 503 504 /50x.html; 
location = /50x.html 
{ 
root html; 
} 
} 
}

What niginx needs to do is specified in the configuration file

events { } #Must write 
http{ #Tomcat server cluster 
upatrem tomcat_server { 
Two tomcat 
server 192.168.1.100:8081 weight=3 maxfails=3 fail_time=10s; 
server 192.168.1.101:8081 weight=2 maxfails=3 fail_time=10s; 
} 
server { #Virtual server, port 80 by default 
proxy_read_timeout proxy_next_upstream_error_timout;#Conditions for failover 
proxy_next_upstream_tries 10; #Limit the number of retries 
proxy_next_upstream_timeout 60s; #Limit retry timeout 
location / { # According to the user's url Go through different processes, general distribution #Respond to the content returned by the proxy request 
proxy_set_header Host $http_host; 
proxy_pass http://tomcat _server; 
} #Static content is returned directly by nginx, and static content is placed on the same server as nginx 
#The following address where the root resource is located. This is the relative path in the nginx installation directory 
root html; 
} 
} 
}

Nginx configures cluster load balancing, and the default mode is polling; You can also specify a load balancing algorithm (default weighted polling)

Nginx failover

Failover: after the failure to allocate the request to server A, continue to allocate the request to server B for processing

avalanche phenomena

In the case of failover, Nginx will allocate more requests to other servers. In the case of high concurrency, the pressure on each server is very high. Adding the pressure of allocation will lead to the collapse of normal servers. Such repetition may cause the avalanche effect of the whole cluster.

Solutions to avalanche

Limit retries proxy_next_upstream_tries 10 ; # The default is 0, which limits the number of retries

Show retry timeout proxy_next_upstream_timeout 60s; # Limit retry timeout

Nginx fuse mechanism

When a proxy server processes a request and a certain number of errors occur (the number of times can be configured), nginx will no longer assign the request to the server for processing at a certain time (the time can be configured). After the fuse time, nginx will try to assign a request to the server for processing again. If it still fails, it will continue to fuse.

maxfails=3 fail_time=10s. The maximum number of errors is 3. After failure, stop allocating services for 10s

Nginx high availability dual machine hot standby

Nginx is an open source and free WEB server software. It is a high-performance HTTP and Reverse proxy The web server also provides IMAP/POP3/SMTP services. On March 12, 2019, it was purchased by F5 hardware manufacturer for us $670 million. It is mainly used to publish website code and provide web information services. Users can access pages through the browser.

By default, Nginx WEB software can only process static web pages, not dynamic web pages directly. Dynamic web pages are handed over to a third-party program for analysis. Nginx officially claims that its concurrency ability to process static web pages can reach 5w/s, which is equivalent to 5-10 times the overall performance of Apache WEB. Because of its stability, rich feature set, sample configuration file and low consumption of system resources famous.

Nginx as a load balancing service: nginx can directly support Rails and PHP programs internally for external services, or as HTTP Agency services External service. Nginx is written in C, which is much better than Perlbal in terms of system resource overhead and CPU efficiency.

Static web page: static web page is generally a web page that does not interact with the background database. The content is rarely updated or hardly updated. The suffix of web page file is usually html,. htm,. End of xml.

Dynamic web page: a dynamic web page is generally a web page that interacts with the back-end database. Its web page content is often updated or updated with the change of the back-end database content. The suffix of the web page file is usually named with asp,. php,. jsp, etc.

2, Manual installation and deployment

To build the Nginx WEB platform manually, there are two main deployment methods:

YUM binary mode;

The deployment method is relatively simple, fast and efficient. It can automatically verify the correctness of software packages, automatically solve the dependencies between software packages, automatically install software services and automatically set them as system services. It can not customize specific modules, functions and parameters of software services. The files and directories after installation are relatively scattered, It is not convenient for later maintenance and management.

MAKE source code compilation mode;

The deployment method is relatively complex, cumbersome and inefficient. It can not automatically verify the correctness of software packages, automatically solve the dependencies between software packages, automatically install software services and automatically set them as system services. It can customize specific modules, functions and parameters of software services, and the files and directories after installation are relatively unified, It is convenient for later maintenance and management.

  1. Download nginx web software from the official website: wget http://nginx.org/download/nginx-1.12.2.tar.gz

Breakpoint continuation stable version: wget -c http://nginx.org/download/nginx-1.16.0.tar.gz

  1. Decompression: tar -xzvf nginx-1.12.2 tar. GZ (- x extract decompression, - z gzip format, - v verbose details, - f file file)
  2. Enter the decompression Directory: cd nginx-1.12.2/
  3. Modify nginx version and hide the effect of version on nginx web startup security. The instructions are as follows:

sed -i -e 's/1.12.2//g' -e 's/nginx\//JWS/g' -e 's/"NGINX"/"JWS"/g' src/core/nginx.h

Because Nginx is a source code program developed based on C language, it cannot be directly used by linux operating system by default. Therefore, it is necessary to compile the source code file with the help of c editor to generate binary files. Therefore, it is necessary to perform three steps of source code package deployment.

  1. Precompiled/ configure --prefix=/usr/local/nginx/ --user=www --group=www --with-http_stub_status_module --with-http_ssl_module (status monitoring module)

Yellow is to select the installation path

Precompiling: it mainly detects the dependent environment and library files required by the Linux system to install the software, detects whether the GCC compilation environment (C compiler) exists in the Linux operating system, specifies the directory of software service deployment, customizes the specific modules, functions and parameters of software service, and finally generates Makefile files.

  1. Compile make -j4 or make four thread quick editing

It mainly reads Makefile files through make compiler, calls GCC compiler environment (C compiler) under Linux operating system, and compiles the source code files in the software package to generate binary files.

The purpose of the Makefile file is to tell the make compiler which source code file to compile from and which source code file to compile from when compiling the source code file & record the dependency information during compilation.

  1. Install make -j4 install

It mainly copies or installs the binary files generated by the make compilation in the second step to the installation directory of the Linux operating system: - prefix=/usr/local/nginx /.

  1. Start nginx: /usr/local/nginx/sbin/nginx
  2. Filter nginx process ps -ef | grep nginx

  1. Close firewall command - iptables -F
  2. Kill process pkill nginx
  3. Configure virtual ip for high availability handoff
  4. cd /etc/sysconfig/network-scripts/
  5. cp ifcfg-enss33 ifconfig-ens33:1 copy the network card as a sub interface and name it ens33:1
  6. vim ifcofig-ens33:1 edit the network card and configure the cat to confirm
TYPE=Ethernet 
BOOTPROTO=static 
DEVICE=ens33:1 
IPADDR=192.168.0.10 Two subnet card addresses niginx identical 
NETMASK=255.255.255.0
  1. ifup ens33:1 wake up network card
  2. ifconfig check whether the addition is successful
  3. ifdown ens33 turn off the network card

1. During compilation,

The first compilation needs to deal with nginx software package dependencies

Install nginx related dependency packages: Yum - y install GCC OpenSSL OpenSSL devel PCRE devel zlib zlib devel

gcc functions: preprocessing, compilation, connection and assembly

openssl function: used for website encrypted communication

pcre function: used to support parsing regular expressions.

zlib function: used to decompress data. When communicating between websites, data is compressed and then transmitted to save network bandwidth by consuming CPU.

Post installation verification:/ configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module ...

2. Error in compiling make: Make: * * * there are no rules to create the target "build" required by "default"

Install related dependent packages required by Nginx

During the initial installation of Nginx, such errors often occur: make: * * * there are no rules to create the target required by "default"

yum install pcre-devel -y

yum install zlib zlib-devel

yum install openssl openssl-devel

//You can also use a command instead

yum install pcre-devel zlib zlib-devel openssl openssl-devel

3. After the nginx compilation and installation, the startup appears: nginx: [emerg] getpwnam("nginx") failed

useradd -s /sbin/nologin -M nginx

4. Startup failed with nginx: [emerg] getpwnam("www") failed

The reason is that the user www was not created

Solution, create composite users

/usr/sbin/groupadd -f www /usr/sbin/useradd -g www www

5. After deployment, there is no content in the web page

Close firewall command - iptables -F

3, Automatic deployment of Nginx WEB based on SHELL programming

SHELL script files are named after sh end;

SHELL script file names usually keep internal relevance. You can use -;

SHELL programming code writing tools: vi, vim, notepad, sublime, gedit, etc;

The first line of SHELL programming code starts with #! It starts with emphasis and identification, followed by the SHELL interpreter type, such as #/ bin/bash;

SHELL programming try to use #, except the first line #! Except the beginning, the rest # indicate notes and descriptions;

SHELL programming variables shall be named with uppercase letters as far as possible, and shall not start with numbers. Variable names cannot be used - you can use;

#!/bin/bash 
#auto install nginx web 
yum install -y wget tar make gzip gcc 
yum install -y pcre pcre-devel zlib-devel 
wget -c http://nginx.org/download/nginx-1.16.0.tar.gz 
ls -l nginx-1.16.0.tar.gz 
tar -xzvf nginx-1.16.0.tar.gz 
cd nginx-1.16.0/ 
useradd -s /sbin/nologin www -M 
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module make 
make install 
/usr/local/nginx/sbin/nginx 
ps -ef|grep nginx 
netstat -tnlp|grep -aiw 80 
setenforce 0 
firewall-cmd --add-port=80/tcp --permanent 
systemctl reload firewalld.service 
iptables -t filter -A INPUT -m tcp -p tcp --dport 80 -j ACCEPT service iptables save


chmod +x auto_install_nginxv1.sh             Add execution permission

=Variable definitions do not support starting with a number. Variable names cannot be -- (horizontal line), but they can be used_

[root@localhost ~]# WEB=www.jd.com

[root@localhost ~]# echo $WEB

www.jd.com

[ root@localhost ~]#MKDIR $web will create a file named www.jd Com file

Clear variable unset WEB

[root@localhost ~]# unset WEB

[ root@localhost ~]#Echo $web, the command is not found

[root@localhost ~]# abc=123

[root@localhost ~]# echo $abc

123

[root@localhost ~]# echo ${abc}d

123d

[root@localhost ~]# NGX_SO=nginx.1.1.tar.gz

[root@localhost ~]# echo $NGX_SO

nginx.1.1.tar.gz

[root@localhost ~]# echo $NGX_SO|sed 's/.tar.gz//g'

nginx.1.1

[root@localhost ~]# echo $NGX_SO|sed 's/.tar.gz/--/g'

nginx.1.1--

v2 [replace variable]

#!/bin/bash 
#auto install nginx web 
NGX_VER="1.16.0" 
NGX_YUM="yum install -y" 
NGX_DIR="/usr/local/nginx" 
NGX_SOFT="nginx-${NGX_VER}.tar.gz" 
NGX_URL="http://nginx.org/download" 
NGX_SRC=$(echo $NGX_SOFT|sed 's/\.tar.*//g') 
NGX_ARGS="--user=www --group=www --with-http_stub_status_module" 
$NGX_YUM wget tar make gzip gcc 
$NGX_YUM pcre pcre-devel zlib-devel 
wget -c $NGX_URL/$NGX_SOFT 
ls -l $NGX_SOFT 
tar -xzvf $NGX_SOFT 
cd $NGX_SRC/ 
useradd -s /sbin/nologin www -M 
./configure --prefix=$NGX_DIR $NGX_ARGS 
make 
make install 
$NGX_DIR/sbin/nginx 
ps -ef|grep nginx 
netstat -tnlp|grep -aiw 80 
setenforce 0 
firewall-cmd --add-port=80/tcp --permanent 
systemctl reload firewalld.service 
iptables -t filter -A INPUT -m tcp -p tcp --dport 80 -j ACCEPT 
service iptables save

auto_install_nginx_web_v3.sh

env can see all the variables of the system

if single expression

if (expression)

Statement 1

fi

if double expression

if (expression)

Statement 1

else

Statement 2

fi #fi end

#!/bin/bash 
#auto install nginx web 
NGX_VER="$1" 
NGX_YUM="yum install -y" 
NGX_DIR="/usr/local/nginx" 
NGX_SOFT="nginx-${NGX_VER}.tar.gz" 
NGX_URL="http://nginx.org/download" 
NGX_SRC=$(echo $NGX_SOFT|sed 's/\.tar.*//g') 
NGX_ARGS="--user=www --group=www --with-http_stub_status_module" 
if [ $# -eq 0 ];then 
echo -e "\033[32m-----------------\033[0m" 
echo -e "\033[32mUsage:{/bin/bash $0 1.12.0|1.16.0|help}\033[0m" 
exit 
fi 
CHECK_NUM=$(rpm -qa|grep -aicwE "gcc|pcre-devel") 
if [ $CHECK_NUM -lt 2 ];then 
$NGX_YUM wget tar make gzip gcc 
$NGX_YUM pcre pcre-devel zlib-devel 
fi 
wget -c $NGX_URL/$NGX_SOFT 
ls -l $NGX_SOFT 
tar -xzvf $NGX_SOFT 
cd $NGX_SRC/ 
useradd -s /sbin/nologin www -M 
./configure --prefix=$NGX_DIR $NGX_ARGS 
make 
make install 
$NGX_DIR/sbin/nginx 
ps -ef|grep nginx 
netstat -tnlp|grep -aiw 80 
setenforce 0 
firewall-cmd --add-port=80/tcp --permanent 
systemctl reload firewalld.service 
iptables -t filter -A INPUT -m tcp -p tcp --dport 80 -j ACCEPT 
service iptables save

Topics: Linux Operation & Maintenance Nginx