Integrate JSP and PHP in Apache

Source: Internet
Author: User
Tags php web server apache tomcat

Integrate JSP and PHP in Apache

We want everything to be perfect, for example, in an enterprise application, we only need to select a web platform to deploy all of our enterprise applications. But often the ideal is far from reality. In the real world, information systems are often mixed by many different operating systems, platforms, and application environments. And in order to remain compatible with the old system, the system always mixes the current technology with previous legacy technologies, so that it repeats itself. The system becomes more and more complex.

For such a vicious change, many people are going to be on the brink, especially the developers, who will face almost a crash-like challenge. This is especially true if you want to deploy two or more technologies in the same application. The example in this article is to deploy both PHP and JSP programs in the same application running in Apache. In this example, there is a way to quickly deploy both of these web programs. Developers can also use this approach to deal with other similar challenges.

Perhaps a lot of people will ask, should I do as well as the PHP and JSP-based web programs deployed in the same Web application? I'll answer that question. The basic answer to this question is to first configure Apache as a PHP Web server and use Apache to process all php/cgi requests, while using Tomcat to process all other requests (including JSP requests). Before we get started, let's look at what software is needed:

Three software is required to complete the example of this article:
1. Apache HTTP Server
2. Apache Tomcat
3. Apache Tomcat Connectors

First, the technology used in the solution

For the above questions, we have several solutions to consider. These solutions are as follows:

1. Make the Apache server the primary or default request processor, passing only servlet/jsp requests to the Java container.

2. Use the Java container as the primary or default request processor, allowing only php/cgi requests to be processed by Apache.

3. Do not use Apache and run the PHP/CGI program in Tomcat.

The first two solutions above are similar. Both of these methods introduce a large number of requests to one server (tomcat or Apache) and then use a different server to process individual requests (JSP or PHP). The third approach, though seemingly simple, is tempting. However, Apache is very efficient in handling PHP and provides a more secure mechanism, so Tomcat is not a good place to handle PHP programs.

In this article we will take the second approach, which is to use Tomcat as the primary Web server to receive most requests, while Apache acts as a fallback server to process php/cgi requests, as shown in relationship 1. If the reader has a unique understanding of the first method, it can easily be set according to the second method.



Figure 1 Tomcat as the primary request processor, Apache as a secondary request processor

Second, configure Apache

The first step in the entire process is very important and also the most complex. That is, you need to install the Apache server and then configure Apache to support Apache Tomcat. For the installation, we can search Google or Baidu related articles, this article is not too detailed to tell. But how to configure it, first we need to use an Apache Tomcat connector, we can choose MOD_JK or MOD_JK2. I highly recommend the use of mod_jk here, because MOD_JK was abandoned three years earlier. We'll start by opening the Apache configuration file httpd.conf, and we'll see something like this:


#
# use name-based virtual hosting.
#
#NameVirtualHost *:80

#
# VirtualHost Example:
# Almost any Apache directive could go into a VirtualHost container.
# The first VirtualHost section was used for requests without a known
# server name.
#

In the following section, a VirtualHost node will be created that captures all HTTP requests on port 80,
and map them as described above (Tomcat is the default, Apache only handles PHP and CGI requests).


<virtualhost *:80>
# Set DocumentRoot equal to Tomcat ROOT context
Documentroot/www/webapps/root

# Exclude PHP & CGI (let Apache handle)
Setenvif Request_uri \.php NO-JK
Setenvif Request_uri \.cgi NO-JK

# process every request (except PHP and CGI) with MOD_JK
# will process from Apache to Tomcat
Jkmount/* Myworker

</VirtualHost>

In the above description, we should be able to easily change it to the first method, that is, only to Jkmount processing JSP or servlet request to Tomcat

Once we have configured the environment properly, we can package and publish the application to the Web server. There is no difference between publishing an application to Apache and publishing it to Tomcat, and you need to create a web-inf/web.xml file, unlike if you are requesting a PHP or CGI program that bypasses Tomcat and passes it directly to Apache for processing.

Although changing the root directory of Apache (the default is ' Dtdocs ') is not convenient for publishing applications. But we can point the Apache root directory to the Tomcat publishing directory. So php/cgi can and JSP in the same war/ear package. We simply place the JSP, HTML, and PHP pages in the catalog to complete the release. This is very convenient for development, or for testing.

In addition, publishing PHP, JSP, and CGI to the same directory also allows them to share the same supporting files (CSS, JS, images, and so on). Also, this model provides the complete structure of a Web application and makes it easier to call the PHP/CGI program from the Java Web directory (relatively).

Iv. Summary


Whether you're being asked to integrate legacy Web applications into a new Web application at work, or add an open source system to a custom Web application, or simply integrate two different Web applications together, it's possible to integrate different technologies into the same Web application. In a word, converting different technologies to the same technology platform is the best solution

Integrate JSP and PHP in Apache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.