Collation
One of my colleagues helped the school develop several websites using PHP this semester. When I went to deploy the website on the server yesterday, I found that the server was running the ASP Website on IIS, I have to run the JSP website under Tomcat again. This time, I have to install Apache httpd that runs PHP again. I don't know what to do next. I have heard of it, and it's probably only in school. However, the problem still needs to be solved due to perspiration. In this case, there are mainly three deployment strategies.
1. IIS, Apache httpd, and tomcat run different websites as web servers and use different ports, such as IIS (80), Apache httpd (8080), and Tomcat (9090 ). The most obvious advantage of this method is that the configuration is simple. The three servers run each website without affecting each other. The disadvantage is that the server needs to open ports 8080 and 9090, and the corresponding PHP website and JSP website must be added with port numbers 80, 90, which are hard to remember.
2. Use IIS and Apache httpd for Web server, and tomcat for servlet container and IIS integration. In this way, IIS supports both ASP and JSP websites. Therefore, you can select the default port number 80, while Apache httpd uses 8080 to run the PHP website separately. The advantage of this method is that you only need to open port 8080 separately. Both ASP and JSP websites can use the default port, which is more user-friendly. In addition, if the JSP website has a large access volume, you can use IIS to process static resources (such as HTML, image, JS, and CSS), which is much more efficient than Tomcat's built-in web server. The disadvantage is that additional configuration is required to integrate IIS and tomcat, Which is troublesome.
3. Similar to policy 2, but Tomcat is integrated with Apache httpd. The corresponding Apache httpd uses port 80 to run php and JSP websites, while IIS uses port 8080 to run ASP websites.
There are no problems with the above three policies for school website deployment. Policy 1 is preferred if you are lazy, but considering that Apache and tomcat are more integrated in the production environment, we also choose the third strategy. After all, it is best for students to learn something :)
The following describes howUse Tomcat Connector JK to integrate apache2.2 and tomcat 7 in Windows.
1. Download JK
First, go to Tomcat's official website to download JK. The latest version is 1.2.32. Select Binary releases.
Bytes
Decompress the zip package and place the mod_jk.so dynamic link library file in the Apache installation directory (% Apache_home %.
2. Define Tomcat workers
What is Tomcat worker? According to the official JK documentation, Tomcat worker is a tomcat instance or a tomcat process. It represents a web server that processes servlet requests or other content requests. We know that the Apache httpd server can only process static Web resources. If you encounter a Servlet request, you need to submit it to the Tomcat worker defined by us for processing. So how to define Tomcat worker?
In JK, use the property FileWorkers. PropertiesTo define one or more Tomcat instances. Enter the % apache_home % conf directory, create a new workers. properties file, and enter the following content:
# Woker. list multiple workers can be defined and separated by spaces. Here we only need one worker.
Worker. List = localworker1
# Specify the attribute of localworker1
Worker. localworker1.type = ajp13 # Connect using the AJP/1.3 Protocol
Worker. localworker1.host = localhost # Tomcat on the local machine
Worker. localworker1.port = 8009 # default port number of AJP/1.3
In this file, we define a tomcat instance named localworker1 and specify it to communicate with Tomcat using the ajpv13 protocol. What does this mean? Go to the tomcat installation directory (% Tomcat_home %In the conf directory, check the default configuration of the ajpv13 connector in server. xml:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
The above indicates that the local Tomcat instance starts the AJP/8009 connector on port 1.3, and the Apache httpd server will communicate with the Tomcat instance through the AJP/1.3 protocol! Therefore, the preceding worker configuration must be consistent with that of the ajp13 connector of Tomcat.
For more information about workers. properties, see the http://tomcat.apache.org/connectors-doc/reference/workers.html.
3. Define routing rules
With Tomcat woker, we also need to specify the request to be processed by the httpd server and the request to Tomcat, that is, the routing rule. Generally, we send requests to static Web resources (such as HTML, image, CSS, and JS files) to the Web server and send requests to Servlet and JSP to Tomcat for processing. A routing rule is usually calledUriworkermap. Properties. Next, create the uriworkermap. properties file in the directory % apache_home % Conf, and enter the following content:
/* = Localworker1
# Static files cannot be processed by any worker and are handed over to Apache httpd
! /*. Gif = *
! /*. Jpg = *
! /*. PNG = *
! /*. CSS = *
! /*. Js = *
! /*. Htm = *
! /*. Html = *
Here, we will first hand over all requests to the previously defined Tomcat worker for processing, in use! The syntax indicates that some static resources are processed by Apache httpd. This isWriting Method officially recommended by JK! I found this method popular in tutorials on other websites:
/*.jsp=localworker1
/*.do=localworker1
/servlet/*=localworker1
This method specifies all JSP requests, Action requests (Action requests using the Struts framework usually use. Do as the suffix), and Servlet requests to Tomcat for processing, and the remaining requests to Apache httpd for processing. It is strongly not recommended. I think this writing method has at least two shortcomings:
1. limits the URL format of the JSP website. For example, the urlpattern of all servlets on the JSP website must be in the/servlet/* format, otherwise the request will not reach tomcat.
2.Major security risks!Without additional configuration, users will be able to access the Web. xml and class files under the WEB-INF directory of the website. This is because Tomcat itself can prohibit users from accessing the WEB-INF directory, and Apache does not. This routing method only sends JSP and Servlet requests to Tomcat for processing. Apache httpd naturally returns files to users by default when user requests/WEB-INF/Web. xml. The first writing method does not have this problem, because/WEB-INF/is still handled by Tomcat.
For more information about uriworkermap. properties, see the http://tomcat.apache.org/connectors-doc/reference/uriworkermap.html.
4. Define virtualhost
Someone may ask, if the PHP file request is configured as above, it will not be handed over to Tomcat for processing. Hey, no, because PHP and JSP websites use different domain names. Apache httpd supports configuring virtual hosts, that is, multiple domain names with one IP address. In this case, we only need to start JK for the JSP website.
The VM configuration can be written in % apache_home % CONF/httpd. conf, but it is recommended to modify % apache_home % CONF/extra/Httpd-vhosts.confFile. Assume that our PHP website domain name isWww.phpsite.com, The JSP website domain name isWww.jspsite.comThe configuration is as follows:
NameVirtualHost *:80
# php website host
<VirtualHost *:80>
ServerAdmin xxx@xxx.com
ServerName www.phpsite.com
ErrorLog "logs/www.phpsite.com-error.log"
CustomLog "logs/www.phpsite.com-access.log" common
DocumentRoot "E:/PHP_WWW"
DirectoryIndex index.html index.php
</VirtualHost>
# java website host
<VirtualHost *:80>
ServerAdmin xxx@xxx.com
ServerName www.jspsite.com
ErrorLog "logs/www.jspsite.com-error.log"
CustomLog "logs/www.jspsite.com-access.log" common
DirectoryIndex index.html index.jsp
JkAutoAlias "E:/JSP_WWW"
JkMountFile "conf/uriworkermap.properties"
</VirtualHost>
E:/php_www and E:/jsp_www are the root directories of the PHP website and JSP website respectively.
Jkautoalias maps the context of the JSP website to the directory space of Apache. Therefore, the user accesses the ingress.
Jkmountfile indicates the path of the routing rule attribute file we just defined.
5. Modify Tomcat server. xml
Open below% Tomcat_home % CONF/server. xmlMake the following changes:
1) because IIS still occupies port 8080 on our server, we need to disable the default HTTP Server Service of Tomcat or change the port. Find the following configuration and comment it out.
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
2) The Directory of the JSP website is E:/jsp_www. Therefore, you need to modify the default appbase.
<Host name="localhost" appBase="E:/JSP_WWW"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
3) for testing, copy the examples directory under % atat_home % webapps/to E:/jsp_www.
4) Run % tomcat_home % bin/startup. BAT to start Tomcat. The following log shows that the startup is successful.
6. Modify the httpd. conf file of Apache.
Next we will modify the Apache main configuration file httpd. conf to open% Apache_home % CONF/httpd. conf.
1) first confirm that the Apache httpd startup port is 80:
Listen 80
2) locate the module loading area (starting with a large string of loadmodule), and add the following three lines to load jk_module:
Loadmodule jk_module modules/mod_jk.so
Jkworkersfile "CONF/workers. properties" # specify the previously configured worker File
Jklogfile "logs/mod_jk.log"
3) Specify the DocumentRoot of the PHP website. If PHP has been configured before, you should be able to find the following configuration (for convenience, I have removed all comments ):
DocumentRoot "E:/PHP_WWW"
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
<Directory "E:/PHP_WWW">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
4) continue to find the include file section, remove "#" before "# include CONF/extra/httpd-vhosts.conf", including the virtual host configuration file we just modified.
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
5) Save and exit and start Apache httpd.
7. test whether the configuration is successful.
Note: If you want to integrate Apache and tomcat on your computer without a domain name, you can modify the hosts file and add it at the bottom of the file:
127.0.0.1 www.phpsite.com
127.0.0.1 www.jspsite.com
1) Test the PHP website. Create index. php In the E:/php_www directory and enter:
<?
phpinfo();
?>
Open your browser and visit "www.phpsite.com". The following page shows that the PHP website is running properly.
2) Test the JSP website. Access "www.jspsite.com/examples" and "www.jspsite.com/examples/servlets/servlet/helloworldexample". The previous user tests whether static resource index.html can be accessed.
We can also test whether we can access the WEB-INF directory, "www.jspsite.com/examples/web-inf/web.xml", according to the first uriworkermap. properties syntax above is not accessible :)
Other Integration Methods
After apache2.2, apart from using JK to integrate Apache httpd and tomcat, there is also a more convenient way to use the newly added mod_proxy_ajp module, which I have not tried yet, if you are interested, try it. However, JK is more powerful in general because it also supports Tomcat cluster and Server Load balancer-related configurations.
Welcome to reprint, but reprint please indicate the source of http://www.cnblogs.com/codingmyworld/archive/2011/12/08/2281057.html