Mac 环境下搭建 Nginx + Tomcat集群, Nginx + Tomcat集群是大家常用的一种搭配, 好处有很多, 而我做这个的初衷就2个目的, 1: 解决tomcat的复杂均衡问题, 2. 当我上线的时候, 启动tomcat, 能够做到外部访问不间断。
搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程
CentOS下搭建Nginx+Tomcat实现集群负载与session复制
下面开始我们的步骤:
第一步: 安装我们的Nginx, Mac上是利用brew安装的;
[linuxidc@:~]$ brew install nginx
==> Installing nginx dependency: pcre
==> Downloading ######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/pcre/8.34 --enable-utf8 --enable-unic
==> make
==> make test
==> make install /usr/local/Cellar/pcre/8.34: 140 files, 4.1M, built in 58 second>
==> Installing nginx
差不多就这一步, 我们看看是够安装成功:
[linuxidc@:~]$ nginx -V
nginx version: nginx/1.4.4
TLS SNI support enabled
configure arguments: --prefix=/usr/local/Cellar/nginx/1.4.4 --with-http_ssl_module --with-pcre --with-ipv6 --sbin-path=/usr/local/Cellar/nginx/1.4.4/bin/nginx --with-cc-opt=-I/usr/local/include --with-ld-opt=-L/usr/local/lib --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-http_gzip_static_module
能看见, 这里安装的是 1.4.4版本的.
顺便告诉大家, brew安装的文件路径一般默认在:
[linuxidc@:Cellar]$ pwd
/usr/local/Cellar
然而, nginx的真正路径在:
[linuxidc@:nginx]$ pwd
/usr/local/etc/nginx
我们启动nginx服务器:
[linuxidc@:~]$ sudo nginx
Password:
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] still could not bind()
输入你的个人密码, 就算是启动了:
这就算启动了. 很简单吧~
如果是查看nginx启动进程
[linuxidc@:~]$ ps -ef|grep nginx
0 8220 1 0 9:07PM ?? 0:00.00 nginx: master process nginx
502 8322 8220 0 9:34PM ?? 0:00.87 nginx: worker process
502 8323 8220 0 9:34PM ?? 0:00.15 nginx: worker process
502 8704 8598 0 8:27PM ttys000 0:00.00 grep nginx
关闭nginx服务器:
[linuxidc@:~]$ sudo nginx -s stop
============= 现在就差不多把nginx的安装说明白了. OK吗?
第二步: 我们在本机部署2台Tomcat容器, 并且要正常跑起来. 具体看看:
你需要下载一个Tomcat, 这个我觉得不需要在说怎么下载Tomcat了吧..我一般会去官方下载tar包.
apache-tomcat-7.0.37
下载之后, 我们将名字修改为:
drwxr-xr-x 16 linuxidc staff 544 Jun 21 18:39 tomcat-7-2
drwxr-xr-x 16 linuxidc staff 544 Jun 21 18:41 tomcat-7-3
我这里暂且从2开始取名吧~ 因为我本地还有一个tomcat-7-1, 自己开发用的. 反正这只是一个文件夹的名字而已, 应该不会造成混淆. 我们就以 tomcat-7-2, tomcat-7-3, 开举例子:
如果需要让2台Tomcat同时启动, 我们是需要修改配置文件的, 因为Tomcat绑定的端口号是唯一的, 其实我们就是让他们的端口号不冲突而已. 这样子就能跑起2台Tomcat了.