CentOS 6.5 + Nginx 1.8.0 + PHP 5.6(with PHP-FPM) 負載均衡源碼安裝 之 (一)Nginx安裝篇

CentOS 6.5 minimal安裝再也不贅述html

Nginx源碼安裝nginx

1.安裝wget下載程序c++

yum -y install wget

2.安裝編譯環境:gcc gcc-c++ automake autoconf libtool make瀏覽器

yum -y install gcc gcc-c++ automake autoconf libtool make

3.安裝相關依賴包(目前採用的是源碼安裝,放置到源碼目錄,也可以使用其餘如yum方式安裝):服務器

PCRE庫(用於支持http rewrite)ide

cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz
tar zxvf pcre-8.37.tar.gz
cd pcre-8.37
./configure
make
make install

zlib庫(用於支持http gzip)測試

cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz
tar zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install

4.建立下載文件存放的目錄(後面下載的都放入此目錄),並進入ui

cd /root
mkdir download
cd download

5.下載nginx源碼包(v1.8.0)最近穩定版this

wget http://nginx.org/download/nginx-1.8.0.tar.gz

6.解壓、編譯、安裝(採用默認安裝配置,但pcre的位置須要指定)spa

tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --with-pcre=/usr/local/src/pcre-8.37
make
make install

最終nginx的安裝路徑信息爲:

Configuration summary
  + using PCRE library: /usr/local/src/pcre-8.37
  + OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found
  + using system zlib library

  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/logs/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: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
View Code

7.咱們測試運行下服務器

/usr/local/nginx/sbin/nginx

而後本地經過wget訪問下:

cd ~
wget http://localhost

訪問結果以下,表示已經能夠訪問:

--2015-10-29 03:06:59--  http://localhost/
正在解析主機 localhost... ::1, 127.0.0.1
正在鏈接 localhost|::1|:80... 失敗:拒絕鏈接。
正在鏈接 localhost|127.0.0.1|:80... 已鏈接。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度:612 [text/html]
正在保存至: 「index.html」

100%[======================================>] 612         --.-K/s   in 0s

2015-10-29 03:06:59 (54.4 MB/s) - 已保存 「index.html」 [612/612])
View Code

咱們也能夠經過外面瀏覽器去訪問這個虛擬機,直接在瀏覽器輸入該虛擬機ip: http://192.168.168.131 (具體IP以本身虛擬機爲準),應該會展現出下頁面(若沒法訪問,參考最下面):

 

8.以上雖然能夠訪問了,但每次須要手動去調用才執行,因此咱們須要:

8.1.編寫一個服務腳本,存放到/etc/init.d/nginx (/etc/init.d目錄通常用於存放系統全部的服務程序),腳本內容以下:

  1 #!/bin/sh
  2 #
  3 # nginx - this script starts and stops the nginx daemin
  4 #
  5 # chkconfig:   - 85 15
  6 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
  7 #               proxy and IMAP/POP3 proxy server
  8 # processname: nginx
  9 # config:      /usr/local/nginx/conf/nginx.conf
 10 # pidfile:     /usr/local/nginx/logs/nginx.pid
 11 
 12 # Source function library.
 13 . /etc/rc.d/init.d/functions
 14 
 15 # Source networking configuration.
 16 . /etc/sysconfig/network
 17 
 18 # Check that networking is up.
 19 [ "$NETWORKING" = "no" ] && exit 0
 20 
 21 nginx="/usr/local/nginx/sbin/nginx"
 22 prog=$(basename $nginx)
 23 
 24 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 25 
 26 lockfile=/var/lock/subsys/nginx
 27 
 28 start() {
 29     [ -x $nginx ] || exit 5
 30     [ -f $NGINX_CONF_FILE ] || exit 6
 31     echo -n $"Starting $prog: "
 32     daemon $nginx -c $NGINX_CONF_FILE
 33     retval=$?
 34     echo
 35     [ $retval -eq 0 ] && touch $lockfile
 36     return $retval
 37 }
 38 
 39 stop() {
 40     echo -n $"Stopping $prog: "
 41     killproc $prog -QUIT
 42     retval=$?
 43     echo
 44     [ $retval -eq 0 ] && rm -f $lockfile
 45     return $retval
 46 }
 47 
 48 restart() {
 49     configtest || return $?
 50     stop
 51     start
 52 }
 53 
 54 reload() {
 55     configtest || return $?
 56     echo -n $"Reloading $prog: "
 57     killproc $nginx -HUP
 58     RETVAL=$?
 59     echo
 60 }
 61 
 62 force_reload() {
 63     restart
 64 }
 65 
 66 configtest() {
 67   $nginx -t -c $NGINX_CONF_FILE
 68 }
 69 
 70 rh_status() {
 71     status $prog
 72 }
 73 
 74 rh_status_q() {
 75     rh_status >/dev/null 2>&1
 76 }
 77 
 78 case "$1" in
 79     start)
 80         rh_status_q && exit 0
 81         $1
 82         ;;
 83     stop)
 84         rh_status_q || exit 0
 85         $1
 86         ;;
 87     restart|configtest)
 88         $1
 89         ;;
 90     reload)
 91         rh_status_q || exit 7
 92         $1
 93         ;;
 94     force-reload)
 95         force_reload
 96         ;;
 97     status)
 98         rh_status
 99         ;;
100     condrestart|try-restart)
101         rh_status_q || exit 0
102             ;;
103     *)
104         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
105         exit 2
106 esac
View Code

而後給此腳本添加運行權限:

chmod +x /etc/init.d/nginx

測試此腳本(中止服務、啓動服務、重啓服務),成功後會打印「肯定」字樣:

service nginx stop
service nginx start
service nginx restart

8.2.將服務腳本註冊爲系統服務並隨系統啓動

chkconfig nginx on

到此,Nginx就已安裝完畢並可提供服務了

相關文章
相關標籤/搜索