Apache2.4.29+Tomcat 8.5.29负载均衡群集最强解析

公司是做电商的,线上的应用服务器大多都是Apache+Tomcat或者Nginx+Tomcat ,虽然之前也对Apache和Nginx的配置文件熟悉,可对这种负载均衡群集配置还不是太熟悉,而日常工作经常需要搞这些东西,所以下决心搞明白哈哈哦。结合公司的线上配置,结合官方的文档说明,特整理如下,方便以后查阅,毕竟好记性不如烂笔头。

一:本文涉及的相关环境和软件版本

IP :192.168.86.129/24 ,通过NAT模式同宿主机相连

CentOS-7.4-minimal、http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso

Apache/2.4.29 (Unix)、http://httpd.apache.org/download.cgi#apache24

Apr/1.6.3https://apr.apache.org/download.cgi#aprutil1

Apr-util/1.6.1、https://apr.apache.org/download.cgi#aprutil1

Tomcat/8.5.29、https://tomcat.apache.org/download-80.cgi

jdk-8u161-linux-x64、http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

tomcat-connectors-1.2.43 http://tomcat.apache.org/download-connectors.cgi

二:安装基础环境(本文安装包均提前下载放在/opt下)

[[email protected] opt]# yum -y install wget unzip lrzsz openssl openssl-devel autoconf libtool gcc*

执行yum命令时若出现这个错误:Error: database disk image is malformed

这是说明yum的缓存出错,需要清理缓存,执行yum clean dbcache命令即可

配置环境时configure的主要工作就是生成Makefile,编译源码时的make命令就是根据Makefile来进行编译

1.安装apr/apr-util/pcre

[[email protected] opt]# rpm -q apr

apr-1.4.8-3.el7_4.1.x86_64

[[email protected] opt]# rpm -q apr-util

apr-util-1.5.2-6.el7.x86_64

[[email protected] opt]# rpm -e apr --nodeps

[[email protected] opt]# rpm -e apr-util --nodeps

[[email protected] opt]# tar zxvf apr-1.6.3.tar.gz

[[email protected] opt]# cd apr-1.6.3

[[email protected] apr-1.6.3]# ./configure --prefix=/usr/local/apr

报错如下图

blob.png

网上的方法:http://www.noobyard.com/article/p-suwrplhz-dh.html$RM "$cfgfile这一行注释,在configure文件30976行

[[email protected] apr-1.6.3]# make && make install

成功会有以下提示

/usr/bin/install -c -m 755 /opt/apr-1.6.3/build/mkdir.sh /usr/local/apr/build-1

for f in make_exports.awk make_var_export.awk; do \

/usr/bin/install -c -m 644 /opt/apr-1.6.3/build/${f} /usr/local/apr/build-1; \

done

/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk

/usr/bin/install -c -m 644 /opt/apr-1.6.3/build/apr_common.m4 /usr/local/apr/build-1

/usr/bin/install -c -m 644 /opt/apr-1.6.3/build/find_apr.m4 /usr/local/apr/build-1

/usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config

这时候/usr/local下就会有apr的目录文件

[[email protected] apr-1.6.3]# cd ../

[[email protected] opt]# tar zxvf apr-util-1.6.1.tar.gz

[[email protected] opt]# cd apr-util-1.61

[[email protected] apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr/

[[email protected] apr-util-1.6.1]# make && make install

发现报错fatal error: expat.h: No such file or directory

解决办法:根据提示安装expat包,yum -y install expat-devel,之后再重新编译。

blob.png

成功会有以下提示

----------------------------------------------------------------------

Libraries have been installed in:

/usr/local/apr-util//lib

If you ever happen to want to link against installed libraries

in a given directory, LIBDIR, you must either use libtool, and

specify the full pathname of the library, or use the '-LLIBDIR'

flag during linking and do at least one of the following:

- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable

during execution

- add LIBDIR to the 'LD_RUN_PATH' environment variable

during linking

- use the '-Wl,-rpath -Wl,LIBDIR' linker flag

- have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for

more information, such as the ld(1) and ld.so(8) manual pages.

----------------------------------------------------------------------

/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util//lib

/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util//bin/apu-1-config

[[email protected] apr-util-1.6.1]# cd ..

[[email protected] opt]# cd pcre-8.41

[[email protected] pcre-8.41]# ./configure --prefix=/usr/local/pcre

[[email protected] pcre-8.41]# make  && make install

成功会有以下提示

ln -sf pcre_get_substring_list.3 /usr/local/pcre/share/man/man3/pcre32_get_substring_list.3

ln -sf pcre_jit_exec.3 /usr/local/pcre/share/man/man3/pcre32_jit_exec.3

ln -sf pcre_jit_stack_alloc.3 /usr/local/pcre/share/man/man3/pcre32_jit_stack_alloc.3

ln -sf pcre_jit_stack_free.3 /usr/local/pcre/share/man/man3/pcre32_jit_stack_free.3

ln -sf pcre_maketables.3 /usr/local/pcre/share/man/man3/pcre32_maketables.3

ln -sf pcre_pattern_to_host_byte_order.3 /usr/local/pcre/share/man/man3/pcre32_pattern_to_host_byte_order.3

ln -sf pcre_refcount.3 /usr/local/pcre/share/man/man3/pcre32_refcount.3

ln -sf pcre_study.3 /usr/local/pcre/share/man/man3/pcre32_study.3

ln -sf pcre_utf32_to_host_byte_order.3 /usr/local/pcre/share/man/man3/pcre32_utf32_to_host_byte_order.3

ln -sf pcre_version.3 /usr/local/pcre/share/man/man3/pcre32_version.3

make[3]: Leaving directory `/opt/pcre-8.41'

make[2]: Leaving directory `/opt/pcre-8.41'

make[1]: Leaving directory `/opt/pcre-8.41'

至于为什么要安装这些东西,请看官方文档

blob.png

2.安装apache

最好以root安装和启动Apache,以root运行之后,apache就会把它的派生进程切换到非root用户

[[email protected] pcre-8.41]# cd ..

[[email protected] opt]# tar zxvf httpd-2.4.29.tar.gz

[[email protected] opt]# cd httpd-2.4.29ith-apr=

[[email protected] httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-cgi --enable-ssl --enable-rewrite --enable-proxy --enable-proxy-http --enable-proxy-ajp --enable-proxy-banlancer --with-zlib --w/usr/local/apr --with-apr-util=/usr/local/apr-util --with-ssl=/etc/pki/tls  --enable-mods-shared=all --enable-mpms-shared=all --with-mpm=event --with-pcre=/usr/local/pcre

简单解释如下

--prefix=/usr/local/httpd 安装到此文件夹

--sysconfdir=/etc/httpd2.4  指定配置文件路径       

--enable-so  启动模块动态装卸载        

--enable-ssl 编译ssl模块        

--enable-cgi 支持cgi机制(能够让静态web服务器能够解析动态请求的一个协议)        

--enable-rewrite  支持url重写            

--with-zlib  支持数据包压缩              

--with-pcre  支持正则表达式        

--with-apr=/usr/local/apr  指明依赖的apr所在目录        

--with-apr-util=/usr/local/apr-util/  指明依赖的apr-util所在的目录        

--enable-modules=most      用most可以将一些不常用的,不在缺省常用模块中的模块编译进来

--enable-mods-shared=all 意思是动态加载所有模块,如果去掉-shared话,是静态加载所有模块        

--enable-mpms-shared=all   以共享方式编译的模块        

--with-mpm=event        指明httpd的工作方式为event

查看apache有那几个mpm

# ./configure --help|grep mpm

查看apache可以加载那些模块

./configure --help|grep enable

官网的解释如下

blob.png

编译参数--with-ssl可以指明路径也可以不指,如果要指,可以通过下面命令openssl version -a查看

编译为三种都支持:--enable-mpms-shared=all,这样在编译的时候会在modules目录下自动编译出三个MPM文件的so,然后通过修改httpd.conf配置文件更改MPM(去掉LoadModule mpm_event_module modules/mod_mpm_*.so相应的模块注释)

在apache的早期版本2.0默认prefork,2.2版本是worker,2.4版本是event

[[email protected] ~]# openssl version -a

OpenSSL 1.0.2k-fips  26 Jan 2017

built on: reproducible build, date unspecified

platform: linux-x86_64

options:  bn(64,64) md2(int) rc4(16x,int) des(idx,cisc,16,int) idea(int) blowfish(idx)

compiler: gcc -I. -I.. -I../include  -fPIC -DOPENSSL_PIC -DZLIB -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DKRB5_MIT -m64 -DL_ENDIAN -Wall -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic -Wa,--noexecstack -DPURIFY -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DRC4_ASM -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM -DECP_NISTZ256_ASM

OPENSSLDIR: "/etc/pki/tls"

engines:  rdrand dynamic

blob.png

[[email protected] httpd-2.4.29]# make && make install

成功会有如下提示

mkdir /usr/local/httpd/icons

mkdir /usr/local/httpd/logs

Installing CGIs

mkdir /usr/local/httpd/cgi-bin

Installing header files

mkdir /usr/local/httpd/include

Installing build system files

mkdir /usr/local/httpd/build

Installing man pages and online manual

mkdir /usr/local/httpd/man

mkdir /usr/local/httpd/man/man1

mkdir /usr/local/httpd/man/man8

mkdir /usr/local/httpd/manual

make[1]: Leaving directory `/opt/httpd-2.4.29'

3.安装connectors

[[email protected] opt]# tar zxvf tomcat-connectors-1.2.43-src.tar.gz

[[email protected] opt]# cp -ar tomcat-connectors-1.2.43-src /usr/local/

[[email protected] opt]# cd /usr.local/tomcat-connectors-1.2.43-src

[[email protected] tomcat-connectors-1.2.43-src]# cd native/

[[email protected] native]# ./buildconf.sh

buildconf: checking installation...

buildconf: autoconf version 2.69 (ok)

buildconf: libtool  version 2.4.2 (ok)

buildconf: libtoolize --automake --copy

buildconf: aclocal

buildconf: autoheader

buildconf: automake -a --foreign --copy

buildconf: autoconf

[[email protected] native]# ./configure --with-apxs=/usr/local/httpd/bin/apxs

[[email protected] native]# make && make install

编译后红箭头的文件变大,这是我自己发现的,哈哈哦

blob.png

[[email protected] native]# cd apache-2.0/

[[email protected] apache-2.0]# ls

bldjk54.qclsrc  config.m4  Makefile.apxs     Makefile.in  mod_jk.a  mod_jk.la  mod_jk.o   NWGNUmakefile

bldjk.qclsrc    Makefile   Makefile.apxs.in  Makefile.vc  mod_jk.c  mod_jk.lo  mod_jk.so

[[email protected] apache-2.0]# cp mod_jk.so /usr/local/httpd/modules/

cp: overwrite ‘/usr/local/httpd/modules/mod_jk.so’? y

blob.png

4.安装tomcat/jdk

[[email protected] apache-2.0]# cd /opt

[[email protected] opt]# tar zxvf apache-tomcat-8.5.29.tar.gz

[[email protected] opt]# cp -ar apache-tomcat-8.5.29 /usr/local/apache-tomcat8

[[email protected] opt]# tar zxvf jdk-8u161-linux-x64.tar.gz

[[email protected] opt]# cp -ar jdk1.8.0_161/ /usr/local/

配置全局环境变量 vi /etc/profile

export JAVA_HOME=/usr/local/jdk1.8.0_161

export JRE_HOME=/usr/local/jdk1.8.0_161/jre

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/jdk1.8.0_161/bin

export CLASSPATH=./:/usr/local/jdk1.8.0_161/lib:/usr/local/jdk1.8.0_161/jre/lib

刷新变量使其生效source /etc/profile

至此搞一段落,基础软件已安装完毕。