ubuntu下配置apache反向代理多个tomcat

安装多个tomcat在同一台服务器上:(来源:http://www.javacodegeeks.com/2011/08/multiple-tomcat-instances-on-single.html)

In this post we will see how to run multiple tomcat instances on a single machine and under a single user account.

We first see the tomcat directory structure, where each folder has the following purpose:

bin -  contains all binary and script files for running tomcat.

lib – contains all shared libraries used for tomcat

conf - contains configuration information like which port tomcat can bind, etc.

logs – contains all log files

temp – this folder is used by Tomcat for temporary files

webapps – this folder is very important, we put here all the application war files

work – if an application contains JSPs, then each JSP is translated and converted into a servlet and is stored here


When we run Tomcat, it uses 5 environment variables. They are:

  • CATALINA_HOME

  • CATALINA_BASE

  • CATALINA_TMPDIR

  • JRE_HOME/JAVA_HOME

  • CLASSPATH

In the above list, CATALINA_HOME and JAVA_HOME are mandatory environment variables. All others are optional and can be calculated using CATALINA_HOME.

CATALINA_HOME – this environment variable should point to tomcat base folder, where tomcat binary are  installed/extracted. so based on CATALINA_HOME we can get bin and lib folder

CATALINA_BASE – If we not specified then CATALINA_HOME value is set. This variable pointed to configuration and webapps folder. Based on this variable server uses conf, logs, temp, webapps, work folders.

A usual way to run Tomcat is to only set the CATALINA_HOME environment variable and run the startup.sh script file. The startup.shfile automatically calculates and assigns the values of other variables which we have not set.

The startup.sh file sets the environment variable and  then calls catalina.sh. This file reads CATALINA_BASE value, attaches conf i.e $CATALINA_BASE/conf folder and gets server.xml. This file is the heart of Tomcat’s configuration. It contains all configuration information, like shutdown port, connector post, host name, application folder, etc. For example, Tomcat usually uses 8080 as a connector port, so we can access it at http://localhost:8080/.

If we set the $CATALINA_BASE explicitly, then Tomcat uses our variable to search and get the server.xml file from our target place, i.e what we specified in CATALINA_BASE. 

This trick can be used to run multiple Tomcat instances in a single machine. We don’t need to change the CATALINA_HOME value. We just need to change the CATALINA_BASE value before start/shutdown Tomcat.

Create one folder named “tomcat-instance1” and copy conf, logs, temp, webapps, work folder from CATALINA_HOME folder and change conf/server.xml file in tomcat-instance1. We need to change these ports: shutdown port, connector port, ajp port and redirect port.

Shutdown port – This port is used for shutting down Tomcat. When we call the shutdown.sh script, it sends a signal to shutdown port. This port is where the Tomcat Java process listens. If such signal is received, the process then cleans up and exits by itself.

Connector port -This port is the actual port that exposes the application to an outside client. 

ajp port – This port may be used by a web server (e.g. Apache httpd server) to communicate with Tomcat. This port is also used when we setup a load balanced server.

Redirect port – If the Connector is supporting non-SSL requests and a request is received for which a security constraint requires SSL, Catalina will automatically redirect the request to this port. 

Let’s see the sample server.xml file:

1<server port="8005" shutdown="SHUTDOWN">
2    .....
3    <connector
4        connectiontimeout="20000"port="8080"
5        protocol="org.apache.coyote.http11.Http11NioProtocol"
6        redirectport="8443" />
7    <connector port="8009" protocol="AJP/1.3" redirectport="8443" />
8</server>

So, we change these ports to different numbers, because once a port is binded, then an other process can’t bind it again. In tomcat-instance1/conf/server.xml file I configured server port =8105, connector port = 8181, ajp port = 8109.

1<server port="8105" shutdown="SHUTDOWN">
2    .....
3    <connector
4        connectiontimeout="20000" port="8181"
5        protocol="org.apache.coyote.http11.Http11NioProtocol"
6        redirectport="81443" />
7    <connector port="8109" protocol="AJP/1.3" redirectport="81443" />
8</server>

Now we can create two script files for starting up and shutting down the tomcat-instance1.

startup-instance1.sh

1export CATALINA_BASE= /home/ramki/tomcat-instance1
2cd $CATALINA_HOME/bin
3./startup.sh

shutdown-instance1.sh

1export CATALINA_BASE= /home/ramki/tomcat-instance1
2cd $CATALINA_HOME/bin
3./shutdown.sh

Here we explicitly set the CATALINA_BASE variable and point it to the new tomcat-instance1. Then we go to the CATALINA_HOME/bin folder because all binary files for running tomcat are still present there. Then we use the startup/shutdown cripts.

Based on the above technique, we can create many instance folders and change the conf/server.xml file port values and run that instance with their own newly created script files.

把工程与域名绑定

    1.在申请域名的网站把自己服务器的ip和申请的域名绑定。

    2.在tomcat的server.xml文件中作如下配置

        

    <Host name="自己申请的域名"  appBase="自己的应用所在的文件夹"

            unpackWARs="false" autoDeploy="true"

            xmlValidation="false" xmlNamespaceAware="false">

      </Host>

    自己的应用所在的文件夹应位于tomcat的根目录下,里面的子文件夹名应为ROOT.

安装并配置apache:

    ubuntu之apache正向代理及反向代理(ProxyPass\ProxyPassReverse)

环境是UBUNTU 最新版apache2安装的目录结构有变化网上很多文章都不适用了。

准备

如果你原程序有问题,先清理一下

[plain] view plaincopyprint?

  1. apt-get --purge remove apache2  

  2. apt-get --purge remove apache2.2-common  

  3. apt-get autoremove  

  4. apt-get --purge remove apache-common  

  5. apt-get --purge remove apache  

  6. sudo find /etc -name "*apache*" -exec rm -rf {} /;  

  7. sudo rm -rf /var/www  



安装

[plain] view plaincopyprint?

  1. apt-get install apache2  

  2. service apache2 stop  


配置


(1)

[plain] view plaincopyprint?

  1. a2enmod proxy proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http  


(2)
修改配置 sudo vim /etc/apache2/mods-enabled/proxy.conf

如果有内容设置则设置如下:

[plain] view plaincopyprint?

  1. <IfModule mod_proxy.c>  

  2.     #turning ProxyRequests on and allowing proxying from all may allow  

  3.     #spammers to use your proxy to send email.  

  4. ProxyRequests Off  

  5. <Proxy *>  

  6.     Order deny,allow  

  7.     #Deny from all  

  8.     #Allow from .your_domain.com  

  9. </Proxy>  


(3)
修改配置 vim /etc/apache2/sites-enabled/default

[plain] view plaincopyprint?

  1. <VirtualHost *:80>  

  2.          ServerName 自已的域名1

  3.          ServerAlias 自已的域名1  

  4.          ProxyPass / http://自已的域名1:端口号1/  

  5.          ProxyPassReverse / http://自已的域名1:端口号1/  

  6. </VirtualHost>  

  1. <VirtualHost *:80>  

  2.          ServerName 自已的域名2

  3.          ServerAlias 自已的域名2

  4.          ProxyPass / http://自已的域名2:端口号2/  

  5.          ProxyPassReverse / http://自已的域名2:端口号2/  

  6. </VirtualHost>  



service apache2 restart

    

















转载于:https://my.oschina.net/mingdegewu/blog/217653

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值