一、环境部署流程:安装JDK
### --- 安装JDK(java解析器)
~~~ 首先安装gcc
[root@server21 ~]# yum install -y gcc*
### --- 将软件包tomcat.zip上传、解压、并移至指定位置
[root@server21 ~]# unzip tomcat.zip
[root@server21 tomcat]# tar -zxvf jdk-7u25-linux-x64.tar.gz
[root@server21 tomcat]# cp -r jdk1.7.0_25/ /usr/local/jdk1.7
### --- 配置JDK的环境变量
[root@server21 tomcat]# vim /etc/profile // 末尾添加以下内容
#java
export JAVA_HOME=/usr/local/jdk1.7
export JAVA_BIN=/usr/local/jdk1.7/bin
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[root@server21 tomcat]# source /etc/profile // 加载环境变量生效
### --- 查看java是否安装成功
[root@server21 tomcat]# java -version
java version "1.7.0_25" // 能显示java版本说明java部署成功了
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
二、安装tomcat
### --- 将软件包上传、解压、并复制到指定目录下
[root@server21 ~]# cd tomcat
[root@server21 tomcat]# tar -zxvf apache-tomcat-7.0.42.tar.gz
[root@server21 tomcat]# cp -r apache-tomcat-7.0.42 /usr/local/tomcat
### --- 配置tomcat的环境变量
[root@server21 tomcat]# vim /etc/profile // 末尾添加如下内容
#tomcat
export TOMCAT_HOME=/usr/local/tomcat
export PATH=$PATH:$TOMCAT_HOME/bin
[root@server21 ~]# source /etc/profile
### --- 将tomcat的启动脚本赋予执行权限
~~~ 根据自己的需求添加执行权限
[root@server21 ~]# chmod +x /usr/local/tomcat/bin/*
### --- 开启tomcat
[root@server21 ~]# ll /usr/local/tomcat/bin/catalina.sh // 该文件有执行权限
-rwxr-xr-x 1 root root 19877 Feb 4 09:49 /usr/local/tomcat/bin/catalina.sh
[root@server21 ~]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.7
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
### --- 在客户端访问页面进行测试http://20.20.20.21:8080/
~~~ http://20.20.20.21:8080/可以正常访问到apache的测试页面
[root@server21 ~]# netstat -antp |grep :8080
tcp 0 0 :::8080 :::* LISTEN 1856/java
三、安装MySQL数据库
### --- 安装依赖包ncurses-devel
[root@server21 ~]# yum install -y ncurses-devel gcc*
### --- 将MySQL文件进行传输到20.20.20.21上进行安装
[root@server21 tomcat]# tar -zxvf mysql-5.1.55.tar.gz
[root@server21 mysql-5.1.55]# useradd -r -s /sbin/nologin mysql
[root@server21 mysql-5.1.55]# ./configure --prefix=/usr/local/mysql --with-charset=utf8 \
> --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312 \
> && make && make install
### --- 生成配置文件
[root@server21 mysql-5.1.55]# cp -a support-files/my-medium.cnf /etc/my.cnf
[root@server21 mysql-5.1.55]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@server21 mysql-5.1.55]# ln -s /usr/local/mysql/sbin/* /usr/local/sbin/
### --- 初始化数据库,生成授权表
[root@server21 mysql-5.1.55]# cd /usr/local/mysql/
[root@server21 mysql]# ./bin/mysql_install_db --user=mysql
Please report any problems with the /usr/local/mysql/bin/mysqlbug script!
### --- 生成启动管理脚本,启动mysql并设置开机自启
[root@server21 ~]# cd tomcat/mysql-5.1.55/support-files/
[root@server21 support-files]# cp -a mysql.server /etc/init.d/mysqld
[root@server21 support-files]# chmod +x /etc/init.d/mysqld
[root@server21 support-files]# chkconfig -add mysqld
-add: unknown option
[root@server21 support-files]# chkconfig --add mysqld
[root@server21 support-files]# chkconfig mysqld on
[root@server21 support-files]# service mysqld start
Starting MySQL. SUCCESS!
### --- 为数据库的管理员root设置登录密码
[root@server21 support-files]# mysqladmin -uroot password 123456
### --- 登录数据库,查看是否安装正确
[root@server21 support-files]# mysql -uroot -p
Enter password: 123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
四、安装nginx
### --- 解压nginx
[root@server21 tomcat]# tar -zxvf nginx-1.2.6.tar.gz
### --- 安装nginx依赖包
[root@server21 tomcat]# yum install -y pcre-devel zlib-devel gcc*
### --- 添加用户
[root@server21 tomcat]# useradd -r -s /sbin/nologin nginx
### --- 编译并安装
[root@server21 tomcat]# cd nginx-1.2.6
[root@server21 nginx-1.2.6]# ./configure --user=nginx --group=nginx && make && make install
### --- 修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
user nginx;
upstream tomcat { // 添加负载调度(为了后期扩展更多Tomcat服务器方便)
server 20.20.20.21:8080;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location / { // 在server里面添加反向代理配置;此配置可能文件报错,建议使用下面配置
proxy_pass http://tomcat;
proxy_set_header Host $host;
}
#OR:也可以合并到卸载一个配置里面
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat;
proxy_set_header Host $host;
}
### --- 重启服务
[root@server21 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@server21 ~]# nginx // 启动nginx
[root@server21 ~]# pkill -HUP nginx // 重启nginx
[root@server21 ~]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 47965/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 50210/nginx
tcp 0 0 :::8080 :::* LISTEN 1856/java
### --- 在客户端进行测试,输入nginx地址,打开的为tomcat部署的网站
~~~ # http://20.20.20.21/
~~~ 通过nginx对tomcat进行负载调度,实则访问出来的apache是通过nginx负载调度出来的,
~~~ 而实实在在提供服务的还是tomcat
五、部署Java 的WAR包
### --- 在/usr/local/tomcat/conf/server.xml配置文件中的server区域中添加标红内容
~~~ 项目名称为atyanqi.war
[root@server21 ~]# vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="atyanqi.war" debug="0" privileged="true"/>
### --- 将war包拷贝到webapps目录中
[root@server21 ~]# cd /usr/local/tomcat/
[root@server21 tomcat]# cp -a /root/tomcat/atyanqi.war /usr/local/tomcat/webapps/
### --- 删除网站的ROOT目录
root@server21 tomcat]# rm -rf webapps/ROOT/ // 有的话删除没有忽略
### --- 重启tomcat服务
[root@server21 tomcat]# /usr/local/tomcat/bin/catalina.sh stop
[root@server21 tomcat]# /usr/local/tomcat/bin/catalina.sh start
[root@server21 tomcat]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 47965/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 50210/nginx
tcp 0 0 :::8009 :::* LISTEN 50381/java
tcp 0 0 :::8080 :::* LISTEN 50381/java
### --- 测试
### --- 使用客户端浏览访问Nginx服务的端口
~~~ #注意:原本的tomcat使用8080端口进行访问,也可以将端口修改为80 使用IP直接访问,
~~~ 修改/usr/local/tomcat/conf/server.xml配置中的8080改变为80
~~~ (切记不要和其他web服务器冲突)
~~~ #使用Chrome测试http://20.20.20.21/访问atyanqi java项目包;
~~~ 第一次访问比较慢,是因为第一次需要对atyanqi.wra包解压缩,所以速度比较慢。
~~~ 可以访问到atyanqi.wra包内容
六、创建多个tomcat实例(拓展)
### --- 首先将tomcat进行停止
[root@server21 ~]# /usr/local/tomcat/bin/catalina.sh stop
### --- 拷贝原来的tomcat到另外一个目录,如tomcat-2,清除logs目录
[root@server21 ~]# cd /usr/local/
[root@server21 local]# cp -a tomcat/ tomcat-2
[root@server21 local]# cd tomcat-2/
[root@server21 tomcat-2]# rm -rf logs/*
### --- 修改Tomcat-2中的/conf/server.xml文件,
~~~ 把shutdown和Connector端口修改成另外的数值,关闭端口修改为8006,连接端口修改为8090
[root@server21 tomcat-2]# vim conf/server.xml
<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
### --- 修改startup.sh和shudown.sh文件
~~~ 在第一行均加入:export CATALINA_HOME=/usr/local/tomcat-2
[root@server21 tomcat-2]# vim /usr/local/tomcat-2/bin/startup.sh
export CATALINA_HOME=/usr/local/tomcat-2 // 添加在开头
[root@server21 tomcat-2]# vim /usr/local/tomcat-2/bin/shutdown.sh
### --- 启动tomcat
[root@server21 ~]# /usr/local/tomcat/bin/catalina.sh start
[root@server21 ~]# /usr/local/tomcat-2/bin/catalina.sh start
[root@server21 ~]# netstat -antp // 查看8080和8090 端口是否正常启动
tcp 0 0 :::8080 :::* LISTEN 50458/java
tcp 0 0 :::8080 :::* LISTEN 50458/java
### --- 修改nginx的配置文件
~~~ 可以直接将8090写入配置好的Nginx负载均衡中。
[root@server21 ~]# vim /usr/local/nginx/conf/nginx.conf
upstream tomcat {
server 20.20.20.21:8080;
server 20.20.20.21:8090;
}
[root@server21 ~]# pkill -HUP nginx
~~~ 修改访问页面,为了实验的成功性
[root@server21 ~]# cp -a tomcat/atyanqi.psc /usr/local/tomcat-2/webapps/
[root@server21 ~]# rm -rf /usr/local/tomcat-2/webapps/atyanqi.war
[root@server21 ~]# mv /usr/local/tomcat-2/webapps/atyanqi.psc /usr/local/tomcat-2/webapps/atyanqi.war
### --- 查看结果
~~~ 浏览http://192.168.10.20:8080 OR http://192.168.10.20
~~~ 浏览http://192.168.10.20:8090