本章目录:
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) 下载软件与解压
[[email protected] ~]# 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
[[email protected] ~]# tar xf /root/tomcat/apache-tomcat-7.0.37.tar.gz -C /opt/
[[email protected] ~]# mv /opt/apache-tomcat-7.0.37/ /opt/tomcat
[[email protected] ~]# cd /opt/tomcat/bin/
2) java程序 JDK
[[email protected] ~]# tar xf /root/tomcat/jdk-7u15-linux-x64.tar.gz -C /opt/
[[email protected] ~]# mv /opt/jdk1.7.0_15/ /opt/jdk
3) 启动服务
[[email protected] ~]# export JAVA_HOME=/opt/jdk
[[email protected] ~]# 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
[[email protected] ~]# netstat -tnlp |grep :80
tcp 0 0 :::8009 :::* LISTEN 3273/java
tcp 0 0 :::8080 :::* LISTEN 3273/java
方式1 使用java方式启动tomcat
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# /opt/tomcat/bin/shutdown.sh
[[email protected] ~]# /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程序
[[email protected] ~]# 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
[[email protected] bin]# tar xf commons-daemon-native.tar.gz
[[email protected] bin]# cd commons-daemon-1.0.13-native-src/unix
[[email protected] unix]# ./configure --with-java=/opt/jdk/
[[email protected] unix]# make
[[email protected] unix]# cp jsvc /opt/tomcat/bin/
定义启动脚本
[[email protected] unix]# cp /root/tomcat/tomcat-init.sh /etc/init.d/tomcat -a
[[email protected] unix]# vim /etc/init.d/tomcat
JAVA_HOME=/opt/jdk
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/opt/tomcat
TOMCAT_USER=root
[[email protected] unix]# chkconfig --add tomcat
[[email protected] unix]# service tomcat start
[[email protected] 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/
------了解软件目录结构
[[email protected] ~]# 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 --日志文件
[[email protected] ~]# 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>
----------------------------------------------------------
主配置文件的参数
[[email protected] 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
[[email protected] ~]# yum -y install mysql-server mysql
[[email protected] ~]# 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
[[email protected] ~]# mkdir /webserver/java01 -p
[[email protected] ~]# mkdir /webserver/java02 -p
--解压JavaCenter_Home页面
[[email protected] tomcat]# cd /root/tomcat/
[[email protected] tomcat]# unzip JavaCenter_Home_2.0_Source_UTF8.zip
[[email protected] tomcat]# mv JavaCenter_Home_2.0_Source_UTF8/source/WebRoot/* /webserver/java01/
[[email protected] tomcat]# rm -fr JavaCenter_Home_2.0_Source_UTF8
--解压ejforum页面
[[email protected] tomcat]# unzip ejforum-2.3.zip
[[email protected] 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>
[[email protected] ~]# service tomcat restart
[[email protected] ~]# 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
编辑配置文件 填写数据库的库名 与帐号密码
--导出数据库的表结构
[[email protected] ~]# find /root/tomcat/ejforum-2.3/install/ |grep sql$ |grep mysql
[[email protected] ~]# mysql -ujavauser02 -p1234 runjava02 < /root/tomcat/ejforum-2.3/install/script/easyjforum_mysql.sql
[[email protected] ~]# 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
[[email protected] ~]# yum -y install httpd
2> 编译或者下载jk_mod
/root/tomcat/mod_jk-1.2.28-httpd-2.2.X.so
[[email protected] ~]# cp /root/tomcat/mod_jk-1.2.28-httpd-2.2.X.so /etc/httpd/modules/mod_jk.so
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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的虚拟主机 发布页面
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html index.html.var index.jsp
[[email protected] ~]# 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>
[[email protected] ~]# service httpd restart
--测试
http://java01.uplooking.com:8080/space.jsp?do=home
http://java02.uplooking.com/forum-1-1-1.html ---伪静态页面
测试动静分离?
--测试静态
1) 关闭tomcat ,apache能够正常访问静态页面
[[email protected] ~]# service tomcat stop
[[email protected] ~]# service httpd status
httpd (pid 6349) is running...
能否访问静态图片(正常访问)
http://java01.uplooking.com/data/avatar/noavatar_middle.gif
--测试动态
[[email protected] ~]# service tomcat start
将apache的DocumentRoot 改变/var/tmp ---无法读取静态数据
tomcat读取动态页面数据
[[email protected] ~]# service tomcat start
[[email protected] ~]# vim /etc/httpd/conf.d/java.conf
DocumentRoot /var/tmp
[[email protected] ~]# 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 代理服务器
稳定性
功能丰富
系统资源的消耗低
源码安装
[[email protected] ~]# 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
编译前的准备
新建程序用户
[[email protected] nginx-1.4.2]# useradd -M -r nginx -s /sbin/nologin
支持pcre包
[[email protected] ~]# tar xf /root/lnmp/pcre-8.12.tar.bz2 -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/pcre-8.12/
[[email protected] pcre-8.12]# ./configure && make && make install
支持ssl认证
[[email protected] nginx-1.4.2]# yum -y install openssl-devel
编译与安装
[[email protected] ~]# tar xf /root/lnmp/nginx-1.4.2.tar.gz -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/nginx-1.4.2/
[[email protected] 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端口
[[email protected] nginx-1.4.2]# make && make install
---了解目录结构
[[email protected] ~]# 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身份启动软件
[[email protected] ~]# vim /opt/nginx/conf/nginx.conf
user nginx nginx;
--检测语法
[[email protected] ~]# /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
[[email protected] ~]# /opt/nginx/sbin/nginx
[[email protected] ~]# netstat -tnlp |grep nginx
定义启动脚本
[[email protected] ~]# cp /root/lnmp/nginx.init.d /etc/init.d/nginx -a
[[email protected] ~]# chkconfig --add nginx
[[email protected] ~]# chkconfig nginx on
[[email protected] ~]# service nginx restart
完善命令的环境变量
[[email protected] ~]# echo "export PATH=$PATH:/opt/nginx/sbin" >> /etc/profile
[[email protected] ~]# source /etc/profile
[[email protected] ~]# 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源
[[email protected] ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/6/x86_64
gpgcheck=0
enabled=1
[[email protected] ~]# 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 ----