Centos 6.5 Install haproxy-nginx

来源:互联网 发布:现代优化设计方法举例 编辑:程序博客网 时间:2024/05/10 21:36
nginx依赖包安装

yum install zlib pcre pcre-devel openssl openssl-devel -y

yum install gcc -y

yum install gcc-c++ -y


创建nginx用户
useradd -s /sbin/nologin nginx

下载nginx
wget http://nginx.org/download/nginx-1.10.0.tar.gz

解压nginx
tar -zxvf nginx-1.10.0.tar.gz

cd / nginx-1.10.0
./configure \
--user=nginx \
--group=nginx \
--prefix=/opt/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/tmp/nginx/client_body \
--http-proxy-temp-path=/tmp/nginx/proxy \
--http-fastcgi-temp-path=/tmp/nginx/fastcgi \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre \
--with-http_realip_module \
--with-http_sub_module

make
make install

#########################################################################################
Nginx 编译参数
--user            指定启动程序所属用户
--group          指定组
--prefix          指定安装路径
--sbin-path    设置nginx二进制文件的路径名
--conf-path    指定配置文件路径
--error-log-path    错误日志文件路径
--http-log-path    指定访问日志文件路径
--http-client-body-temp-path    设置存储HTTP客户端请求主体的临时文件路径
--http-proxy-temp-path            设置存储HTTP代理临时文件的路径
--http-fastcgi-temp-path          设置存储HTTP fastcgi的临时文件的路径
--pid-path          设置nginx.pid文件路径
--lock-path        设置nginx.lock文件路径
--with-openssl    启用SSL
--with-pcre        启用正则表达式
--with-http_stub_status_module    安装可以监控nginx状态的模块
--with-http_ssl_module                启用SSL支持
--with-http_gzip_static_module    启用gzip压缩
#########################################################################################

nginx端口状态
netstat -ntlp | grep nginx


控制nginx服务的命令
1、启动:nginx
2、停止:nginx -s stop
3、退出:nginx -s quit
4、重启:nginx -s reopen
5、重新加载:nginx -s reload
6、平滑启动:kill -HUP pid(kill -HUP `cat /var/run/nginx.pid`)


创建nginx自启动脚本
vi /etc/init.d/nginx
 
#!/bin/bash
# chkconfig: - 18 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
 
NGINX_SBIN="/usr/sbin/nginx"
NGINX_CONF="/etc/nginx/nginx.conf"
NGINX_PID="/var/run/nginx.pid"
RETVAL=0
prog="Nginx"
 
#Source networking configuration
. /etc/sysconfig/network
# Check networking is up
[ ${NETWORKING} = "no" ] && exit 0
[ -x $NGINX_SBIN ] || exit 0
 
start() {
        echo -n $"Starting $prog: "
        touch /var/lock/subsys/nginx
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}
 
stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /var/lock/subsys/nginx /var/run/nginx.pid
        RETVAL=$?
        echo
        return $RETVAL
}
 
reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}
 
restart(){
        stop
        start
}
 
configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
 
exit $RETVAL

创建这个目录不然报错,无法启动服务
mkdir -p /tmp/nginx/client_body

赋予nginx脚本权限
chmod -R 777 /etc/init.d/nginx

设置nginx开机启动
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
service nginx stop

防火墙设置通过nginx
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

查看nginx版本
nginx -v

测试nginx访问
在浏览器输入http://Your-IP/

配置代理
 cd /etc/nginx/

vi nginx.conf
upstream lottery {
        server 10.11.12.143:8080;
        server 10.11.12.139:8080;
    }

 location / {
            root   /var/www/benet;
            index  index.html index.htm;
            proxy_pass http://tomcat;
        }


下载haproxy
cd /usr/local/
wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.9.tar.gz

解压haproxy
tar zxvf haproxy-1.6.9.tar.gz

重命名
mv haproxy-1.6.9 haproxy

cd haproxy

查看系统版本号
uname -a

编译安装
make TARGET=linux26 prefix=/usr/local/haproxy

make install PREFIX=/usr/local/haproxy


配置系统服务
cd /etc/init.d/

创建启动脚本
vim haproxy
#!/bin/bash
### BEGIN INIT INFO
#Manage the HAProxy
### END INIT INFO
#chkconfig: 2345 10 90
#description:haproxy     

bin=/usr/local/haproxy/sbin/haproxy
config=/usr/local/haproxy/conf/haproxy.cfg
pid=/usr/local/haproxy/var/run/haproxy.pid
opts=" -f ${config} -p ${pid} -D -V "
sleep_time=1

start() {
    echo -e "Starting HAProxy......"

    ${bin} ${opts}

    if [ "$?" != "0" ] ; then
        sleep ${sleep_time}
        echo " failed"
        exit 1
    else
        sleep ${sleep_time}
        echo " done"
    fi
}

stop() {
    if [ ! -e ${pid} ] ; then
        echo -e "HAProxy is not running"
        exit 0
    fi

    echo -e "Shutting down HAProxy......"

    kill $(cat ${pid})

    if [ -e ${pid} ] ; then
        rm -f ${pid}
    fi

    if [ "$?" != "0" ] ; then
        sleep ${sleep_time}
        echo " failed"
        exit 1
    else
        sleep ${sleep_time}
        echo " done"
    fi
}
reload(){
        ${bin} -f ${config}  -st $(cat ${pid})
        echo -e "HAProxy is reload......"
}
checkconfig(){
        ${bin} -c -f ${config}
        echo -e "haproxy file is ok"
}
restart() {
    stop
    start
}

case "$1" in
    start)
        start
    ;;

    stop)
        stop
    ;;

    restart)
        restart
    ;;
    reload)
        reload
    ;;
  checkconfig)
        checkconfig
  ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|checkconfig}"
        exit 1
    ;;
esac

赋予权限
chmod -R 777 /etc/init.d/haproxy

设置开机启动
chkconfig haproxy on

查看服务状态
netstat -anplt | grep haproxy

查看服务状态及目录结构
ps -ef |grep haproxy |grep -v grep
0 0
原创粉丝点击