Apache integrates tomcat to connect to mysql

Source: Internet
Author: User
Tags apache tomcat

This article is performed after the LAMP environment is installed. To view the LAMP installation configuration, see my previous LAMP installation documentation.
Lab environment:
Linux: Centos6.0 (64-bit)
Apache: httpd-2.2.19.tar.gz
Mysql: mysql-5.5.12.tar.gz
Software Used
Tomcat: apache-tomcat-7.0.20.tar.gz
Tomcat Connector: tomcat-connectors-1.2.32-src.tar.gz
JDK: jdk-6u13-linux-i586.bin
Tomcat database connection: mysql-connector-java-5.1.17.zip
First, check whether JDK is installed in your system.


 
Uninstall the built-in jdk


 
Install jre. Note: If your environment has JAVA Development, You need to download JDK. If it is only a simple JAVA environment, you only need to install JRE. That is to say, JDK contains all the contents of JRE.
Set the permission of the JRE file to executable, and then run the file

 
Modify system environment variables for debugging and use of other installation software.


 
Export JAVA_HOME =/usr/local/jdk
Export PATH = $ PATH: $ JAVA_HOME/bin

 
Install TOMCAT and unzip the tomcat installation package.
 


 
Configure the tomcat environment, and add the tomcat environment variable at the end (the premise is to install jdk)

 
There is a startup file under the Bin directory, shutdown. sh to close tomcat, startup. sh is to start tomcat

 

 
Enter http: // 192.168.10.49: 8080 to open the default tomcat page.

 
Finally, let the server automatically run tomcat at startup.
The simplest method is to automatically start Tomcat through startup. sh and edit vi/etc/rc. d/rc. local.
Next, we need to integrate apache and tomcat with a connector called apache tomcat.

Native directory (different versions may have different imitation locations)
Confirm the location of your apache apxs File

 
Compile and generate mod_jk

 
Copy the generated mod_jk.so file after compilation. Pay attention to the location of your apache modules directory.


 
Add index. jsp to DirectoryIndex

Add the statement about loading mod_jk.

 
Create two configuration files mod_jk.conf under/usr/local/apache2/conf /.

 
Specifies the workers file required for mod_jk module work. location of properties, where jk logs are stored, jk log level, select the log format, JkOptions sends key SSL dimensions for display, and JkRequestLogFormat sets the required format, send all servlet and jsp requests to Tomcat through the ajp13 protocol for Tomcat to process
JkWorkersFile/usr/local/apache2/conf/workers. properties
 
###### Where to put jk logs
JkLogFile/usr/local/apache2/logs/mod_jk.log
 
###### Set the jk log level [debug/error/info]
JkLogLevel info
 
###### Select the log format
JkLogStampFormat "[% a % B % d % H: % M: % S % Y]"
 
###### JkOptions indicate to send ssl key size,
JkOptions + ForwardKeySize + ForwardURICompat-ForwardDirectories
 
###### JkRequestLogFormat set the request format
JkRequestLogFormat "% w % V % T"
 
###### Send all servlet and jsp requests to Tomcat through the ajp13 protocol for Tomcat to process
JkMount/servlet/* worker1
JkMount/*. jsp worker1
JkMount/*. do worker1

 
Create two configuration files workers. propertie under/usr/local/apache2/conf /.
Define a worker named worker1 ajp13 and type


 
Find the tomcat configuration file path: edit the Tomcat configuration file server. xml

 
Add the following content to the <Host> segment:

 
Compile an index. jsp webpage

 
Restart tomcat and apache services

 
Integration completed

 
Jsp uses mysql-connector-java-5.1.13-bin.jar to connect to mysql database,
Decompress the zip package,

 
Cp mysql-connector-java-5.1.17-bin.jar/usr/local/tomcat/lib/
Put the mysql-connector-java-5.1.13-bin.jar under the tomcat directory lib

 
Compile a test page test_mysql.jsp. If the page shows OK, it indicates that you can connect to the database.
 

 
<% @ Page language = "java" %>
<% @ Page import = "com. mysql. jdbc. Driver" %>
<% @ Page import = "java. SQL. *" %>
<%
String driverName = "com. mysql. jdbc. Driver ";
String userName = "root ";
String userPasswd = "123 ";
String dbName = "test ";
String url = "jdbc: mysql: // localhost/" + dbName + "? User = "+ userName +" & password = "+ userPasswd;
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
Try
{
Connection connection = DriverManager. getConnection (url );
Out. println ("o k! ");
Connection. close ();
}
Catch (Exception e)
{
Out. println ("connent mysql error:" + e );
}
%>

 
Restart tomcat and apache


This article is from the "ch" blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.