Build practical apache and tomcat in load balancing environment
Linux basic environment
The load balancer environment needs to be built under linux, so it is necessary to have a basic linux system. It is recommended that you install it according to the basic linux environment in http://edu.51cto.com/course/10209.html, so that you can avoid detours.
Installation of Apache
1. Download the http2.4 version and extract the http package
2 、 / configure-- prefix=/opt/apache2/-- with--mpm=worker-- enable-so-- enable-mods-shared=all-- enable-forward=shared-- enable-speling=shared-- enable-rewrite=shared-- enable-proxy=shared-- enable-proxy-ajp=shared-- enable-dav=shared-- enable-mod_proxy_http=shared-- enable-mod_proxy_ftp=shared-- enable-mod_proxy_ajp=shared-- enable-mod_proxy_balancer=shared-- enable-mod_proxy_connect=shared-- with-apr=/opt/apr-- with- Apr-util=/opt/apr-util-with-pcre=/opt/pcre
3. Make & & make install
PS: if there is a problem with running the command, you can usually solve the google error by yourself.
JDK installation
1. Download the corresponding version of Java1. 7
A) phenomenon: how to view the Linux version:
U name-m: 32-bit kernel if i386 or i686
2. Install Java1. seven
Download the rpm package, and then chmod gives 777 permissions before installing
3. Configure JDK environment variables
A 、 v i / etc/profile
Add at the end:
Export JAVA_HOME=/usr/java/jdk1.7.0_60
Export CLASSPATH=.:$JAVA_HOME/lib
Export PATH=$JAVA_HOME/bin:$PATH
B, source / etc/profile: reload environment variables
4. Java-version: verify whether the installation is successful
PS: I don't know where to download jdk. You can download it here: http://www.xqtesting.com/blog/jdk-103.html
Installation of Tomcat
Tomcat uses 7.0. installation and cluster configuration:
1. Decompress tomcat
2. Mv becomes tomcat1
3. Mv to / opt
4. Cp-r tomcat1 tomcat2
5. Write vi / etc/profile to export TOMCAT_HOME=/ opt / tomcat1
6. The server.xml file of tomcat1 remains the default, and the tomcat2 file is modified as follows
I. Vi / opt / tomcat 2 / conf/server.xml
ii. / Server port=8005 is changed to 9005 in command mode in VI
iii. / Connector port=8080 is changed to 9090 in command mode in VI
iv. / AJP=8009 is changed to 9009 in command mode in VI
7. Enter http://192.168.0.98:8080 and http://192.168.0.98:9090 on IE
You can see the following interface on the success!
Modify the http.conf file
Add: at the end of the httpd.conf:
I nclude / opt / apache2/conf/mod_jk.conf
Install mod_jk.conf
1. Go to http://www.apache.org/dist/tomcat/tomcat-connectors/jk/ to download
Tomcat-connectors-1.2.40-src.tar.gz
2 、 tar xzvf tomcat-connectors-1.2.40-src.tar.gz
3 、 mv tomcat-connectors-1.2.40-src / usr/local
4 、 cd / usr/local/tomcat-connectors-1.2.40-src/native
5. / configure-- with-java-home=/usr/java/jdk1.7.0_60-- with-apxs=/ opt / apache2/bin/apxs
6. Make & & make install
7 、 cp apache-2.0/mod_jk.so / opt / apache2/modules/
8. With the following picture, it will be a success:
Configure mod_jk.conf
Create the mod_jk.conf file under / opt / apache2/conf and write the following:
# load mod_jk Module
LoadModule jk_module / opt/apache2/modules/mod_jk.so
# load workers in the cluster
JkWorkersFile / opt/apache2/conf/workers.properties
# request processing allocation file for loading workers
JkMountFile / opt/apache2/conf/uriworkermap.properties
# specify the log output file of jk
# JkLogFile / opt/apache2/logs/mod_jk.log
# specify log level
# JkLogLevel warn
# specify the format of the log
JkLogStampFormat "[a% b% d% H:%M:%S% Y]"
# specify which requests are to be processed by tomcat. "controller" is the name of the load distribution controller specified in workers.propertise
JkMount / *. * controller
Configure the workers.properties file
Create the workers.properties file under / opt / apache2/conf. Configuration content:
# server list
Worker.list=controller,tomcat1,tomcat2
# = tomcat1=
Worker.tomcat1.port=8009 # ajp13 port number, which is configured in server.xml under tomcat. Default is 8009 (same as the AJP of your tomcat1)
The host address of worker.tomcat1.host=localhost # tomcat. If it is not local, enter the ip address.
Worker.tomcat1.type=ajp13
The weighted proportion of worker.tomcat1.lbfactor=1 # server. The higher the value, the more requests will be allocated.
# = tomcat2=
Worker.tomcat2.port=9009 # ajp13 port number, which is configured in server.xml under tomcat. Default is 8009 (same as the AJP of your tomcat2)
The host address of worker.tomcat2.host=localhost # tomcat. If it is not local, enter the ip address.
Worker.tomcat2.type=ajp13
The weighted proportion of worker.tomcat2.lbfactor=1 # server. The higher the value, the more requests will be allocated.
# = controller, load balancer controller =
Worker.controller.type=lb # server is called controller and is used for load balancer
Worker.retries=3 # retries
Worker.controller.balance_workers=tomcat1,tomcat2
# worker.controller.sticky_session=ture
Configure the uriworkermap.properties file
Create the uriworkermap.properties file under / opt / apache 2 / conf. Configuration content:
# all requests are handled by controller, the server
/ * = controller
# all requests that contain jkstatus are handled by status, the server
/ jkstatus=status
# the "!" here It means "not".
! / * .gif=controller
! / * .jpg=controller
! / * .png=controller
! / * .css=controller
! / * .js=controller
! / * .htm=controller
! / * .html=controller
Verify whether the load balancing is successful
1. Create a new test.jsp in / opt / tomcat1/webapps/test, as follows:
Tomcat 1 test
2. Create an identical test.jsp in / opt / tomcat2/webapps/test, with the following code:
Tomcat 2 test
3. Visit http://192.168.0.98/test/test.jsp and use multiple forcefully to access different tomcat (preferably using Firefox or chrome browser)