TyCoding 2013-06-27
一台测试机,为了多个项目同时在一台测试机上测试,可以使用了nginx挂载多个tomcat应用。我使用的ubuntu 12.0.4。
准备:下载nginx : http://nginx.org/download/nginx-1.5.0.tar.gz
: 下载tomcat:http://mirrors.cnnic.cn/apache/tomcat/tomcat-6/v6.0.37/bin/apache-tomcat-6.0.37.tar.gz
如果链接无效,可以去官网上面下载。也可以使用附件里的包。
export.zip:http://pan.baidu.com/share/link?shareid=2574750123&uk=168777878
tomcat:http://pan.baidu.com/share/link?shareid=2589332527&uk=168777878
solr.zip:http://pan.baidu.com/share/link?shareid=2595428937&uk=168777878
1.安装nginx
ubuntu系统自带了一个版本的系统 需要先卸载:sudo apt-get --purge remove nginx
创建nginx存放目录,这个可以随便定,我放在/export/servers/nginx 首先创建文件夹,命令如下:
$cd / $mkdir export $cd export $mkdir servers $cd servers $mkdir nginx $cd nginx
把之前下载的nginx 包 放到/export/servers/nginx 目录下并解压压缩包
$tar -xzvf nginx-1.5.0.tar.gz $cd nginx-1.5.0
接下来编译安装:
$cd /export/servers/nginx/nginx-1.5.0 $./configure --prefix=/export/servers/nginx/nginx-1.5.0/sbin $make $sudo make install
执行可能报错:
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
原因缺少pcre library
解决方法:
下载pcre 官网 http://www.pcre.org/
下载地址: http://sourceforge.net/projects/pcre/files/pcre/8.33/pcre-8.33.tar.gz/download
#解压包 $tar -xzvf pcre-8.33.tar.gz $cd pcre-8.33 $sudo ./configure --prefix =/usr/local/pcre $sudo make $sudo make install
悲剧的是在执行./confugure时候还是会报错
configure: error: You need a C++ compiler for C++ support.
从错误中看没有g++,没办法只能安装
$sudo apt-get install g++
然后执行上面的安装pcre 如果还是报错,看看少什么就安装什么。
我这最后安装成功,如果回去继续安装nginx,继续报错,少了zlib按照上面的方法安装
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib= <path> option.
解决上面的问题 继续重复之前的安装。这次终于没有问题了,也可以在安装是先disbale这两个模块,以后需要了安装。执行下面:
$./configure --prefix=/export/servers/nginx/nginx-1.5.0/sbin --withou t-http_rewrite_module --without-http_gzip_module $make $make install启动nginx
$cd /export/servers/nginx/nginx-1.5.0/sbin/sbin $./nginx浏览器访问localhost
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
nginx安装成功:
nginx是个优秀的反向代理服务器,用户访问web应用的时候,直接面对的的nginx,而真正的web服务器就挂载在nginx后面,继而解决之前的如何将tomcat挂载到nginx下面,接下来讨论
1.部署tomcat 实例
我创建两个tomcat实例,然后分别在两个tomcat下面,各放一个solr应用,如果想了解solr可以参考(http://lucene.apache.org/solr/),其实solr就是一个web应用。具体就不介绍了。我主要是搭建一个master/slave solr应用。同时挂在nginx下面。首先安装两个tomcat:
创建两个文件夹:
/export/home/tomcat/domains/master.solr.com/server1 和 /export/home/tomcat/domains/slave.solr.com/server1
解压tomcat-6.0.37.tar.gz 然后把里面的所有文件全部拷贝到这两个文件下面。
root@lcpc:/export/home/tomcat/domains/master.solr.com/server1$ ls bin lib logs RELEASE-NOTES temp work conf LICENSE NOTICE RUNNING.txt webapps上面是两个文件夹的内容。tomcat安装完成,如果添加更多项目,类似。
接下来创建项目的文件的存放文件夹,因为有两个项目所以创建两个文件夹:
/export/data/WebRoot/master.solr.com 和 /export/data/WebRoot/slave.solr.com
root@lcpc:/export/data/WebRoot/master.solr.com$ ls admin.html css favicon.ico img js META-INF tpl WEB-INF文件夹内文件列表如上,
接下来修改tomcat配置文件,添加tomcat项目存放的路径,这样便于维护。
1.在/export/home/tomcat/domains/master.solr.com/server1/conf/Catalina/localhost增加文件ROOT.xml,docBase指向项目文件所在的文件夹<?xml version='1.0' encoding='UTF-8'?>$ <Context path="/" docBase="/export/data/WebRoot/master.solr.com">$ </Context>
<?xml version='1.0' encoding='UTF-8'?> <Context path="/" docBase="/export/data/WebRoot/slave.solr.com"> </Context>修改tomcat的http端口号码,由于在同一个机子运行两个tomcat,端口号肯定不能相同的。需要修改的/export/home/tomcat/domains/master.solr.com/server1/conf/server.xml文件
<Server port="8000" shutdown="SHUTDOWN"> 该端口改为8000,tomcat服务的 端口号。<Connector port="9000" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 该端口改为9000同样的修改/export/home/tomcat/domains/master.solr.com/server1/conf/server.xml文件,端口号码不能重复
<Server port="8001" shutdown="SHUTDOWN"> 该端口改为8001 <Connector port="9001" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 该端口改为9001 http端口号tomcat配置全部修改完成,分别进入两个tomcat安装目录的bin文件夹,启动master tomcat
root@lcpc:/export/home/tomcat/domains/master.solr.com/server1/bin$ ./startup.sh Using CATALINA_BASE:/export/home/tomcat/domains/master.solr.com/server1 Using CATALINA_HOME:/export/home/tomcat/domains/master.solr.com/server1 Using CATALINA_TMPDIR: /export/home/tomcat/domains/master.solr.com/server1/temp Using JRE_HOME:/opt/java/jdk1.6.0_38/jre Using CLASSPATH:/export/home/tomcat/domains/master.solr.com/server1/bin /bootstrap.jar
root@lcpc:/export/home/tomcat/domains/slave.solr.com/server1/bin$ ./startup.sh Using CATALINA_BASE:/export/home/tomcat/domains/slave.solr.com/server1 Using CATALINA_HOME:/export/home/tomcat/domains/slave.solr.com/server1 Using CATALINA_TMPDIR:/export/home/tomcat/domains/slave.solr.com/server 1/temp Using JRE_HOME:/opt/java/jdk1.6.0_38/jre Using CLASSPATH:/export/home/tomcat/domains/slave.solr.com/server1/bin /bootstrap.jar然后在浏览器中访问127.0.0.1:9000 和127.0.0.1:9001可以看solr的主界面,使用两个不同的端口号都成功启动。
说明tomcat没有问题,接下来就是让这两个tomcat挂在到nginx下面,然后我们通过域名访问,nginx会自动负载到各自的web应用中,接下来要做的就是修改nginx的配置文件
首先在/export/servers/nginx/nginx-1.5.0/sbin/conf下面创建一个存放nginx配置文件的domains文件夹,在domains创建两个配置文件master.solr.nginx.conf和slave.solr.nginx.conf ,还需要创建两个存放log文件的目录 /export/servers/nginx/nginx-1.5.0/sbin/logs/master.solr.com和 /export/servers/nginx/nginx-1.5.0/sbin/logs/slave.solr.com
#master nginx conf upstream tomcat_master.solr { server 127.0.0.1:9000 weight=10 max_fails=2 fail_timeout=30s; } server { listen 80; server_name master.solr.com; #项目域名 access_log /export/servers/nginx /nginx-1.5.0/sbin/logs/master.solr.com/master.solr.com_access.log main; #存放log文件目录 error_log /export/servers/nginx /nginx-1.5.0/sbin/logs/master.solr.com/master.solr.com_error.log warn; error_page 411 = @my_error; location @my_error { } root /export/data/WebRoot/master.solr.com;#master 项目文件的目录 location / { proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#反向代理获取用户访问的ip地址 proxy_pass http://tomcat_master.solr; expires 1d; } location /logs/ { autoindex off; deny all; } }
upstream tomcat_slave.solr { server 127.0.0.1:9001 weight=10 max_fails=2 fail_timeout=30s; } server { listen 80; server_name slave.solr.com;#项目域名 slave access_log /export/servers/nginx /nginx-1.5.0/sbin/logs/slave.solr.com/slave.solr.com_access.log main;#slave 访问log error_log /export/servers/nginx /nginx-1.5.0/sbin/logs/slave.solr.com/slave.solr.com_error.log warn; error_page 411 = @my_error; location @my_error { } root /export/data/WebRoot/slave.solr.com; #slave项目文件存放目录,之前已经配置过了 location / { proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://tomcat_slave.solr; expires 1d; } location /logs/ { autoindex off; deny all; } }以上两个文件创建之后,修要修改nginx的总的配置文件 /export/servers/nginx/nginx-1.5.0/sbin/conf/nginx.conf
user username username; worker_processes 2; error_log /export/servers/nginx /nginx-1.5.0/sbin/logs/error.log warn; #配置nginx error log位置 pid /export/servers /nginx/nginx-1.5.0/sbin/logs/nginx.pid; worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; #配置工作连接数 } http { include mime.types; # default_type text/plain; #application/octet-stream default_type application/octet-stream; server_tokens off; log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' #charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 0; tcp_nodelay on; client_body_buffer_size 512k; fastcgi_intercept_errors on; proxy_connect_timeout 90; proxy_read_timeout 180; proxy_send_timeout 180; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; server_name_in_redirect off; proxy_hide_header X-Powered-By; error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 506 = http://www.baidu.com/search/error.html; #借用下百度的error页 include domains/*; #所有的配置文件都包含到nginx.conf文件中了这里面的文件就是master.solr.nginx.conf ,slave.solr.nginx.conf server { listen 80; server_name www.baidu.com; location / { access_log off; } location /logs { access_log off; } location /html { access_log off; } } }
root@lcpc:/export/servers/nginx/nginx-1.5.0/sbin/sbin# ./nginx
vim /etc/hosts 127.0.0.1 master.solr.com 127.0.0.1 slave.solr.com打开浏览器访问master.solr.com 和slave.solr.com现实solr管理页
访问不同的域名,nginx自动去访问了相应的项目,打开/export/servers/nginx
/nginx-1.5.0/sbin/logs/master.solr.com/master.solr.com_access.log 可以看到
刚刚的访问log
127.0.0.1 - - [29/Jun/2013:00:33:15 +0800] "GET /null/admin/system?wt= json HTTP/1.1" 404 1142 "http://127.0.0.1/" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0" server_names_hash_ bucket_size128 127.0.0.1 - - [29/Jun/2013:00:35:38 +0800] "GET /null/admin/syste m?wt=json HTTP/1.1" 404 1142 "http://master.solr.com/" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0" s erver_names_hash_bucket_size128 127.0.0.1 - - [29/Jun/2013:00:36:10 +0800] "GET /null/admin/syste m?wt=json HTTP/1.1" 404 1142 "http://master.solr.com/" "Mozilla/5. 0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0" server_names_hash_bucket_size128
打开/export/servers/nginx/nginx-1.5.0/sbin/logs/slave.solr.com/slave.solr.com_access.log
127.0.0.1 - - [29/Jun/2013:00:29:07 +0800] "GET /null/admin/system?wt= json HTTP/1.1" 404 1142 "http://slave.solr.com/" "Mozilla/5.0 (X11; Ub untu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0" server_names_ha sh_bucket_size128 127.0.0.1 - - [29/Jun/2013:00:29:07 +0800] "GET /img/ico/construction.p ng HTTP/1.1" 200 861 "http://slave.solr.com/css/styles/common.css" "Mo zilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/1 7.0" server_names_hash_bucket_size128 127.0.0.1 - - [29/Jun/2013:00:29:07 +0800] "GET /img/favicon.ico HTTP/ 1.1" 200 1454 "-" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Geck o/20100101 Firefox/17.0" server_names_hash_bucket_size128 127.0.0.1 - - [29/Jun/2013:00:38:37 +0800] "GET /null/admin/system?wt=j son HTTP/1.1" 404 1142 "http://slave.solr.com/" "Mozilla/5.0 (X11; Ubu ntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0" server_names_ha sh_bucket_size128
总结:1.安装nginx
2.安装tomcat 配置项目文件路径
3.配置项目nginx upstream ,配置nginx总的配置文件
4.修改host文件用于测试