标签:apache-tomcat+jdk+mysql+java应用(论坛)
本章目录:
1、tomcat的概述
2、tomcat的架构详解
3、部署apache-tomcat+JDK+mysql+java应用(论坛)
4、整合apache+apache-tomcat实现动静页面分离
5、测试动静分离是否成功:
1、tomcat及JSP的概述:
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器(*.jsp)。
一般用于小型企业的jsp页面,因为一个tomcat最大连接数一般在1024以下,处理能力有限。
常用JSP WEB服务器:
1、WebLogic(Oracle) --是BEA公司的产品,是目前应用最广泛的Web服务器,支持JavaEE规范,而且不断的完善以适应新的开发要求.
2、WebSphere(IBM) --IBM公司的产品,支持JavaEE规范
3、Jboss(Redhat) --大名鼎鼎的红帽的产品,原来属于开源组织Jboss,后被红帽收购。支持JavaEE规范,免费
4、Tomcat(apache) --在小型的应用系统或者有特殊需要的系统中,可以使用一个免费的Web服务器:Tomcat,该服务器支持全部JSP以及Servlet规范
2、tomcat的架构详解
---- Server[N个Service(Engine)---多个host(虚拟主机)]
<Server>
<Listener />
<GlobaNamingResources>
</GlobaNamingResources
<Service>
<Connector />
<Engine>
<Logger />
<Realm />
<host>
<Logger />
<Context />
</host>
</Engine>
</Service>
</Server>
-----------------------------------------------------------------------
安装tomcat
tar包(源码包) bin包(静态库) rpm包(二进制包)
1) 下载软件与解压
[root@tomcat ~]# lftp 192.168.0.254
lftp 192.168.0.254:~> cd notes/weekend/project/software/
lftp 192.168.0.254:/notes/weekend/project/software> mirror tomcat/
lftp 192.168.0.254:/notes/weekend/project/software> exit
[root@tomcat ~]# tar xf /root/tomcat/apache-tomcat-7.0.37.tar.gz -C /opt/
[root@tomcat ~]# mv /opt/apache-tomcat-7.0.37/ /opt/tomcat
[root@tomcat ~]# cd /opt/tomcat/bin/
2) java程序 JDK
[root@tomcat ~]# tar xf /root/tomcat/jdk-7u15-linux-x64.tar.gz -C /opt/
[root@tomcat ~]# mv /opt/jdk1.7.0_15/ /opt/jdk
3) 启动服务
[root@tomcat ~]# export JAVA_HOME=/opt/jdk
[root@tomcat ~]# sh /opt/tomcat/bin/startup.sh
Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /opt/jdk
Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -tnlp |grep :80
tcp 0 0 :::8009 :::* LISTEN 3273/java
tcp 0 0 :::8080 :::* LISTEN 3273/java
方式1 使用java方式启动tomcat
[root@tomcat ~]# vim /opt/tomcat/bin/startup.sh
CATALINA_BASE=/opt/tomcat
CATALINA_HOME=/opt/tomcat
JRE_HOME=/opt/jdk
export CATALINA_BASE CATALINA_HOME JRE_HOME
[root@tomcat ~]# vim /opt/tomcat/bin/shutdown.sh
CATALINA_BASE=/opt/tomcat
CATALINA_HOME=/opt/tomcat
JRE_HOME=/opt/jdk
export CATALINA_BASE CATALINA_HOME JRE_HOME
[root@tomcat ~]# /opt/tomcat/bin/shutdown.sh
[root@tomcat ~]# /opt/tomcat/bin/startup.sh
netstat -tnlp |grep :80
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 3357/java
tcp 0 0 :::8009 :::* LISTEN 3357/java
tcp 0 0 :::8080 :::* LISTEN 3357/java
--直接关闭tomcat程序
[root@tomcat ~]# telnet localhost 8005
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is ‘^]‘.
SHUTDOWN
Connection closed by foreign host.
8005 控制server端口
8009 ajp协议 内部程序连接 apache|mysql|tomcat(*.jar)
8080 http协议 用户访问端口
方式2 以jsvc方式启动tomcat
编译jsvc程序
./configure --with-java=/usr/java
make
[root@tomcat bin]# tar xf commons-daemon-native.tar.gz
[root@tomcat bin]# cd commons-daemon-1.0.13-native-src/unix
[root@tomcat unix]# ./configure --with-java=/opt/jdk/
[root@tomcat unix]# make
[root@tomcat unix]# cp jsvc /opt/tomcat/bin/
定义启动脚本
[root@tomcat unix]# cp /root/tomcat/tomcat-init.sh /etc/init.d/tomcat -a
[root@tomcat unix]# vim /etc/init.d/tomcat
JAVA_HOME=/opt/jdk
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/opt/tomcat
TOMCAT_USER=root
[root@tomcat unix]# chkconfig --add tomcat
[root@tomcat unix]# service tomcat start
[root@tomcat unix]# netstat -tnlp |grep :80
tcp 0 0 :::8009 :::* LISTEN 4192/jsvc.exec
tcp 0 0 :::8080 :::* LISTEN 4192/jsvc.exec
访问
http://192.168.0.1:8080/
------了解软件目录结构
[root@tomcat ~]# find /opt/tomcat/ -maxdepth 1
/opt/tomcat/
/opt/tomcat/bin --存放二进制命令
/opt/tomcat/conf --存放配置文件
/opt/tomcat/webapps --存放页面代码*.jsp
/opt/tomcat/lib ---内部库文件*.jar
/opt/tomcat/temp --临时文件
/opt/tomcat/work --中间结果集
/opt/tomcat/logs --日志文件
[root@tomcat ~]# find /opt/tomcat/conf/ |grep xml$
/opt/tomcat/conf/web.xml --资源(识别那些文件类型
/opt/tomcat/conf/server.xml --主配置文件
/opt/tomcat/conf/context.xml --联系人
/opt/tomcat/conf/tomcat-users.xml --用户
/opt/tomcat/conf/server.xml
<Server>
<Listener />
<GlobaNamingResources>
</GlobaNamingResources
<Service>
<Connector />
<Engine>
<Logger />
<Realm />
<host>
<Logger />
<Context />
</host>
</Engine>
</Service>
</Server>
-------------------------------------
修改监听端口
# vim /opt/tomcat/conf/server.xml
<Connector port="80" protocol="HTTP/1.1" --把8080改成80的话重启后就监听80
connectionTimeout="20000"
redirectPort="8443" />
--测试后再改回8080端口
-------------------------------------
目录布署
# mkdir /opt/tomcat/webapps/abc
--把abc目录与ROOT同级或者是在ROOT的下一级都是可行的
# vim /opt/tomcat/webapps/abc/time.jsp
<html>
<body>
<center>
<H1><%=new java.util.Date()%></H1>
</center>
</body>
</html>
----------------------------------------------------------
主配置文件的参数
[root@tomcat unix]# cp /opt/tomcat/conf/server.xml /opt/tomcat/conf/server.xml.old
<?xml version=‘1.0‘ encoding=‘utf-8‘?> --xml格式
<Server port="8005" shutdown="SHUTDOWN"> --8005控制端口
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> --库的的连接
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container" --tomcat基于帐号密码验证
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" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" --8080端口 用户连接
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> --8009端口 内部连接
<Engine name="Catalina" defaultHost="localhost">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps" --虚拟主机
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
name="localhost" --域名
appBase="webapps" --页面家目录 $TOMCAT/webapps/
unpackWARs="true" --是否压缩内库文件
autoDeploy="true" --自动发布页面代码
deployOnStartup:
如果此项设为true,表示Tomcat服务器启动时会自动发布appBase目录下所有的Web应用.如果Web应用中的server.xml没有相应的<Context>元素,将采用Tomcat默认的Context (/opt/tomcat/webapps)
<Context>--当前虚拟主机的java页面的家目录
<Context>=/webserver/java ---- Tomcat默认的ContextTomcat (/opt/tomcat/webapps)
<Server>
<Listener />
<GlobaNamingResources>
</GlobaNamingResources
<Service>
<Connector />
<Engine>
<Logger />
<Realm />
<host>
<Logger />
<Context />
</host>
</Engine>
</Service>
</Server>
-----------------------------------------------------------------------------------------------
apache 能够处理静态页面 不能处理jsp页面
tomcat 处理静态页面(速度慢) 处理jsp页面
结合apache与tomcat 实现动静分离
方案1
apache+tomcat+JDK+mysql+java应用(论坛)
方案2
nginx(反向代理LVS|squid)+tocmat+JDK+mysql+java应用(论坛)
----------------------------------------------------------------------
apache(jk.so)--->tomcat+JDK+mysql+java应用
--apache前端调试器(直接解析静态页面/把动态页面发送后端中间件服务器)
--tomcat接收动态页面的请求,呼叫jdk来编译动态页面
--JDK 编译java程序(页面),调度mysql
----------------------tomcat+jdk+mysql+java应用(*.jsp)----------------------
1 安装JDK
2 安装tomcat
3 安装mysql
[root@tomcat ~]# yum -y install mysql-server mysql
[root@tomcat ~]# service mysqld start
mysql> delete from mysql.user where user=‘‘;
mysql> update mysql.user set password=password(‘123456‘) where user=‘root‘;
mysql> flush privileges;
mysql> create database runjava01 default charset utf8;
mysql> create database runjava02 default charset utf8;
mysql> grant all on runjava01.* to ‘javauser01‘@‘localhost‘ identified by ‘123‘;
mysql> grant all on runjava02.* to ‘javauser02‘@‘localhost‘ identified by ‘1234‘;
4 上传jsp页面
ejforum
JavaCenter_Home
[root@tomcat ~]# mkdir /webserver/java01 -p
[root@tomcat ~]# mkdir /webserver/java02 -p
--解压JavaCenter_Home页面
[root@tomcat tomcat]# cd /root/tomcat/
[root@tomcat tomcat]# unzip JavaCenter_Home_2.0_Source_UTF8.zip
[root@tomcat tomcat]# mv JavaCenter_Home_2.0_Source_UTF8/source/WebRoot/* /webserver/java01/
[root@tomcat tomcat]# rm -fr JavaCenter_Home_2.0_Source_UTF8
--解压ejforum页面
[root@tomcat tomcat]# unzip ejforum-2.3.zip
[root@tomcat tomcat]# mv ejforum-2.3/ejforum/* /webserver/java02/
5 通过tomcat发布页面
<!--虚拟主机1 java01.uplooking.com -->
<Host name="java01.uplooking.com" appBase="webapps"
unpackWARs="true" autoDeploy="true" deployOnStartup="true" >
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="java01.uplooking.com_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="/webserver/java01" />
</Host>
<!--虚拟主机2 java02.uplooking.com -->
<Host name="java02.uplooking.com" appBase="webapps"
unpackWARs="true" autoDeploy="true" deployOnStartup="true" >
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="java02.uplooking.com_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="/webserver/java02" />
</Host>
[root@tomcat ~]# service tomcat restart
[root@tomcat ~]# echo "192.168.0.1 java01.uplooking.com java02.uplooking.com " >> /etc/hosts
6 先安装JavaCenter_Home页面
java01
使用web界面安装
http://java01.uplooking.com:8080/install
java02
编辑配置文件 填写数据库的库名 与帐号密码
--导出数据库的表结构
[root@tomcat ~]# find /root/tomcat/ejforum-2.3/install/ |grep sql$ |grep mysql
[root@tomcat ~]# mysql -ujavauser02 -p1234 runjava02 < /root/tomcat/ejforum-2.3/install/script/easyjforum_mysql.sql
[root@tomcat ~]# vim /webserver/java02/WEB-INF/conf/config.xml
<!-- DB Connection Pool - Hsqldb
<database maxActive="10" maxIdle="10" minIdle="2" maxWait="10000"
username="sa" password=""
driverClassName="org.hsqldb.jdbcDriver"
url="jdbc:hsqldb:file:/WEB-INF/data/ejf;shutdown=true"
sqlAdapter="sql.HsqldbAdapter"/>
DB Connection Pool - Mysql -->
<database maxActive="10" maxIdle="10" minIdle="2" maxWait="10000"
username="javauser02" password="1234"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/runjava02?characterEncoding=gbk&autoReconnect=true&autoReconnectForPools=true&zeroDateTimeBehavior=convertToNull"
sqlAdapter="sql.MysqlAdapter"/>
访问页面并发布贴
用户admin|密码admin
http://java02.uplooking.com:8080
------------------------------------------------------
整合apache+apache-tomcat实现动静页面分离
apache + tomcat 实现动静分离,统一入口
apache --解析静态页面
tomcat --解析动态页面
LoadModule jk_module modules/mod_jk.so --加载一个mod_jk模块
Include conf/mod_jk.conf--模块对应配置文件
JkWorkersFile conf/workers.properties--定义一个机器人,专门搬运动态页面给tomcat解析
JkMountFile conf/uriworkermap.properties--定义哪些属于动态页面的类型
1> 安装apache
[root@tomcat ~]# yum -y install httpd
2> 编译或者下载jk_mod
/root/tomcat/mod_jk-1.2.28-httpd-2.2.X.so
[root@tomcat ~]# cp /root/tomcat/mod_jk-1.2.28-httpd-2.2.X.so /etc/httpd/modules/mod_jk.so
[root@tomcat ~]# vim /etc/httpd/conf/httpd.conf
LoadModule jk_module modules/mod_jk.so
Include conf/mod_jk.conf
JkWorkersFile conf/workers.properties
JkMountFile conf/uriworkermap.properties
[root@tomcat ~]# vim /etc/httpd/conf/mod_jk.conf --jk配置文件
JkLogLevel info
JkLogFile logs/jk.log
JkShmFile logs/jk-runtime-status
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkRequestLogFormat "%w %V %T"
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
[root@tomcat ~]# vim /etc/httpd/conf/workers.properties --定义apache 通过机器人worker1 连接tomcat
workers.tomcat_home=/opt/tomcat
workers.java_home=/opt/jdk
worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.lbfactor=1
[root@tomcat ~]# vim /etc/httpd/conf/uriworkermap.properties 定义列表
/*.jsp*=worker1--- 所有的JSP页面都交给tomcat
/forum-*.html=worker1 --- 伪静态页面(一个jsp页面)
!/*.gif worker1
!/*.jpg worker1
!/*.png worker1
!/*.ico worker1
!/*.css worker1
!/*.js worker1
!/*.htm worker1
!/*.html worker1
!/*.asp worker1
!/*.aspx worker1
!/*.txt worker1
!/*.swf worker1
!/*.flv worker1
!/*.php worker1
定义apache的虚拟主机 发布页面
[root@tomcat ~]# vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html index.html.var index.jsp
[root@tomcat ~]# vim /etc/httpd/conf.d/java.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /webserver/java01
ServerName java01.uplooking.com
ErrorLog logs/java01.uplooking.com-error_log
CustomLog logs/java01.uplooking.com-access_log combined
JkMountFile conf/uriworkermap.properties
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /webserver/java02
ServerName java02.uplooking.com
ErrorLog logs/java02.uplooking.com-error_log
CustomLog logs/java02.uplooking.com-access_log combined
JkMountFile conf/uriworkermap.properties
</VirtualHost>
<Directory "/webserver">
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
[root@tomcat ~]# service httpd restart
--测试
http://java01.uplooking.com:8080/space.jsp?do=home
http://java02.uplooking.com/forum-1-1-1.html ---伪静态页面
测试动静分离?
--测试静态
1) 关闭tomcat ,apache能够正常访问静态页面
[root@tomcat ~]# service tomcat stop
[root@tomcat ~]# service httpd status
httpd (pid 6349) is running...
能否访问静态图片(正常访问)
http://java01.uplooking.com/data/avatar/noavatar_middle.gif
--测试动态
[root@tomcat ~]# service tomcat start
将apache的DocumentRoot 改变/var/tmp ---无法读取静态数据
tomcat读取动态页面数据
[root@tomcat ~]# service tomcat start
[root@tomcat ~]# vim /etc/httpd/conf.d/java.conf
DocumentRoot /var/tmp
[root@tomcat ~]# service httpd restart
http://java01.uplooking.com/space.jsp?do=home
--成功访问 图片无法下载
++++++++++++++++++++++++++++nginx+++++++++++++++++++++++++++++++++++++++++++++++++
一、nginx的概述及常见Web服务器的介绍
二、nginx安装和启动
三、Nginx 网站服务配置示例
四、添加 nginx 状态统计
五、Nginx下的虚拟主机配置
六、访问控制
七、配置Nginx实现https
八、整合LNMP
九、如何整合tomcat
http://nginx.org/
http://tengine.taobao.org/
特点
十分轻量级的HTTP服务器
高性能的HTTP
反向代理服务器
也是一个IMAP/POP3/SMTP/FTP 代理服务器
稳定性
功能丰富
系统资源的消耗低
源码安装
[root@nginx ~]# lftp 192.168.0.254
lftp 192.168.0.254:~> cd notes/weekend/project/
lftp 192.168.0.254:/notes/weekend/project> cd software/
lftp 192.168.0.254:/notes/weekend/project/software> mirror lnmp/
lftp 192.168.0.254:/notes/weekend/project/software> exit
编译前的准备
新建程序用户
[root@nginx nginx-1.4.2]# useradd -M -r nginx -s /sbin/nologin
支持pcre包
[root@nginx ~]# tar xf /root/lnmp/pcre-8.12.tar.bz2 -C /usr/local/src/
[root@nginx ~]# cd /usr/local/src/pcre-8.12/
[root@nginx pcre-8.12]# ./configure && make && make install
支持ssl认证
[root@nginx nginx-1.4.2]# yum -y install openssl-devel
编译与安装
[root@nginx ~]# tar xf /root/lnmp/nginx-1.4.2.tar.gz -C /usr/local/src/
[root@nginx ~]# cd /usr/local/src/nginx-1.4.2/
[root@nginx nginx-1.4.2]# ./configure --user=nginx--group=nginx --prefix=/opt/nginx/ --with-http_stub_status_module --with-http_ssl_module
--user=nginx
--group=nginx
--prefix=/opt/nginx/
--with-http_stub_status_module --发布nginx状态
--with-http_ssl_module --开启443端口
[root@nginx nginx-1.4.2]# make && make install
---了解目录结构
[root@nginx ~]# find /opt/nginx/ -maxdepth 2
/opt/nginx/
/opt/nginx/conf --配置文件目录
/opt/nginx/conf/win-utf
/opt/nginx/conf/uwsgi_params.default --模板文件
/opt/nginx/conf/fastcgi_params.default
/opt/nginx/conf/mime.types --nginx类型
/opt/nginx/conf/nginx.conf --主配置文件
/opt/nginx/conf/uwsgi_params
/opt/nginx/conf/scgi_params.default
/opt/nginx/conf/fastcgi_params fastcgi变量
/opt/nginx/conf/scgi_params
/opt/nginx/conf/koi-utf --nginx官方默认语言编码
/opt/nginx/conf/fastcgi.conf fastcgi配置文件
/opt/nginx/conf/mime.types.default
/opt/nginx/conf/fastcgi.conf.default
/opt/nginx/conf/koi-win
/opt/nginx/conf/nginx.conf.default
/opt/nginx/logs --日志目录
/opt/nginx/html --页面
/opt/nginx/html/index.html --首页
/opt/nginx/html/50x.html ---错误代码 404 403
/opt/nginx/sbin
/opt/nginx/sbin/nginx --二进制命令
如何启动
以nginx身份启动软件
[root@nginx ~]# vim /opt/nginx/conf/nginx.conf
user nginx nginx;
--检测语法
[root@nginx ~]# /opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx//conf/nginx.conf test is successful
[root@nginx ~]# /opt/nginx/sbin/nginx
[root@nginx ~]# netstat -tnlp |grep nginx
定义启动脚本
[root@nginx ~]# cp /root/lnmp/nginx.init.d /etc/init.d/nginx -a
[root@nginx ~]# chkconfig --add nginx
[root@nginx ~]# chkconfig nginx on
[root@nginx ~]# service nginx restart
完善命令的环境变量
[root@nginx ~]# echo "export PATH=$PATH:/opt/nginx/sbin" >> /etc/profile
[root@nginx ~]# source /etc/profile
[root@nginx ~]# which nginx
/opt/nginx/sbin/nginx
访问:
http://192.168.0.1/
----安装rpm包
官方nginx源
epel源 --https://fedoraproject.org/wiki/EPEL/zh-cn
aliyun源
--------1) 官方nginx源 + epel源
[root@node2 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/6/x86_64
gpgcheck=0
enabled=1
[root@node2 ~]# cat /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
-------2) 阿里云epel源+aliyun(centos6)
nginx实例
nginx+squid(反向代理)
lnmp(php)
+ mysql
nginx+tomcat (jsp)
-------------------------------------
---- CDN ----
本文出自 “11625502” 博客,请务必保留此出处http://11635502.blog.51cto.com/11625502/1786188
标签:apache-tomcat+jdk+mysql+java应用(论坛)
原文地址:http://11635502.blog.51cto.com/11625502/1786188