前段时间,公司接单子做一套JavaWeb系统。作为主要负责人,我们小组的工作一切顺利。但在交付前,客户要求该Web系统做到一键安装部署。这是因为该系统使用环境为机房局域网,每个机房需要部署一套,客户要求可以随时用光盘拷贝安装。
最终我们做到了10秒一键部署服务器。这是一套JavaWeb系统, 使用了MySql以及Tomcat,部署目标机器为WinXP及以上,32位或64位通用。
为了满足公司项目的保密要求,本文中将称该系统为“system”,并将所有涉及到系统名称的地方替换为system。
一键部署整体使用bat批处理实现,主要需要处理tomcat、JDK以及mysql。
同时为了方便用户使用,也打包了谷歌浏览器。
为了兼容WinXP,所使用的mysql、Chrome版本均为兼容WinXP的版本。 mysql5.5 以及 Chrome 49.0.2623.112 。
为了实现32位以及64位的通用,所有软件均使用32位版本。
在磁盘根目录(例如D盘)新建文件夹system_setup,作为一键部署安装包的根目录。这里请在磁盘根目录创建该文件夹,以防mysql、tomcat不识别特殊路径(如中文路径、包含空格字符路径等)。
打开system_setup,创建“开始一键部署安装.bat”。若不知道如何创建bat文件,可以新建txt文件,并将后缀名一并更改即可。用相同的方式创建“一键卸载.bat”。这两个bat文件的内容暂时留空即可,稍后会在 Step5 给出。 另外,创建一个systemData文件夹,用来放除去前面提到的这两个bat以外的所有东西。
完成该步骤以后,system_setup目录将如下图所示
当tomcat、JDK、mysql、Chrome都添加到 systemData systemData 文件夹将会如下图所示
其中, createlink.vbs 是创建一个桌面快捷方式, logo.ico 是桌面快捷方式的图标。
createlink.vbs代码如下:
cd = createobject("Scripting.FileSystemObject").GetFolder(".").Path
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop=WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop & "\系统名称.lnk")
oShellLink.TargetPath= cd & "\Chrome\Application\chrome.exe"
oShellLink.Arguments="http://localhost:8080/tomcatWebappsSystemDirName/"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = cd & "\logo.ico"
oShellLink.WorkingDirectory = ""
oShellLink.Save
上面代码中的“系统名称”以及 tomcatWebappsSystemDirName 需要大家根据网站实际情况来替换。
下面将分别讲述该如何处理tomcat、JDK、mysql、Chrome。
所使用tomcat版本为apache-tomcat-7.0.90,JDK版本为 jdk1.7.0_03 ,若使用其他版本,请注意tomcat与JDK版本的搭配问题。
下载 apache-tomcat-7.0.90 后,解压到 apache-tomcat-7.0.90 文件夹内,并将该文件夹整个复制到systemData文件夹内。将JavaWeb的部署文件夹放入apache-tomcat-7.0.90\webapps中。
下载 jdk1.7.0_03 ,解压到 jdk1.7.0_03文件夹内,将 jdk1.7.0_03 文件夹放入新建文件夹Java32中, 并将Java32文件夹放入apache-tomcat-7.0.90\bin中。
在 apache-tomcat-7.0.90\bin 文件夹下新建 init_tomcat.bat ,并将以下代码复制到该文件中:
@echo off
cd..
set CATALINA_HOME=%cd%
set CATALINA_BASE=%cd%
cd bin
echo ----- tomcat service install begin ...-----
call "%cd%\service.bat" install tomcat7
echo ----- tomcat service install success !-----
echo ----- tomcat service start begin ...-----
sc config tomcat7 start= auto
sc start tomcat7
echo ----- tomcat service start success !-----
wmic service where name="tomcat7" changestartmode "automatic"
wmic service where name="tomcat7" startservice
选中 apache-tomcat-7.0.90\bin\service.bat ,右键编辑, 在 rem注释段后面的 setlocal 行下面添加
set JAVA_HOME=%cd%\Java32\jdk1.7.0_03
打开 apache-tomcat-7.0.90\bin\Java32\jdk1.7.0_03 , 添加文件 init_jdk.bat ,编辑内容为:
@echo off
echo ----- jdk init begin ...-----
set jdkpath=%cd%
setx JAVA_HOME "%jdkpath%" -m
setx CLASSPATH ".;%%JAVA_HOME%%\lib\tools.jar;%%JAVA_HOME%%\lib\dt.jar" -m
echo %Path%|find /i "%java_home%" && set IsNull=true || set IsNull=false
if not %IsNull%==true (
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_SZ /d "%Path%;%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin" /f
setx Path "%%JAVA_HOME%%\bin;%Path%"
)
echo ----- jdk init success ! -----
至此,tomcat以及JDK配置结束。相关目录截图如下
所用mysql版本为5.5,为了兼容WinXP。
下载安装后,使用navcat等管理软件将所需数据库表以及数据等导入。停止mysql服务后,将整个mysql文件夹复制到 system_setup\systemData 中。
打开 mysql\bin ,新建 init_mysql.bat , 代码如下:
@echo off
cd ..
echo datadir="%cd%\data" >> my.ini
echo secure-file-priv="%cd%\uploads" >> my.ini
echo ----- mysql service install begin ...-----
bin\mysqld install mysql --defaults-file="%cd%\my.ini"
echo ----- mysql service install succee ! -----
cd bin
echo ----- mysql service start begin ...-----
net start mysql
sc config mysql start=auto
net stop mysql
net start mysql
echo ----- mysql service start success ! -----
my.ini在文章末尾的附录A中给出参考。
至此,mysql配置完成。
相关截图如下:
安装 Chrome 49.0.2623.112 ,找到安装路径,将整个Chrome文件夹复制到systemData中即可。
所有东西准备就绪,下面就是一键部署安装.bat与一键卸载.bat的代码了。
现在的目录结构截图如下:
开始一键部署安装.bat代码如下:
@echo off
echo ----- 一键部署安装 begin ... ------
cd /d %~dp0
set appDir=%cd%
echo 启动数据库配置...
cd systemData\mysql\bin
call init_mysql.bat
echo 启动Web服务器配置...
cd %appDir%
cd systemData\apache-tomcat-7.0.90\bin
call init_tomcat.bat
cd %appDir%
cd systemData
start createlink.vbs
echo 启动运行环境配置...
cd %appDir%
cd systemData\apache-tomcat-7.0.90\bin\Java32\jdk1.7.0_03
call init_jdk.bat
echo install end...
echo 安装完毕!
pause
echo ----- 一键部署安装 成功 !-----
pause
一键卸载.bat代码如下:
sc stop mysql
ping /n 2 127.0.0.1 >nul
sc delete mysql
ping /n 2 127.0.0.1 >nul
sc stop tomcat7
ping /n 2 127.0.0.1 >nul
sc delete tomcat7
ping /n 3 127.0.0.1 >nul
del %USERPROFILE%\Desktop\桌面链接名字.lnk
ping /n 2 127.0.0.1 >nul
echo 卸载完毕,按任意键退出
pause
请将上面代码中的桌面链接名字改为系统名字。
至此,整个 JavaWeb + tomcat + mysql 的一键部署安装就结束了。在使用时,只需要将整个system_setup文件夹拷贝,运行 “开始一键部署安装.bat” 即可一键部署安装。
以下文件上传到了博主的资源页:
下载地址为:
https://download.csdn.net/download/wf824284257/10898363
# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8
# Here follows entries for some specific programs
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-beep
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
# The MySQL server
[mysqld]
federated
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
character-set-server=utf8
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1
# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,
# MASTER_USER=, MASTER_PASSWORD= ;
#
# where you replace , , by quoted strings and
# by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host =
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user =
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port =
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = C:\\mysql\\data\\
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = C:\\mysql\\data\\
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
port=3306
character-set-server=utf8
default-storage-engine=INNODB
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
log-output=FILE
general-log=0
general_log_file="B3-GONGWEI.log"
slow-query-log=1
slow_query_log_file="B3-GONGWEI-slow.log"
long_query_time=10
log-error="B3-GONGWEI.err"
server-id=1
max_connections=500
query_cache_size=0
table_open_cache=2000
tmp_table_size=14M
thread_cache_size=10
myisam_max_sort_file_size=100G
myisam_sort_buffer_size=19M
key_buffer_size=8M
read_buffer_size=37K
read_rnd_buffer_size=256K
innodb_flush_log_at_trx_commit=1
# The size of the buffer InnoDB uses for buffering log data. As soon as
# it is full, InnoDB will have to flush it to disk. As it is flushed
# once per second anyway, it does not make sense to have it very large
# (even with long transactions).
innodb_log_buffer_size=1M
innodb_buffer_pool_size=8M
innodb_log_file_size=48M
innodb_thread_concurrency=9
innodb_autoextend_increment=64
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
back_log=80
flush_time=0
join_buffer_size=256K
max_allowed_packet=4M
max_connect_errors=100
open_files_limit=4161
query_cache_type=0
sort_buffer_size=256K
table_definition_cache=1400
binlog_row_event_max_size=8K
sync_master_info=10000
sync_relay_log=10000
sync_relay_log_info=10000