Tomcat是一個輕量級應用服務器,在中小型系統和併發訪問用戶不是不少的場合下被廣泛使用,是開發和調試JSP程序的首選。能夠這樣認爲,當在一臺機器上配置好Tomcat服務器,可利用它響應對HTML 頁面的訪問請求。實際上Tomcat 部分是Nginx服務器的擴展,但它是獨立運行的,因此當你運行tomcat時,它實際上做爲一個與Nginx 獨立的進程單獨運行的。html
安裝tomcatjava
Tomcat的安裝分爲兩個步驟:安裝JDK和安裝Tomcat.mysql
JDK(Java Development Kit)是Sun Microsystems針對Java開發員的產品。自從Java推出以來,JDK已經成爲使用最普遍的Java SDK. JDK是整個Java的核心,包括了Java運行環境,Java工具和Java基礎的類庫。因此要想運行jsp的程序必需要有JDK的支持,理所固然安裝 Tomcat的前提是安裝好JDK.linux
安裝JDKweb
下載JDKsql
cd /usr/local/src/數據庫
下載JDK官網:apache
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htmlvim
安裝: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/p_w_upload/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
找到:
<Connector port="8080" protocol="HTTP/1.1"
修改成:
<Connector port="80" protocol="HTTP/1.1"
2. 配置新的虛擬主機
cd /usr/local/tomcat/conf/
vim server.xml
找到</Host>下一行插入新的<Host>內容以下:
<Host name="www.123.cn" appBase="/data/tomcatweb"
unpackWARs="false" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="./" debug="0" reloadable="true" crossContext="true"/>
</Host>
保存後,重啓tomcat:
service tomcat stop
service tomcat start
測試tomcat
先建立tomcat的測試文件:
vim /data/tomcatweb/111.jsp
加入以下內容:
<html><body><center>
Now time is: <%=new java.util.Date()%>
</center></body></html>
保存後,使用curl測試:
[root@localhost ~]# curl -xlocalhost:80 www.123.cn/111.jsp
看看運行結果是不是:
<html><body><center>
Now time is: Thu Jun 13 15:26:03 CST 2013
</center></body></html>
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
<Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"
enableLookups="false" acceptCount="1000"
useBodyEncodingForURI="true"
URIEncoding="UTF-8"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
vi /usr/local/tomcat/conf/server.xml +128
<Host name="localhost" appBase="/home/www/cphd"
unpackWARs="true" autoDeploy="true">
<Context reloadable="true" docBase="cphd" path="" />
配置Tomcat多實例:
cp -r /usr/local/tomcat /usr/local/tomcat2
而後修改相應端口和虛擬主機便可。
保存後,重啓tomcat:
service tomcat stop
service tomcat start
檢查Tomcat啓動進程:
ps -ef | grep tomcat