centos7 編譯安裝 nginx 1.9.2

安裝 nginx 依賴庫

yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed

下載 nginx 源碼

wget -c http://nginx.org/download/nginx-1.9.2.tar.gz
tar -zxvf nginx-1.9.2.tar.gz

下載 pcre/zlib 包

下載pcre的tar包並解壓,以便支持Nginx的Rewrite功能php

下載zlib的tar包並解壓,以便支持Nginx的Gzip壓縮功能nginx

wget -c http://git.typecodes.com/libs/php/pcre-8.36.tar.gz && tar -zxf pcre-8.36.tar.gz
wget -c http://git.typecodes.com/libs/nginx/zlib-1.2.8.tar.gz && tar -zxf zlib-1.2.8.tar.gz

將兩個文件夾放入 nginx 的文件夾裏

目錄git

mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
mkdir -p /var/run/nginx

==========================================================================bash

安裝 Nginx

切換到 nginx 源碼目錄,執行 configure 文件dom

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/run/nginx.pid  \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-pcre=pcre-8.36 \
--with-zlib=zlib-1.2.8 \
--with-debug \
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-stream \
--with-ld-opt="-Wl,-E"

各參數釋意優化

./configure \
--prefix=/usr/local/nginx \                     [Nginx安裝目錄]
--sbin-path=/usr/local/sbin/nginx \             [Nginx的 nginx sbin 命令路徑]
--conf-path=/usr/local/nginx/etc/nginx.conf \   [Nginx的配置文件]
--error-log-path=/var/log/nginx/error.log \     [Nginx的錯誤日誌]
--http-log-path=/var/log/nginx/access.log \     [Nginx的訪問日誌]
--pid-path=/run/nginx.pid  \                    [Nginx的進程ID]
--lock-path=/var/lock/nginx.lock \
--user=nginx \                                  [Nginx所屬用戶]
--group=nginx \                                 [Nginx所屬用戶組]
--with-http_ssl_module \                        [Nginx的ssl模塊]
--with-http_spdy_module \                       [Nginx的Google spdy模塊 反正我是編譯不經過去掉了]
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \               [Nginx的gzip壓縮模塊]
--with-http_perl_module \
--with-pcre=~/pcre-8.36 \                      [你解壓後的pcre的文件包路徑]
--with-zlib=~/zlib-1.2.8 \                     [你解壓後的zlib的文件包路徑]
--with-debug \                                 [容許DEBUG]
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-stream \                                [Nginx1.9.0+特有的stream模塊]
--with-ld-opt="-Wl,-E"                         [gcc的編譯優化]

編譯安裝ui

注意如今已經不須要編譯安裝 pcre 和 zlib 了,直接指定他們的文件路徑,便可將其編譯到 nginx 內部模塊this

通常是不會報錯的 只要你把依賴包和組件都安裝完整spa

make && make install

註冊爲系統服務.net

#!/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:     /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 命令路徑
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/nginx.lock

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
}

# killproc nginx -QUIT
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 3
    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

根據本身的實際安裝的配置作必定的修改:

# config:      /usr/local/nginx/conf/nginx.conf 配置文件路徑
# pidfile:     /run/nginx.pid pid 路徑

nginx="/usr/local/nginx/sbin/nginx"  bin路徑

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 配置文件路徑

lockfile=/var/lock/nginx.lock 鎖路徑

保存爲 /etc/init.d/nginx 並賦予執行權限便可 chmod a+x /etc/init.d/nginx

即可使用 service nginx start|stop|status|restart 等命令控制 nginx 服務

相關文章:

PHP7.0.0正式版編譯安裝LAMP/LNMP

nginx 隱藏 index.php 和 開啓pathinfo 模式的配置

相關文章
相關標籤/搜索