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

Nginx+Keepalived+Tomcat 源码

第一部分 环境准备
一: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)
二:tomcat服务器两台(服务器池)
系统:Linux—CentOS7.4
IP地址:192.168.80.102(TM01)
192.168.80.103(TM02)
软件需求:java环境jdk包(jdk-8u144-linux-x64.tar.gz)
tomcat安装包(apache-tomcat-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

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_forwarded_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:1m;
    #    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/tomcat.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_forwarded_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/nginx"
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启动

192.168.80.101

---------------------配置从服务器--------------------------------
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

----------测试验证----------
一:查看从服务器状态
[[email protected] ~]# ip addr show dev ens33 //查看漂移地址

//由于主服务器在运行,漂移地址并未同步过来

//当主服务器在运行的时候,从服务器虚拟地址并未生成,nginx服务并未随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

第三部分  部署服务器池—搭建Tomcat
//以下在两台tomcat服务器上操作
第一步:部署第一个节点服务器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环境部署完成

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

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

 192.168.80.103

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

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

原文地址:http://blog.51cto.com/13701082/2341446

时间: 2024-10-15 07:44:48

Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用的相关文章

LNMP架构的搭建(nginx+mysql+php源码安装 )+Discuz论坛的搭建

主机环境 redhat6.5 64位 实验环境 服务端 ip172.25.29.1  nginx+mysql+php 安装包   nginx-1.10.1.tar.gz         mysql-boost-5.7.11.tar.gz    cmake-2.8.12.2-4.el6.x86_64.rpm php-5.6.20.tar.bz2         re2c-0.13.5-1.el6.x86_64.rpm         libmcrypt-2.5.8-9.el6.x86_64.rpm

ubuntu 14.04 nginx + mysql + php源码安装

本文参考了 http://www.tuicool.com/articles/3iUNFnA  并在细节上做了一些补充.使安装过程更加顺畅. 首先要说的是ubuntu 上操作需要加sudo命令,所以在以下的命令上都要以sudo开始. 先安装一些依赖资源gcc  g++ openssl  libssl-dev (RedHat.centos才是openssl-devel) sudo apt-get install openssl sudo apt-get install libssl-dev sudo

Centos 6.3下使用源码安装Mysql 5.7.10

最近想尝试下新版本的Mysql 5.7.10,于是开始尝试玩下. 在该次安装中,使用源码编译安装Mysql,编译器使用cmake.这里选择的版本是mysql-5.7.10,cmake的版本是cmake-2.8.10.2注:在下载源码包的时候,请注意选择Platform为“Source Code”.环境:[[email protected] mysql]$ sudo cat /etc/redhat-releaseCentOS release 6.3 (Final)[[email protected

亲测CentOS 6.6 x86_64下源码安装LAMP平台(APACHE 2.4.16+MYSQL 5.6.17+PHP 5.6.7)

一.编译安装apache 1.安装编译环境: #yum -y groupinstall 'Development tools' #yum -y groupinstall 'Desktop Platform Development' #yum install wget gcc gcc-c++ make re2c curl curl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel libmcrypt libm

CentOs下MySQL5.6.32源码安装

1. 安装好--安装MySQL需要的包 yum install -y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio libaio-devel bzr bison ncurses5-devel ncurses-devel autoconf 2. [[email protected] ~]# cd /usr/local/src //下载MySQL源码包 [[email protected] src

centos下配置LNMP环境(源码安装)

准备工作,安装依赖库 yum -y install gcc automake autoconf libtool make gcc-c++ glibc libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncu

Centos 7.0 编译安装LNMP(Linxu+nginx+mysql+php)之源码安装nginx (一)

nginx简介:       Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日. 其将源代码以类BSD许可证的形式发布,因它的稳定性.丰富的功能集.示例配置文件和低系统资源的消耗而闻名.2011年6月1日,nginx 1.0.4发布. Nginx是一款轻量级的Web 服务器

CentOS 6.5 (32bit) + Nginx 1.8.0 + php-fpm 源码安装从零开始

CentOS 6.5 minimal安装不再赘述 Nginx源码安装 1.安装wget下载程序 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) cd

CentOS源码安装 Tomcat/8.0.24

依个人的习惯,喜欢将源码安装在/usr/local这个目录下面: 第一步:下载源码 wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.24/bin/apache-tomcat-8.0.24.tar.gz 第二步:解压源码: tar -zxvf apache-tomcat-8.0.24.tar.gz mv apache-tomcat-8.0.24 /usr/local/tomcat 第三步:启动tomcat cd /usr/local