nginx+tomcat 企业级jsp项目部署

项目实验思路及重点内容(步骤及重难知识点)
1.环境:
主机 功能介绍 ip地址 主要软件
nginx1 nginx高可用节点,同时作为反向代理服务 192.168.200.202 nginx+keepalived
nginx2 nginx高可用节点,同时作为反向代理服务 192.168.200.203 nginx+keepalived
tomcat1 后端jsp网站服务器 192.168.200.204 jdk+tomcat+SL项目
tomcat2 后端jsp网站服务器 192.168.200.205 jdk+tomcat+SL项目
mysql 后端数据系统 192.168.200.206 mysql

2.实施步骤:
步骤 说明
第一步 配置nginx1:Nginx调度器构建、keepalived服务构建
第二步 配置nginx2:Nginx调度器构建、keepalived服务构建
第三步 tomcat1:tomcat安装配置、SLsalesystem项目发布
第四步 tomcat2:tomcat安装配置、SLsalesystem项目发布
第五步 SLsalesystem应用连接mysql数据库

3.重点、难点:
重点一 集群中网站访问处理过程:
1.客户端访问www.linuxfan.cn-->dns域名解析:将www.linuxfan.cn解析为(192.168.200.100和200);
2.访问nginx:通过keepalived提供VIP(100和200)、Nginx提供80端口响应客户端建立TCP连接;处理网页读取配置项(location{index.jsp;proxy_pass http://tomcat_servers}-->upstream tomcat_servers{server 192.168.200.204:8080和205(tomcat)})
3.访问tomcat:nginx作为代理服务器访问tomcat的8080建立tcp连接,请求index.jsp网页,读取tomcat配置(ROOT目录或docbase指定目录,找到默认项目,找到index.jsp),nginx将获取的网页发送客户端浏览器;
4.访问数据库:网页请求访问数据库,tomcat的app数据库配置(webapps/项目/WEB-INF/classes/jdbc.properties)提供访问数据库参数(地址、用户、密码、数据库及其他性能参数),tomcat进程访问数据库并请求数据库,返回给nginx,nginx在返回客户端;

项目技术点:

  1. dns轮询:将www.linuxfan.cn解析为192.168.200.100和192.168.200.200
  2. Keepalived双实例确保nginx的高可用和负载均衡,避免备设备闲置,使用track配置监控nginx状态,当nginx故障时关闭keepalived,实现故障自动切换;
  3. Nginx的负载均衡和反向代理管理后端tomcat,upstream配置项实现tomcat负载均衡,proxy_pass配置实现反向代理加速;
  4. Tomcat服务器上传项目到webapps目录,通过修改项目的JDBC参数连接后端数据库;
  5. Mysql数据创建项目数据库、授权并导入项目数据。
    重点二 Keepalived双实例:
  6. 作用:充分利用设备资源,结合DNS轮询提高整体架构的性能,为nginx提供负载均衡;
  7. 双实例的原理(配置文件):使用vrrp_instance创建两个实例(V1和V2),对于V1实例nginx1设备为主、nginx2设备为从,对于V2实例nginx1设备为从、nginx2为主;每个实例设置独立的vip接收客户端的访问实现负载均衡和高可用。设备之间互为主备提供网站的高可用。
  8. nginx对外提供服务,如果nginx服务故障,keepalived如何知道并完成主备切换?
    1)全局配置中设置vrrp_script脚本:
    vrrp_script nginx {
    script /opt/chk_nginx.sh ##指定脚本路径及名称
    interval 2 ##检测的频率
    weight -10 ##权重
    }
    2)实例中调用vrrp_script脚本完成跟踪:
    track_script {
    nginx ##vrrp_script定义的名称
    }
    3)/opt/chk_nginx.sh脚本的功能:实现检测nginx服务状态,如果服务检测失败,停止keepalived,实现故障切换。
    重点三 百度扩展学习:推荐虫部落快搜(必应、高级搜索、搜狗微信或知乎)
    Nginx配置文件:大家百度gzip压缩配置、open_file文件打开配置、proxy代理配置、fastcgi配置;
    Tomcat优化:内存优化、连接池优化、并发量的优化。
    重点四 Tomcat项目连接数据库:
  9. 数据库:创建数据库、授权、导入项目数据(找开发或在SVN等版本管理服务器下载);
  10. 项目修改jdbc参数:/usr/local/tomcat7/webapps/项目名称/WEB-INF/classes/jdbc.properties重要配置:
    driverClassName=com.mysql.jdbc.Driver ##指定连接数据库驱动
    url=jdbc\:mysql\://192.168.200.206\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8 ##访问数据库的url=jdbc\:mysql\://数据库ip地址\:数据库的端口
    uname=root ##授权的用户名
    password=123456 ##授权的密码
    以上配置为大多数据java项目数据库连接配置,偶尔会在WEB-INF/web.xml文件中配置数据库连接

三.项目实验步骤(操作截图或者操作命令)
(一)配置nginx1:
1.添加DNS解析:登录192.168.200.254
[root@localhost ~]# vi /var/named/chroot/var/named/linuxfan.cn.zone ##添加
nginx1 IN A 192.168.200.202
nginx2 IN A 192.168.200.203
tomcat1 IN A 192.168.200.204
tomcat2 IN A 192.168.200.205
mysql IN A 192.168.200.206
www IN A 192.168.200.100
www IN A 192.168.200.200
:wq
[root@localhost ~]# /etc/init.d/named restart
停止 named: [确定]
启动 named: [确定]
[root@nginx1 ~]# nslookup

server 192.168.200.254
Default server: 192.168.200.254
Address: 192.168.200.254#53
www.linuxfan.cn
Server: 192.168.200.254
Address: 192.168.200.254#53

Name: www.linuxfan.cn
Address: 192.168.200.100

exit

[root@nginx1 ~]#
修改主机名:
[root@nginx1 ~]# vi /etc/sysconfig/network
NETWORKING=yes
IPV6=no
HOSTNAME=nginx1.linuxfan.cn
:wq
[root@nginx1 ~]# reboot
相同的方法修改其他主机的主机名。
2.nginx1上安装nginx:192.168.200.202
[root@nginx1 ~]# useradd -M -s /sbin/nologin nginx
[root@nginx1 ~]# yum -y install pcre-devel zlib-devel openssl-devel &>/dev/null
[root@nginx1 ~]# wget ftp://ftp.linuxfan.cn/tools/nginx-1.6.2.tar.gz &>/dev/null
[root@nginx1 ~]# tar zxf nginx-1.6.2.tar.gz -C /usr/src/
[root@nginx1 ~]# cd /usr/src/nginx-1.6.2/
[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_flv_module --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module &&make &&make install
注意:--with-file-aio启用文件读写;--with-http_flv_module启用flv文件的处理;--with-http_gzip_static_module启用压缩传输;--with-http_realip_module启用真实ip
[root@nginx nginx-1.6.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@nginx nginx-1.6.2]# cd

3.快速编写nginx安装脚本:192.168.200.202

[root@nginx1 ~]# cat .bash_history >nginx_install.sh ##读取历史命令并写入安装脚本
[root@nginx1 ~]# vi nginx_install.sh ##修改内容如下
#!/bin/bash
##by linuxfan.cn 2017-6-12
useradd -M -s /sbin/nologin nginx
yum -y install pcre-devel zlib-devel openssl-devel &>/dev/null
wget ftp://ftp.linuxfan.cn/tools/nginx-1.6.2.tar.gz &>/dev/null
tar zxf /root/nginx-1.6.2.tar.gz -C /usr/src/
cd /usr/src/nginx-1.6.2/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_flv_module --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module &&make &&make install
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
nginx -t
:wq

4.配置nginx1的nginx服务:192.168.200.202
[root@nginx1 ~]# cd /usr/local/nginx/conf/
[root@nginx1 conf]# cp nginx.conf{,.bak}
[root@nginx1 conf]# vi nginx.conf ##修改配置文件如下
:%g/^$/d ##删除空行
user nginx;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
client_header_buffer_size 32k;
large_client_header_buffers 4 128k;
client_max_body_size 512m;
open_file_cache max=65535 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;

gzip  on;
gzip_static on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_min_length 1024;
gzip_vary on;
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 512k;
fastcgi_buffers 6 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
client_body_buffer_size 128k;

proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 54k;
proxy_temp_file_write_size 2m;
proxy_ignore_client_abort on;
proxy_cache_path /usr/local/nginx/cache_temp levels=2:2 keys_zone=cache_temp:128m
inactive=30m max_size=2g;
proxy_cache_valid 200 302 10m;
include /usr/local/nginx/conf/conf.d/.conf;
}
:wq
[root@nginx1 ~]# nginx -t
[root@nginx1 ~]# mkdir /usr/local/nginx/conf/conf.d
[root@nginx1 ~]# vim /usr/local/nginx/conf/conf.d/server.conf
server {
listen 80;
server_name www.linuxfan.cn 192.168.200.202;
index index.html index.htm index.jsp;
root /usr/local/nginx/html;
access_log /usr/local/nginx/logs/tomcat.linuxfan.cn_access.log main;
location ~ {
index index.jsp;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat_servers;
}
location ~ .
.(gif|jpg|jpeg|bmp|swf|png)$ {
expires 30d;
}
location ~ .*.(js|css)$ {
expires 1h;
}
}
[root@nginx1 ~]# vim /usr/local/nginx/conf/conf.d/pool.conf
upstream tomcat_servers {
ip_hash;
server 192.168.200.204:8080 weight=1 max_fails=3 fail_timeout=10s;
server 192.168.200.205:8080 weight=1 max_fails=3 fail_timeout=10s;
}
:wq
[root@nginx1 ~]# nginx -t
[root@nginx1 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 ~]# nginx
[root@nginx1 ~]# ps aux |grep nginx
root 1009 0.0 0.2 176052 1072 ? Ss 01:07 0:00 nginx: master process nginx
nginx 1010 0.0 1.1 180308 5564 ? S 01:07 0:00 nginx: worker process
nginx 1011 0.0 1.1 180308 5496 ? S 01:07 0:00 nginx: worker process
nginx 1012 0.0 0.3 176208 1484 ? S 01:07 0:00 nginx: cache manager process
nginx 1013 0.0 0.2 176208 1452 ? S 01:07 0:00 nginx: cache loader process
root 1016 0.0 0.1 103256 836 pts/0 S+ 01:07 0:00 grep nginx
[root@nginx1 ~]#
nginx:负载均衡(upstream)、反向代理(proxy)
5.配置nginx1的keepalived:
[root@nginx1 ~]# yum -y install keepalived
[root@nginx1 ~]# chkconfig keepalived on
[root@nginx1 ~]# chkconfig --list keepalived
[root@nginx1 conf]# cd /etc/keepalived/
[root@nginx1 keepalived]# cp keepalived.conf keepalived.conf.bak
[root@nginx1 keepalived]# vim keepalived.conf
global_defs {
notification_email {br/>linuxfan360@163.com
}
notification_email_from root@linuxfan.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_1
}

vrrp_script nginx {
script /opt/chk_nginx.sh
interval 2
weight -10
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.200.100
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 99
nopreempt ##非抢占模式
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.200.200
}
}
[root@nginx1 keepalived]#
[root@nginx1 keepalived]# /etc/init.d/keepalived start
[root@nginx1 keepalived]# ps aux |grep keep
root 917 0.0 0.2 110104 1128 ? Ss 00:37 0:00 /usr/sbin/keepalived -D
root 918 0.0 0.6 114416 2992 ? S 00:37 0:00 /usr/sbin/keepalived -D
root 920 0.0 0.4 114280 2260 ? S 00:37 0:00 /usr/sbin/keepalived -D
root 989 0.0 0.1 103256 840 pts/0 S+ 00:50 0:00 grep keep

[root@nginx1 ~]# vim /opt/chk_nginx.sh
#!/bin/bash
#check nginx server status
NGINX="/usr/local/nginx/sbin/nginx"
nginxpid=$(ps -C nginx --no-header |wc -l)
if [ $nginxid -eq 0 ];then
$NGINX
sleep 3
nginxpid=$(ps -C nginx --no-header |wc -l)
if [ $nginxpid -eq 0 ];then
/etc/init.d/keepalived stop
echo "Keepalived stopped, please check your Nginx! " |tee -a /var/log/messages
fi
fi
:wq
[root@nginx1 ~]#
[root@nginx1 ~]# ps -C nginx --no-header
1009 ? 00:00:00 nginx
1010 ? 00:00:00 nginx
1011 ? 00:00:00 nginx
1012 ? 00:00:00 nginx
[root@nginx1 ~]# ps -C nginx --no-header |wc -l
4
(二)配置nginx2:192.168.200.203
1.配置nginx2:安装nginx
[root@nginx2 ~]# scp root@192.168.200.202:/root/nginx_install.sh ./ ##复制安装脚本
The authenticity of host '192.168.200.202 (192.168.200.202)' can't be established.
RSA key fingerprint is 4b:4f:5d:e7:42:f2:c5:55:ea:f8:7b:70:26:fe:ec:f3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.200.202' (RSA) to the list of known hosts.
root@192.168.200.202's password:
nginx_install.sh 100% 558 0.5KB/s 00:00
[root@nginx2 ~]# sh -x nginx_install.sh ##安装nginx
2.配置nginx2:配置nginx
[root@nginx2 ~]# mkdir /usr/local/nginx/conf/conf.d
[root@nginx2 ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@nginx2 ~]# scp root@192.168.200.202:/usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf
[root@nginx2 ~]# scp root@192.168.200.202:/usr/local/nginx/conf/conf.d/* /usr/local/nginx/conf/conf.d/
[root@nginx2 ~]# sed -i 's/202/203/g' /usr/local/nginx/conf/conf.d/server.conf ##修改ip地址
[root@nginx2 ~]# grep server_name /usr/local/nginx/conf/conf.d/server.conf ##验证
server_name www.linuxfan.cn 192.168.200.203;
[root@nginx2 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx2 ~]# nginx
[root@nginx2 ~]# ps aux |grep nginx
root 3674 0.0 0.2 176052 1072 ? Ss 01:24 0:00 nginx: master process nginx
nginx 3675 0.0 1.1 180308 5564 ? S 01:24 0:00 nginx: worker process
nginx 3676 0.0 1.1 180308 5496 ? S 01:24 0:00 nginx: worker process
nginx 3677 0.0 0.3 176208 1484 ? S 01:24 0:00 nginx: cache manager process
nginx 3678 0.0 0.2 176208 1452 ? S 01:24 0:00 nginx: cache loader process
root 3680 0.0 0.1 103256 840 pts/0 S+ 01:24 0:00 grep nginx
[root@nginx2 ~]#

3.配置nginx2:配置keepalived
[root@nginx2 ~]# yum -y install keepalived ##安装keepalived
[root@nginx2 ~]# cd /etc/keepalived/
[root@nginx2 keepalived]# cp keepalived.conf keepalived.conf.bak
[root@nginx2 keepalived]# chkconfig keepalived on
[root@nginx2 keepalived]# chkconfig --list keepalived
keepalived 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启.6:关闭
[root@nginx2 keepalived]#
[root@nginx2 keepalived]# scp root@192.168.200.202:/etc/keepalived/keepalived.conf ./ ##复制配置文件
[root@nginx2 keepalived]# vim keepalived.conf ##修改如下
global_defs {
notification_email {br/>linuxfan360@163.com
}
notification_email_from root@linuxfan.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_2
}

vrrp_script nginx {
script /opt/chk_nginx.sh
interval 2
weight -10
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.200.100
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.200.200
}
}
[root@nginx2 keepalived]#
[root@nginx2 ~]# scp root@192.168.200.202:/opt/chk_nginx.sh /opt
root@192.168.200.202's password:
chk_nginx.sh 100% 353 0.3KB/s 00:00
[root@nginx2 ~]# /etc/init.d/keepalived start
正在启动 keepalived: [确定]
[root@nginx2 ~]# ps aux |grep keep
root 3714 0.0 0.2 110104 1128 ? Ss 01:39 0:00 /usr/sbin/keepalived -D
root 3716 0.0 0.6 114404 2956 ? S 01:39 0:00 /usr/sbin/keepalived -D
root 3717 0.0 0.4 114280 2260 ? S 01:39 0:00 /usr/sbin/keepalived -D
root 3729 0.0 0.1 103256 840 pts/0 S+ 01:39 0:00 grep keep
[root@nginx2 ~]#
4.测试高可用:
[root@nginx1 ~]# ip a sh eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:6a:41:a2 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.100/32 scope global eth0
inet6 fe80::20c:29ff:fe6a:41a2/64 scope link
valid_lft forever preferred_lft forever
[root@nginx1 ~]#
[root@nginx2 ~]# ip a sh eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:6e:54:fc brd ff:ff:ff:ff:ff:ff
inet 192.168.200.203/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.200/32 scope global eth0
inet6 fe80::20c:29ff:fe6e:54fc/64 scope link
valid_lft forever preferred_lft forever

(三)安装tomcat1及发布项目:192.168.200.204
1.安装tomcat+jdk:
[root@tomcat1 ~]# history -c
[root@tomcat1 ~]# >.bash_history
执行如下命令:
wget ftp://ftp.linuxfan.cn/tools/{apache-tomcat-7.0.54.tar.gz,jdk-7u65-linux-x64.gz}
tar zxf jdk-7u65-linux-x64.gz ##下载软件
mv jdk1.7.0_65/ /usr/local/java
tar zxf apache-tomcat-7.0.54.tar.gz
mv apache-tomcat-7.0.54 /usr/local/tomcat7
vim /etc/profile.d/java.sh
export JAVA_HOME="/usr/local/java"
export PATH=$PATH:$JAVA_HOME/bin:/usr/local/tomcat7/bin
:wq
source /etc/profile
java -version
/usr/local/tomcat7/bin/startup.sh
netstat -utpln |grep 8080
tail /usr/local/tomcat7/logs/catalina.out
elinks --dump http://192.168.200.204:8080

2.编写脚本:
[root@tomcat1 ~]# cat .bash_history >jdk_tomcat_install.sh
[root@tomcat1 ~]# vim jdk_tomcat_install.sh
#!/bin/bash
#by linuxfan.cn 2017-6-12
wget ftp://ftp.linuxfan.cn/tools/{apache-tomcat-7.0.54.tar.gz,jdk-7u65-linux-x64.gz} &&>/dev/null
tar zxf jdk-7u65-linux-x64.gz
mv jdk1.7.0_65/ /usr/local/java
tar zxf apache-tomcat-7.0.54.tar.gz
mv apache-tomcat-7.0.54 /usr/local/tomcat7
cat <<END >> /etc/profile.d/java.sh
export JAVA_HOME="/usr/local/java"
export PATH=\$PATH:\$JAVA_HOME/bin:/usr/local/tomcat7/bin
END
source /etc/profile
java -version
/usr/local/tomcat7/bin/startup.sh
netstat -utpln |grep 8080
tail /usr/local/tomcat7/logs/catalina.out
:wq
[root@tomcat1 ~]#

3.发布项目(也叫上线):
[root@tomcat1 ~]# cd /usr/local/tomcat7/conf/
[root@tomcat1 conf]# cp server.xml server.xml.bak
[root@tomcat1 conf]# vim server.xml
126 <Context path="" docBase="SLSaleSystem" reloadable="true" debug="0">
127 </Context>
:wq
[root@tomcat1 conf]# cd
上传项目:

[root@tomcat1 ~]# tar zxf SLSaleSystem.tar.gz -C /usr/local/tomcat7/webapps/
[root@tomcat1 ~]# ls /usr/local/tomcat7/webapps/
docs examples host-manager manager ROOT SLSaleSystem
[root@tomcat1 ~]# /usr/local/tomcat7/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat7
Using CATALINA_HOME: /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
[root@tomcat1 ~]# /usr/local/tomcat7/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat7
Using CATALINA_HOME: /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat1 ~]#

(四)安装tomcat2及发布项目:192.168.200.205
1.安装tomcat7+jdk:
[root@tomcat2 ~]# scp root@192.168.200.204:/root/jdk_tomcat_install.sh ./
[root@tomcat2 ~]# sh -x jdk_tomcat_install.sh
2.发布项目:
[root@tomcat2 ~]# cd /usr/local/tomcat7/conf/
[root@tomcat2 conf]# mv server.xml server.xml.bak
[root@tomcat2 conf]# scp root@192.168.200.204:/usr/local/tomcat7/conf/server.xml ./
[root@tomcat2 conf]# cd ../webapps/
[root@tomcat2 webapps]# rsync -a root@192.168.200.204:/usr/local/tomcat7/webapps/SLSaleSystem ./
[root@tomcat2 webapps]# ls
docs examples host-manager manager ROOT SLSaleSystem
[root@tomcat2 webapps]#
[root@tomcat2 webapps]# source /etc/profile
[root@tomcat2 webapps]# /usr/local/tomcat7/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat7
Using CATALINA_HOME: /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
[root@tomcat2 webapps]# /usr/local/tomcat7/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat7
Using CATALINA_HOME: /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat2 webapps]#

(五)安装并配置mysql:
1.安装mysql:
[root@mysql ~]# yum -y install ncurses-* gcc gcc-c++
[root@mysql ~]# wget ftp://ftp.linuxfan.cn/tools/{cmake-2.8.6.tar.gz,mysql-5.5.22.tar.gz}
[root@mysql ~]# tar zxf cmake-2.8.6.tar.gz -C /usr/src/
[root@mysql ~]# cd /usr/src/cmake-2.8.6/
[root@mysql cmake-2.8.6]# ./configure &&gmake &&gmake install
[root@mysql ~]# useradd -M -s /sbin/nologin mysql
[root@mysql ~]# tar zxf mysql-5.5.22.tar.gz -C /usr/src/
[root@mysql ~]# cd /usr/src/mysql-5.5.22/
[root@mysql mysql-5.5.22]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc/ &&make &&make install
[root@mysql mysql-5.5.22]# /bin/cp support-files/mysql.server /etc/init.d/mysqld
[root@mysql mysql-5.5.22]# /bin/cp support-files/my-medium.cnf /etc/my.cnf
[root@mysql mysql-5.5.22]# chmod +x /etc/init.d/mysqld
[root@mysql mysql-5.5.22]# chkconfig --add mysqld
[root@mysql mysql-5.5.22]# chkconfig --list mysqld
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启.6:关闭
[root@mysql mysql-5.5.22]#
[root@mysql mysql-5.5.22]# echo "PATH=\$PATH:/usr/local/mysql/bin" >>/etc/profile
[root@mysql mysql-5.5.22]# source /etc/profile
[root@mysql mysql-5.5.22]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@mysql mysql-5.5.22]# chown -R mysql.mysql /usr/local/mysql/
[root@mysql mysql-5.5.22]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
[root@mysql mysql-5.5.22]# cd
[root@mysql ~]#
[root@mysql ~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@mysql ~]# mysqladmin -uroot password 123123
实际工作初始化数据库时推荐使用:mysql_secure_installation设置root密码、禁用远程登录、删除匿名账户、删除测试数据库、重新加载权限表-->delete from mysql.user where user='';
[root@mysql ~]# mysql -uroot -p123123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.22-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> create database slsaledb;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on slsaledb.* to root@'192.168.200.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye
[root@mysql ~]#

2.导入项目数据:
[root@tomcat1 ~]# vi /usr/local/tomcat7/webapps/SLSaleSystem/WEB-INF/classes/jdbc.properties
url=jdbc\:mysql\://192.168.200.206\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
:wq
[root@tomcat1 ~]# scp /usr/local/tomcat7/webapps/SLSaleSystem/WEB-INF/classes/jdbc.properties root@192.168.200.205:/usr/local/tomcat7/webapps/SLSaleSystem/WEB-INF/classes/ ##复制数据库连接配置文件
[root@tomcat1 ~]#
[root@mysql ~]# scp root@192.168.200.204:/root/slsaledb-2014-4-10.sql ./ ##复制数据库文件
[root@mysql ~]# mysql -uroot -p123123 slsaledb <slsaledb-2014-4-10.sql ##导入项目数据
[root@mysql ~]# mysql -uroot -p123123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.5.22-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use slsaledb;
Database changed
mysql> show tables;
+-------------------------+
| Tables_in_slsaledb |
+-------------------------+
| BASICS_PARAMETER |
| INFO_ANNEXES |
| INVENTORY |
| MULTI_LAN |
| ORDER_INFO |
| ORDER_LIST |
| USER_ACCOUNT_201312 |
| USER_ACCOUNT_201404 |
| USER_ACCOUNT_LOG_201404 |
| USER_BUY |
| USER_BUY_BONUS |
| USER_CASH |
| USER_PAIR_201312 |
| USER_PLACE |
| USER_POINT |
| USER_POINT_GOODS |
| USER_RECHARGE |
| USER_REFER |
| affiche |
| au_authority |
| au_function |
| au_role |
| au_user |
| data_dictionary |
| goods_info |
| goods_pack |
| goods_pack_affiliated |
| information |
| leave_message |
| reply |
| uploadtemp |
+-------------------------+
31 rows in set (0.00 sec)

mysql> quit
Bye
[root@mysql ~]#

3.访问验证:用户名: admin 密码: 123456

四.项目实验总结(遇到的错误及解决方案,难点、重点解说,扩展学习)
1.错误及解决方案:
1)错误一:keepalived优先级未调整,导致VIP不切换
解决方法:修改nginx2上优先级:vi /etc/keepalived/keepalived.conf中同时修改状态和优先级。
2)错误二:mysql安装过程没复制my-medium.cnf到/etc/my.cnf
解决方法:复制
3)错误三:slsale启动失败:
[root@tomcat2 ~]# cat /usr/local/tomcat7/logs/catalina.out
严重: The web application [/SLSaleSystem] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.(内存不足)
解决方法:echo 3 >/proc/sys/vm/drop_caches-->重启tomcat
2.重点、难点:
1)重点一:

2)重点二:

  1. 扩展学习
    1)Nginx代理配置项:
    proxy_pass http://192.168.10.10; ##用于指定反向代理的服务器池
    proxy_set_header Host $hoxt; ##当后端web服务器也配置了多个虚拟主机时,需要用该header来区分反向代理哪个主机名
    prox_set header X-Forwarded-For $remote_addr ##如果后端web服务器上的程序需要获取用户ip,可以从该header头获取
    proxy_set_header http_user_agent $http_user_agent; ##判断访问端是苹果,安卓,win还是mac
    proxy_body_buffer_size ##用于指定客户端请求主体缓冲区大小,可以理解为先保存到本地在传给用户
    proxy_connect_timeout ##表示与后端服务器连接的超时时间,即发起握手等候响应的超时时间
    proxy_send_timeout ##表示后端服务器的数据回传时间,即在规定的时间内后端服务器必须传完所有的数据,否则,nginx将断开这个连接
    proxy_read_timeout ##设置nginx从代理的后端服务器获取信息的时间,表示连接建立成功之后,nginx等待后端服务器的响应时间,其实nginx已经进入后端的排队之中等候处理
    proxy_buffer_size ##设置缓冲区大小,默认,该个、缓冲区大小等于指令proxy_buffers设置的大小
    proxy_buffers ##设置缓冲区的数量和大小。nginx从代理的后端服务器获取的响应信息,会保存到缓冲区
    proxy_busy_buffers_size ##用于设置系统忙碌时可以使用的proxy_buffers大小,官方推荐为proxy_buffers*2
    proxy_tmep_file_write_size ##指定proxy缓存临时文件的大小
    proxy_next_upstream http_502 http_504 http_503 error timeout invalid_header; ##请求出错后,转向下一个节点

2)Nginx 启用gzip压缩
(1) 网页压缩

  网页压缩是一项由 WEB 服务器和浏览器之间共同遵守的协议,也就是说 WEB 服务器和浏览器都必须支持该技术,所幸的是现在流行的浏览器都是支持的,包括 IE、FireFox、Opera 等;服务器有 Apache 和 IIS 等。双方的协商过程如下:  
  首先浏览器请求某个 URL 地址,并在请求的头 (head) 中设置属性 accept-encoding 值为 gzip, deflate,表明浏览器支持 gzip 和 deflate 这两种压缩方式(事实上 deflate 也是使用 gzip 压缩协议,下面我们会介绍二者之间的区别);

  WEB 服务器接收到请求后判断浏览器是否支持压缩,如果支持就传送压缩后的响应内容,否则传送不经过压缩的内容;
  浏览器获取响应内容后,判断内容是否被压缩,如果是则解压缩,然后显示响应页面的内容。

在实际的应用中我们发现压缩的比率往往在 3 到 10 倍,也就是本来 50k 大小的页面,采用压缩后实际传输的内容大小只有 5 至 15k 大小,这可以大大节省服务器的网络带宽,同时如果应用程序的响应足够快时,网站的速度瓶颈就转到了网络的传输速度上,因此内容压缩后就可以大大的提升页面的浏览速度。
(2)配置指令详细注释:

gzip on|off

默认值: gzip off

开启或者关闭gzip模块

gzip_static on|off

nginx对于静态文件的处理模块

该模块可以读取预先压缩的gz文件,这样可以减少每次请求进行gzip压缩的CPU资源消耗。该模块启用后,nginx首先检查是否存在请求静态文件的gz结尾的文件,如果有则直接返回该gz文件内容。为了要兼容不支持gzip的浏览器,启用gzip_static模块就必须同时保留原始静态文件和gz文件。这样的话,在有大量静态文件的情况下,将会大大增加磁盘空间。我们可以利用nginx的反向代理功能实现只保留gz文件。

可以google"nginx gzip_static"了解更多

gzip_comp_level 4

默认值:1(建议选择为4)

gzip压缩比/压缩级别,压缩级别 1-9,级别越高压缩率越大,当然压缩时间也就越长(传输快但比较消耗cpu)。

gzip_buffers 4 16k

默认值: gzip_buffers 4 4k/8k

设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。 例如 4 4k 代表以4k为单位,按照原始数据大小以4k为单位的4倍申请内存。 4 8k 代表以8k为单位,按照原始数据大小以8k为单位的4倍申请内存。

如果没有设置,默认值是申请跟原始数据相同大小的内存空间去存储gzip压缩结果。

gzip_types mime-type [mime-type ...]

默认值: gzip_types text/html (默认不对js/css文件进行压缩)

压缩类型,匹配MIME类型进行压缩

不能用通配符 text/*

(无论是否指定)text/html默认已经压缩

设置哪压缩种文本文件可参考 conf/mime.types

gzip_min_length 1k

默认值: 0 ,不管页面多大都压缩

设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。

建议设置成大于1k的字节数,小于1k可能会越压越大。 即: gzip_min_length 1024

gzip_http_version 1.0|1.1

默认值: gzip_http_version 1.1(就是说对HTTP/1.1协议的请求才会进行gzip压缩)

识别http的协议版本。由于早期的一些浏览器或者http客户端,可能不支持gzip自解压,用户就会看到乱码,所以做一些判断还是有必要的。

注:99.99%的浏览器基本上都支持gzip解压了,所以可以不用设这个值,保持系统默认即可。

假设我们使用的是默认值1.1,如果我们使用了proxy_pass进行反向代理,那么nginx和后端的upstream server之间是用HTTP/1.0协议通信的,如果我们使用nginx通过反向代理做Cache Server,而且前端的nginx没有开启gzip,同时,我们后端的nginx上没有设置gzip_http_version为1.0,那么Cache的url将不会进行gzip压缩

gzip_proxied [off|expired|no-cache|no-store|private|no_last_modified|no_etag|auth|any] ...

默认值:off

Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。

off - 关闭所有的代理结果数据的压缩
expired - 启用压缩,如果header头中包含 "Expires" 头信息
no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息
no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息
private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息
no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息
any - 无条件启用压缩

gzip_vary on

和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩

gzip_disable "MSIE [1-6]."

禁用IE6的gzip压缩,又是因为杯具的IE6。当然,IE6目前依然广泛的存在,所以这里你也可以设置为“MSIE [1-5].”

IE6的某些版本对gzip的压缩支持很不好,会造成页面的假死,今天产品的同学就测试出了这个问题

后来调试后,发现是对img进行gzip后造成IE6的假死,把对img的gzip压缩去掉后就正常了
为了确保其它的IE6版本不出问题,所以建议加上gzip_disable的设置

3)nginx的open_file_cache文件缓存:
(1)Nginx 的 open_file_cache 相关配置可以缓存静态文件的元信息,在这些静态文件被频繁访问时可以显着提升性能。
被缓存的文件元信息包括:
fd,文件被打开一次后,fd保留使用
size
path
last modified time

这里有个配置示例:

open_file_cache max=64 inactive=30d;
open_file_cache_min_uses 8;
open_file_cache_valid 3m;
max=64 表示设置缓存文件的最大数目为 64, 超过此数字后 Nginx 将按照 LRU 原则丢弃冷数据。

inactive=30d 与 open_file_cache_min_uses 8 表示如果在 30 天内某文件被访问的次数低于 8 次,那就将它从缓存中删除。

open_file_cache_valid 3m 表示每 3 分钟检查一次缓存中的文件元信息是否是最新的,如果不是则更新之。

(2)为什么只缓存文件元信息而不缓存文件内容?
这个问题的关键是 sendfile(2).

Nginx 在 serve 静态文件的时候用的是 sendfile(2), 当然前提是你配置了 sendfile on, sendfile(2) 直接在 kernel space 内传输数据,对比使用 read(2)/write(2) 省去了两次 kernel space 与 user space 之间的数据拷贝。而同时这些被频繁读取的静态文件的内容会被 OS 缓存到 kernel space。在这样的机制下,我们缓存中有文件的 fd 和 size,直接调用 sendfile(2) 就可以了。

如果要 Nginx 连内容一起缓存,那就需要每次文件变化都要用 read(2) 将数据从 kernel space 复制到 user space,然后放在 user space,每次应答请求的时候再从 user space 复制到 kernel space 然后写入 socket。比起前面的方式,这样的方式毫无优点。

(3)在文件缓存更新周期内文件发生变化了会发生什么?
上面提到的配置中,30 天无访问丢弃,每 3 分钟做一次信息有效性监测,我们暂且把 3 分钟叫做缓存更新周期。那在这 3 分钟之内文件发生变化了会怎样呢?

文件被删除:由于 nginx 还持有原文件的 fd,所以你删除此文件后,文件并不会真正消失, client 还是能通过原路径访问此文件。即便你删除后又新建了一个同名文件,在当前缓存更新周期内能访问到的还是原文件的内容。

文件内容被修改:文件内容被修改可以分为两种情况:文件大小不变或增大
由于 nginx 缓存了文件的 size 并且使用 这个缓存中 size 调用 sendfile(2),所以此种情况的后果是:
从文件开始到原 size 字节中的变化可以被 client 看到。
原 size 之后的内容不会被 sendfile(2) 发送,因此 client 看不到此部份内容。
文件大小减小
此种情况下,由于同样原因,nginx 在 HTTP Header 中告诉 client 文件大小还是原来的尺寸,而 sendfile(2) 只能发送真正的文件数据,长度小于 HTTP Header 中设置的大小,所以 client 会等待到自己超时或者 Nginx 在 epoll_wait 超时后关闭连接。
(4)如何设置?
如果你的静态文件内容变化频繁并且对时效性要求较高,一般应该把 open_file_cache_valid 设置的小一些,以便及时检测和更新。
如果变化相当不频繁的话,那就可以设置大一点,在变化后用 reload nginx 的方式来强制更新缓存。
对静态文件访问的 error 和 access log 不关心的话,可以关闭已提升效率。
4)关于FastCGI 的几个指令:https://www.cnblogs.com/yuanzai12345/p/5951860.html
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10minactive=5m;
这个指令为FastCGI 缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间。
fastcgi_connect_timeout 300;
指定连接到后端FastCGI 的超时时间。
fastcgi_send_timeout 300;
向FastCGI 传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI 传送请求的超时时间。
fastcgi_read_timeout 300;
接收FastCGI 应答的超时时间,这个值是指已经完成两次握手后接收FastCGI 应答的超时时间。
fastcgi_buffer_size 4k;
指定读取FastCGI 应答第一部分需要用多大的缓冲区,一般第一部分应答不会超过1k,由于页面大小为4k,所以这里设置为4k。
fastcgi_buffers 8 4k;
指定本地需要用多少和多大的缓冲区来缓冲FastCGI 的应答。
fastcgi_busy_buffers_size 8k;
这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers 的两倍。
fastcgi_temp_file_write_size 8k;
在写入fastcgi_temp_path 时将用多大的数据块,默认值是fastcgi_buffers 的两倍。
fastcgi_cache TEST
开启FastCGI 缓存并且为其制定一个名称。个人感觉开启缓存非常有用,可以有效降低CPU 负载,并且防止502 错误。
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
为指定的应答代码指定缓存时间,如上例中将200,302 应答缓存一小时,301 应答缓存1 天,其他为1 分钟。
fastcgi_cache_min_uses 1;
缓存在fastcgi_cache_path 指令inactive 参数值时间内的最少使用次数,如上例,如果在5 分钟内某文件1 次也没有被使用,那么这个文件将被移除。
fastcgi_cache_use_stale error timeout invalid_header http_500;
不知道这个参数的作用,猜想应该是让nginx 知道哪些类型的缓存是没用的。以上为nginx 中FastCGI 相关参数,另外,FastCGI 自身也有一些配置需要进行优化,如果你使用php-fpm 来管理FastCGI,可以修改配置文件中的以下值:
<value name=”max_children”>60</value>
同时处理的并发请求数,即它将开启最多60 个子线程来处理并发连接。
<value name=”rlimit_files”>102400</value>
最多打开文件数。
<value name=”max_requests”>204800</value>
每个进程在重置之前能够执行的最多请求数。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值