實驗環境:
IP:10.0.0.10
系統:CentOS 6.4
jdk版本:java version "1.8.0_65"
tomcat版本:Apache Tomcat/8.0.24
apache版本:Apache/2.2.27 (Unix)
思路:
1、安裝Apache
2、安裝tomcat
3、安裝mod_jk
4、加載mod_jk
5、關閉tomcat的偵聽端口
6、應用分離(靜态與動态頁面分離)
安裝apache
[root@oracle src]# tar xf httpd-2.2.27.tar.gz
[root@oracle src]# cd httpd-2.2.27
[root@oracle httpd-2.2.27]# ./configure
--prefix=/usr/local/apache2 ---指定安裝路徑
--enable-proxy-balancer ---apache代理balancer子產品
--enable-proxy-http ---apache代理http子產品
--enable-proxy-connect ---apache 代理連接配接子產品
--enable-proxy ---apache代理子產品
--enable-rewrite ---基本的URL操縱規則,即啟用URL重寫規則
--enable-proxy-ajp ---apache代理ajp子產品
[root@oracle httpd-2.2.27]# mkdir -p /data/{www,wwwroot} 建兩個程式家目錄
[root@oracle httpd-2.2.27]# make && make install 編譯安裝
apache配置檔案主檔案:
[root@oracle ROOT]# cat httpd.conf
30:ServerRoot "/usr/local/apache2" ---指定apache程式安裝的家目錄
41:Listen 80 ---指定監聽端口
56:<IfModule !mpm_netware_module>
57:<IfModule !mpm_winnt_module>
66:User daemon ---指定apache運作的使用者
67:Group daemon ---指定apache運作的組
69:</IfModule>
70:</IfModule>
89:ServerAdmin [email protected] ---指定網站管理者郵箱
105:DocumentRoot "/data" ---指定程式家目錄
115:<Directory />
116: Options FollowSymLinks
117: AllowOverride None
118: Order deny,allow
119: Deny from all
120:</Directory>
132:<Directory "/data"> ---指定程式家目錄
145: Options Indexes FollowSymLinks
152: AllowOverride None
157: Order allow,deny
158: Allow from all
160:</Directory>
166:<IfModule dir_module>
167: DirectoryIndex index.html 指定首頁檔案
168:</IfModule>
174:<FilesMatch "^\.ht">
175: Order allow,deny
176: Deny from all
177: Satisfy All
178:</FilesMatch>
187:ErrorLog "logs/error_log" ---指定錯誤日志檔案路徑
194:LogLevel warn ---指定日志級别
196:<IfModule log_config_module>
201: LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
202: LogFormat "%h %l %u %t \"%r\" %>s %b" common
204: <IfModule logio_module>
206: LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
207: </IfModule>
216: CustomLog "logs/access_log" common
223:</IfModule>
225:<IfModule alias_module>
252: ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
254:</IfModule>
256:<IfModule cgid_module>
262:</IfModule>
268:<Directory "/usr/local/apache2/cgi-bin">
269: AllowOverride None
270: Options None
271: Order allow,deny
272: Allow from all
273:</Directory>
284:DefaultType text/plain
286:<IfModule mime_module>
291: TypesConfig conf/mime.types --指定檔案類型
308: AddType application/x-compress .Z ---指定壓縮類型
309: AddType application/x-gzip .gz .tgz ---指定打包類型
332:</IfModule>
396:Include conf/extra/httpd-vhosts.conf ---此處必須打開,否則無法使用虛拟主機
414:<IfModule ssl_module>
415:SSLRandomSeed startup builtin
416:SSLRandomSeed connect builtin
417:</IfModule>
418:LoadModule jk_module modules/mod_jk.so
419:JkWorkersFile /usr/local/apache2/conf/workers.properties --指定負載均衡清單檔案
420:JkMountFile /usr/local/apache2/conf/uriworkermap.properties --指定檔案類型處理
421:JkMountFile /usr/local/apache2/conf/uriworkermap1.properties --指定檔案類型處理
422:JkLogFile /usr/local/apache2/logs/mod_jk.log --指定mod_jk日志檔案
423:JkLogLevel info --指定日志級别
424:JkLogStampformat "[%a %b %d %H:%M:%S %Y]" ---指定日志格式
[root@oracle extra]# cat httpd-vhosts.conf |grep -vnE "^#|^$"
19:NameVirtualHost *:80
27:<VirtualHost *:80>
28: ServerAdmin [email protected]
29: DocumentRoot "/data/www"
30: ServerName www.test1.com
31: ServerAlias test1.com
32: DirectoryIndex index.html
33: ErrorLog "logs/dummy-test1-error_log"
34: CustomLog "logs/dummy-test1-access_log" common
35:
36: JkMountFile conf/uriworkermap.properties 指定對于動靜态檔案處理的程式
43:
45:</VirtualHost>
47:<VirtualHost *:80>
48: ServerAdmin [email protected] 管理者郵箱
49: DocumentRoot "/data/wwwroot" 程式加目錄
51: ServerAlias test2.com 站點别名
52: DirectoryIndex index.html 首頁檔案
53: ErrorLog "logs/dummy-test2-error_log" 錯誤日志
54: CustomLog "logs/dummy-test2-access_log" common 通路日志
56:JkMountFile conf/uriworkermap1.properties 指定對于動靜态檔案處理的程式
57:</VirtualHost>
在虛拟主機目錄裡面分别建兩個index.html檔案。并啟動apache程式,用域名通路兩個網站(注:此處需要在本地做hosts解析)如果可以通路,則apache正常運作了(需關閉防火牆和安全政策)
[root@oracle data]# /usr/local/apache2/bin/apachectl -t 測試配置檔案是否Ok
Syntax OK
[root@oracle data]# /usr/local/apache2/bin/apachectl start 啟動apache
[root@oracle data]# cd /data/
[root@oracle data]# cd www
[root@oracle www]# ls
[root@oracle www]# echo "this is test1 web" > index.html
[root@oracle www]# cd ../wwwroot/
[root@oracle wwwroot]# echo "this is test2 web" > index.html
[root@oracle wwwroot]# ps -ef|grep httpd
root 54795 1 0 11:12 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 54796 54795 0 11:12 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 54797 54795 0 11:12 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 54798 54795 0 11:12 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 54799 54795 0 11:12 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 54800 54795 0 11:12 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
root 54803 2708 0 11:15 pts/1 00:00:00 grep httpd
apache安裝完畢。
安裝jk_mod:
# tar -xzf tomcat-connectors-1.2.32-src.tar.gz
# cd tomcat-connectors-1.2.32-src/
# cd native/
# ./configure --with-apxs=/usr/local/apache2/bin/apxs
# make && make install #
ll /usr/local/apache2/modules/mod_jk.so
-rwxr-xr-x 1 root root 1076923 Feb 20 19:14 /usr/local/apache2/modules/mod_jk.so
安裝jdk,因為tomcat的運作必須要jdk的支援。
[root@oracle src]# rpm -qa|grep java ---檢視是否安裝有jdk且版本是否支援
java-1.5.0-gcj-1.5.0.0-29.1.el6.x86_64
gcc-java-4.4.7-3.el6.x86_64
java_cup-0.10k-5.el6.x86_64
[root@oracle src]# rpm -e --nodeps java-1.5.0-gcj-1.5.0.0-29.1.el6.x86_64 gcc-java-4.4.7-3.el6.x86_64 java_cup-0.10k-5.el6.x86_64 ---如版本過低則解除安裝
[root@oracle src]# ls
apache-tomcat-8.0.24.zip httpd-2.2.27.tar.gz jdk-8u65-linux-x64.rpm tomcat-connectors-1.2.32-src.tar.gz
[root@oracle src]# rpm -ivh jdk-8u65-linux-x64.rpm
Preparing... ########################################### [100%]
1:jdk1.8.0_65 ########################################### [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...
jfxrt.jar...
[root@oracle java]# vim /etc/profile ---末尾添加
unset i
export JAVA_HOME=/usr/java/jdk1.8.0_65
export PATH=$JAVA_HOME/bin:$PATH:
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
unset -f pathmunge
[root@oracle java]# source /etc/profile
[root@oracle java]# echo $JAVA_HOME
/usr/java/jdk1.8.0_65
解壓tomcat程式:
[root@oracle src]# unzip apache-tomcat-8.0.24.zip
[root@oracle src]# mv apache-tomcat-8.0.24 tomcat1
[root@oracle src]# \cp -r tomcat1 tomcat2
[root@oracle src]# cd /usr/local/src/tomcat1/bin
[root@oracle bin]# chmod +x *.sh
[root@oracle src]# cd /usr/local/src/tomcat2/bin
修改tomcat1和tomcat2的service.xml檔案
tomcat1的配置檔案:
[root@oracle bin]# cat ../conf/server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="www.test1.com" appBase="webapps" --->hostname與httpd-vhost裡面相對應
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/data/www"/> --->docBase與虛拟主機裡面的DR對應
</Host>
</Engine>
</Service>
</Server>
tomcat2的配置檔案:
[root@oracle conf]# cat server.xml|grep -v "^#"
<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
<Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />
<Host name="www.test2.com" appBase="webapps" --->hostname與httpd-vhost裡面相對應
<Context path="" docBase="/data/wwwroot"/> --->docBase與虛拟主機裡面的DR對應
[root@oracle conf]# cat uriworkermap1.properties
/*=tomcat2 需要給tomcat2處理的檔案類型
!/*.jpg=tomcat2 給apache處理的檔案類型
!/*.gif=tomcat2
!/*.png=tomcat2
!/*.bmp=tomcat2
!/*.html=tomcat2
!/*.htm=tomcat2
!/*.swf=tomcat2
!/*.css= tomcat2
!/*.js= tomcat2
[root@oracle conf]# cat uriworkermap.properties
/*=tomcat1 需要給tomcat1處理的檔案類型
!/*.jpg=tomcat1 給apache處理的檔案類型
!/*.gif=tomcat1
!/*.png=tomcat1
!/*.bmp=tomcat1
!/*.html=tomcat1
!/*.htm=tomcat1
!/*.swf=tomcat1
!/*.css= tomcat1
!/*.js= tomcat1
[root@oracle conf]# cat workers.properties
worker.list=tomcat1,tomcat2 指定負載均衡器名稱
worker.tomcat1.type=ajp13 協定
worker.tomcat1.host=localhost 主機名
worker.tomcat1.port=8009 端口,必須和tomcat1裡面的對應
worker.tomcat1.lbfactor=1 權重
worker.tomcat2.type=ajp13 協定
worker.tomcat2.host=localhost 主機名
worker.tomcat2.port=8019 端口,必須和tomcat2裡面的對應
worker.tomcat2.lbfactor=1 權重
上傳網站網站即可,重新開機apache與tomcat1和tomcat2
<a href="http://blog.51cto.com/chenjisong/1740903">www.test1.com效果:</a>
<a href="http://s5.51cto.com/wyfs02/M00/7A/B6/wKioL1axnIvjg1gyAAUvbJ0fWPA346.png" target="_blank"></a>
本文轉自陳繼松 51CTO部落格,原文連結:http://blog.51cto.com/chenjisong/1740903,如需轉載請自行聯系原作者