Centos 7 deploy Nginx Web Services

I. service foundation of Nginx

Nginx is specially developed for performance optimization. Its most famous advantages are its stability, low system resource consumption and high processing capacity for HTTP concurrent connection (a single physical server can support 30000-50000 concurrent requests). Because of this, a large number of enterprises providing social networks, news information, e-commerce and virtual hosts have chosen nginx to provide Web services.

1. Advantages of Nginx service

Nginx is a very high-performance Web and reverse proxy server with many excellent features:

  • High concurrent connection: the official test can support 50000 concurrent connections, running 2 ~ 3W concurrent connections in the actual production environment;
  • Low memory consumption: under the 3W concurrent connection, only the 10 NGINX processes started consume 150M memory (15M*10=150M);
  • The configuration file is very simple: the style is as easy to understand as the program;
  • Low cost: as an open-source software, Nginx can be used free of charge, while the purchase of F5 BIG-IP, NetScaler and other hardware load balancing switches requires more than 100000 to several hundred thousand RMB;
  • Support rewrite rules: distribute HTTP requests to different back-end server groups according to different domain names and URL s;
  • Built in health check function: if the back-end web server of Nginx Proxy goes down, the front-end access will not be affected;
  • Save bandwidth: support GZIP compression, can add browser local cache Header;
  • High stability: for reverse agent, the probability of downtime is very small;

II. Installation and deployment of Nginx

1. Premise preparation

The latest stable version of Nginx is 1.12.0, and its installation files can be obtained from the official website Nginx official website / download.

1) one Centos 7 server;
2) one Windows client:
3) Centos 7 operating system image;
4) Nginx image;
All the images and software packages used to install Nginx can access the network disk extraction: https://pan.baidu.com/s/18iRCuiMEyGbEFSeBp17uVQ
Extraction code: qszt

2. Start to install and deploy Nginx server

1) mount Linux CD, copy nginx dependent program to / usr/src / directory

[root@centos02 ~]# Mount / dev / CDROM / MNT / <! -- mount CD -- >
mount: /dev/sr0 Write protected, will be mounted read-only
[root@centos02 ~]# CP / MNT / Nginx-1.6.0.tar.gz / usr/src / <! -- copy Nginx package to / usr/src / directory -- >

2) switch LAMP disc and copy all data under mnt directory to / usr/src / directory

[root @ centos02 ~] (umount / MNT / & lt;! -- uninstall disc -- & gt;

[root@centos02 ~]# Mount / dev / CDROM / MNT / <! -- mount CD -- >
mount: /dev/sr0 Write protected, will be mounted read-only
[root@centos02 ~]# CP / MNT / * / usr/src / <! -- copy all data in the CD directory to / usr/src / directory -- >

3) switch to the operating system CD and install nginx dependent programs

[root @ centos02 ~] (umount / MNT / & lt;! -- uninstall disc -- & gt;

[root@centos02 ~]# Mount / dev / CDROM / MNT / <! -- mount CD -- >
mount: /dev/sr0 Write protected, will be mounted read-only
[root@centos02 ~]# RM - RF / etc / yum. Repos. D / CentOS - * <! -- clear the system's own Yum source -- >
[root@centos02 ~]# Yum - y install PCRE devel zlib devel <! -- install the dependency program of Nginx -- >
[root@centos02 ~]# Useradd - M - S / SBIN / nologin Nginx <! -- create user to manage Nginx -- >
[root@centos02 ~]# tar zxvf /usr/src/nginx-1.6.0.tar.gz -C /usr/src/ 
<!--decompression Nginx software package-->
[root@centos02 ~]# cd /usr/src/nginx-1.6.0/ 
[root@centos02 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx
--user=nginx --group=nginx --with-http_stub_status_module
<!--To configure Nginx-->
[root@centos02 nginx-1.6.0]# Make & & make install <! -- compile and install nginx -- >
[root@centos02 ~]# Ln - S / usr / local / Nginx / SBIN / * / usr / local / SBIN / <! -- optimize Nginx execution commands -- >

3. Basic management of Nginx service

[root@centos02 ~]# Nginx <! -- start nginx Service -- >
[root@centos02 ~]# netstat -anptu | grep nginx 
                     <!--Monitor Nginx Whether the service started successfully-->
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4663/nginx: master  
[root@centos02 ~]# Kill - s quit Nginx <! -- close Nginx Service -- >
[root@centos02 ~]# Kill - 3 Nginx <! -- close Nginx Service -- >
[root@centos02 ~]# Kill - 1 nginx <! -- restart nginx -- >
[root@centos02 ~]# Kill - s HUP nginx <! -- restart nginx -- >
[root@centos02 ~]# VIM / etc / init.d/Nginx <! -- write Nginx service management script -- >
#!/bin/bash
#chkconfig: 35 90 30
#description:nginx server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill 0s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0 (start|stop|restart|reload)"
exit 1
esac
exit 0
[root@centos02 ~]# Chmod + X / etc / init.d/nginx <! -- add script execution permission -- >
[root@centos02 ~]# Chkconfig -- add nginx <! -- add as system service -- >
[root@centos02 ~]# Chkconfig -- level 35 nginx on <! -- set power on auto start -- >
[root@centos02 ~]# /Etc / init.d/Nginx stop <! -- script stops Nginx Service -- >
[root@centos02 ~]# /Etc / init.d/Nginx start <! -- script start Nginx Service -- >
[root@centos02 ~]# /Etc / init.d/Nginx restart <! -- script restart Nginx Service -- >

The client configuration is the same network card and network segment as the Nginx server. Set the gateway to access the Nginx website server

4. Common configuration files of Nginx

[root@centos02 ~]# ls -ld /usr/local/nginx/conf/nginx.conf
-rw-r--r-- 1 root root 2656 11 Month 2817:22
/usr/local/nginx/conf/nginx.conf       <!--Nginx Primary profile for-->
[root@centos02 ~]# ls -ld /usr/local/nginx/sbin/
drwxr-xr-x 2 root root 19 11 Month 2817:22
/usr/local/nginx/sbin/      <!--Administration Nginx Service procedure document-->
[root@centos02 ~]# ls -ld /usr/local/nginx/html/
drwxr-xr-x 2 root root 40 11 Month 2817:22
/usr/local/nginx/html/      <!--Nginx Site root for-->
[root@centos02 ~]# ls -ld /usr/local/nginx/logs/
drwxr-xr-x 2 root root 58 11 Month 2817:47 
/usr/local/nginx/logs/   <!--Nginx Web logging for-->

5. Modify the Nginx main configuration file

[root@centos02 ~]# cp /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf.bak   <!--Backup master profile-->
[root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf 
                            <!--Edit Master profile-->
  3 user  nginx;       <!--Running user-->
  4 worker_processes  1;     <!--Number of work processes-->
  6 error_log  logs/error.log;   <!--Location of the error log file-->
12 pid        logs/nginx.pid;     <!--PID Location of files-->
16     use epoll;        <!--Use epoll Model-->
17     worker_connections  1024; <!--1024 connections per process-->
29     #Access < log logs / access.log main; <! -- access log location -- >
31     sendfile        on;           <!--Enable efficient file transfer mode-->
35     keepalive_timeout  65;       <!--Connection hold timeout-->
39     server { <!--server The beginning of, a server Represents a virtual host-->
40         listen       80;     <!--Web Monitoring configuration of service-->
41         server_name  localhost; <!--Site name( FQDN)-->
44         charset utf-8;    <!--Default character set for web pages-->
48         location / {     <!--Root configuration-->
49             root   html; <!--Location of the site root, relative to the installation directory-->
50             index  index.html index.html;<!--Default home page (index page)-->
51         }
84     }            <!--server End-->
  • listen: the IP address is allowed to be limited while the port is limited. The form of "IP address: port number" is adopted. The root statement is used to set the path of the web page document in a specific access location. It defaults to the html / directory under the nginx installation directory. It can be changed to / var/www/html and other paths as required. However, after the change, the nginx user should have read permission.

  • Worker? Processes: indicates the number of work processes. If the server is composed of multiple CPUs or uses multi-core processors, you can refer to the total number of CPU cores to specify the number of work processes. The specific meaning is reflected in the worker connections configuration item.

  • worker_connections: this configuration item specifies the connections processed by each process, generally below 10000 (default is 1024), which is associated with the above configuration item of the number of working processes. For example, if the number of working processes is 8 and each process processes 4096 connections, the number of connections allowed for Nginx to provide services has exceeded 30000 (4096 * 8 = 32768). Of course, it depends on the performance of physical conditions such as server hardware and network bandwidth.

6. Configure the access status statistics of Nginx

[root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf 
 <!--modify nginx Profile, specify access location and open stub_status To configure-->
 52         location /status {
 53                 stub_status     on;
 54                 access_log off;
 55         }
[root@centos02 ~]# /Etc / init.d/Nginx restart <! -- restart Nginx Service -- > 

Client access status statistics page:

  • Active connections: indicates the current number of active connections;

  • server accepts handled requests: indicates the connection information that has been processed. Three numbers in turn indicate the number of connections that have been processed, the number of successful TCP handshakes, and the number of requests that have been processed.

3. Configure virtual host

1. Install DNS server

[root@centos02 ~]# yum -y install bind bind-chroot bind-utils 
                              <!--install DNS-->
[root@centos02 ~]# echo "" > /etc/named.conf 
           <!--Empty main profile-->
[root@centos02 ~]# VIM / etc / named.conf <! -- modify the main configuration file -- >
options {
        listen-on port 53 { 192.168.100.20; };
        directory "/var/named";
}
zone    "benet.com"     IN      {
        type    master;
        file    "benet.com.zone";
}
zone    "accp.com"      IN      {
        type    master;
        file    "accp.com.zone";
}
[root@centos02 ~]# named-checkconf -z /etc/named.conf
              <!--Check the master profile for configuration errors-->
[root@centos02 ~]# vim /var/named/benet.com.zone  
               <!--To configure benet.com Forward resolution region of-->
$TTL    86400
@       SOA     benet.com.      root.benet.com(
        2019112801
        1H
        15M
        1W
        1D
)
@       NS      centos02.benet.com.
centos02 A      192.168.100.20
www      A      192.168.100.20
[root@centos02 ~]# chmod +x /var/named/benet.com.zone  
                    <!--Adding execution permissions to the resolution zone profile-->
[root@centos02 ~]# chown named:named
/var/named/benet.com.zone <!--Modify the main genus group-->
[root@centos02 ~]# named-checkzone benet.com
/var/named/benet.com.zone   
         <!--inspect benet.com Whether the forward resolution zone profile is wrong-->
zone benet.com/IN: loaded serial 2019112801
OK
[root@centos02 ~]# cp /var/named/benet.com.zone
/var/named/accp.com.zone  
   <!--copy benet.com Forward parsing area to accp.com Forward resolution region-->
[root@centos02 ~]# vim /var/named/accp.com.zone  
                    <!--modify accp.com Forward parsing zone profile-->
$TTL    86400
@       SOA     accp.com.       root.accp.com(
        2019112801
        1H
        15M
        1W
        1D
)
@       NS      centos02.accp.com.
centos02 A      192.168.100.20
www      A      192.168.100.20
[root@centos02 ~]# named-checkzone accp.com
/var/named/accp.com.zone  
           <!--inspect accp.com Whether the forward resolution zone profile is wrong-->
[root@centos02 ~]# vim /etc/sysconfig/network-scripts/
ifcfg-ens32  <!--Edit network card add master DNS-->
DNS1=192.168.100.20   <!--Add master DNS-->
[root@centos02 ~]# Systemctl restart network <! -- restart network card service -- >
[root@centos02 ~]# Systemctl start named <! -- start DNS server -- >
[root@centos02 ~]# Systemctl enable named <! -- set power on auto start -- >
[root@centos02 ~]# nslookup www.benet.com  
                 <!--Whether the domain name resolution test is normal-->
Server:     192.168.100.20
Address:    192.168.100.20#53

Name:   www.benet.com
Address: 192.168.100.20

[root@centos02 ~]# nslookup www.accp.com
                  <!--Whether the domain name resolution test is normal-->
Server:     192.168.100.20
Address:    192.168.100.20#53

Name:   www.accp.com
Address: 192.168.100.20

2. Configure domain name based virtual host

The configuration file of Nginx uses the "http {}" demarcation mark to set the HTTP server, including the access log, HTTP port, web page directory, default character set, connection retention, as well as the web site global settings such as virtual web host and php parsing, most of which are contained in the sub demarcation mark "server {}". "Server {}" represents a specific site setting.

            <!--Create virtual host site root--> 
[root@centos02 ~]# mkdir -p /var/www/benetcom  
[root@centos02 ~]# mkdir -p /var/www/accpcom  
              <!--Create web home page for virtual host--> 
[root@centos02 ~]# echo "www.benet.com" > 
/var/www/benetcom/index.html
[root@centos02 ~]# echo "www.accp.com" > 
/var/www/accpcom/index.html
[root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf  
                     <!--modify Nginx Primary profile supports virtual hosts-->
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
        server  {           <!--server start-->
                listen www.benet.com:80;  <!--Monitored domain name and port-->
                server_name www.benet.com;     <!--Website name-->
                charset utf-8;          <!--Default character set-->
                access_log      logs/www.benet.com.access.log; 
                                                    <!--Access log location-->
                error_log       logs/www.benet.com.error.log;
                                                       <!--Error log location-->
                location / {       <!--Root configuration-->
                                root /var/www/benetcom/; <!--Site root-->
                                index index.html;       <!--default page-->
                        }
                }       <!--server Ending-->

<!--Please refer to the above notes for the following configuration-->
        server  {
                listen www.accp.com:80;
                server_name www.accp.com;
                charset utf-8;
                access_log      logs/www.accp.com.access.log;
                error_log       logs/www.accp.com.error.log;
                location / {
                                root /var/www/accpcom/;
                                index index.html;
                        }
                }
[root@centos02 ~]# Nginx - T <! -- check whether nginx is configured incorrectly -- >
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
[root@centos02 ~]# Systemctl restart named <! -- restart DNS Service -- >
[root@centos02 ~]# /Etc / init.d/Nginx restart <! -- restart Nginx Service -- >

Client adds DNS address, access domain name test success

——————Thank you for reading——————

Tags: Linux Nginx network vim DNS

Posted on Tue, 03 Dec 2019 16:26:11 -0500 by bhavesh