/ 中存储网

Apache + tomcat服务器环境配置步骤

2013-08-27 10:57:34 来源:kejihao

一、安装启动apache mysql

[[email protected] ~]# netstat -tnlp |grep 80       

tcp        0      0 :::80                       :::*                        LISTEN      1960/httpd          

二、单独使用Tomcat(Mysql)

[[email protected] jsp]# rpm -qa |grep java

java-1.4.2-gcj-compat-1.4.2.0-40jpp.115

[[email protected] jsp]# rpm -e --nodeps java-1.4.2-gcj-compat-1.4.2.0-40jpp.115

[[email protected] jsp]# java -version

-bash: /usr/bin/java: 没有那个文件或目录

==安装JDK

[[email protected] jsp]# rpm -ivh jdk-7u11-linux-i586.rpm --force

[[email protected] jsp]# java -version

java version "1.7.0_11"

Java(TM) SE Runtime Environment (build 1.7.0_11-b21)

Java HotSpot(TM) Client VM (build 23.6-b04, mixed mode, sharing)

[[email protected] jsp]# ls /usr/java/jdk1.7.0_11

[[email protected] jsp]# vim /etc/profile   设置环境变量

JAVA_HOME=/usr/java/jdk1.7.0_11

PATH=$PATH:$JAVA_HOME/bin

export JAVA_HOME PATH

[[email protected] jsp]# source /etc/profile

[[email protected] jsp]# echo $PATH

/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/java/jdk1.7.0_11/bin

==Tomcat

[[email protected] jsp]# tar xvf apache-tomcat-7.0.34.tar.gz -C /usr/local/

[[email protected] jsp]# cd /usr/local/

[[email protected] local]# ln -sv apache-tomcat-7.0.34 tomcat

创建指向“apache-tomcat-7.0.34”的符号链接“tomcat”

[[email protected] local]# vim /etc/profile 定义Tomcat环境变量

CATALINA_HOME=/usr/local/tomcat

export CATALINA_HOME

[[email protected] local]# source /etc/profile

[[email protected] local]# env |grep HOME

CATALINA_HOME=/usr/local/tomcat

JAVA_HOME=/usr/java/jdk1.7.0_11

HOME=/root

[[email protected] local]# /usr/local/tomcat/bin/catalina.sh start 启动

Using CATALINA_BASE:   /usr/local/tomcat

Using CATALINA_HOME:   /usr/local/tomcat

Using CATALINA_TMPDIR: /usr/local/tomcat/temp

Using JRE_HOME:        /usr/java/jdk1.7.0_11

Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

[[email protected] ~]# netstat -tnlp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address            Foreign Address             State        PID/Program name 

tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      2920/java           

tcp        0      0 :::8009                     :::*                        LISTEN      2920/java           

tcp        0      0 :::8080                     :::*                        LISTEN      2920/java           

tcp        0      0 :::80                       :::*                        LISTEN      2135/httpd          

=================================================

System V脚本   可选项

#[[email protected] ~]# vim /etc/init.d/tomcat

#!/bin/bash

# Init file for Tomcat server daemon

#

# chkconfig: 2345 96 14

# description: Tomcat server daemon

JAVA_OPTS='-Xms64m -Xmx128m'

JAVA_HOME=/usr/java/jdk1.7.0_11

CATALINA_HOME=/usr/local/tomcat

export JAVA_OPTS JAVA_HOME CATALINA_HOME

exec $CATALINA_HOME/bin/catalina.sh $*

[[email protected] ~]# chmod a+x /etc/init.d/tomcat 

[[email protected] ~]# chkconfig tomcat --list

tomcat 服务支持 chkconfig,但它在任何级别中都没有被引用(运行“chkconfig --add tomcat”)

[[email protected] ~]# 

[[email protected] ~]# chkconfig --add tomcat

[[email protected] ~]# chkconfig tomcat --list

tomcat          0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

[[email protected] ~]# chkconfig tomcat on

[[email protected] ~]# 

[[email protected] ~]# service tomcat stop

Using CATALINA_BASE:   /usr/local/tomcat

Using CATALINA_HOME:   /usr/local/tomcat

Using CATALINA_TMPDIR: /usr/local/tomcat/temp

Using JRE_HOME:        /usr/java/jdk1.7.0_11

Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

[[email protected] ~]# service tomcat start

Using CATALINA_BASE:   /usr/local/tomcat

Using CATALINA_HOME:   /usr/local/tomcat

Using CATALINA_TMPDIR: /usr/local/tomcat/temp

Using JRE_HOME:        /usr/java/jdk1.7.0_11

Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

=================================================

测试:

http://192.168.2.251:8080/

安装tomcat-native

[[email protected] ~]# yum groupinstall -y "Development tools" "Development libraries"

[[email protected] ~]# yum -y install openssl-devel apr-devel

[[email protected] jsp]# cd /usr/local/tomcat/bin/

[[email protected] bin]# ls

bootstrap.jar       commons-daemon-native.tar.gz  digest.bat        shutdown.sh           tool-wrapper.bat

catalina.bat        configtest.bat                digest.sh         startup.bat           tool-wrapper.sh

catalina.sh         configtest.sh                 setclasspath.bat  startup.sh            version.bat

catalina-tasks.xml  cpappend.bat                  setclasspath.sh   tomcat-juli.jar       version.sh

commons-daemon.jar  daemon.sh                     shutdown.bat      tomcat-native.tar.gz

[[email protected] bin]# tar xf tomcat-native.tar.gz    

[[email protected] bin]# cd tomcat-native-1.1.24-src/jni/native/

[[email protected] native]# ls

build       build-outputs.mk  include          NMAKEmakefile      srclib

buildconf   config.layout     libtcnative.dsp  NMAKEmakefile.inc  tcnative.dsp

build.conf  configure         libtcnative.dsw  os                 tcnative.pc.in

BUILDING    configure.in      Makefile.in      src                tcnative.spec

[[email protected] native]# ./configure --with-apr=/usr/ --with-ssl --with-java-home=/usr/java/jdk1.7.0_11/

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

[[email protected] native]# echo "/usr/local/apr/lib" > /etc/ld.so.conf.d/apr.conf

[[email protected] native]# ldconfig   重新加载库文件

======================================

为查看Tomcat运行状态添加用户:

/usr/local/tomcat/conf/server.xml Tomcat配置文件

/usr/local/tomcat/conf/server.xml    Tomcat配置文件

[[email protected] ~]# vim /usr/local/tomcat/conf/tomcat-users.xml

 <role rolename="manager-gui"/>

 <role rolename="manager-scripts"/>

 <role rolename="manager-status"/>

 <user username="tomcat" password="111111" roles="manager-gui,manager-scripts,manager-status,admin-gui"/>

[[email protected] ~]# service tomcat stop

[[email protected] ~]# service tomcat start

======================================

===为Tomcat提供连接Mysql的文件

[[email protected] jsp]# tar xf mysql-connector-java-5.0.8.tar.gz 

[[email protected] jsp]# cd mysql-connector-java-5.0.8

[[email protected] mysql-connector-java-5.0.8]# ls

build.xml  COPYING  docs                    mysql-connector-java-5.0.8-bin.jar  README.txt

CHANGES    debug    EXCEPTIONS-CONNECTOR-J  README                              src

[[email protected] mysql-connector-java-5.0.8]# cp mysql-connector-java-5.0.8-bin.jar /usr/local/tomcat/lib/

[[email protected] ~]# service tomcat stop

[[email protected] ~]# service tomcat start

===配置Tomcat虚拟主机:JavaCenter_Home

[[email protected] jsp]# ls /usr/local/tomcat/webapps/  

docs  examples  host-manager  manager  ROOT

[[email protected] jsp]# ls /usr/local/tomcat/webapps/ROOT 默认网站的主目录

asf-logo.png       bg-nav-item.png  favicon.ico        tomcat.gif        WEB-INF

asf-logo-wide.gif  bg-nav.png       index.jsp          tomcat.png

bg-button.png      bg-upper.png     RELEASE-NOTES.txt  tomcat-power.gif

bg-middle.png      build.xml        tomcat.css         tomcat.svg

[[email protected] jsp]# 

[[email protected] jsp]# mkdir /usr/local/tomcat/webapps/blog 为新网站建立主目录

[[email protected] jsp]# vim /usr/local/tomcat/conf/server.xml tomcat主配置文件 

<Host name="www.blog.com"  appBase="webapps" unpackWARs="true" autoDeploy="true">

<Context docBase="blog" path="" />

</Host>

==JSP网站

[[email protected] jsp]# unzip JavaCenter_Home_2.0_Source_UTF8.zip

[[email protected] jsp]# cd JavaCenter_Home_2.0_Source_UTF8/source/WebRoot/

[[email protected] WebRoot]# ls

admin        avatar.jsp         data         help.jsp    js.jsp       rss.jsp    userapp.jsp

admincp.jsp  config.properties  do.jsp       image       link.jsp     source     WEB-INF

api          contact.jsp        editor.jsp   index.jsp   magic.jsp    space.jsp  xmlrpc.jsp

app.jsp      cp.jsp             errors       install     META-INF     template

attachment   crossdomain.xml    favicon.ico  invite.jsp  network.jsp  theme

[[email protected] WebRoot]# 

[[email protected] WebRoot]# cp -rf * /usr/local/tomcat/webapps/blog/

[[email protected] ~]# service tomcat stop

[[email protected] ~]# service tomcat start

http://www.blog.com:8080  是否支持连接数据库 之前要安装好mysql数据库

http://www.blog.com:8080/install

三、整合Apache、Tomcat

确定建立了两个虚拟机:

www.126.com  /webroot1/126 默认网站目录  PHP

www.blog.com  /usr/local/tomcat/webapps/blog/ JSP(Tomcat:8080)

[[email protected] ~]# mkdir /webroot1

[[email protected] ~]# mkdir /webroot1/126

[[email protected] ~]# echo "welcome to 126" > /webroot1/126/index.html

[[email protected] ~]#  vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>

DocumentRoot "/webroot/126"

ServerName www.126.com

</VirtualHost>

<VirtualHost *:80>

DocumentRoot "/usr/local/tomcat/webapps/blog/"

ServerName www.blog.com

</VirtualHost>

<Directory "/webroot/126">

Require all granted

</Directory>

<Directory "/usr/local/tomcat/webapps/blog/">

Require all granted

</Directory>

[[email protected] ~]# service httpd restart

必须确保两个网站都能访问,此时JSP网只能访问到,不能解析

整合

[[email protected] jsp]# tar xvf  tomcat-connectors-1.2.31-src.tar.gz

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

[[email protected] native]# ls

aclocal.m4  buildconf.sh  common        docs  Makefile.am  NEWS        scripts

apache-1.3  BUILDING.txt  configure     iis   Makefile.in  nt_service  STATUS.txt

apache-2.0  CHANGES       configure.in  jni   netscape     README.txt  TODO.txt

[[email protected] native]# chmod 755 buildconf.sh 

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

====

[[email protected] ~]# which apxs

/usr/sbin/apxs

[[email protected] ~]# 

[[email protected] ~]# rpm -qf /usr/sbin/apxs 

httpd-devel-2.2.3-63.el5

[[email protected] native]# ./configure --with-apxs=/usr/sbin/apxs   场景一:RPM包关联Apache

[[email protected] native]# ./configure --with-apxs=/usr/local/apache2/bin/apxs 场景二:源码包关联Apache

====

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

=======================================================

编译时产生错误及解决方案

[[email protected] native]# make

Making all in common

make[1]: Entering directory `/root/jsp/tomcat-connectors-1.2.31-src/native/common'

make[1]: Nothing to be done for `all'.

make[1]: Leaving directory `/root/jsp/tomcat-connectors-1.2.31-src/native/common'

Making all in apache-2.0

make[1]: Entering directory `/root/jsp/tomcat-connectors-1.2.31-src/native/apache-2.0'

/usr/local/apache2/build/libtool --silent --mode=compile gcc -std=gnu99 -I/usr/local/apache2/include  -DHAVE_CONFIG_H -DUSE_APACHE_MD5 -I ../common -I /usr/java/jdk1.7.0_11/include -I /usr/java/jdk1.7.0_11/include/unix -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -g -O2 -pthread -DHAVE_APR  -I/usr/local/apache2/include -I/usr/local/apache2/include  -DHAVE_CONFIG_H -g -O2 -pthread -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -c mod_jk.c -o mod_jk.lo

mod_jk.c: In function 'init_ws_service':

mod_jk.c:735: error: 'conn_rec' has no member named 'remote_ip'

mod_jk.c:999: error: 'conn_rec' has no member named 'remote_ip'

mod_jk.c:999: error: 'conn_rec' has no member named 'remote_ip'

make[1]: *** [mod_jk.lo] 错误 1

make[1]: Leaving directory `/root/jsp/tomcat-connectors-1.2.31-src/native/apache-2.0'

make: *** [all-recursive] 错误 1

解决方法:

[[email protected] native]# vim apache-2.0/mod_jk.c 

735:

改之前

 s->remote_addr = r->connection->remote_ip; 

改之后 

 s->remote_addr = r->connection->client_ip;

999           

改之前     

STRNULL_FOR_NULL(r->connection->remote_ip),

改之后

STRNULL_FOR_NULL(r->connection->client_ip),

重新编译:

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

[[email protected] native]# make clean 清除以前编译产生的目标文件 .o的文件

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

==================================================

[[email protected] native]# ls /etc/httpd/modules/mod_jk.so 

/etc/httpd/modules/mod_jk.so

正式开始整合。。。

==== vim /etc/httpd/conf/httpd.conf=====

DirectoryIndex index.html index.jsp

LoadModule jk_module modules/mod_jk.so 加载mod_jk.so

jkWorkersFile conf.d/workers.properties

jkLogFile logs/mod_jk.log

jkLogLevel info

=== vim /etc/httpd/conf.d/workers.properties 

ps=/

worker.list=tomcat1,tomcat2,loadbalancer

worker.tomcat1.port=8080

worker.tomcat1.host=127.0.0.1

workes.tomcat1.type=ajp13

worker.tomcat1.lbfactor=100

=== vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>

DocumentRoot "/usr/local/tomcat/webapps/blog/"

ServerName www.blog.com

jkmount /*.jsp loadbalancer

jkmount /servlet/* loadbalancer

jkmount /application/* loadbalancer

</VirtualHost>

[[email protected] native]# service httpd restart

[[email protected] native]# 

[[email protected] native]# service tomcat stop

[[email protected] native]# service tomcat start

编译:Apache + tomcat 简单配置