欢迎光临
我们一直在努力

Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用

本站教程收集整理的这篇文章主要介绍了Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用,本站教程本站觉得挺不错的,现在分享给大家,也给大家做个参考。

Nginx+Keepalived+@R_696_7949@ 源码

第一部分 环境准备
一:Nginx+keepalived服务器两台(调度器,双机热备)
系统:Linux—CentOS7.4
IP地址:192.168.80.100(lvs01)
192.168.80.101(lvs02)
软件需求:Nginx安装包(Nginx-1.13.9.tar.gz)
Keepalived安装包(keepalived-1.4.2.tar.gz)
二:@R_696_7949@服务器两台(服务器池)
系统:Linux—CentOS7.4
IP地址:192.168.80.102(TM01)
192.168.80.103(TM02)
软件需求:java环境jdk包(jdk-8u144-linux-x64.tar.gz)
@R_696_7949@安装包(apache-@R_696_7949@-8.5.23.tar.gz)

192.168.80.100

1、源码安装Nginx

yum install -y \        //安装编译工具及插件
 gcc  gcc-c++  make  openssl-devel  zlib-devel \

useradd -s /sbin/nologin -M Nginx --创建一个不能登录服务器,并且没有家目录的用户

tar vxf Nginx-?.tar.gz ---解压Nginx软件包

cd Nginx-1.13.9 --切换到解压目录里

./configure \               //个性化配置
 --user=Nginx --group=Nginx --with-file-aio  --with-http_flv_module  --with-http_stub_status_module  --with-http_ssl_module  --with-http_gzip_static_module  --with-http_realip_module

@H_52_25@make && make install –编译且安装

vi /usr/local/Nginx/conf/Nginx.conf --Nginx主配置文件

user Nginx Nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

pid        logs/Nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_for@R_262_10225@ed_for"‘;

    access_log  logs/access.log  main;
        add_header X-Server $hostname;
        server_names_hash_bucket_size 128;
        server_name_in_redirect off;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;

    #keepalive_timeout  0;
    keepalive_timeout  60;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 128k;
        client_max_body_size 512m;
        open_file_cache max=65535 inactive=20s;
        open_file_cache_valid 30s;
        open_file_cache_min_uses 1;

    gzip  on;
        gzip_static on;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_min_length 1024;
        gzip_vary on;
        gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+RSS;

server_tokens off;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 512k;
        fastcgi_buffers 6 512k;
        fastcgi_busy_buffers_size 512k;
        fastcgi_temp_file_write_size 512k;
        fastcgi_intercept_errors on;
        clienT_Body_buffer_size 128k;

        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        proxy_buffer_size 32k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 2m;
        proxy_ignore_client_abort on;
        proxy_cache_path /usr/local/Nginx/cache_temp levels=2:2

keys_zone=cache_temp:128m inactive=30m max_size=2g;
        proxy_cache_valid 200 302 10m;
        include /usr/local/Nginx/conf/conf.d/*.conf;

    server {
        listen       80;
        server_name  localhost;

        charset UTF-8;

        #access_log  logs/host.access.log  main;

        LOCATIOn / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        LOCATIOn = /50x.html {
            root   html;
        }

        # proxy the php scripts to Apache listening on 127.0.0.1:80
        #
        #LOCATIOn ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the php scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #LOCATIOn ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILename  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files,if Apache‘s document root
        # concurs with Nginx‘s one
        #
        #LOCATIOn ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-,name-,and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    LOCATIOn / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # httpS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1R_551_11845@;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    LOCATIOn / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
cd /usr/local/Nginx/conf/    --切换到Nginx配置文件目录中
mkdir conf.d  --创建一个 conf.d的目录
cd conf.d/
vi lvs01.conf          //新建子配置文件
server {
        listen 80;
        server_name lvs01 192.168.80.100;       //服务器名称与IP地址
        index index.html index.jsp;
        root /usr/local/Nginx/html;
        access_log /usr/local/Nginx/logs/@R_696_7949@.aa.com_access.log main;
        LOCATIOn ~ .*\.jsp$ {
        index index.jsp;
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Client-IP $remote_addr;
        proxy_set_header X-For $proxy_add_x_for@R_262_10225@ed_for;
        proxy_pass http://center_pool;
        }
        LOCATIOn ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 30d;
        proxy_pass http://center_pool;
        }
        LOCATIOn ~ .*\.(js|css)?$ {
        expires 1h;
        proxy_pass http://center_pool;

        }
}

vi pool.conf       //创建服务器池
upstream center_pool {               //默认轮询
        server 192.168.80.102:8080;
        server 192.168.80.103:8080;
}
//制作启动脚本
[[email?protected] conf.d]# vi /etc/init.d/Nginx
#!/bin/bash
#chkconfig: 35 99 20
#description: Nginx service Control Script
PROG="/usr/local/Nginx/sbin/美国高防vpsNginx"
PIDF="/usr/local/Nginx/logs/Nginx.pid"
case "$1" in
  start)
    $PROG
    ;;
  stop)
    kill -s QUIT $(cat $PIDF)
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  reload)
    kill -s HUP $(cat $PIDF)
    ;;
  *)
        echo "Usage: $0 {start|stop|restart|reloaD}"
        exit 1
esac
exit 0
chmod +x /etc/init.d/Nginx        //增加执行权限
chkconfig --add Nginx             //加入系统管理服务
service Nginx start               //启动Nginx服务
netstat -anpt | grep 80

2、源码编译keepalived

yum -y install \         //安装插件
> popt-devel > kernel-devel > openssl-devel
tar xvf keepalived-1.4.2.tar.gz   --解压keepalived压缩包
 cd keepalived-1.4.2            --切换到解压目录
 ./configure --prefix=/           --配置
 make && make install        --编译与安装

cp keepalived/etc/init.d/keepalived /etc/init.d/

//以下编辑keepalived配置文件
cd /etc/keepalived/
vi keepalived.conf

! Configuration File for keepalived
global_defs {
        route_id Nginx-01
   }
vrrp_script Nginx {
        script "/opt/Nginx.sh"
        interval 2
        weight -10
}
vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        Nginx
        }
    virtual_ipaddress {
        192.168.80.100
    }
}
//注意,主备的优先级配置的相差50

创建脚本文件
判断keepalived进程是否存在,在就启动Nginx不在就关闭
vi /opt/Nginx.sh

#!/bin/bash 
#Filename:Nginx.sh
A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
        /etc/init.d/Nginx start
else
        /etc/init.d/Nginx stop
fi

chmod +x /opt/Nginx.sh --给脚本文件加执行权限

service keepalived start --启动keepalived,会连Nginx一块启动

ip addr show dev ens32 --查看漂移地址是否生成

---------------测试验证------------
[[email?protected] keepalived]# systemctl stop keepalived    //关闭keepalived服务
[[email?protected] keepalived]# killall -9 Nginx              //关闭nignx服务
[[email?protected] keepalived]# netstat -anpt | grep 80       //80端口已停止运行

[[email?protected] keepalived]# systemctl start keepalived     //开启keepalived服务
[[email?protected] keepalived]# netstat -anpt | grep 80       //Nginx随keepalived启动

———————配置从服务器——————————–
1、源码编译安装Nginx
从服务器源码安装Nginx,和主服务器一样,根据主服务器操作在做一遍
需要修改的地方

2、源码安装keepalived
操作和主服务器相同,在写到Nginx.sh脚本文件时不同,其它的操作一模一样。

#!/bin/bash
#Filename:Nginx.sh
A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
        /etc/init.d/Nginx start
else
        /etc/init.d/Nginx stop
fi

chmod +x /opt/Nginx.sh --给脚本执行权限

service keepalived start --启动 keepalived

二:模拟主服务故障

  1. 主服务器

killall 命令需要安装 : yum install psmisc -y
//80端口已不运行
[[email?protected] ~]# ip addr show dev ens32

//漂移地址消失,不工作

  1. 从服务器
    [[email?protected] ~]# ip addr show dev ens32 //查看漂移地址

    //漂移地址自动生成
    [[email?protected] ~]# netstat -anpt | grep 80

  2. 主服务器
    [[email?protected] ~]# service keepalived start
    [[email?protected] ~]# ip addr show dev ens33

[[email?protected] ~]# netstat -anpt | grep 80

//主服务器已恢复工作

  1. 从服务器
    [[email?protected] ~]# ip addr show dev ens33

//漂移地址自动移除
[[email?protected] ~]# netstat -anpt | grep 80

//Nginx自动停止服务
//双机热备验证成功

#  192.168.80.102

第三部分  部署服务器池—搭建@R_696_7949@
//以下在两台@R_696_7949@服务器上操作
第一步:部署第一个节点服务器TM01(192.168.80.102)
----------部署java环境,jdk---------

[[email?protected] ~]# tar xvf jdk-8u144-linux-x64.tar.gz //解压jdk
[[email?protected] ~]# cp -r jdk1.8.0_144/ /usr/local/java //创建java源目录
[[email?protected] ~]# vi /etc/profile
最后添加以下内容:
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
保存退出
[[email?protected] ~]# source /etc/profile //立即生效
[[email?protected] ~]# java –version //查看版本

java环境部署完成

———-部署@R_696_7949@———-
[[email?protected] ~]# tar xvf apache-@R_696_7949@-8.5.23.tar.gz //解压apache-@R_696_7949@
[[email?protected] ~]# cp -r apache-@R_696_7949@-8.5.23 /usr/local/@R_696_7949@8 //创建@R_696_7949@源目录
//做个软链接,使@R_696_7949@开启与关闭更加方便
[[email?protected] ~]# ln -s /usr/local/@R_696_7949@8/bin/startup.sh /usr/bin/@R_696_7949@up
[[email?protected] ~]# ln -s /usr/local/@R_696_7949@8/bin/shutdown.sh /usr/bin/@R_696_7949@down
[[email?protected] ~]# @R_696_7949@up //开启@R_696_7949@

[[email?protected] ~]# netstat -anpt | grep 8080

-----------验证------------
 1. Win7下访问默认主页:http://192.168.80.102:8080
 ![](https://s1.51cto.com/images/blog/201901/11/09e2caf1242ACBb82147737b8b7dca10.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
 //@R_696_7949@部署成功
 2. 服务器池中有两台@R_696_7949@服务器,为了便于识别,主页上添加点标记
 [[email?protected] ~]# vi /usr/local/@R_696_7949@8/webapps/ROOT/index.jsp
 添加一行内容:
 ![](https://s1.51cto.com/images/blog/201901/11/2e8898788728c2c24a27e827b56f5b85.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk=)
 保存退出
 [[email?protected] ~]# @R_696_7949@down
 [[email?protected] ~]# @R_696_7949@up       //重启
//再次访问默认主页http://192.168.80.102:8080
 ![](https://s1.51cto.com/images/blog/201901/11/502b68e27abeed12fd200763ccf04f65.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk=)

 192.168.80.103

提示:操作与192.168.80.102基本一样
需要修改的地方就是把 SERVER  AA  改成  SERVER BB j 就可以了

 --------测试------
 地址栏里输入:192.168.80.188:8080,点击刷新按钮查看是否动态轮询。

本站总结

以上是本站教程为你收集整理的Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用全部内容,希望文章能够帮你解决Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用所遇到的程序开发问题。

如果觉得本站教程网站内容还不错,欢迎将本站教程推荐给好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。