CentOS6.5環(huán)境怎么安裝nginx服務(wù)器及配置負(fù)載均衡-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“CentOS6.5環(huán)境怎么安裝nginx服務(wù)器及配置負(fù)載均衡”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),貴州企業(yè)網(wǎng)站建設(shè),貴州品牌網(wǎng)站建設(shè),網(wǎng)站定制,貴州網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,貴州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

1、下載pcre, 是一個(gè)用c語言編寫的正則表達(dá)式函數(shù)庫

[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
[root@localhost download]# tar zxvf pcre-8.36.tar.gz

2、下載zlib庫

[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://ncu.dl.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz
[root@localhost download]# tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8

4、下載ssl

[root@localhost zlib-1.2.8]# cd ..
[root@localhost download]# wget http://www.openssl.org/source/openssl-1.0.1p.tar.gz
[root@localhost download]# cd openssl-1.0.1c
[root@localhost openssl-1.0.1c]# tar -zxvf openssl-1.0.1c.tar.gz

5、下載nginx

[root@localhost download]# wget http://nginx.org/download/nginx-1.2.8.tar.gz
[root@localhost download]# tar -zxvf nginx-1.2.8.tar.gz

6、安裝

[root@localhost download]mv pcre-8.36 /usr/local/
[root@localhost download]mv zlib-1.2.8 /usr/local/
[root@localhost download]mv openssl-1.0.1c /usr/local/
[root@localhost download]mv nginx-1.2.8 /usr/local/
[root@localhost download]cd /usr/local/
[root@localhost local]# cd pcre-8.36
[root@localhost pcre-8.36]# ./configure&&make&&make install
[root@localhost pcre-8.36] cd ../zlib-1.2.8
[root@localhost zlib-1.2.8]# ./configure && make && make install
[root@localhost zlib-1.2.8]# cd ../openssl-1.0.1c
[root@localhost openssl-1.0.1c]# ./config && make && make install
[root@localhost openssl-1.0.1c]# cd ../nginx-1.2.8
[root@localhost nginx-1.2.8]# ./configure --prefix=/usr/local/nginx && make && make install

7、啟動(dòng)nginx

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

8、測(cè)試

在確??梢赃B接到服務(wù)器的電腦上,瀏覽器輸入裝了nginx的機(jī)器的ip地址,會(huì)看到welcome to nginx!的提示說明安裝配配置成功了。

CentOS6.5環(huán)境怎么安裝nginx服務(wù)器及配置負(fù)載均衡

9、設(shè)置開機(jī)自動(dòng)啟動(dòng)(shell腳本處理)

[root@localhost logs]# vi /etc/init.d/nginx

添加以下shell腳本。

#!/bin/bash
# nginx startup script for the nginx http server
# it is v.0.0.2 version.
# 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: /var/run/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=/var/run/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

保存后,設(shè)置權(quán)限讓所有人可以操作。

[root@localhost ~]# chmod a+x /etc/init.d/nginx
[root@localhost logs]# /etc/init.d/nginx status
nginx (pid 2417 2416) is running...
[root@localhost logs]# /etc/init.d/nginx stop
stopping nginx: [ ok ]
[root@localhost logs]# /etc/init.d/nginx start
starting nginx: [ ok ]
[root@localhost logs]# /etc/init.d/nginx status
nginx (pid 2454 2452) is running...
[root@localhost ~]# vi /etc/rc.local

添加:

/etc/init.d/nginx start

重啟電腦就會(huì)生效。

注意:腳本里面關(guān)于啟動(dòng)目錄的,如果你的安裝地址不一樣,要進(jìn)行修改。

負(fù)載均衡配置:

現(xiàn)有兩部服務(wù)器:

192.168.137.197     (按照以上操作安裝有nginx,作為轉(zhuǎn)發(fā)機(jī),虛擬機(jī))
192.168.137.33     (無安裝nginx,運(yùn)行有測(cè)試用的web工程,虛擬機(jī))
10.10.33.59    (無安裝nginx,運(yùn)行有測(cè)試用的web工程,本地電腦地址)
,運(yùn)行有測(cè)試用的web工程
127.0.0.1 web_app


[root@a conf]# vi /usr/local/nginx/conf/nginx.conf

配置如下展示:

worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
 include    mime.types;
 default_type application/octet-stream;
 fastcgi_intercept_errors on;
 charset utf-8;
 server_names_hash_bucket_size 128;
 client_header_buffer_size 4k;
 large_client_header_buffers 4 32k;
 client_max_body_size 300m;
 sendfile on;
 tcp_nopush   on;
 keepalive_timeout 60;
 tcp_nodelay on;
 client_body_buffer_size 512k;
 proxy_connect_timeout  5;
 proxy_read_timeout    60;
 proxy_send_timeout    5;
 proxy_buffer_size    16k;
 proxy_buffers      4 64k;
 proxy_busy_buffers_size 128k;
 proxy_temp_file_write_size 128k;
 gzip on;
 gzip_min_length 1k;
 gzip_buffers   4 16k;
 gzip_http_version 1.1;
 gzip_comp_level 2;
 gzip_types    text/plain application/x-javascript text/css application/xml;
 gzip_vary on;
###2012-12-19 change nginx logs
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
       '$status $body_bytes_sent "$http_referer" '
       '"$http_user_agent" $request_time $remote_addr';
upstream web_app {
server 192.168.137.197:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.137.33:8080 weight=1 max_fails=2 fail_timeout=30s;
server 10.10.33.59:8080 weight=1 max_fails=2 fail_timeout=30s;
}
####chinaapp.sinaapp.com
server {
  listen 80;
  server_name chinaapp.sinaapp.com;
  index index.jsp index.html index.htm;
  #發(fā)布目錄/data/www
  root /data/www;
  location /
  {
  proxy_next_upstream http_502 http_504 error timeout invalid_header;
  proxy_set_header host $host;
  proxy_set_header x-real-ip $remote_addr;
  proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
  proxy_pass http://web_app;
  expires   3d;
  }
 }
}

設(shè)置host:

[root@a conf]# vi /etc/hosts

添加hosts:

127.0.0.1 web_app

啟動(dòng)nginx

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

啟動(dòng)三部服務(wù)器的web應(yīng)用:

在192.168.137.197執(zhí)行指令:

[root@a program]# curl "http://web_app/index.jsp"

測(cè)試結(jié)果如下:

CentOS6.5環(huán)境怎么安裝nginx服務(wù)器及配置負(fù)載均衡

常見問題及解決辦法:

1、安裝pcre時(shí)提示configure: error: you need a c++ compiler for c++ support.

原因是沒有安裝c++編譯器,采用下面的命令安裝:

[root@localhost pcre-8.37]# yum install -y gcc gcc-c++

2、啟動(dòng)nginx失敗

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: no such file or directory

原因是64bit的系統(tǒng),但是默認(rèn)取了/usr/local/lib里面的包

檢查方法:

[root@localhost nginx]# cd /usr/local/
[root@localhost local]# ls /lib64/ |grep pcre
libpcre.so.0
libpcre.so.0.0.1
[root@localhost local]# ls /lib/ |grep pcre

說明缺失的包在lib64

設(shè)置軟連接來解決:

[root@localhost local]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
[root@localhost local]# cd nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

3、啟動(dòng)報(bào)錯(cuò):

報(bào)錯(cuò):nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

解決辦法:sudo fuser -k 80/tcp

端口被占用,關(guān)閉占用端口

“CentOS6.5環(huán)境怎么安裝nginx服務(wù)器及配置負(fù)載均衡”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

分享標(biāo)題:CentOS6.5環(huán)境怎么安裝nginx服務(wù)器及配置負(fù)載均衡-創(chuàng)新互聯(lián)
文章鏈接:http://muchs.cn/article48/dchchp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈Google、網(wǎng)站維護(hù)、響應(yīng)式網(wǎng)站、App設(shè)計(jì)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)