Nginx安裝配置


方式①(nginx用戶)

1:下載nginx-1.8.1.tar.gz並上傳到服務器中並解壓

2:執行設置編譯安裝環境

      yum -y groupinstall "Development Tools" "Server Platform Development" 

3:安裝pcre-devel

      yum -y install pcre-devel openssl openssl-devel

4:創建nginx用戶組

      useradd -r nginx

5:執行nginx啟動配置

./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-debug

      (參數可參考nginx編譯參數)

      如下圖所示標識啟動配置成功

     

6:安裝

      make && make install

7:配置nginx環境變量

      vi /etc/porfile

      export PATH=/usr/local/nginx/sbin:$PATH

      重新加載配置文件source /etc/profile

 

方式②(root用戶)

1:下載nginx-1.8.1.tar.gz並上傳到服務器中並解壓

2:安裝pcre-devel和openssl-devel

      yum -y install pcre-devel

      yum -y install openssl-devel

3:執行nginx啟動配置

./configure --prefix=/usr/local/nginx --with-http_mp4_module --with-http_flv_module --with-http_realip_module --with-http_ssl_module

      (參數可參考nginx編譯參數)

      如下圖所示標識啟動配置成功

     

4:安裝

      make && make install

5:配置nginx環境變量

      vi /etc/porfile

      export PATH=/usr/local/nginx/sbin:$PATH

      重新加載配置文件source /etc/profile

 

啟動、停止、重啟

      /usr/local/nginx/sbin/nginx

      /usr/local/nginx/sbin/nginx -s stop

      /usr/local/nginx/sbin/nginx -s reload

開機自啟動

1、在linux系統的/etc/init.d/目錄下創建nginx文件,使用如下命令:

      vim /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/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

 

這個腳本其實也不是我寫的,誰寫的呢,nginx官方寫的,腳本地址:http://wiki.nginx.org/RedHatNginxInitScript ,不過要注意,如果你是自定義編譯安裝的nginx,需要根據您的安裝路徑修改下面這兩項配置:

nginx=”/usr/local/nginx/sbin/nginx” 修改成nginx執行程序的路徑。

NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” 修改成配置文件的路徑。

2、保存腳本文件后設置文件的執行權限

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

3、設置終端模式開機啟動:

      chkconfig nginx on

 

錯誤信息:

nginx: [emerg] unexpected end of file, expecting ";" or "}" in /etc/mime.types:1299

解決方法:

  Debian7系統自帶的/etc/mime.types替換為Nginx的conf目錄下的mime.types即可

 

Nginx編譯參數(./configure)


./configure --help
–prefix= 指向安裝目錄

–sbin-path 指向(執行)程序文件(nginx)

–conf-path= 指向配置文件(nginx.conf)

–error-log-path= 指向錯誤日志目錄

–pid-path= 指向pid文件(nginx.pid)

–lock-path= 指向lock文件(nginx.lock)(安裝文件鎖定,防止安裝文件被別人利用,或自己誤操作。)

–user= 指定程序運行時的非特權用戶

–group= 指定程序運行時的非特權用戶組

–builddir= 指向編譯目錄

–with-rtsig_module 啟用rtsig模塊支持(實時信號)

–with-select_module 啟用select模塊支持(一種輪詢模式,不推薦在高載環境下使用)禁用:–without-select_module

–with-poll_module 啟用poll模塊支持(功能與select相同,與select特性相同,為一種輪詢模式,不推薦在高載環境下使用)

–with-file-aio 啟用file aio支持(一種APL文件傳輸格式)

–with-ipv6 啟用ipv6支持

–with-http_ssl_module 啟用ngx_http_ssl_module支持(使支持https請求,需已安裝openssl)

–with-http_realip_module 啟用ngx_http_realip_module支持(這個模塊允許從請求標頭更改客戶端的IP地址值,默認為關)

–with-http_addition_module 啟用ngx_http_addition_module支持(作為一個輸出過濾器,支持不完全緩沖,分部分響應請求)

–with-http_xslt_module 啟用ngx_http_xslt_module支持(過濾轉換XML請求)

–with-http_image_filter_module 啟用ngx_http_image_filter_module支持(傳輸JPEG/GIF/PNG 圖片的一個過濾器)(默認為不啟用。gd庫要用到)

–with-http_geoip_module 啟用ngx_http_geoip_module支持(該模塊創建基於與MaxMind GeoIP二進制文件相配的客戶端IP地址的ngx_http_geoip_module變量)

–with-http_sub_module 啟用ngx_http_sub_module支持(允許用一些其他文本替換nginx響應中的一些文本)

–with-http_dav_module 啟用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:創建集合,COPY和MOVE方法)默認情況下為關閉,需編譯開啟

–with-http_flv_module 啟用ngx_http_flv_module支持(提供尋求內存使用基於時間的偏移量文件)

–with-http_gzip_static_module 啟用ngx_http_gzip_static_module支持(在線實時壓縮輸出數據流)

–with-http_random_index_module 啟用ngx_http_random_index_module支持(從目錄中隨機挑選一個目錄索引)

–with-http_secure_link_module 啟用ngx_http_secure_link_module支持(計算和檢查要求所需的安全鏈接網址)

–with-http_degradation_module  啟用ngx_http_degradation_module支持(允許在內存不足的情況下返回204或444碼)

–with-http_stub_status_module 啟用ngx_http_stub_status_module支持(獲取nginx自上次啟動以來的工作狀態)

–without-http_charset_module 禁用ngx_http_charset_module支持(重新編碼web頁面,但只能是一個方向–服務器端到客戶端,並且只有一個字節的編碼可以被重新編碼)

–without-http_gzip_module 禁用ngx_http_gzip_module支持(該模塊同-with-http_gzip_static_module功能一樣)

–without-http_ssi_module 禁用ngx_http_ssi_module支持(該模塊提供了一個在輸入端處理處理服務器包含文件(SSI)的過濾器,目前支持SSI命令的列表是不完整的)

–without-http_userid_module 禁用ngx_http_userid_module支持(該模塊用來處理用來確定客戶端后續請求的cookies)

–without-http_access_module 禁用ngx_http_access_module支持(該模塊提供了一個簡單的基於主機的訪問控制。允許/拒絕基於ip地址)

–without-http_auth_basic_module禁用ngx_http_auth_basic_module(該模塊是可以使用用戶名和密碼基於http基本認證方法來保護你的站點或其部分內容)

–without-http_autoindex_module 禁用disable ngx_http_autoindex_module支持(該模塊用於自動生成目錄列表,只在ngx_http_index_module模塊未找到索引文件時發出請求。)

–without-http_geo_module 禁用ngx_http_geo_module支持(創建一些變量,其值依賴於客戶端的IP地址)

–without-http_map_module 禁用ngx_http_map_module支持(使用任意的鍵/值對設置配置變量)

–without-http_split_clients_module 禁用ngx_http_split_clients_module支持(該模塊用來基於某些條件划分用戶。條件如:ip地址、報頭、cookies等等)

–without-http_referer_module 禁用disable ngx_http_referer_module支持(該模塊用來過濾請求,拒絕報頭中Referer值不正確的請求)

–without-http_rewrite_module 禁用ngx_http_rewrite_module支持(該模塊允許使用正則表達式改變URI,並且根據變量來轉向以及選擇配置。如果在server級 別設置該選項,那么他們將在 location之前生效。如果在location還有更進一步的重寫規則,location部分的規則依然會被執行。如果這個URI重寫是因為 location部分的規則造成的,那么 location部分會再次被執行作為新的URI。 這個循環會執行10次,然后Nginx會返回一個500錯誤。)

–without-http_proxy_module 禁用ngx_http_proxy_module支持(有關代理服務器)

–without-http_fastcgi_module 禁用ngx_http_fastcgi_module支持(該模塊允許Nginx 與FastCGI 進程交互,並通過傳遞參數來控制FastCGI 進程工作。 )FastCGI一個常駐型的公共網關接口。

–without-http_uwsgi_module 禁用ngx_http_uwsgi_module支持(該模塊用來醫用uwsgi協議,uWSGI服務器相關)

–without-http_scgi_module 禁用ngx_http_scgi_module支持(該模塊用來啟用SCGI協議支持,SCGI協議是CGI協議的替代。它是一種應用程序與HTTP服務接口標准。它有些像FastCGI但他的設計 更容易實現。)

–without-http_memcached_module 禁用ngx_http_memcached_module支持(該模塊用來提供簡單的緩存,以提高系統效率)

-without-http_limit_zone_module 禁用ngx_http_limit_zone_module支持(該模塊可以針對條件,進行會話的並發連接數控制)

–without-http_limit_req_module 禁用ngx_http_limit_req_module支持(該模塊允許你對於一個地址進行請求數量的限制用一個給定的session或一個特定的事件)

–without-http_empty_gif_module 禁用ngx_http_empty_gif_module支持(該模塊在內存中常駐了一個1*1的透明GIF圖像,可以被非常快速的調用)

–without-http_browser_module 禁用ngx_http_browser_module支持(該模塊用來創建依賴於請求報頭的值。如果瀏覽器為modern ,則$modern_browser等於modern_browser_value指令分配的值;如 果瀏覽器為old,則$ancient_browser等於 ancient_browser_value指令分配的值;如果瀏覽器為 MSIE中的任意版本,則 $msie等於1)

–without-http_upstream_ip_hash_module 禁用ngx_http_upstream_ip_hash_module支持(該模塊用於簡單的負載均衡)

–with-http_perl_module 啟用ngx_http_perl_module支持(該模塊使nginx可以直接使用perl或通過ssi調用perl)

–with-perl_modules_path= 設定perl模塊路徑

–with-perl= 設定perl庫文件路徑

–http-log-path= 設定access log路徑

–http-client-body-temp-path= 設定http客戶端請求臨時文件路徑

–http-proxy-temp-path= 設定http代理臨時文件路徑

–http-fastcgi-temp-path= 設定http fastcgi臨時文件路徑

–http-uwsgi-temp-path= 設定http uwsgi臨時文件路徑

–http-scgi-temp-path= 設定http scgi臨時文件路徑

-without-http 禁用http server功能

–without-http-cache 禁用http cache功能

–with-mail 啟用POP3/IMAP4/SMTP代理模塊支持

–with-mail_ssl_module 啟用ngx_mail_ssl_module支持

–without-mail_pop3_module 禁用pop3協議(POP3即郵局協議的第3個版本,它是規定個人計算機如何連接到互聯網上的郵件服務器進行收發郵件的協議。是因特網電子郵件的第一個離 線協議標 准,POP3協議允許用戶從服務器上把郵件存儲到本地主機上,同時根據客戶端的操作刪除或保存在郵件服務器上的郵件。POP3協議是TCP/IP協議族中 的一員,主要用於 支持使用客戶端遠程管理在服務器上的電子郵件)

–without-mail_imap_module 禁用imap協議(一種郵件獲取協議。它的主要作用是郵件客戶端可以通過這種協議從郵件服務器上獲取郵件的信息,下載郵件等。IMAP協議運行在 TCP/IP協議之上, 使用的端口是143。它與POP3協議的主要區別是用戶可以不用把所有的郵件全部下載,可以通過客戶端直接對服務器上的郵件進行操作。)

–without-mail_smtp_module 禁用smtp協議(SMTP即簡單郵件傳輸協議,它是一組用於由源地址到目的地址傳送郵件的規則,由它來控制信件的中轉方式。SMTP協議屬於 TCP/IP協議族,它幫助每台計算機在發送或中轉信件時找到下一個目的地。)

–with-google_perftools_module 啟用ngx_google_perftools_module支持(調試用,剖析程序性能瓶頸)

–with-cpp_test_module 啟用ngx_cpp_test_module支持

–add-module= 啟用外部模塊支持

–with-cc= 指向C編譯器路徑

–with-cpp= 指向C預處理路徑

–with-cc-opt= 設置C編譯器參數(PCRE庫,需要指定–with-cc-opt=”-I /usr/local/include”,如果使用select()函數則需要同時增加文件描述符數量,可以通過–with-cc- opt=”-D FD_SETSIZE=2048”指定。)

–with-ld-opt= 設置連接文件參數。(PCRE庫,需要指定–with-ld-opt=”-L /usr/local/lib”。)

–with-cpu-opt= 指定編譯的CPU,可用的值為: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

–without-pcre 禁用pcre庫

–with-pcre 啟用pcre庫

–with-pcre= 指向pcre庫文件目錄

–with-pcre-opt= 在編譯時為pcre庫設置附加參數

–with-md5= 指向md5庫文件目錄(消息摘要算法第五版,用以提供消息的完整性保護)

–with-md5-opt= 在編譯時為md5庫設置附加參數

–with-md5-asm 使用md5匯編源

–with-sha1= 指向sha1庫目錄(數字簽名算法,主要用於數字簽名)

–with-sha1-opt= 在編譯時為sha1庫設置附加參數

–with-sha1-asm 使用sha1匯編源

–with-zlib= 指向zlib庫目錄

–with-zlib-opt= 在編譯時為zlib設置附加參數

–with-zlib-asm= 為指定的CPU使用zlib匯編源進行優化,CPU類型為pentium, pentiumpro

–with-libatomic 為原子內存的更新操作的實現提供一個架構

–with-libatomic= 指向libatomic_ops安裝目錄

–with-openssl= 指向openssl安裝目錄

–with-openssl-opt 在編譯時為openssl設置附加參數

–with-debug 啟用debug日志

nginx編譯時不是功能加的越多越好,應該盡可能少編譯模塊,不用的最好不要加入。
編譯好的nginx可通過 /usr/local/nginx/sbin/nginx -V 查看編譯時的參數(具體路徑更具實際情況而定)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM