Nginx1.10.2安裝配置

1、yum安裝編譯nginx需要的包

yum -y install  gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

軟件包說明:

zlib: 爲nginx提供gzip模塊,需要zlib庫支持
pcre: 爲支持地址重寫rewrite功能
openssl:爲nginx提供ssl功能、

2、創建nginx用戶組和用戶

groupadd srs
useradd -r -g srs -s /sbin/nologin -M srs

3、下載nginx源碼包,將源碼包放在/usr/local/src目錄下
下載頁面:http://nginx.org/en/download.html
這裏用的是 nginx-1.10.2.tar.gz
下載地址:http://nginx.org/download/nginx-1.10.2.tar.gz
4.進入src/目錄

cd /usr/local/src/

5.解壓nginx源碼包

tar -zxf nginx-1.10.2.tar.gz
cd nginx-1.10.2
./configure \
--prefix=/usr/local/nginx/ \
--with-pcre \
--with-http_sub_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--user=www \
--group=www

make
make install

7、修改nginx目錄權限

 chown -R nginx:nginx /etc/nginx

8、將nginx服務腳本加入到init.d/目錄

vim /etc/init.d/nginx

9、加入下面內容,保存退出

#!/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:      /usr/local/nginx/conf/nginx.conf
# 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/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' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   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

10、爲nginx腳本添加可執行權限

chmod +x /etc/init.d/nginx

11、將nginx加入系統服務

chkconfig --add nginx

12、修改服務的默認啓動等級

chkconfig nginx on

13、啓動nginx

service nginx start

訪問url 如 http:192.168.3.189、
頁面顯示正常,配置成功
nginx安裝完畢

添加防火牆允許

vim /etc/sysconf/iptables

添加安全策略使80端口可以被外部訪問

-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

重啓防火牆

/etc/init.d/iptables restart
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章