WEB 服務應用 Nginx之安裝篇

來源:互聯網
上載者:User

標籤:hup   net   lin   amp   creating   option   basename   c/s   table   

一、Nginx 源碼包安裝與配置

1、環境準備

作業系統、核心版本: CentOS 6.8  2.6.32-642.el6.x86_64
Nginx 軟體版本: nginx-1.10.2

2、建立Nginx使用者

[[email protected] ~]# groupadd -g 108 -r nginx[[email protected] ~]# useradd -u 108 -r -g 108 nginx[[email protected] ~]# id nginxuid=108(nginx) gid=108(nginx) groups=108(nginx)

3、安裝Nginx所需依賴包

# yum -y install pcre-devel openssl-devel gcc-c++ zlib-devel

4、編譯安裝Nginx-1.10.2

# cd /root/soft# tar -xf nginx-1.10.2.tar.gz# cd nginx-1.10.2# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_dav_module# make# make install

5、為nginx提供SysV init啟動指令碼(/etc/init.d/nginx)

#!/bin/sh

# nginx - this script starts and stops the nginx daemon 

# 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 
# config:      /etc/sysconfig/nginx 
# pidfile:     /var/run/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="/usr/local/nginx/etc/conf/nginx.conf" 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
lockfile=/var/lock/subsys/nginx 
make_dirs() { 
   # make required directories 
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -` 
   options=`$nginx -V 2>&1 | grep ‘configure arguments:‘` 
   for opt in $options; do 
       if [ `echo $opt | grep ‘.*-temp-path‘` ]; then 
           value=`echo $opt | cut -d "=" -f 2` 
           if [ ! -d "$value" ]; then 
               # echo "creating" $value 
               mkdir -p $value && chown -R $user $value 
           fi 
       fi 
   done 

start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    make_dirs 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && 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 
    sleep 1 
    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

6、Nginx啟動指令碼賦予執行許可權

# chmod +x /etc/init.d/nginx

7、添加到服務管理列表,並設定開機自動啟動服務

# chmod +x /etc/init.d/nginx 
# chkconfig --add nginx
# chkconfig nginx on  
# chkconfig --list ngin
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off

8、啟動Nginx服務

Nginx啟動前檢查文法:(修改配置,重啟服務前一定要先檢查設定檔文法。)
[[email protected] nginx-1.10.2]# /usr/local/nginx/sbin/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

啟動Nginx服務:
[[email protected] nginx-
1.10.2
]# /etc/init.d/nginx startStarting nginx:                          [ OK ]

9、查看Nginx服務對應連接埠是否啟動成功

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   4547  root    6u  IPv4  17508      0t0  TCP *:http (LISTEN)
nginx   4548 nginx    6u  IPv4  17508      0t0  TCP *:http (LISTEN)
[[email protected] nginx-1.10.2]# netstat -lnt| grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN     
tcp        0      0 :::32980                    :::*                        LISTEN

10、檢查Nginx啟動的實際效果,通過瀏覽器訪問

好了到這裡Nginx的安裝就完成了,接下來會介紹Ngnix相關的配置!

WEB 服務應用 Nginx之安裝篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.