第3章 Nginx - resource.boxuegu.comresource.boxuegu.com/nginx/file/jc.pdf · 第3章 Nginx...

download 第3章 Nginx - resource.boxuegu.comresource.boxuegu.com/nginx/file/jc.pdf · 第3章 Nginx 的安装 学习 ... 而言,服务器的采购和维护 ... 关于其他内容读者可参考Linux

If you can't read please download the document

Transcript of 第3章 Nginx - resource.boxuegu.comresource.boxuegu.com/nginx/file/jc.pdf · 第3章 Nginx...

  • Java.Net Php

    400-618-4000

    3 Nginx

    Linux

    Nginx

    Nginx

    Nginx Windows

    VMware Linux Linux

    CentOS Nginx

    3.1 Linux

    Linux

    Linux

    3.1.1 CentOS

    VMware Linux

    Linux

    CentOS

    1

    VMware

    CentOS-6.8-x86_64-bin-DVD1.iso

    CentOS-6.8-x86_64-minimal.iso

    VMware 3-1

    3-1

    512MB 1024MB

    NAT

  • Java.Net Php

    400-618-4000

    10 GB

    USB

    3-1 512MB CentOS

    1 VMware-

    2()

    3 3-1

    3-1

    (iso)CentOSVMware

    4 Linux CentOS

    3-2

  • Java.Net Php

    400-618-4000

    3-2

    VMware

    VMware

    5

    3-3

    3-3

    6 10G 4G

    7 3-4

  • Java.Net Php

    400-618-4000

    3-4

    8 3-5

    3-5

    3-5 USB

    CD/DVD(IDE)

    ISO CentOS CentOS

    9

    2

    CentOS VMware

    3-2

    3-2 CentOS

  • Java.Net Php

    400-618-4000

    Install or upgrade on existing system

    Disc Found Skip

    English

    / UTC

    root 123456

    CentOS Minimal

    3-2

    CentOS 3-6 root

    123456

    3-6 CentOS

    1

    free-m MB

    [root@localhost ~]# free -m

    total used free shared buffers cached

    Mem: 995 168 827 0 6 41

  • Java.Net Php

    400-618-4000

    -/+ buffers/cache: 121 874

    Swap: 1023 0 1023

    Mem total 995Mused

    168Mfree 827M Linux

    2

    df-lh

    [root@localhost ~]# df -lh

    Filesystem Size Used Avail Use% Mounted on

    /dev/mapper/VolGroup-lv_root

    8.3G 646M 7.2G 9% /

    tmpfs 498M 0 498M 0% /dev/shm

    /dev/sda1 477M 28M 425M 7% /boot

    Filesystem Size Used

    Avail Use%Mounted on 3

    /dev/mapper/VolGroup-lv_root 8.3G

    646M

    CentOS

    3.1.2

    Linux WebFTPDNSDHCP

    VMware Linux

    1 VMware

    VMware

    - 3-7

  • Java.Net Php

    400-618-4000

    3-7 VMware

    3-7 VMware NAT

    VMnet0VMnet8 VMnet1

    1

    IP 3-8

    1192.168.1.3

    192.168.1.2

    192.168.1.1

    WAN

    2

    192.168.1.4

    VMnet0

    LAN

    3-8 VMnet0

    3-8 VMware

    2NAT

    NAT VMware

    3-9

  • Java.Net Php

    400-618-4000

    1192.168.78.3

    192.168.1.1

    LAN WAN

    2192.168.78.4

    VMnet8

    VMnet8192.168.78.1

    NAT

    NAT192.168.78.2

    DHCP

    DHCPIP192.168.1.2

    3-9 VMnet8

    3-9 VMnet8 NAT IP

    192.168.1.2

    VMnet8

    3

    NAT NAT

    VMnet1 3-10

    1192.168.50.2

    192.168.1.2

    192.168.1.1

    LAN

    WAN

    2192.168.50.3

    VMnet1

    VMnet1192.168.50.1

    DHCP

    DHCP

    3-10 VMnet1

    VMnet8 VMnet1 VMware

    Windows

    ipconfig VMent8 VMnet1 3-11

    3-11 VMware

  • Java.Net Php

    400-618-4000

    3-11 VMnet1 IP 192.168.50.1VMnet8 IP 192.168.78.1

    IP VMware IP IP

    IP VMware

    2

    VMware NAT

    VMware -

    3-12

    3-12

    3-12

    MAC

    3 Linux

    VMware Linux Linux

    ifconfig -a 3-13

    3-13

  • Java.Net Php

    400-618-4000

    3-13 2 1 eth0 0 2

    lo eth0 lo

    IP 127.0.0.1 Loopback Address

    VMware NAT DHCP

    IP IP

    IP IP

    1 IP

    eth0 ifup eth0 eth0

    ifcfg-eth0

    [root@localhost ~]# cd /etc/sysconfig/network-scripts

    ifcfg-eth0

    [root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0.bak

    vi

    [root@localhost network-scripts]# vi ifcfg-eth0

    DEVICE=eth0

    HWADDR=00:0C:29:48:2A:8A

    TYPE=Ethernet

    UUID=de5dcf98-9e30-4d0e-a578-4ddcea528ae6

    ONBOOT=no

    NM_CONTROLLED=yes

    BOOTPROTO=dhcp

    ONBOOT BOOTPROTO

    BOOTPROTO IP dhcp IP

    ONBOOT no yes

    service network reload

    ifconfig eth0 3-14

    3-14

    3-14 eth0 IP 192.168.78.128 NAT

    VMware

    2 IP

    IP IP ifcfg-eth0 BOOTPROTO

    static IPADDRIP IP

  • Java.Net Php

    400-618-4000

    VMware NAT IP 192.168.78.0VMnet8 IP 192.168.78.1NAT

    IP 192.168.78.2DHCP 192.168.78.128192.168.78.254 192.168.78.3192.168.78.127

    IP IP

    ifcfg-eth0

    BOOTPROTO=static

    IPADDR=192.168.78.3

    NETMASK=255.255.255.0

    GATEWAY=192.168.78.2

    DNS1=192.168.78.2

    BOOTPROTO dhcp static IPADDRIP NETMASK

    GATEWAY DNS1

    DNS

    service network reload

    DNS

    [root@localhost ~]# route | grep default

    default 192.168.78.2 0.0.0.0 UG 0 0 0 eth0

    [root@localhost ~]# cat /etc/resolv.conf

    nameserver 192.168.78.2

    4

    Linux Windows Linux

    ping Windows ping

    IP 3-15

    3-15 ping

    3-15 IP 192.168.78.3 4 ping 4

    32 1 TTL 64 TTL

    64 1 64

    ping Windows ping

    Windows ( - ICMPv4-In)

    ping 3-16

  • Java.Net Php

    400-618-4000

    3-16 ping

    3-16 Linux ping -c4 ping 4

    Ctrl + C

    ping ping baidu.com ping

    VMware

    VMnet1VMnet8

    3.1.3

    CentOS 6.8

    SSHSecure Shell Shell

    Man-in-the-MiddleAttack

    SSH

    1 SSH

    SSH Windows XshellSecureCRT

    Windows Linux

    Xshell Xshell

    http://www.netsarang.com Xshell

    Xshell

    1 Xshell 3-17

    -

    3-17

    2 3-17 Xshell

    - 3-18

  • Java.Net Php

    400-618-4000

    3-18

    3 IP

    SSH 22

    4 Linux root

    123456 3-19

    3-19

  • Java.Net Php

    400-618-4000

    5linux 3-20

    xterm Num Lock

    3-20

    6 3-21

    3-21

    7192.168.78.3

    3-22

    3-22

  • Java.Net Php

    400-618-4000

    3-22

    Xftp Xftp

    Xftp

    2 SFTP

    SFTPSecure File Transfer Protocol SSH

    Xftp SFTP Xshell

    Xftp

    1 Xftp Xshell

    Xftp 3-23

    3-23 Xftp

    3-23 Windows Linux

    2Xftp Windows

    Notepad++ Xftp -

    notepad++.exe 3-24

  • Java.Net Php

    400-618-4000

    3-24

    3 Notepad++

    Notepad++

    Notepad++ Linux UTF8

    BOM UNIX Linux

    3.1.4

    CentOS 6.8 gccgcc-c++

    gcc gcc-c++ Nginx

    wget wget HTTPHTTPS

    FTP HTTP 3-3

    3-3

    gcc C

    gcc-c++ C++

    wget

    CentOS yum

    [root@localhost ~]# yum -y install gcc gcc-c++ wget

    install-yyes

    yesno

    3-25

    3-25 yum

    wget wget

    [root@localhost ~]# wget http://nginx.org/download/nginx-1.10.1.tar.gz

    nginx-1.10.1.tar.gz

    Linux date

    BIOS VMware BIOS

    CentOS Windows

  • Java.Net Php

    400-618-4000

    UTC

    Windows UTC

    VMware

    NTP

    1 yum ntpdate

    CentOS ntpdate yum

    [root@localhost ~]# yum -y install ntpdate

    2

    NTP ntp.org

    [root@localhost ~]# ntpdate cn.pool.ntp.org

    28 Sep 15:57:58 ntpdate[1215]: step time server 115.28.122.198 offset 1306.825 sec

    [root@localhost ~]# date

    Wed Sep 28 15:58:15 CST 2016

    cn.pool.ntp.org date

    Linux

    Linux RPM

    Linux Red HatCentOSFedora

    yum Yellow dog Updater, Modified DebainUbuntu

    APTAdvanced Packaging Tools

    CentOS RPM yum RPM

    yum

    3.2 Linux Nginx

    Linux Linux

    Nginx Nginx Nginx Nginx

    Web Windows Web

  • Java.Net Php

    400-618-4000

    3.2.1 Nginx

    Nginx Nginx Mainline version

    Stable version Legacy versions Linux

    Windows 3-26

    3-26 Nginx

    1.10.1 3-26 Stable version 2 nginx-1.10.1

    Nginx Linux

    Windows Xftp Linux

    URL wget

    Nginx .tar.gz Linux tar -zxvf

    z gzipx v f

    [root@localhost ~]# tar -zxvf nginx-1.10.1.tar.gz

    nginx-1.10.1 cd

    ls

    [root@localhost ~]# cd nginx-1.10.1

    [root@localhost nginx-1.10.1]# ls

    auto CHANGES.ru configure html man src

    CHANGES conf contrib LICENSE README

    src Nginx

    man Nginx

    html

    contrib

    conf Nginx

    auto configure

    configure Nginx makefile

  • Java.Net Php

    400-618-4000

    CHANGESCHANGES.ruLICENSE README Nginx

    3.2.2 Nginx

    1

    Nginx pcre zlib openssl

    Nginx Nginx 3-4

    3-4 Nginx

    pcre-devel Nginx rewrite

    zlib-devel Nginx gzip

    openssl-devel Nginx ssl SSL

    yum Nginx

    [root@localhost ~]# yum -y install pcre-devel openssl-devel

    openssl-devel zlib-devel yum

    zlib-devel 3-27

    3-27

    2 Nginx

    Nginx Nginx

    1 nginx

    [root@localhost ~]# cd nginx-1.10.1

    2 Nginx Nginx

    [root@localhost nginx-1.10.1]# ./configure \

    --prefix=/usr/local/nginx \

    --with-http_ssl_module

    ./configure

    Nginx--prefix Nginx

    /usr/local/nginx--with-http_ssl_module

    Nginx http_ssl_module

    12 \ 3

    /

    ./configure --prefix=/usr/local/nginx --with-http_ssl_module

  • Java.Net Php

    400-618-4000

    Nginx Nginx

    --with-

    3-28 PCREOpenSSL

    zlib

    3-28 Nginx

    3 make Nginx

    [root@localhost nginx-1.10.1]# make && make install

    &&

    makemake install

    Nginx 3-29

    3-29 Nginx

    devel devel

    Linux devel devel zlib zlib-devel

    devel devel

    devel devel

  • Java.Net Php

    400-618-4000

    yum -y install devel devel devel

    yum devel

    3.2.3 Nginx

    1 Nginx

    Nginx Nginx sbin Nginx

    [root@localhost ~]# cd /usr/local/nginx/sbin

    [root@localhost sbin]# ./nginx

    Nginx ps Nginx

    [root@localhost ~]# ps aux | grep nginx

    ps auxgrep nginx

    Nginx 3-30

    3-30 Nginx

    3-30 2 Nginx master processworker process

    3 grep nginx Nginx Nginx 1

    Nginx root nobody 2 ID PID

    8170 8171

    2 Nginx

    Nginx

    1

    Nginx -sstop

    [root@localhost sbin]# ./nginx s stop

    2

    stop Nginx Nginx

    quit

    [root@localhost sbin]# ./nginx s quit

    3 kill killall

    Linux kill killall

    #

    [root@localhost ~]# kill Nginx PID

    #

    [root@localhost ~]# killall nginx

    Nginx Nginx 3-5

    3-5 Nginx

  • Java.Net Php

    400-618-4000

    nginx -s reload Nginx

    nginx -s reopen

    nginx -c //nginx.conf Nginx

    nginx -t

    nginx -t -c //nginx.conf Nginx

    nginx -v

    nginx -V

    3

    Nginx 80 HTTP 80

    Nginx Nginx Linux

    netstat -tlnp 3-31

    3-31

    3-31 Nginx TCP 80 Nginx

    netstat 4 tlnp tcp

    PID

    3.2.4

    Nginx Web

    CentOS iptables Nginx HTTP 80

    Web 80

    iptables 80

    [root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

    TCP 80

    3-6

    3-6 iptables

    -I INPUT INPUT

    -p tcp tcpudpicmp tcp

    --dport 80 80

    -j ACCEPT (ACCEPTDROPREJECTREDIRECT )

    ACCEPT

    [root@localhost ~]# service iptables status

  • Java.Net Php

    400-618-4000

    3-32 Chain INPUT (policy ACCEPT)

    num 1 tcp dpt:80

    3-32

    iptables

    [root@localhost ~]# service iptables save

    iptables

    [root@localhost ~]# service iptables restart

    Windows URL

    http:// IP 3-33

    3-33

    3.2.5

    Nginx Nginx Nginx

    1

  • Java.Net Php

    400-618-4000

    Nginx Nginx

    Nginx Nginx

    Nginx

    Linux nginx

    echo $PATH

    [root@localhost ~]# echo $PATH

    /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

    :

    /usr/local/sbin

    command not found

    /bin /sbin sbin

    /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin

    Nginx /usr/local/sbin

    nginx /usr/local/sbin nginx

    [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx

    ln -s Windows

    1 2

    /usr/local/sbin ll 3-34

    3-34

    nginx Nginx

    # Nginx

    [root@localhost ~]# nginx s quit

    # Nginx

    [root@localhost ~]# nginx

    #

    [root@localhost ~]# nginx s reload

    2

    Linux service

    service network reload service iptables stop service 1

    2 startstoprestartreload

    CentOS service /etc/init.d shell

    #

    [root@localhost ~]# /etc/init.d/network restart

    # service

    [root@localhost ~]# service network restart

    network shell restart Nginx

    /etc/init.d nginx shell

    vi /etc/init.d/nginx shell Nginx startstopreload

    restart 4

    1 #! /bin/bash

  • Java.Net Php

    400-618-4000

    2 DAEMON=/usr/local/nginx/sbin/nginx

    3 case "$1" in

    4 start)

    5 echo "Starting nginx daemon..."

    6 $DAEMON && echo "SUCCESS"

    7 ;;

    8 stop)

    9 echo "Stopping nginx daemon..."

    10 $DAEMON -s quit && echo "SUCCESS"

    11 ;;

    12 reload)

    13 echo "Reloading nginx daemon..."

    14 $DAEMON -s reload && echo "SUCCESS"

    15 ;;

    16 restart)

    17 echo "Restarting nginx daemon..."

    18 $DAEMON -s quit

    19 $DAEMON && echo "SUCCESS"

    20 ;;

    21 *)

    22 echo "Usage: service nginx {start|stop|restart|reload}"

    23 exit 2

    24 ;;

    25 esac

    1 bash shell 2

    DAEMON Nginx $

    3~25 case $1 shell 1

    1 case 21~24

    23 exit 0

    nginx

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

    service nginx 3-35

    3-35 nginx

    shell

  • Java.Net Php

    400-618-4000

    shell

    shell

    1shell

    shell #! shell

    bash shell

    #! /bin/bash

    2case

    case

    case $ in

    1)

    1

    ;;

    2)

    2

    ;;

    *)

    ;;

    esac

    case case in

    );;

    [0-9]|*)

    *) case esac

    3

    chkconfig Nginx chkconfig

    chkconfig [--add][--del][--list][]

    --add nginx--del

    --list

    3-36

    3-36

  • Java.Net Php

    400-618-4000

    Nginx /etc/init.d/nginx chkconfig

    nginx 2

    # chkconfig: 35 85 15

    35 Nginx 3 5

    -8515 Nginx SK

    S85 K15 0~99

    Nginx S K

    chkconfig Nginx

    [root@localhost ~]# chkconfig --add nginx

    chkconfig --list 3-37 nginx

    7 3 5 on off

    Linux Nginx

    3-37 nginx

    CentOS /etc/rc .d

    3 /etc/rc3.d 3-38

  • Java.Net Php

    400-618-4000

    3-38

    3-38 /etc/init.d S K

    S K

    S K nginx S85 K15

    3 5 Nginx

    S start K

    stop# chkconfig:

    chkconfig

    3.3 Windows Nginx

    Nginx Linux Windows Windows

    Windows Nginx

    1

    Nginx http://nginx.org Stable version windows Nginx

    nginx-1.10.1.zipC:\web\nginx1.10 3-39

    3-39 Nginx

  • Java.Net Php

    400-618-4000

    3-39 Windows Nginx conf Nginx contrib

    docs html logs temp

    2 Nginx

    Windows Nginx nginx.exe Nginx

    cmd tasklist 3-40

    3-40

    Nginx 80 80 Micorsoft

    IISApache HTTP Server Tomcat Web netstat -ano 80

    3-41

    3-41

    Nginx cmd nginx.exe

    nginx.exe Linux

    C:\web\nginx1.10>nginx.exe s stop

    3

    Nginx http://localhosthttp://127.0.0.1

    3-42 Windows Nginx

  • Java.Net Php

    400-618-4000

    3-42

    Linux Linux Nginx Web

    Windows Nginx

    Nginx Linux

    crontab

    CentOS

    1 VMware