Tomcat是一个轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。可以这样认为,当在一台机器上配置好Tomcat服务器,可利用它响应对HTML 页面的访问请求。实际上Tomcat 部分是Nginx服务器的扩展,但它是独立运行的,所以当你运行tomcat时,它实际上作为一个与Nginx 独立的进程单独运行的。
安装tomcat
Tomcat的安装分为两个步骤:安装JDK和安装Tomcat.
JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品。自从Java推出以来,JDK已经成为使用最广泛的Java SDK. JDK是整个Java的核心,包括了Java运行环境,Java工具和Java基础的类库。所以要想运行jsp的程序必须要有JDK的支持,理所当然安装 Tomcat的前提是安装好JDK.
安装JDK
下载JDK
cd /usr/local/src/
下载JDK官网:
http://www.Oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
安装:rpm -ivh jdk-7-linux-i586.rpm
设置环境变量
vim /etc/profile
向文件里面追加以下内容:
JAVA_HOME=/usr/java/jdk1.7.0_45
JRE_HOME=/usr/java/jdk1.7.0_45/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
使修改生效
[root@localhost ~]# source /etc/profile //使修改立即生效
[root@localhost ~]# echo $PATH //查看PATH值
检测是否设置正确:
java -version
安装Tomcat
cd /usr/local/src/
wget http://www.aminglinux.com/bbs/data/attachment/forum/apache-tomcat-7.0.14.tar.gz
如果觉得这个版本不适合,可以到官方网站(http://tomcat.apache.org/)下载。
tar zxvf apache-tomcat-7.0.14.tar.gz
mv apache-tomcat-7.0.14 /usr/local/tomcat
cp -p /usr/local/tomcat/bin/catalina.sh /etc/init.d/tomcat
vim /etc/init.d/tomcat
在第二行加入以下内容:
# chkconfig: 112 63 37
# description: tomcat server init script
# Source Function Library
. /etc/init.d/functions
JAVA_HOME=/usr/local/jdk1.6.0_23/
CATALINA_HOME=/usr/local/tomcat
保存文件后,执行以下操作:
chmod 755 /etc/init.d/tomcat
chkconfig --add tomcat
chkconfig tomcat on
启动tomcat:
service tomcat start
查看是否启动成功:
ps aux |grep tomcat
如果有进程的话,请在浏览器中输入http://IP:8080/ 你会看到tomcat的主界面。
配置tomcat
1. 配置tomcat服务的访问端口
tomcat默认启动的是8080,如果你想修改为80,则需要修改server.xml文件:
vim /usr/local/tomcat/conf/server.xml
找到:
修改为:
2. 配置新的虚拟主机
cd /usr/local/tomcat/conf/
vim server.xml
找到下一行插入新的内容如下:
unpackWARs="false" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
保存后,重启tomcat:
service tomcat stop
service tomcat start
测试tomcat
先创建tomcat的测试文件:
vim /data/tomcatweb/111.jsp
加入如下内容:
Now time is:
保存后,使用curl测试:
[root@localhost ~]# curl -xlocalhost:80 www.123.cn/111.jsp
看看运行结果是否是:
Now time is: Thu Jun 13 15:26:03 CST 2013
tomcat连接MySQL数据库:
cat jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/cphd?useUnicode=true&characterEncoding=utf-8
jdbc.username=WordPress
jdbc.password=WordPress
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_query_cache=true
hibernate.schemaUpdate=false
Nginx配置所有jsp的页面均交由tomcat处理:
server {
listen 80;
server_name baidu.com www.baidu.com;
index index.html index.jsp;
root /home/www/cphd/cphd;
rewrite ^/(.*)$ https://www.baidu.com/$1 permanent;
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~* (\.jsp)|(\.do)$ {
include proxy.conf;
proxy_pass http://127.0.0.1:8080;
}
location /cphd {
root /home/www/cphd;
include proxy.conf;
proxy_pass http://127.0.0.1:8080;
}
}
server {
listen 443 ssl;
server_name baidu.com www.baidu.com;
index index.html index.jsp;
root /home/www/cphd/cphd;
ssl on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA;
#ssl_prefer_server_ciphers on;
#ssl_dhparam /etc/pki/tls/private/dhparam.pem;
ssl_certificate /etc/pki/tls/certs/baidu.com.crt;
ssl_certificate_key /etc/pki/tls/private/baidu.com.key;
#ssl_session_tickets off;
#ssl_session_timeout 1d;
#ssl_session_cache shared:SSL:1m;
add_header Strict-Transport-Security 'max-age=31536000'; # remember the certificate for a year and automatically connect to HTTPS for this domain
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~* (\.jsp)|(\.do)$ {
include proxy.conf;
proxy_pass http://127.0.0.1:8080;
}
location /cphd {
root /home/www/cphd;
include proxy.conf;
proxy_pass http://127.0.0.1:8080;
}
}
Tomcat配置:
vi /usr/local/tomcat/conf/server.xml +71
enableLookups="false" acceptCount="1000"
useBodyEncodingForURI="true"
URIEncoding="UTF-8"
connectionTimeout="20000"
redirectPort="8443" />