这篇文章主要是面对初学者和创业公司,目的是在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
wget http://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
tar zvfx jdk-7u79-linux-x64.tar.gz
修改LINUX启动配置
vi /etc/profile
在文件底部加入如下内容
#set java environment
JAVA_HOME=/usr/java/jdk1.7.0_79
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOEM/lib/dt.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
然后执行如下命令,让配置生效:
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
加入如下内容
#set tomcat environment
TOMCAT_HOME=/usr/local/apache-tomcat-7.0.65
export TOMCAT_HOME
CATALINA_HOME=/usr/local/apache-tomcat-7.0.65
export CATALINA_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
在文件最开始#!/bin/sh处下加入如下内容:
### 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 –ln 如果8080端口开通,说明启动成功
3.安装MYSQL 5.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数据库去掉
启动之后创建用户xd,首先执行命令进入MYSQL操作环境
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 –r now
4.安装NGINX
使用yum安装nginx
yum -y install nginx
启动nginx
/etc/init.d/nginx start
netstat命令看到80端口已经启动,则安装成功
netstate –ln
删除ngin默认测试页
rm -rf /usr/share/nginx/html/*
配置NGINX支持TOMCAT
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;
index index.html index.htm;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8080;
}
保存退出之后重启nginx服务器
/etc/init.d/nginx restart
5.配置TOMCAT
修改server.xml
cd /usr/local/apache-tomcat-7.0.65/conf
vi server.xml
找到HOST节点修改内容如下:
<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,则
配置支持utf8中文环境,找到Connector节点:
<Connectorport="8080" protocol="HTTP/1.1"
connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI="true" />
支持中文utf8修改如下:
<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
标红的部分80端口(nginx),8080端口(tomcat),3306端口(mysql)如果出现,表示成功了