天天看點

LNMP環境搭建——Nginx篇

LNMP環境搭建——Nginx篇

1.Nginx配置檔案測試

root@kallen:/usr/local/nginx/sbin# nginx -t 
 nginx: the configuration file /etc/nginx/nginx.conf  syntax is ok 
 nginx: configuration file /etc/nginx/nginx.conf test is successful
           

2.Nginx啟動

3.Nginx負載均衡

Nginx Architecture:

LNMP環境搭建——Nginx篇

Nginx LoadBalance:

LNMP環境搭建——Nginx篇

Nginx High Availability:

LNMP環境搭建——Nginx篇

Nginx Access Process:

LNMP環境搭建——Nginx篇

Nginx 的 upstream 目前支援4 種方式的配置設定——

(1)輪詢(預設) :

每個請求按時間順序逐一配置設定到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。

(2)weight :

指定輪詢幾率,weight 和通路比率成正比,用于後端伺服器性能不均的情況。

(3)ip_hash :

每個請求按通路ip 的hash 結果配置設定,這樣每個訪客固定通路一個後端伺服器,可以解決session 的問題。可以針對同一個C 類位址段中的用戶端選擇同一個後端伺服器,除非那個後端伺服器宕了才會換一個。

(4)fair(第三方):

按後端伺服器的響應時間來配置設定請求,響應時間短的優先配置設定。

(5)url_hash(第三方):

按通路url 的hash 結果來配置設定請求,使每個url 定向到同一個後端伺服器,後端伺服器為緩存時比較有效。

4.Nginx安裝及配置

(1) nginx源碼安裝

[root@kallen ~]# wget http://syslab.comsenz.com/downloads/linux/nginx-0.9.6.tar.gz
[root@kallen ~]# tar zxvf nginx-0.9.6.tar.gz
[root@kallen ~]# cd nginx-0.9.6
           
./configure --prefix=/usr/local/nginx 
--sbin-path=/usr/local/nginx/sbin/nginx 
--conf-path=/usr/local/nginx/conf/nginx.conf 
--error-log-path=/usr/local/nginx/logs/error.log 
--http-log-path=/usr/local/nginx/logs/access.log 
--pid-path=/usr/local/nginx/var/nginx.pid 
--lock-path=/usr/local/nginx/var/nginx.lock 
--http-client-body-temp-path=/dev/shm/nginx_temp/client_body 
--http-proxy-temp-path=/dev/shm/nginx_temp/proxy 
--http-fastcgi-temp-path=/dev/shm/nginx_temp/fastcgi 
--user=www --group=www 
--with-cpu-opt=pentium4F 
--without-select_module 
--without-poll_module 
--with-http_realip_module 
--with-http_sub_module 
--with-http_gzip_static_module 
--with-http_stub_status_module 
--without-http_ssi_module 
--without-http_userid_module 
--without-http_geo_module 
--without-http_memcached_module 
--without-http_map_module 
--without-mail_pop3_module 
--without-mail_imap_module 
--without-mail_smtp_module 
--with-pcre=/usr/local/src/pcre-/ 
--with-zlib=/usr/local/zlib
           
[root@kallen ~]# make && make install 
[root@kallen ~]# mkdir /dev/shm/nginx_temp
           

有的nginx版本編譯時會因為pcre編譯不過去,需要修改一下

--with-pcre=/usr/local/src/pcre-8.32

,前提是已經下載下傳了pcre源碼包pcre-7.8.tar.gz,并解壓到

/usr/local/src/pcre-8.32

,不需要編譯pcre.

在實際安裝過程中可能需要手動安裝以下依賴包:

a. 安裝依賴軟體

b. 下載下傳相關軟體

wget http://jaist.dl.sourceforge.net/project/pcre/pcre//pcre-.tar.gz
wget http://zlib.net/zlib-.tar.gz
wget http://www.openssl.org/source/openssl-g.tar.gz
wget http://www.canonware.com/download/jemalloc/jemalloc-.tar.bz2
wget http://tengine.taobao.org/download/tengine-.tar.gz
           

c. 安裝Pcre

tar zxvf pcre-.tar.gz
  cd pcre-
 ./configure --prefix=/usr/local/pcre-
  make && make install
           

d. 安裝Zlib

tar zxvf zlib-.tar.gz
  cd zlib-
  ./configure --prefix=/usr/local/zlib-
  make && make install
           

[ERROR]-1:

./configure: error: the HTTP gzip module requires the zlib library. 
You can either disable the module by using --without-http_gzip_module 
option, or install the zlib library into the system, or build the zlib library 
statically from the source with nginx by using --with-zlib=<path> option
           

[ERROR]-2:

configure: error: You need a C++ compiler for C++ support.
make[1]: *** [/usr/local/src/pcre-8.32/Makefile] Error 1
make[1]: Leaving directory /home/kallen/MyDOC/nginx-1.8.0
make: *** [build] Error 2
           

安裝完成後的配置資訊如下:

[Nginx Configuration Summary]
Configuration summary
+ using PCRE library: /usr/local/src/pcre-
+ OpenSSL library is not used
+ using builtin md5 code
+ sha1 library is not found
+ using zlib library: /usr/local/zlib
  nginx path prefix:"/usr/local/nginx"
  nginx binary file:"/usr/local/nginx/sbin/nginx"
  nginx configuration prefix:"/usr/local/nginx/conf"
  nginx configuration file:"/usr/local/nginx/conf/nginx.conf"
  nginx pid file:"/usr/local/nginx/var/nginx.pid"
  nginx error log file:"/usr/local/nginx/logs/error.log"
  nginx http access log file:"/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files:"/dev/shm/nginx_temp/client_body"
  nginx http proxy temporary files:"/dev/shm/nginx_temp/proxy"
  nginx http fastcgi temporary files:"/dev/shm/nginx_temp/fastcgi"
  nginx http uwsgi temporary files:"uwsgi_temp"
  nginx http scgi temporary files:"scgi_temp"
           

(2) 編寫nginx啟動腳本

寫入以下内容:

#!/bin/bash
#        
# Startup script for the Nginx HTTP Server
#
# Kallen Ding, Apr 23 2015

NGINX_PATH="/usr/local/nginx/"
OPTIONS="-c ${NGINX_PATH}conf/nginx.conf"
prog=nginx
nginx=${NGINX_PATH}sbin/nginx
pidfile=/var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

start() {
    echo -n "Starting $prog: "
    daemon --pidfile=${pidfile} $nginx $OPTIONS
    RETVAL=$?
    echo
    return $RETVAL
}

stop() {
    echo -n "Stopping $prog: "
    killproc -p ${pidfile} $nginx
    RETVAL=$?
    echo
}
reload() {
    echo -n $"Reloading $prog: "
    killproc -p ${pidfile} $nginx -HUP
    RETVAL=$?
    echo
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  reload)
        reload
    ;;
  status)
    status $prog
    RETVAL=$?
    ;;
  *)
    echo "Usage: $prog {start|stop|restart|reload|status}"
    RETVAL=
esac

exit $RETVAL
           

儲存後,更改

/etc/init.d/nginx

的權限

[root@kallen ~]# chmod 755 /etc/init.d/nginx
[root@kallen ~]# chkconfig --add nginx
[root@kallen ~]# chkconfig nginx on
           
【注】文中所有圖檔均來自Nginx官網.
熱門推薦
  • 在RHEL6.5中配置本地YUM源
  • Ubuntu下Zabbix安裝及使用問題
  • MySQL雙主熱備問題處理
  • Rsync同步錯誤處理
github stack mail
LNMP環境搭建——Nginx篇
LNMP環境搭建——Nginx篇
LNMP環境搭建——Nginx篇

繼續閱讀