天天看點

Nginx1.10.1部署實戰

1、系統資訊

[root@nginx-server ~]# cat /etc/redhat-release
CentOS release 6.6 (Final)
[root@nginx-server ~]# uname -r
2.6.32-504.el6.x86_64
[root@nginx-server ~]# uname -m
x86_64
[root@nginx-server ~]# uname -n
nginx-server
[root@nginx-server ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:02:BD:AA
          inet addr:192.168.100.131  Bcast:192.168.100.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe02:bdaa/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:55736 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9189 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:77355420 (73.7 MiB)  TX bytes:790859 (772.3 KiB)      

2、軟體資訊

pcre:pcre-8.39.tar.gz
openssl:openssl-1.0.2a.tar.gz
zlib:zlib-1.2.7.tar.gz
nginx:nginx-1.10.1.tar.gz      

3、環境準備

[root@nginx-server ~]# mkdir /software
[root@nginx-server ~]# ll -d /software/
drwxr-xr-x. 2 root root 4096 2017-01-12 16:04 /software/
[root@nginx-server ~]# cd /software/
[root@nginx-server software]# yum -y install lrzsz gd-devel
[root@nginx-server software]# rz
[root@nginx-server software]# ll
total 8624
-rw-r--r--. 1 root root  909077 2016-06-01 00:38 nginx-1.10.1.tar.gz
-rw-r--r--. 1 root root   29542 2015-04-03 10:42 nginx.vim
-rw-r--r--. 1 root root 5262089 2015-03-27 11:01 openssl-1.0.2a.tar.gz
-rw-r--r--. 1 root root 2062258 2016-09-26 15:01 pcre-8.39.tar.gz
-rw-r--r--. 1 root root  560351 2015-03-27 10:49 zlib-1.2.7.tar.gz      

4、安裝pcre支援正規表達式

[root@nginx-server software]# tar xvfz pcre-8.39.tar.gz -C /usr/local/src/
[root@nginx-server software]# cd /usr/local/src/pcre-8.39/
[root@nginx-server pcre-8.39]# ./configure --prefix=/usr/local/pcre-8.39
[root@nginx-server pcre-8.39]# make && make install
[root@nginx-server pcre-8.39]# ln -s /usr/local/pcre-8.39/ /usr/local/pcre
[root@nginx-server pcre-8.39]# ll /usr/local/pcre
lrwxrwxrwx. 1 root root 21 2017-01-12 16:10 /usr/local/pcre -> /usr/local/pcre-8.39/      

5、安裝openssl支援加密通路

[root@nginx-server software]# tar xvfz openssl-1.0.2a.tar.gz -C /usr/local/src/
[root@nginx-server software]# cd /usr/local/src/openssl-1.0.2a/
[root@nginx-server openssl-1.0.2a]# ./config --prefix=/usr/local/openssl-1.0.2a
[root@nginx-server openssl-1.0.2a]# make && make install
[root@nginx-server openssl-1.0.2a]# ln -s /usr/local/openssl-1.0.2a/ /usr/local/openssl
[root@nginx-server openssl-1.0.2a]# ll /usr/local/openssl
lrwxrwxrwx. 1 root root 26 2017-01-12 16:17 /usr/local/openssl -> /usr/local/openssl-1.0.2a/      

6、安裝zlib支援壓縮

[root@nginx-server software]# tar xvfz zlib-1.2.7.tar.gz -C /usr/local/src/
[root@nginx-server software]# cd /usr/local/src/zlib-1.2.7/
[root@nginx-server zlib-1.2.7]# ./configure --prefix=/usr/local/zlib-1.2.7
[root@nginx-server zlib-1.2.7]# make && make install
[root@nginx-server zlib-1.2.7]# ln -s /usr/local/zlib-1.2.7/ /usr/local/zlib
[root@nginx-server zlib-1.2.7]# ll /usr/local/zlib
lrwxrwxrwx. 1 root root 22 2017-01-12 16:19 /usr/local/zlib -> /usr/local/zlib-1.2.7/      

7、安裝nginx

[root@nginx-server software]# useradd -s /sbin/nologin -M www
[root@nginx-server software]# id www
uid=500(www) gid=500(www) groups=500(www)
[root@nginx-server software]# tar xvfz nginx-1.10.1.tar.gz -C /usr/local/src/
[root@nginx-server software]# cd /usr/local/src/nginx-1.10.1/
[root@nginx-server nginx-1.10.1]# ./configure \
--prefix=/usr/local/nginx-1.10.1 \
--sbin-path=/usr/local/nginx-1.10.1/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_p_w_picpath_filter_module \
--with-pcre=/usr/local/src/pcre-8.39/ \
--with-zlib=/usr/local/src/zlib-1.2.7 \
--with-openssl=/usr/local/src/openssl-1.0.2a
[root@nginx-server nginx-1.10.1]# make && make install
[root@nginx-server nginx-1.10.1]# ln -s /usr/local/nginx-1.10.1/ /usr/local/nginx
[root@nginx-server nginx-1.10.1]# ll /usr/local/nginx
lrwxrwxrwx. 1 root root 24 2017-01-12 16:35 /usr/local/nginx -> /usr/local/nginx-1.10.1/      

8、配置檔案支援文法檢查

[root@nginx-server nginx-1.10.1]# mkdir-p ~/.vim/syntax
[root@nginx-server nginx-1.10.1]# mv /software/nginx.vim ~/.vim/syntax
[root@nginx-server nginx-1.10.1]# cat >> ~/.vim/filetype.vim<<EOF
> au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/vhost/* set ft=nginx
> EOF
注釋:修改配置檔案隻有使用絕對路徑才支援文法檢查      
[root@nginx-server nginx-1.10.1]# vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginxdaemin
#
# chkconfig:   -85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
#              proxy and IMAP/POP3 proxy server
# processname: nginx
# config:     /etc/nginx/nginx.conf
# pidfile:    /var/run/nginx/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ]&& exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile="/var/lock/nginx.lock"
 
start() {
    [ -x $nginx] || exit 5
    [ -f$NGINX_CONF_FILE ] || exit 6
    echo -n$"Starting $prog: "
    daemon$nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq0 ] && touch $lockfile
    return$retval
}
 
stop() {
    echo -n$"Stopping $prog: "
    killproc$prog -QUIT
    retval=$?
    echo
    [ $retval-eq 0 ] && rm -f $lockfile
    return$retval
}
 
restart() {
    configtest|| return $?
    stop
    start
}
 
reload() {
    configtest|| return $?
    echo -n$"Reloading $prog: "
    killproc$nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c$NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status>/dev/null 2>&1
}
 
case "$1" in
    start)
       rh_status_q && exit 0
        $1
        ;;
    stop)
       rh_status_q || exit 0
        $1
        ;;
   restart|configtest)
        $1
        ;;
    reload)
       rh_status_q || exit 7
        $1
        ;;
   force-reload)
       force_reload
        ;;
    status)
       rh_status
        ;;
   condrestart|try-restart)
       rh_status_q || exit 0
        ;;
    *)
        echo$"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
[root@nginx-server nginx-1.10.1]# chmod +x /etc/init.d/nginx
[root@nginx-server nginx-1.10.1]# ll /etc/init.d/nginx
-rwxr-xr-x. 1 root root 1900 2017-01-12 16:41 /etc/init.d/nginx
[root@nginx-server nginx-1.10.1]# chkconfig --level 3 nginx on
[root@nginx-server nginx-1.10.1]# chkconfig --list|grep "nginx"
nginx           0:off   1:off   2:off   3:on    4:off   5:off   6:off      
[root@nginx-server nginx-1.10.1]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]
[root@nginx-server nginx-1.10.1]# netstat -tnlup|grep "80"
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      27197/nginx
[root@nginx-server nginx-1.10.1]# ps -ef|grep "nginx"|grep -v "grep"
root     27197     1  0 16:45 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
www      27199 27197  0 16:45 ?        00:00:00 nginx: worker process
[root@nginx-server nginx-1.10.1]# curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>      

繼續閱讀