这篇文章主要是面对初学者和创业公司,目的是在LINUX上安装一个真正能可用的TOMCAT的运行环境,可以作为商用服务器使用,该篇文章的版本会随时更新,保证真正可用。
文档版本:v1.0.0
作者:学涵 电子邮件: qinxuehan@126.com
环境:
Linux版本:CentOs(64位)
JDK7版本:jdk-7u79-linux-x64
Tomcat版本:apache-tomcat-7.0.65
Nginx版本:yum安装
MysqL版本:yum安装5.6版本
1.安装JDK 7
下载参考页:
http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260.html
创建目录,下载JDK,解压
cd/usr/java
wgethttp://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz?AuthParam=1472528863_bea358019bad6edc582bd5efa9d05eef
修改文件名:
mv jdk-7u79-linux-x64.tar.gz\?AuthParam\=1472528863_bea358019bad6edc582bd5efa9d05eef jdk-7u79-linux-x64.tar.gz
tarzvfxjdk-7u79-linux-x64.tar.gz
修改LINUX启动配置
vi /etc/profile
在文件底部加入如下内容
#setjavaenvironment
JAVA_HOME=/usr/java/jdk1.7.0_79
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOEM/lib/dt.jar
PATH=$JAVA_HOME/bin:$PATH
exportJAVA_HOMECLASSPATHPATH
然后执行如下命令,让配置生效:
source/etc/profile
2.安装TOMCAT
mkdir /usr/tomcat
cd /usr/tomcat
wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.65/bin/apache-tomcat-7.0.65.tar.gz
tar zvfx apache-tomcat-7.0.65.tar.gz
mv apache-tomcat-7.0.65 /usr/local/
修改LINUX启动配置
vi/etc/profile
加入如下内容
#settomcatenvironment
TOMCAT_HOME=/usr/local/apache-tomcat-7.0.65
exportTOMCAT_HOME
CATALINA_HOME=/usr/local/apache-tomcat-7.0.65
exportCATALINA_HOME
然后执行如下命令,让配置生效:
source/etc/profile
把启动脚本复制到/etc/init.d/自启动脚本目录中
cp /usr/local/apache-tomcat-7.0.65/bin/catalina.sh /etc/init.d/tomcat
并修改启动脚本
vi /etc/init.d/tomcat
### BEGININIT INFO
#Provides: tomcat
#required-Start: $remote_fs $network
#required-Stop: $remote_fs $network
#Default-Start: 2 3 4 5
#Default-Stop: 0 1 6
#Short-Description: The tomcat Java Application Server
### ENDINIT INFO
JAVA_HOME=/usr/java/jdk1.7.0_79
exportJAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/local/apache-tomcat-7.0.65
export CATALINA_HOME
然后执行chkconfig 让tomcat启动脚本在自启动服务中生效,命令如下
chkconfig --add tomcat
如下命令启动tomcat
/etc/init.d/tomcat start
然后使用netstat �Cln 如果8080端口开通,说明启动成功
3.安装MysqL5.6
wget http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
rpm -i MysqL-community-release-el6-5.noarch.rpm
yum install MysqL-server
MysqL 5.6版本需要配置
vi/etc/my.cnf
修改配置如下:
#binlog server
server_id=1
log-bin=MysqL-bin
binlog_format= ROW
log_bin_trust_function_creators=On
磁盘挂载后,需要建立文件夹:
rm -rf /var/lib/MysqL
mkdir -p /home/data/MysqL
chown -R MysqL:MysqL /home/data/MysqL
ln -s /home/data/MysqL /var/lib/MysqL
chown MysqL:MysqL -R /var/lib/MysqL
如需执行如下操作初始化MysqL:
sudo -u MysqL MysqL_install_db
启动MysqL
/etc/init.d/MysqLd start
执行安全配置:
MysqL_secure_installation
一般把test数据库去掉
MysqL -u root -p
然后在MysqL>命令行环境中执行下列三条语句
CREATE USER 'xd'@'%'IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON*.* TO 'wd'@'%' IDENTIFIED BY '123456' WITH GRANT OPTIONMAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0MAX_USER_CONNECTIONS 0;
flush privileges;
注意:MysqL安装需要LINUX把selinux关闭,不然不能打开端口
vi /etc/selinux/config
修改选项:
SELINUX=disabled
然后重启服务器,如果上述选项已经符合,则无需执行以下命令重启服务器
shutdown �Cr now
4.安装Nginx
使用yum安装Nginx
yum -y install Nginx
启动Nginx
/etc/init.d/Nginx start
netstat命令看到80端口已经启动,则安装成功
netstate �Cln
删除ngin默认测试页
rm -rf /usr/share/Nginx/html/*
mkdir -p /home/html/localhost/app
chown -R Nginx:Nginx /home/html/localhost/*
vi /home/html/localhost/app/index.html
输入:hello world!
使用vi打开默认配置文件
vi /etc/Nginx/conf.d/default.conf
location /app {
root/home/html/localhost/app;
indexindex.html index.htm;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8080;
}
/etc/init.d/Nginx restart
5.配置TOMCAT
修改server.xml
cd /usr/local/apache-tomcat-7.0.65/conf
vi server.xml
<Hostname="localhost"appBase="webapps"
unpackWARs="true"autoDeploy="true">
<Context path="/" docBase="myapp">
<!-- Default set of monitoredresources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
<!-- SingleSignOn valve,shareauthentication between web applications
Documentation at:/docs/config/valve.html -->
<!--
<ValveclassName="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes allexample.
Documentation at:/docs/config/valve.html
Note: The pattern used isequivalent to using pattern="common" -->
<ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs"
prefix="localhost_access_log."suffix=".txt"
pattern="%h %l %u %t"%r" %s %b" />
</Host>
注意:其中标红的Context节点docBase的myapp是我们的war包名称,比如myapp.war,则
<Connectorport="8080" protocol="HTTP/1.1"
connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI="true" />
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
useBodyEncodingForURI="true"
redirectPort="8443" URIEncoding="UTF-8"/>
然后把我们的myapp.war复制到/usr/local/apache-tomcat-7.0.65/webapps/目录中
cp /root/myapp.war/usr/local/apache-tomcat-7.0.65/webapps/
重新启动tomcat
/etc/init.d/tomcat stop
/etc/init.d/tomcat start
使用netstat命令查看8080端口是否启动
netstat -ln
附录:查看tomcat日志
tail -f/usr/local/apache-tomcat-7.0.65/logs/catalina.out
6.结束
使用netstat -ln查看各个端口是否启动
netstat -ln
得到如下列表:
[root@localhost ~]# netstat -ln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN