Linux操做系統:CentOS6.5 64bit\Debian8.4 64bitphp
最新版Nginx: 1.9.3\1.10.0linux
一、下載Nginxnginx
centos wget http://nginx.org/download/nginx-1.9.3.tar.gzweb
debian wget http://nginx.org/download/nginx-1.10.0.tar.gz vim
二、安裝依賴的庫centos
centos: yum -y install gcc automake autoconf libtool make libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embedbash
debian: apt-get install -y gcc make vim build-essential unzip lrzsz服務器
三、安裝pcre,用於重寫rewritecookie
centos: wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz網絡
debian: wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
將pcre安裝到 /usr/local/src
[root@bkjia softs]# cd /usr/local/src/ [root@bkjia src]# tar pcre-8.37.tar.gz [root@bkjia src]# cd pcre-8.37 [root@bkjia pcre-8.37]# ./configure [root@bkjia pcre-8.37]# make [root@bkjia pcre-8.37]# make install
四、安裝zlib,用於gzip壓縮
安裝到/usr/local/src
[root@bkjia src]# wget http://zlib.net/zlib-1.2.8.tar.gz [root@bkjia src]# tar -zxv zlib-1.2.8.tar.gz [root@bkjia src]# cd zlib-1.2.8 [root@bkjia zlib-1.2.8]# ./configure [root@bkjia zlib-1.2.8]# make [root@bkjia zlib-1.2.8]# make install
五、安裝openssl,用於ssl
安裝到/usr/local/src
centos: wget http://www.openssl.org/source/openssl-1.0.2g.tar.gz
debian: wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz
[root@bkjia src]# tar -zxvf openssl-1.0.2g.tar.gz [root@bkjia src]# cd openssl-1.0.2g [root@bkjia zlib-1.2.8]# ./configure [root@bkjia zlib-1.2.8]# make [root@bkjia zlib-1.2.8]# make install
六、安裝Nginx
安裝到/usr/local/nginx
[root@bkjia softs]# cd /usr/local [root@bkjia local]# pwd /usr/local [root@bkjia local]# [root@bkjia local]# cd nginx-1.9.3 [root@bkjia nginx-1.9.3]# ./configure \ --prefix=/usr/local/nginx #默認也是這個路徑 --with-http_ssl_module \ --with-pcre=/usr/local/src/pcre-8.37 \ #用於重寫rewrite --with-zlib=/usr/local/src/zlib-1.2.8 \ #用於gzip壓縮 --with-openssl=/usr/local/src/openssl-1.0.1d #用於ssl --with-stream #默認不安裝,TCP負載均衡功能,固然不用的能夠不用裝
若是增長sticky負載均衡保持cookies的話,參考https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/overview,編譯nginx的時候後面加個,不過這個zip很難下到,我下面有上傳附件
--add-module=/app/soft/nginx-sticky/
[root@bkjia nginx-1.9.3]# make [root@bkjia nginx-1.9.3]# make install
七、設置開機自啓動
debian官方建議:https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/#,主要求改安裝路徑和pid文件路徑,測試勉強夠用
#! /bin/sh ### BEGIN INIT INFO # Provides: nginx # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the nginx web server # Description: starts nginx using start-stop-daemon ### END INIT INFO PATH=/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/nginx/sbin/nginx NAME=nginx DESC=nginx test -x $DAEMON || exit 0 # Include nginx defaults if available if [ -f /etc/default/nginx ] ; then . /etc/default/nginx fi set -e case "$1" in start) echo -n "Starting $DESC: " start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/nginx.pid \ --exec $DAEMON -- $DAEMON_OPTS echo "$NAME." ;; stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/nginx.pid \ --exec $DAEMON echo "$NAME." ;; restart|force-reload) echo -n "Restarting $DESC: " start-stop-daemon --stop --quiet --pidfile \ /var/run/nginx.pid --exec $DAEMON sleep 1 start-stop-daemon --start --quiet --pidfile \ /var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS echo "$NAME." ;; reload) echo -n "Reloading $DESC configuration: " start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/nginx.pid \ --exec $DAEMON echo "$NAME." ;; *) N=/etc/init.d/$NAME echo "Usage: $N {start|stop|restart|force-reload}" >&2 exit 1 ;; esac exit 0
centos(網絡版): 在/etc/init.d/下創建nginx文件
[root@bkjia init.d]# vi nginx #!/bin/bash # nginx Startup script for the Nginx HTTP Server # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /usr/local/nginx/logs/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/usr/local/nginx/logs/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
賦予可執行權限
[root@bkjia init.d]# chmod a+x /etc/init.d/nginx
設置開機啓動
[root@bkjia init.d]# chkconfig --add /etc/init.d/nginx
[root@bkjia init.d]# chkconfig nginx on
啓動:
[root@bkjia lib64]# service nginx start
Starting nginx: [ OK ]
[root@bkjia lib64]#
八、啓動報錯處理
[root@bkjia init.d]# service nginx start
Starting nginx: /usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[FAILED]
使用ldd看nginx包含的動態函式庫
[root@bkjia src]# ldd $(which /usr/local/nginx/sbin/nginx)
linux-vdso.so.1 => (0x00007fff89fff000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003978400000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x000000397b800000)
libpcre.so.1 => not found
libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007ffd9a115000)
libz.so.1 => /lib64/libz.so.1 (0x0000003977c00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003978000000)
/lib64/ld-linux-x86-64.so.2 (0x0000003977800000)
libfreebl3.so => /lib64/libfreebl3.so (0x000000397ac00000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003978800000)
[root@bkjia src]# cd /
能夠看到 libpcre.so.1 => not found
解決方法:
進入/lib64目錄中手動連接下
[root@bkjia init.d]# cd /lib64/
[root@bkjia lib64]# ln -s libpcre.so.0.0.1 libpcre.so.1
而後再啓動。
更多Nginx相關教程見如下內容:
CentOS 6.2實戰部署Nginx+MySQL+PHP
使用Nginx搭建WEB服務器
搭建基於Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服務器全過程
CentOS 6.3下Nginx性能調優
CentOS 6.3下配置Nginx加載ngx_pagespeed模塊
CentOS 6.4安裝配置Nginx+Pcre+php-fpm
Nginx安裝配置使用詳細筆記
Nginx日誌過濾 使用ngx_log_if不記錄特定日誌