最近学习java,机器上原来装了Apache+php+MySQL,就加一个Tomcat上去。(以下在win中操作,linux我用debian,apt-get,然后小改一下配置文件,大致相同)
一: 安装apache 并配置支持PHP
将apache安装在 D:\Net\apache2 目录(随自己喜好)下,将php-5.x.x-Win32.zip 里内容解压到 D:\Net\php 里。找到php目录里的php.ini-dist重命名为 php.ini并拷到 windows目录里(win2k里为winnt)如我的php.ini是拷到 C:\windows目录。再将php目录里的 php5ts.dll,libmysql.dll 拷到 系统目录(system/system32) 如我的是C:\windows\system32里
配置apache里的httpd.conf
打开 D:\Apache2\conf\httpd.conf 这个文件
找到 DocumentRoot "D:/Net/Apache2/htdocs" 将其改为你的WEB目录(可不改)如我的为 DocumentRoot "D:/Net/website"
找到 DirectoryIndex index.html index.html.var 在后面加入 index.htm index.php index.jsp
选择安装模式: 模块化模式安装 或 CGI模式安装 (选一样即可)
--------------模块化安装配置------------------------------------
找到 #LoadModule ssl_module modules/mod_ssl.so 这行,在此行后加入一行
LoadModule php5_module d:/php/php5apache2.dll
其中d:/php/php5apache2.dll 为你php目录中php5apache2.dll所在的位置
找到 AddType application/x-gzip .gz .tgz 这行,在此行后加入
ScriptAlias /php/ "d:/php/"
AddType application/x-httpd-php .php
Action application/x-httpd-php "/php/php-cgi.exe"
---------------------------------------------------------------
此时PHP环境基本已经配置成功
在WEB根目录(如我的D:\website)里建一个名为test.php的文件内容如下
<? echo phpinfo(); ?>
重新启动apache服务
用浏览器打开
http://localhost/test.php
如果可以看到php配置输出信息就OK了
二:安装mysql
将mysql安装到指定目录,(首选4.1,5.0的有的老程序可以支持的不够好,不过5.0的可以强制使用4.0模式工作)如我的是d:/Net/mysql。
增加mysql用户从本机访问数据库的权限(运行JSP需要),注意括号内的内容不要输入:
#cd /usr/local/mysql/bin
#./mysql
mysql>GRANT ALL PRIVILEGES ON *.* TO mysql(用户名)@localhost.localdomain(本机的主机名) IDENTIFIED BY ''(密码);
mysql>quit
三、配置php.ini并测试mysql(php.ini为 d:\windows下的 php.ini)
找到extension_dir = "./" 改为 extension_dir = "d:/php/ext"
找到
;extension=php_mysql.dll
;extension=php_mbstring.dll
将';'去掉改为
extension=php_mysql.dll
extension=php_mbstring.dll
(MySQL5中有php_mysqli.dll)
找到
;session.save_path = "/tmp"
将';'去掉 设置你保存session的目录,如
session.save_path = "D:/Net/php/session_temp";
宽字符集设定
; Set default language
mbstring.language = Neutral; Set default language to Neutral(UTF-8) (default)
mbstring.language = English; Set default language to English
mbstring.language = Japanese; Set default language to Japanese
;; Set default internal encoding
;; Note: Make sure to use character encoding works with PHP
mbstring.internal_encoding = UTF-8 ; Set internal encoding to UTF-8
;; HTTP input encoding translation is enabled.
mbstring.encoding_translation = On
;; Set default HTTP input character encoding
;; Note: Script cannot change http_input setting.
mbstring.http_input = pass ; No conversion.
mbstring.http_input = auto ; Set HTTP input to auto
; "auto" is expanded to "ASCII,JIS,UTF-8,EUC-JP,SJIS"
mbstring.http_input = SJIS ; Set HTTP2 input to SJIS
mbstring.http_input = UTF-8,SJIS,EUC-JP ; Specify order
;; Set default HTTP output character encoding
mbstring.http_output = pass ; No conversion
mbstring.http_output = UTF-8 ; Set HTTP output encoding to UTF-8
;; Set default character encoding detection order
mbstring.detect_order = auto ; Set detect order to auto
mbstring.detect_order = ASCII,JIS,UTF-8,SJIS,EUC-JP ; Specify order
;; Set default substitute character
mbstring.substitute_character = 12307 ; Specify Unicode value
mbstring.substitute_character = none ; Do not print character
mbstring.substitute_character = long ; Long Example: U+3000,JIS+7E7E
重启apache服务
在Web根目录下(如 D:\website)建立testdb.php文件内容如下:
<?php
$link=mysql_connect('localhost','root','123456');
if(!$link) echo "fail";
else echo "success";
mysql_close();
?>
用浏览器打开
http://localhost/testdb.php 如果输出success就OK了
MySql5使用4模式工作:my.ini中加上sql-mode='MYSQL40'或start mysqld with the option --sql-mode="MYSQL40"
#Use old password encryption method, my.ini中加上
old_passwords
my.ini中注释掉下面一行
--------REPLACE THIS TEXT------------------------------
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
------WITH THIS TEXT-----------------------
#sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
四、phpmyadmin的安装配置
将phpMyAdmin-2.x.x.zip解压到WEB根目录中去,重命名文件夹为phpmyadmin或其它
打开phpmyadmin/libraries 目录中的 config.default.php
找到
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '123456';
分别填上你mysql的用户和密码即可
五、安装jakarta-tomcat-5.5
添加环境变量CATALINA_HOME,tomcat安装目录。
1.整合Apache和Tomcat,下载mod_jk-apache-2.0.55.so,在httpd.conf中加入
LoadModule jk_module modules/mod_jk-apache-2.0.55.so
JkWorkersFile "d:/Net/Apache2/Apache2/conf/workers.properties"
workers.properties内容
# Usually commented out on production environments
[logger.apache2]
file="D:/Net/Apache2/Apache2/logs/error.log"
level=ERROR
# Provide the basic config needed
[config]
file=D:/Net/Apache2/Apache2/conf/workers2.properties
debug=1
# Provide the location of shm file on the Apache web server
[shm]
file=D:/Net/Apache2/Apache2/conf/jk2.shm
size=1000000
[channel.socket:localhost:8009]
port=8009
host=localhost
#define the worker
[ajp13:localhost:8009]
channel=channel.socket:localhost:8009
# Map the Tomcat examples webapp to the Web server uri space
[uri:/jsp-examples/*]
[uri:/*.jsp]
2.安装mySQL的JDBC驱动,mysql-connector-java-3.1.12.tar.gz,把解压出来的mysql-connector-java-3.1.12-bin.jar拷贝到/tomcat5.5/common/lib中,并添加到java的CLASSPATH中。
JAVA_HOME=.../jdk1.5.0
CLASSPATH=.;%JAVA_HOME%\lib;D:\Net\Tomcat5.5\common\lib\servlet-api.jar;D:\Net\Tomcat5.5\common\lib\mysql-connector-java-3.1.12-bin.jar
测试文件test.jsp
<%@ page contentType="text/html;charset=utf8"%>
<%@ page import="java.sql.*"%>
<html>
测试JSP页面(Tomcat+MySQL)<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost:3306/mysql?user=root&password=***&useUnicode=true&characterEncoding=utf8";
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from user";
ResultSet rs=stmt.executeQuery(sql); %>
| 您的第一个字段内容为: |
您的第二个字段内容为: |
<%while(rs.next()) {%>
|
<%=rs.getString(1)%> |
<%=rs.getString(2)%> |
<%}%>
<%out.print("Tomcat+JDK+mySQL完整测试,恭喜你,数据库操作成功!");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
注:MySQL5.0中文问题,我一直用utf8,中文没有问题。但是很我国人喜欢用gbk。gb2312和gb18030是国家标准,但很多人就是喜欢gbk,gb2312字少,gb18030比较恶心。why no utf8?
gbk解决方案如下:
1、在dos环境下,用mysql --default-character-set=gbk -u root -p 这句话进入mysql~~
2、建数据库、表,如下:
create database admin;
use admin;
CREATE TABLE admin (
admin_name char(20) NOT NULL,
admin_password char(20) NOT NULL
) TYPE=MyISAM, default character set gbk;
3Eclipse中数据库插件中操作数据库时的中文编码问题:
当在Eclipse中装上来你所喜欢的数据库插件的时候,都会出现在你查询某一个数据库之后出现记录中的汉字乱码。解决方法很简单,只要在你建立数据库连接的时候把URL设置成下面这个样子就可以了:
URL=jdbc:mysql://localhost:3306/?useUnicode=true&characterEncoding=GBK
编辑JSP文件时,<%@ page contentType="text/html;charset=GB2312" %>设置成这样子,且这条语句必须是整个JSP文件的第一条语句,就可以避免JSP文件在浏览器中显示时出现乱码;对于如果在JSP网页中要从HTML页面中接收数据,那么在JSP中的接收语句的汉字编码应与数据来源的HTML的汉字编码相同;例如HTML的汉字编码为charset=GBK,那么在JSP中的接收语句就应该这样设置:
request.setCharacterEncoding("GBK");如果还要将接受的数据插入到数据库中(以插入MySQL5.0数据库为例),那么在插入数据库时的汉字编码也应与HTML文件中的汉字编码相同,设置如下:
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample_db?user=dazern&password=5201314&useUnicode=true&characterEncoding=GBK");这样设置后数据库中的数据才不会乱码.