Apache+Tomcat通过ajp_proxy配置php和jsp环境

目标:同时支持解析PHP,JSP

方案:整合 Apache/PHP与Tomcat。考虑到速度,决定让Apache处理普通HTML文档和PHP请求,Tomcat处理JSP请求。

软件环境:阿里云CentOS 6.3 64位 linux,Apache 2.2.15,Tomcat 6

步骤:

(1)yum安装Apache、mysql、php,tomcat直接下载绿色版。

(2)httpd.conf虚拟主机配置

<VirtualHost *:80>
    ServerName www.jsp1.com
    ProxyPass / ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.<span style="font-family: Arial, Helvetica, sans-serif;">jsp2</span>.com
    ProxyPass / ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.php1.com
    DocumentRoot /var/www/php1
    ErrorLog /etc/httpd/logs/php1<span style="font-family: Arial, Helvetica, sans-serif;">-error_log</span>
    CustomLog /etc/httpd/logs/php1-access_log common
</VirtualHost>
前两个是通过ajp将访问的域名转发给tomcat处理,后一个是由php处理。

注意事项,由于本人配置了N次,弄了好长时间,总是只有配置的第一个虚拟主机生效,下面两个总是无效。最后终于找到了问题的所在,要特别加上这面一句。

NameVirtualHost *:80

然后将Apache的端口设置为80

Listen 80

(2)配置tomcat,端口8080

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
	
	<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
	<Listener className="org.apache.catalina.core.JasperListener" />
	<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
	<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
	<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
	
	<GlobalNamingResources>
		<Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
	</GlobalNamingResources>
	
	<Service name="Catalina">
		<Connector port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" 
			connectionTimeout="20000" 
			redirectPort="8443" compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="image/jpeg,image/png,text/html,text/xml,text/javascript,text/css,text/plain" />
		<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
		<Engine name="Catalina" defaultHost="localhost">
			<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
				resourceName="UserDatabase"/>
			<Host name="www.jsp1.com"  appBase="webapps"
				unpackWARs="true" autoDeploy="true"
				xmlValidation="false" xmlNamespaceAware="false">
				<Context path="" docBase="/usr/local/wangdalei/www/jsp1" debug="0" reloadable="true"/>
			</Host>
			<Host name="www.jsp2.com"  appBase="webapps"
				unpackWARs="true" autoDeploy="true"
				xmlValidation="false" xmlNamespaceAware="false">
				<Context path="" docBase="/usr/local/wangdalei/www/jsp2" debug="0" reloadable="true"/>
			</Host>
		</Engine>
	</Service>
	
</Server>


通过上面的配置可以看出,Apache接收到请求后,根据ServerName来匹配到域名后,将jsp项目的请求通过ajp代理转发给tomcat处理,tomcat中得ajp得监听端口是8009。








你可能感兴趣的:(apache,tomcat,jsp,PHP,ajp_proxy)