首页 > 代码库 > tomcat环境搭建和管理及实现jsp通过tomcat连接mysql
tomcat环境搭建和管理及实现jsp通过tomcat连接mysql
升级JDK
[root@duwers21~]# rpm -ivh jdk-8u60-linux-x64.rpm [root@duwers21 ~]# java -version java version "1.8.0_60" Java(TM) SE Runtime Environment (build1.8.0_60-b27) Java HotSpot(TM) 64-Bit Server VM (build25.60-b23, mixed mode) [root@duwers21 ~]#
安装tomcat
[root@duwers21 ~]# tar xvfapache-tomcat-8.0.26.tar.gz -C /usr/local/src/
注意:Tomcat有两种安装方式,一种是直接解压就可以使用的二进制文件,第二种是编译安装,我们这里采用了第一种方法,下载的tomcat软件直接解压就可以执行的二进制文件,因为在官方默认下载的便是使用第一种方法安装,那么下载解压后的目录如下:
tomcat
|---bin Tomcat:存放启动和关闭tomcat脚本;
|---conf Tomcat:存放不同的配置文件(server.xml和web.xml);
|---lib:包含Tomcat使用的jar文件.unix平台此目录下的任何文件都被加到Tomcat的classpath中;
|---logs:存放Tomcat执行时的LOG文件;
|---webapps:Tomcat的主要Web发布目录(包括应用程序示例);
|--- ROOT:tomcat的家目录
|--- index.jsp:Tomcat的默认首页文件
|---work:存放jsp编译后产生的class文件或servlet文件存放
|---temp:存放Tomcat运行时所产生的临时文件
[root@duwers21 ~]# mkdir /server [root@duwers21 ~]# mv/usr/local/src/apache-tomcat-8.0.26/ /usr/local/src/apache-tomcat-8.0.26.a [root@duwers21 ~]# cp -R/usr/local/src/apache-tomcat-8.0.26.a /server/tomcat
Tomcat启动脚本
[root@duwers21 ~]# vim /etc/init.d/tomcat
//内容:
[root@duwers21 ~]# cat /etc/init.d/tomcat #!/bin/bash # Tomcat init script for Linux # chkconfig: 2345 96 14 # discription: The Apache Tomcat Server/JSPcontainer JAVA_OPTS=‘-server -Xms64m -Xmx128m‘ JAVA_HOME=/usr/java/jdk1.8.0_60 CATALINA_HOME=/server/tomcat $CATALINA_HOME/bin/catalina.sh $*
#######################################################################
JAVA_OPTS=‘-server -Xms64m -Xmx128m‘是用来设置JAVA相关运行参数的变量
-server 一定要作为第一个参数,在多个CPU时性能佳
-Xms 初始heap(堆)大小,使用最小内存大小,cpu性能高时此值应该设大一些
-Xmx Java heap最大值,使用内存的最大值
上面两个值分配的是JVM的最小和最大值,取决于硬件的物理内存大小,建议为物理内存的一般,不超过80%
JAVA_HOME=/usr/local/jdk1.8.0_60 指定Tomcat目录
$CATALINA_HOME=/usr/local/tomcat $* 指定运行tomcat的脚本
//赋予权限
[root@duwers21 ~]# chmod +x/etc/init.d/tomcat
//启动
[root@duwers21 ~]# service tomcat start Using CATALINA_BASE: /server/tomcat Using CATALINA_HOME: /server/tomcat Using CATALINA_TMPDIR: /server/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /server/tomcat/bin/bootstrap.jar:/server/tomcat/bin/tomcat-juli.jar Tomcat started. [root@duwers21 ~]#
//加入自启动
[root@duwers21 ~]# chkconfig --add tomcat
//查看状态
[root@duwers21 ~]# netstat -antup | grep 8080 tcp 0 0 :::8080 :::* LISTEN 1402/java [root@duwers21 ~]#
测试Tomcat
http://192.168.20.21:8080
点击manager App,需要一个用户名和密码:
创建管理Manger App用户
[root@duwers21 ~]# cp/server/tomcat/conf/tomcat-users.xml /server/tomcat/conf/tomcat-users.xml.back
然后修改tomcat-users.xml,在最后面改成下面的样子:【红色为自定义】
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager-gui"/>
<userusername="redhat" password="123456"roles="manager-gui"/>
<user username="tomcat" password="tomcat"roles="tomcat"/>
<user username="both" password="tomcat"roles="tomcat,role1"/>
<user username="role1" password="tomcat"roles="role1"/>
################################################################################
几种角色
1:“manager-gui”:Allowsaccess to the html interface(允许通过web的方式登录查看服务器信息)。
2:“manager-script”: Allowsaccess to the plain text interface(允许以纯文本的方式访问)。
3:“manager-jmx”: Allowsaccess to the JMX proxy interface(允许jmx的代理访问)。
4:“manager-status”: Allowsaccess to the read-only status pages(允许以只读状态访问)。
http://tomcat.apache.org/tomcat-8.0-doc/manager-howto.html #Configuring_Manager_Application_Access
[root@duwers21 ~]# service tomcat stop Using CATALINA_BASE: /server/tomcat Using CATALINA_HOME: /server/tomcat Using CATALINA_TMPDIR: /server/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /server/tomcat/bin/bootstrap.jar:/server/tomcat/bin/tomcat-juli.jar [root@duwers21 ~]# service tomcat start Using CATALINA_BASE: /server/tomcat Using CATALINA_HOME: /server/tomcat Using CATALINA_TMPDIR: /server/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /server/tomcat/bin/bootstrap.jar:/server/tomcat/bin/tomcat-juli.jar Tomcat started. [root@duwers21 ~]#
点击manager App,输入用户名和密码
搭建基于域名的虚拟主机
[root@duwers21 ~]# ls /server/tomcat/conf/ Catalina catalina.properties logging.properties tomcat-users.xml tomcat-users.xsd catalina.policy context.xml server.xml tomcat-users.xml.back web.xml [root@duwers21 ~]#
#####################################################################
server.xml是Tomcat的主配置文件(全局),服务器设置的,例如端口设置,路径设置。
context里设置tomcat数据源,用来连接数据库。
tomcat_user主要是用户名和密码的设置。
web是默认首页等等之类的设置
[root@duwers21 ~]# vim/server/tomcat/conf/server.xml [root@duwers21 ~]# tail -10/server/tomcat/conf/server.xml <Hostname="duwers21.com" appBase="/var/www/html"> <Context path=""docBase="/var/www/html/web1"/> </Host> <Host name="duwers21.space"appBase="/var/www/html"> <Context path=""docBase="/var/www/html/web2"/> </Host> </Engine> </Service> </Server> [root@duwers21 ~]#
####################################################################################################
appBase指定应用程序(网站)的基本路径,这里可以存放多个程序(网站),一般是相对路径,相对于tomcat的安装目录。
Context path=""为虚拟目录,如果是空,表示直接就是/,如果是如path="aa",那么访问的时候就是site:8080/aa
docBase="……" 为实际目录,就是可以是绝对路径,如果是相对路径就是基于appBase
[root@duwers21 ~]# mkdir -p/var/www/html/{web1,web2} [root@duwers21 ~]# echo duwers21.com >/var/www/html/web1/index.html [root@duwers21 ~]# echo duwers21.space >/var/www/html/web2/index.html [root@duwers21 ~]# service tomcat stop ;service tomcat start Using CATALINA_BASE: /server/tomcat Using CATALINA_HOME: /server/tomcat Using CATALINA_TMPDIR: /server/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /server/tomcat/bin/bootstrap.jar:/server/tomcat/bin/tomcat-juli.jar Using CATALINA_BASE: /server/tomcat Using CATALINA_HOME: /server/tomcat Using CATALINA_TMPDIR: /server/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /server/tomcat/bin/bootstrap.jar:/server/tomcat/bin/tomcat-juli.jar Tomcat started.
修改windows的hosts文件
//最后面添加
192.168.20.21 duwers21.com
192.168.20.21 duwers21.space
网页测试
安装tomcat-Native
omcat 可以使用 apr 来提供更好的伸缩性、性能和集成到本地服务器技术。用来提高 tomcat 的性能。 tomcat native 在具体的运行平台上,提供了一种优化技术,它本身是基于 ARP(Apache Portable(轻便) Runtime)技术
我们应用了 tomcat native 技术之后,tomcat 在跟操作系统级别的交互方面可以做得更好,并且它更像apache 一样,可以更好地作为一台 web server。 tomcat 可以利用 apache 的 apr 接口,使用操作系统的部分本地操作,从而提升性能APR 提升的是静态页面处理能力
关于tomcat-native的相关介绍及下载: http://tomcat.apache.org/download-native.cgi
[root@duwers21 ~]# yum install apr-develgcc gcc-c++ openssl-devel openssl -y [root@duwers21 ~]# tar xftomcat-native-1.1.34-src.tar.gz -C /usr/local/src/ [root@duwers21 ~]# cd/usr/local/src/tomcat-native-1.1.34-src/jni/native/ [root@duwers21 native]# ./configure--with-apr=/usr/ --with-java-home=/usr/java/jdk1.8.0_60/ --with-ssl [root@duwers21 native]# make ; make install;cd
如下图,成功安装
[root@duwers21 ~]# vim /etc/ld.so.conf [root@duwers21 ~]# cat /etc/ld.so.conf include ld.so.conf.d/*.conf /usr/local/apr/lib [root@duwers21 ~]# [root@duwers21 ~]# ldconfig [root@duwers21 ~]# echo"ldconfig" >>/etc/rc.local
[root@duwers21 ~]# service tomcat stop ;service tomcat start Using CATALINA_BASE: /server/tomcat Using CATALINA_HOME: /server/tomcat Using CATALINA_TMPDIR: /server/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /server/tomcat/bin/bootstrap.jar:/server/tomcat/bin/tomcat-juli.jar Using CATALINA_BASE: /server/tomcat Using CATALINA_HOME: /server/tomcat Using CATALINA_TMPDIR: /server/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /server/tomcat/bin/bootstrap.jar:/server/tomcat/bin/tomcat-juli.jar Tomcat started. [root@duwers21 ~]#
//看日志看是否支持native
[root@duwers21 ~]# cat/server/tomcat/logs/catalina.out | grep Native 31-Aug-2016 20:51:59.418 INFO [main]org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR basedApache Tomcat Native library which allows optimal performance in productionenvironments was not found on the java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 31-Aug-2016 21:01:31.483 INFO [main]org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR basedApache Tomcat Native library which allows optimal performance in productionenvironments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 31-Aug-2016 21:12:44.588 INFO [main]org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR basedApache Tomcat Native library which allows optimal performance in productionenvironments was not found on the java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 31-Aug-2016 21:16:34.085 INFO [main]org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR basedApache Tomcat Native library which allows optimal performance in production environmentswas not found on the java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 31-Aug-2016 21:17:05.078 INFO [main]org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR basedApache Tomcat Native library which allows optimal performance in productionenvironments was not found on the java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 31-Aug-2016 21:26:38.015 INFO [main]org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR basedApache Tomcat Native library which allows optimal performance in productionenvironments was not found on the java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib [root@duwers21 ~]#
安装mysql
//环境
[root@duwers21 ~]# yum install -ycmake ncurses-devel
[root@duwers21 ~]# tar xfmysql-5.6.26.tar.gz -C /usr/local/src/ ; cd /usr/local/src/mysql-5.6.26
//创建用户
[root@duwers21 mysql-5.6.26]# useradd -M -s/sbin/nologin mysql
//cmake
[root@duwers21 mysql-5.6.26]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL-USER=mysql
****************************************************************************************************
DCMAKE_INSTALL_PREFIX #制定mysql的安装根目录,目录在安装的时候会自动创建,这个值也可以在服务器启动时,用--basedir来设置
DMYSQL_UNIX_ADDR #服务器与本地客户端进行通信的Unix套接字文件,必须是绝对路径,默认位置/tmp/mysql.sock,可以在服务器启动时,用--socket改变
DDEFAULT_CHARSET #mysql默认使用的字符集,不指定将默认使用Latin1西欧字符集
DDEFAULT_COLLATION #默认字符校对
DWITH_EXTRA_CHARSETS #制定mysql拓展字符集,默认值也是all支持所有的字符集
DWITH_MYISAM_STORAGE_ENGINE #静态编译MYISAM,INNOBASE,MEMORY存储引擎到MYSQL服务
器,这样MYSQL就支持这三种存储引擎
DWITH_INNOBASE_STORAGE_ENGINE
DWITH_MEMORY_STORAGE_ENGINE
DWITH_READLINE #支持readline库
DENABLED_LOCAL_INFILE #允许本地倒入数据,启用加载本地数据
DMYSQL_DATADIR #mysql数据库存放路径
DMYSQL-USER #运行mysql的用户
这些编译参数的帮助寻找方法:
http://www.mysql.com→→Documentation→→选择对应的版本(5.6)→→HTMLOnline→→View→→Installation & Upgrades→→Installing MySQLfrom Source →→MySQL Source-Configuration Options→→
http://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html //最终的URL
//这里应该知道干嘛
[root@duwers21 mysql-5.6.26]# make -j 2 ;make install
//更改属主和属组
[root@duwers21 mysql-5.6.26]# cd &&chown -R mysql:mysql /usr/local/mysql/
//生成配置文件
[root@duwers21 ~]# cp /usr/local/mysql/support-files/my-default.cnf/etc/my.cnf
//生成启动脚本
[root@duwers21 ~]# cp/usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
//脚本应该修改的内容
[root@duwers21 ~]# vim/etc/init.d/mysqld basedir=/usr/local/mysql datadir=/usr/local/mysql/data
//加入开机自启动
[root@duwers21 ~]# chkconfig mysqld on
初始化数据库
[root@duwers21 ~]#/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf--basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
//查看数据库的文件
[root@duwers21 ~]# ls/usr/local/mysql/data/ ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test [root@duwers21 ~]#
//做个连接
[root@duwers21 ~]# ln -s/usr/local/mysql/bin/* /bin/ [root@duwers21 ~]# service mysqld start Starting MySQL. SUCCESS! [root@duwers21 ~]#
//数据库安全
[root@duwers21 ~]# mysql_secure_installation
//测试登录
[root@duwers21 ~]# mysql -uroot -p123456
//修改环境变量
[root@duwers21 ~]# echo"PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile &&source /etc/profile
//修改配置文件
[root@duwers21 ~]# vim /etc/my.cnf [mysqld] character-set-server=utf8 #添加此行,修改mysql数据库默认编码(和低版本的mysql写法不一样, 低版本的写法是:default-character-set=utf8
启动数据库
[root@duwers21 ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@duwers21 ~]# [root@duwers21 ~]#mysql_secure_installation [root@duwers21 ~]# mysql -uroot -p123456
//放一个图
测试数据
//创建数据库 mysql> create database tomcat; Query OK, 1 row affected (0.00 sec) //切换 mysql> use tomcat Database changed //创建表 mysql> create table tt(id int,namevarchar(128)); Query OK, 0 rows affected (0.08 sec) //插入数据 mysql> insert into tt values(1,"come on boy"),(2,"come on girl"); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 //创建用户tomcat并赋予权限 mysql> grant all on tomcat.* totomcat@localhost identified by ‘tomcat‘; Query OK, 0 rows affected (0.00 sec) mysql> \q Bye [root@duwers21 ~]#
//测试tomcat用户
[root@duwers21 ~]# mysql -utomcat -ptomcat
测试jsp链接mysql
安装mysql-connector
[root@duwers21 ~]# tar xvfmysql-connector-java-5.1.36.tar.gz -C /usr/local/src/ [root@duwers21 ~]# cd/usr/local/src/mysql-connector-java-5.1.36/ [root@duwers21 mysql-connector-java-5.1.36]#cp /usr/local/src/mysql-connector-java-5.1.36/mysql-connector-java-5.1.36-bin.jar /server/tomcat/lib/ #只需要复制到tomcat的lib目录下,重启tomcat就可以生效
[root@duwers21mysql-connector-java-5.1.36]# service tomcat stop; service tomcat start Using CATALINA_BASE: /server/tomcat Using CATALINA_HOME: /server/tomcat Using CATALINA_TMPDIR: /server/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /server/tomcat/bin/bootstrap.jar:/server/tomcat/bin/tomcat-juli.jar Using CATALINA_BASE: /server/tomcat Using CATALINA_HOME: /server/tomcat Using CATALINA_TMPDIR: /server/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /server/tomcat/bin/bootstrap.jar:/server/tomcat/bin/tomcat-juli.jar Tomcat started. [root@duwers21mysql-connector-java-5.1.36]#
测试
[root@duwers21 ~]# vim/server/tomcat/webapps/ROOT/mysql.jsp
[root@duwers21 ~]# cat/server/tomcat/webapps/ROOT/mysql.jsp <%@ pagecontentType="text/html;charset=utf-8"%> <%@ pageimport="java.sql.*"%> <html> <body> <% Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url="jdbc:mysql://localhost/tomcat?user=tomcat&password=tomcat&useUnicode=true&characterEncoding=utf-8"; Connection conn=DriverManager.getConnection(url); Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from tt"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()){%> step:<%=rs.getString(1)%> context:<%=rs.getString(2)%><br><br> <%}%> <%out.print("Congratulations!!! JSPconnect MYSQL IS OK!!");%> <%rs.close(); stmt.close(); conn.close(); %> </body> </html> #代码复制注意核对,由于编码问题,复制到vim中可能会增加多余的%号,记得删除
浏览器访问:http://192.168.20.21:8080/mysql.jsp
本文出自 “duwers” 博客,请务必保留此出处http://duwers.blog.51cto.com/7376225/1845977
tomcat环境搭建和管理及实现jsp通过tomcat连接mysql