Nginx deployment!

One: When you use to build Nginx virtual host server, each virtual WEB site has a separate "server {}" configuration section, each listening IP address and port number can be specified individually, of course, the site name is different.

Two: the virtual machine category:

  1: name-based virtual hosts (ip address of a domain name corresponding to a plurality of different domain is a different site, its content is not the same)

  2: Port-based virtual host (server has only one ip address, different port is a different site, its content is not the same)

  3: ip-based virtual host (server has multiple ip addresses, different ip is a different site, its content is not the same)

Two: nginx -t used to detect configuration file syntax

1 following error: error profile line 43

[root@www ~]# nginx -t
nginx: [emerg] "location" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:43
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

The following error 2: worker inside the work area problems

[root@www ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Solution:

[root@www ~]# ulimit -n 10240

=============================================================

Create a single domain name and host-based virtual test

[Root @ localhost ~] # rpm -q httpd // There httpd software must be deleted

Httpd package is not installed

安装支持软件
[root@localhost ~]# rpm -q gcc gcc-c++ zlib-devel pcre-devel make
[root@localhost ~]# yum -y install gcc gcc-c++ zlib-devel pcre-devel make

Finished!

Create a running user, group
[the root @ localhost ~] # the useradd -M -s / sbin / nologin Nginx
[the root @ localhost ~] -l tail # / etc / the passwd; -l tail / etc / Group

导入nginx软件包
[root@localhost ~]# rz -E                                       
rz waiting to receive.
[root@localhost ~]# ls
anaconda-ks.cfg   initial-setup-ks.cfg  nginx-1.16.0.tar.gz   original-ks.cfg
[root@localhost ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.16.0/

编译安装 Nginx
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

 --prefix set the installation directory of Nginx

--user and --group specified users and groups running Nginx

--with-http_stub_status_module enable http_stub_status_module modules to support state statistics

--with-http_ssl_module enable SSL module

--with-http_flv_module FLV enabled module that provides memory-based search for files offset time

[root@localhost nginx-1.16.0]#make

[root@localhost nginx-1.16.0]#make install

[root@localhost nginx-1.16.0]# cd

Nginx-based program to create a link file
[root @ localhost ~] # LS / usr / local / nginx /
conf HTML logs sbin

[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# ll /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 9月 10 17:12 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx

Nginx operation control method
manual approach control Nginx:
nginx -t detection configuration file syntax
execute the main program starts Nginx nginx

[root@localhost ~]# 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
[root@localhost ~]# nginx               //启动nginx服务
[root@localhost ~]# netstat -anpt | grep nginx
tcp     0           0 0.0.0.0:80              0.0.0.0:*               LISTEN 64726/nginx: master

[root@localhost ~]# killall -l nginx
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED
[root@localhost ~]# killall -s HUP nginx               //平滑重启
[root@localhost ~]# killall -s QUIT nginx              //正常停止
[root@localhost ~]# nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
[root@localhost ~]# nginx -s stop

Nginx script writing service

[root@localhost ~]# vim /etc/init.d/nginx

Copy the code

#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Scripts shell
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 -s HUP $(cat $PIDF)
;;
*)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
esac
exit 0

Copy the code

 

[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig --list nginx

nginx 0: Off 1: Off 2: opening 3: On 4: opening 5: opening 6: Off

[root@localhost ~]# /etc/init.d/nginx status
Nginx is stopped
[root@localhost ~]# /etc/init.d/nginx stop
[root@localhost ~]# /etc/init.d/nginx start
[root@localhost ~]# /etc/init.d/nginx status
Nginx is running

[root@localhost ~]# /etc/init.d/nginx reload

Nginx configuration file parsing

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp nginx.conf nginx.conf.origin
[root@localhost conf]# vim nginx.conf

Copy the code
user nginx nginx; // nginx program and program group account
worker_processes 2; // specify the number of processes that are generally consistent with the number of cpu
worker_cpu_affinity 00000001 00000010; // allocate the number of cores for each process
error_log logs / error.log info; // global error log file location
pid logs / nginx.pid; // PID file location
events {
   use epoll; // use epoll model
    worker_connections 10240; // allowed per process maximum number of connections defaults to 1024 generally 10,000 or less
}
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; // bit access log
    sendfile on; // support files Send Timeout
    keepalive_timeout  65;
server {// web service listener configuration
        listen 80; // listen address and port (IP: PORT)
        server_name  www.crushlinux.com;     // site name (the FQDN of the)        charset UTF-8;                       // page the default character set        access_log logs /www.crushlinux.com.access.log main;        

LOCATION / { // root configuration, the root HTML; // HTML site location of the installation location of the root directory index index.html index.htm; // default home } L ocation / Status { stub_status ON; // open state Statistics access_log off; // this position off logging }






     error_page 500 502 503 504 /50x.html; // internal error feedback page
       location = / 50x.html {// an error page configuration
           the root HTML;
        }
    }
}

 
Copy the code

[root@localhost conf]# pwd
/usr/local/nginx/conf

[root@localhost conf]# mkdir ../html/mailcom
[root@localhost conf]# echo "<h1>www.crushlinux.com</h1>" > ../html/index.html

[root@localhost conf]# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost conf]# ulimit -n 65536
[root@localhost conf]# 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

[root@localhost conf]# systemctl stop firewalld
[root@localhost conf]# iptables -F
[root@localhost conf]# setenforce 0

[root@localhost conf]# systemctl restart nginx
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.

[root@localhost conf]# /etc/init.d/nginx reload

 

Active connections represents the number of connections currently active,

The third row of three numbers represent Nginx is currently handled a total 2 connection, 3-way handshake is successfully created, it handled a total two requests.

=======================================================================

:( virtual mainframe applications to create multiple name-based virtual hosting and test)

To create two sites www.crushlinux.com and www.cloud.com

WEB host two virtual root directory are established, tested and ready to Home

 

[root@localhost~]#mkdir /usr/local/nginx/html/crushlinux

 

[root@localhost~]#mkdir /usr/local/nginx/html/cloud

 

[root@localhost~]# echo "<h1>www.crushlinux.com</h1>" >/usr/local/nginx/html/crushlinux/index.html

[root@localhost~]# echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html

[root@localhost~]# vim /usr/local/nginx/conf/nginx.conf

Copy the code
user  nginx nginx;
worker_processes  2;
worker_cpu_affinity 00000001 00000010;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
   use epoll;
    worker_connections  10240;
}
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;
    sendfile        on;
    keepalive_timeout  65;
server {
        listen       80;
        server_name  www.crushlinux.com;
        charset utf-8;
        access_log  logs/www.crushlinux.com.access.log  main;
location / {
            root   html/crushlinux;
            index  index.html index.htm;
        }

location ~ /status {
              stub_status on;
             access_log off;
}

 error_page  500 502 503 504   /50x.html;

          location = /50x.html {
             root html;
   }
}

  server {
         listen 80;
         server_name www.cloud.com;
         charset utf-8;
         access_log logs/cloud.access.log main;
 location / {
         root html/cloud;
         index index.html index.html;
}
         error_page 500 502 503 504 /50x.html;
  location = /50x.html {
         root html;
    }
  }
}

Copy the code

[root @ www ~] # killall -3 nginx // normal stop
[root @ www ~] # nginx // normal start

[root@www ~]# yum -y install elinks
[root@www ~]# elinks --dump http://www.crushlinux.com
www.crushlinux.com
[root@www ~]# elinks --dump http://www.cloud.com
www.cloud.com

 

Guess you like

Origin www.cnblogs.com/L1-5551/p/11518444.html
Recommended