方式:Nginx和两个Tomcat(多个Tomcat方法一样)安装在同一台服务器上,当然Nginx也可以在另一台服务器上,不过没有这个必要。
服务器ip: 119.147.x.x
两个JSP应用:payment和esales
JSP应用XXX对应的域名:local.ibalance.com
JSP应用XXX对应的域名:admin.ibalance.com
注意:上面两个域名在DNS那里都解析到 119.147x.x 这个IP地址。
2.1 JDK下载:可以从oracle官网获得 http://www.oracle.com/technetwork/java/javase/downloads/java-se-6u24-download-338091.html ,根据您自己的操作系统类型和位数选择合适的版本。
2.2 Tomcat下载:可以从Apache官网获得 http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.30/bin/
2.3 Nginx下载:可以从Nginx官网获得 http://nginx.net/
1. #chmod a+x jdk-7-linux-i586.rpm
2. #rpm -ivh jdk-7-linux-i586.rpm
3. [root@exkPicture ~]# java -version
4. java version "1.7.0"
5. Java(TM) SE Runtime Environment (build 1.7.0-b147)
6. Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)
7. #来来来,我们再来一步
8. #alternatives --install /usr/bin/java java /usr/java/jdk1.7.0/bin/java 60
9. #设好我们自己jdk的家
10. #alternatives --config java
11. #瞅瞅看当前所有的 JDK和正在用的JDK 结果如下:
12. 选择[+] 号令
13. -----------------------------------------------
14. *+ 1 /usr/lib/jvm/jre-1.6.0-openjdk/bin/java
15. 2 /usr/java/jdk1.7.0/bin/java
16. 按 Enter 来保存当前选择[+],或键入选择号码:2
17. #我们已经打到这里了,再看一次吧.
18. #java -version
19. java version "1.7.0"
20. Java(TM) SE Runtime Environment (build 1.7.0-b147)
21. Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)
22. #"Oh ~~ Yes!"
1) #vi /etc/profile 文件末尾添加下面内容:
2) #set environment variable
3) export JAVA_HOME=/usr/local/jdk1.7.0
4) export JRE_HOME=$JAVA_HOME/jre
5) export PATH=$PATH:$JAVA_HOME/bin
6) export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
7) 保存退出后
8) #source /etc/profie
9) 至此安装部署完成。
10) 以使设置的环境变量加载到当前session的系统环境中,Tomcat启动和关闭时会使用到这些变量。
1) #cd /usr/local/src
2) #tar xzvf apache-tomcat-6.0.30.tar.gz –C /usr/local/
3) #cd /usr/local/
4) #准备payment应用tomcat
5) #mv apache-tomcat-6.0.30 tomcat_payment
6) #准备esales应用tomcat
7) #cp –r tomcat_paymnet tomcat_esales
创建网站根目录:nginx和tomcat共同使用
#mkdir –p /data/www/htdocs/
在网站根目录中创建两个应用的目录:
#mkdir –p /data/www/htdocs/{payment,esales}
把tomcat启动需要的host-manager,manager(在tomcat_payment/webapps下) 拷贝到两个应用的目录:
#cp
-r /usr/local
/tomcat_payment/webapps/* /data/www/htdocs/payment/
#cp
-r /usr/local
/tomcat_payment/webapps/* /data/www/htdocs/esales/
注意:把payment和esales两个应用程序分别放置在
/data/www/htdocs/payment/ROOT/
/data/www/htdocs/esales/ROOT/
配置两个应用的conf/server.xml:
1)端口配置:
只需要配置其中一个应用的即可(使其区别于另一个),我们配置第二个应用:esales
|
|
|
|
|
|
这样同时启用两个应用的时候,就不会出现端口冲突了。
2) 应用根目录配置:
esales应用:
|
|
payment应用:
|
|
分别启动两个tomcat:
|
/usr/ |
|
/usr/ |
时时查看启动日志,看是否有报错:
|
|
查看所有的端口是否已经在监听;
|
|
创建Nginx用户和组:
#/usr/sbin/groupadd www
#/usr/sbin/useradd –g www wwww –s /sbin/nolog
安装Nginx所需要的库:
#yum install
-y pcre pcre-devel zlib zlib-devel openssl openssl-devel gcc
pcre库用于支持使用perl的兼容正则,zlib库用户支持gzip压缩,openssl用于支持https协议。
非Centos系统的,可以通过下载相应的源码包进行编译安装。
-----------------------------------------------------------------
nginx 安装所需软件包
#1.安装pcre 正则表达式所需包
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.gz
tar -zxvf pcre-8.31.tar.gz
cd pcre-8.31
./configure --prefix=/usr/local/pcre
make ; make install
#2.安装openssl
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
cd openssl-1.0.1c
./config
make ; make install
#3.安装插件upstream,nginx-upstream-jvm-route 解决session不同步问题
wget http://friendly.sinaapp.com//LinuxSoft/nginx-upstream-jvm-route-0.1.tar.gz
tar zxvf nginx-upstream-jvm-route-0.1.tar.gz
#4.安装nginx
wget http://nginx.org/download/nginx-1.2.3.tar.gz
tar -zxvf nginx-1.2.3.tar.gz
cd nginx-1.2.3
patch -p0 <../nginx_upstream_jvm_route/jvm_route.patch
#./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-pcre=/root/pcre-8.31 --add-module=../nginx_upstream_jvm_route/ --with-openssl=/usr/local/ssl/include/openssl
注意:--with-pcre=/root/install/pcre-8.31指向的是源码包解压的路径,而不是安装的路径,否则会报错
make;make install
查看nginx.conf语法是否正确
[root@innovanetest1 sbin]# /usr/local/nginx/sbin/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
查看Nginx是否安装成功
[root@master nginx-1.2.3]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.2.3
built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-pcre=/root/pcre-8.31 --add-module=../nginx_upstream_jvm_route/ --with-openssl=/usr/local/ssl/include/openssl
如果编译过程有报错,很可能是系统没有安装gcc相关编译器或缺少类包,根据错误提示,逐一排查即可。
把nginx命令路径加到系统PATH标量中,方便使用:
#vi /etc/profile 添加下面语句
export PATH=$PATH:/usr/local/nginx/sbin/nginx
#source /etc/profile
这样我们就可以在任意路径直接使用nginx命令来启动停止http服务了。
――――――――――――――――――――――――――――――――-
[root@innovanetest1 conf]# cat nginx.conf
#user nobody;
user www www;
worker_processes 8;
error_log logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
#log format
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#charset gb2312;
charset UTF-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_header_timeout 20;
client_max_body_size 20m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
proxy_connect_timeout 30;
proxy_read_timeout 60;
proxy_send_timeout 20;
proxy_buffer_size 96k;
proxy_buffers 8 256k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
# proxy_store on;
# proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /usr/local/nginx/proxy_temp;
proxy_cache_path /usr/local/nginx/www levels=1:2 keys_zone=cache_one:20m inactive=1d max_size=2g;
gzip on;
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
# limit_zone one $binary_remote_addr 10m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
#set load balancing server list
upstream local.ibalance.com {
server 192.168.1.209:81 max_fails=3 fail_timeout=50s;
}
upstream admin.ibalance.com {
server 192.168.1.209:82 max_fails=3 fail_timeout=50s;
}
#set virtual Seb server
server {
listen 80;
server_name 192.168.1.209:81;
index index.jsp index.html index.php;
# rewrite ^/index.html / last;
#rewrite ^/(.*)_p([0-9]*)_(.*)_e(.*)/(.*)_(.*)_t(.*)_o(.*)_(l|s|k|[0-9]*)$ /searchclassbigkey?skey=$1&pno=$2&sw=$3&pe=$4&rk=$5&dekey=$6&tid=$7&ocs=$8&ocid=$9 last;
#rewrite ^/(.*)_p([0-9]*)_s(.*)/(.*)_(.*)_(.*)$ /searchkey?skey=$1&pno=$2&sw=$3&pe=$4&rk=$5&dekey=$6 last;
# rewrite ^/d-(.*)$ /detaillist?id=$1 last;
location /
{
# proxy_next_upstream http_502 http_504 error timeout invalid_header;
# proxy_headers_hash_max_size 1024;
# proxy_headers_hash_bucket_size 128;
# proxy_cache cache_one;
proxy_cache_valid 200 304 2h;
proxy_cache_valid any 30m;
proxy_cache_key $host$uri$is_args$args;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://local.ibalance.com;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
# proxy_cache cache_one;
proxy_cache_valid 200 304 5h;
proxy_cache_valid any 3m;
proxy_cache_key $host$uri$is_args$args;
expires 8h;
proxy_pass http://local.ibalance.com;
}
location ~ .*\.(js|css|html)$
{
# proxy_cache cache_one;
proxy_cache_valid 200 304 3h;
proxy_cache_valid any 3m;
proxy_cache_key $host$uri$is_args$args;
expires 5h;
proxy_pass http://local.ibalance.com;
}
access_log /usr/local/nginx/logs/local.log access;
}
#
#web server admin.ibalance.com
server {
listen 80;
server_name 192.168.1.209:82;
index index.jsp index.html index.php;
location /
{
proxy_cache cache_one;
proxy_cache_valid 200 304 2h;
proxy_cache_valid any 3m;
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://admin.ibalance.com;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|html)$
{
proxy_cache cache_one;
proxy_cache_valid 200 304 5h;
proxy_cache_valid any 3m;
expires 8h;
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://admin.ibalance.com;
}
access_log /usr/local/nginx/logs/local.log access;
}
}
[root@innovanetest1 conf]#
网站根目录权限设置:
#chown –R www.www /data/www/htdocs
在启动之前,我们先检查配置文件nginx.conf是否正确
#nginx -t
如果出现以下信息则表明配置正确
nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful
启动Nginx
#nginx
查看80端口是否已启动
#netstat -antp
如果修改了配置文件,可以通过下面命令实现平滑重启(或是说不影响业务情况下的重新加载配置文件)
#nginx –s reload