Detailed Installation and Configuration of nginx

Posted by love_php on Sat, 20 Jul 2019 17:41:33 +0200

Detailed Installation and Configuration of nginx

nginx virtual server curl-
  1. Configuration is simple, flexible and portable.
  2. High concurrency (static small file), static tens of thousands of concurrency
  3. Small resource, 2W concurrent, 10 threaded services, memory consumption is only several hundred M
  4. There are many kinds of functions (web,cache, proxy), but each function is not particularly strong.
  5. nginx can cooperate with dynamic services (FASTCGI interface)
  6. Using nginx can limit the speed of ip and the number of connections
  7. nginx has high concurrency, small files of 1-2M can support concurrency of 1-3W at the same time.

Detailed Installation and Configuration of nginx

1. Introduce the common service ports.

  1. 21 ftp; 22 ssh; 25 smtp; 3306 mysql; 873 rsync; 3389 remote desktop; 161 snmp; 111 rpcbind; 80 www http; 443 https; 110 pop3; 53 dns; 514 rsyslog
  2. The installation packages of our commonly used nslookup and dig query domain name resolution tools are bind-utils, such as Yum install bind-utils-y.
  3. We usually have nginx matched, need to test, generally through curl command, curl-I www.baidu.com You can feed back Baidu's head information and install it directly with Yum install curl-y.
  4. If the source packages are installed and some compiler tools are not installed, we can install the Development tools of the development package group, such as the yum group install "Development tools" mode.

2. Advantages of nginx

  1. Configuration is simple, flexible and portable.
  2. High concurrency (static small file), static tens of thousands of concurrency
  3. Small resource, 2W concurrent, 10 threaded services, memory consumption is only several hundred M
  4. There are many kinds of functions (web,cache, proxy), but each function is not particularly strong.
  5. nginx can cooperate with dynamic services (FASTCGI interface)
  6. Using nginx can limit the speed of ip and the number of connections
  7. The concurrency of nginx is very high. A small file of 1-2M can support concurrency of 1-3W at the same time. But nginx can only handle static resources. If it is a dynamic resource, it must use php and database, but the concurrency of the latter two is relatively low.
    There are only 300-800 concurrencies at the same time, so if nginx deals with dynamic resources, it mainly depends on the concurrency of the latter two, which depends on the barrel principle, and the extreme point depends on the short board php and DB.

3. Installation of nginx

  1. pcre must be installed first, because nginx has rewrite function, rewrite module needs pcre support, experience hints, if install what prompt lacks what library, it is generally lack of devel, as long as add-devel in front of it.
[root@maiyat conf]# rpm -qa pcre
pcre-7.8-7.el6.x86_64
[root@maiyat conf]# rpm -qa pcre-devel
pcre-devel-7.8-7.el6.x86_64
  1. Install openssl-devel, because nginx has security modules, so you need to install OpenSSL and openssl-devel
[root@maiyat conf]# rpm -qa openssl
openssl-1.0.1e-57.el6.x86_64
[root@maiyat conf]# rpm -qa openssl-devel
openssl-devel-1.0.1e-57.el6.x86_64
[root@maiyat conf]# 
  1. Install nginx, download the source package, decompress it, configure it through. / configure, and compile and install it. Where. / configure --help
    You can see some configuration parameters, such as the unnecessary parameter - - without-mail_smtp_module cancelled,. / configuration is just a configuration file, not an installation, but a makefile generated.
    There are only three components installed here, and all other necessary components will be installed by default, - user=nginx specify user, - group=nginx specify group, - with-http_ssl_module activate sl encryption module, - with-http_stub_status_module activate status information
wget http://nginx.org/download/nginx-1.6.3.tar.gz
tar xf nginx-1.6.3.tar.gz 
cd nginx-1.6.3
./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
echo $?
make && make install
useradd nginx -s /sbin/nologin -M
id nginx
grep nginx /etc/group
ln -s /application/nginx-1.6.3/ /application/nginx
  1. Note that many people have hinted that gcc is not installed, or make. It's better to install the development library tools when we install it.
    If you install PCRE with source code, installing nginx may not find pcre, and prompting you not to find pcre,
    So when compiling, we have to specify the location of PCRE with -- with=/usr/local/bin/pcre.
  2. After installation, start the service: / application/nginx/sbin/nginx. Check if port 80 is open and the process exists.
[root@maiyat ~]# /application/nginx/sbin/nginx 
[root@maiyat ~]# lsof -i :80                   
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root    6u  IPv4  13412      0t0  TCP *:http (LISTEN)
nginx   1383 nginx    6u  IPv4  13412      0t0  TCP *:http (LISTEN)
[root@maiyat ~]# ps -ef |grep nginx |grep -v grep
root       1382      1  0 23:24 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx      1383   1382  0 23:24 ?        00:00:00 nginx: worker process        
[root@maiyat ~]# 
[root@maiyat ~]# ss -lntup |grep nginx
tcp    LISTEN     0      128                    *:80                    *:*      users:(("nginx",1382,6),("nginx",1383,6))
  1. Enter 192.168.50.2:80 in the client browser to see if you can open the initial page of nginx.

4. Two related commands of nginx service

  1. After changing the nginx configuration file, we can't restart the nginx service in a hurry. We must first check the syntax of the configuration file, such as:
[root@maiyat sbin]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  1. After changing the nginx configuration file, we have to restart nginx to make the configuration effective, and restart smoothly.
[root@maiyat sbin]# /application/nginx/sbin/nginx -s reload
[root@maiyat sbin]# 
  1. View the parameters of nginx installation configuration, / application/nginx/sbin/nginx-V, such as:
[root@maiyat sbin]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
[root@maiyat sbin]# 

5. Detailed Installation Directory Files

  1. After the installation of nginx, the file details are as follows:
[root@maiyat nginx]# ls |grep -v temp |xargs tree -L 1
conf configuration file
|--nginx.conf
html Site catalogue
|-- 50x.html
`-- index.html
logs  log information
|-- access.log
|-- error.log
`-- nginx.pid
sbin  Executive Document
`-- nginx
  1. Nginx configuration file nginx.conf, we can first configure the original backup, and then borrow nginx.conf.default to directly configure, configuration files must pay attention to {}, must appear in pairs, can not be split, such as:
cat nginx.conf
#################################################
#user  nobody;
worker_processes  1;(Normally the same number of servers)
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
//These lines belong to main area, Nginx core function module
 events {
     worker_connections  1024;     One worker How many concurrent connections can be processed
 14 }
 //These lines belong to event area, Nginx core function module
http {
     include       mime.types;
     default_type  application/octet-stream;
 //This belongs to http area, the core function module of Nginx
 ##################################################
 //Instance builds two virtual hosts www.maiyat.com bbs.maiyat.com
 ##################################################
cp nginx.conf nginx.conf.bak
egrep -v "#|^$" nginx.conf.default > nginx.conf
worker_processes  1;     worker Number of processes
events {                 Beginning of Event Block
    worker_connections  1024; each woker Maximum number of connections supported by processes
}  Event Block End
http {             http Block start
    include       mime.types;  Nginx The supported media type library file contains
    default_type  application/octet-stream;  Default media type
    sendfile        on;                  Turn on efficient transmission mode
    keepalive_timeout  65;                connection timed out
    server {                 First server Block start, representing a separate virtual host site
        listen       80;        The port that provides the service, by default, is 80
        server_name  www.maiyat.com;  Domain Name Providing Service
        location / {                      First Location Beginning of Block
            root   html/www;              The site's root directory, relative path, relative to Nginx Installation path
            index  index.html index.htm;   Default Home Page Files, Separated by Spaces
        }                                    The end of the first block
}
        server {
        listen       80;
        server_name  bbs.maiyat.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
		        error_page   500 502 503 504  /50x.html;         Corresponding http Status code usage 50 x.html Respond
        location = /50x.html {                        Location Block start, access 50 x.html
            root   html;                    Specify the corresponding site directory as html
			}
    }
}                 http Block End
  1. Test www.maiyat.com Has bbs.maiyat.com been built?
    3.1 First parse in / etc/hosts, you can have an ip followed by several different domain names, such as
[root@maiyat conf]# cat /etc/hosts
#127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1       maiyat  www.maiyat.com  bbs.maiyat.com  blog.maiyat.com
192.168.50.2    maiyat  www.maiyat.com  bbs.maiyat.com  blog.maiyat.com

3.2 If it's a windows hosts file, it's in C: windows System32 drivers etc.
3.3 Use curl command under Linux to see if the message can be returned, and use browser to enter domain name under windows

[root@maiyat conf]# curl www.maiyat.com
hello world
[root@maiyat conf]# curl bbs.maiyat.com
hello oldboy
[root@maiyat conf]# curl blog.maiyat.com
happy comeon maiyat.com !

6. Virtual hosts can also be configured using ports and IP

  1. Above mentioned is the most commonly used way to configure virtual hosts through domain names, but virtual hosts can also be configured through ports and ip. For example, port-based configuration, just change the listen 80, 80 to 8001 or something in the configuration file directly. Others need not be changed. It is necessary to unify the domain names of several virtual hosts and restart the nginx service.
[root@maiyat conf]# vi nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8001;
        server_name  www.maiyat.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
}
        server {
        listen       8002;
        server_name  www.maiyat.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
server {
        listen       8003;
        server_name  www.maiyat.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}
[root@maiyat conf]# cd ../sbin/
[root@maiyat sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@maiyat sbin]# ./nginx -s reload
[root@maiyat sbin]# lsof -i :8001
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root   10u  IPv4  13964      0t0  TCP *:vcom-tunnel (LISTEN)
nginx   1476 nginx   10u  IPv4  13964      0t0  TCP *:vcom-tunnel (LISTEN)
[root@maiyat sbin]# lsof -i :8002
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root   11u  IPv4  13965      0t0  TCP *:teradataordbms (LISTEN)
nginx   1476 nginx   11u  IPv4  13965      0t0  TCP *:teradataordbms (LISTEN)
[root@maiyat sbin]# lsof -i :8003
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root   12u  IPv4  13966      0t0  TCP *:mcreport (LISTEN)
nginx   1476 nginx   12u  IPv4  13966      0t0  TCP *:mcreport (LISTEN)
[root@maiyat sbin]# curl www.maiyat.com:8001
hello world
[root@maiyat sbin]# curl www.maiyat.com:8002
hello oldboy
[root@maiyat sbin]# curl www.maiyat.com:8003
happy comeon maiyat.com !

2. Configure the virtual host based on ip, pay attention to restoring the port to 80 when modifying.

[root@maiyat sbin]# cd ../conf
[root@maiyat conf]# vi nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       192.168.50.250:80;
        server_name  www.maiyat.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
}
        server {
        listen       192.168.50.251:80;
        server_name  www.maiyat.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
server {
        listen       192.168.50.253:80;
        server_name  www.maiyat.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}
[root@maiyat conf]# 
[root@maiyat conf]# cd ../sbin/
[root@maiyat sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: [emerg] bind() to 192.168.50.250:80 failed (99: Cannot assign requested address)
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test failed
[root@maiyat sbin]# ip addr add 192.168.50.250/24 dev eth0 
[root@maiyat sbin]# ip addr add 192.168.50.251/24 dev eth0 
[root@maiyat sbin]# ip addr add 192.168.50.253/24 dev eth0 
[root@maiyat sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@maiyat sbin]# ./nginx -s reload
[root@maiyat sbin]# curl 192.168.50.250
hello world
[root@maiyat sbin]# curl 192.168.50.251
hello oldboy
[root@maiyat sbin]# curl 192.168.50.253
happy comeon maiyat.com

Topics: PHP Nginx curl OpenSSL lsof