Showing posts with label Apache. Show all posts
Showing posts with label Apache. Show all posts

Tomcat + Apache HTTP: Servers Connecting Guide

Apache Tomcat is a Servlet/JSP container and used to deploy dynamic Java contents like JSPs and Servlets. Tomcat has some web server capabilities, however it is not a full blown web server to serve high traffic web sites. In the meantime Apache HTTP server is a full blown web server. So in general Tomcat is configured along with Apache HTTP server to maintain a healthy web site with dynamic content. This article will cover the steps of configuring HTTP server and Tomcat connection, however this will not discuss installation details of Apache or Tomcat.

System Requirements

  • Apache HTTP server installed
    • download from here
    • we used version 2.2.10 for this article
  • Apache Tomcat installed
    • download from here
    • we used version 5.5 here
Short Names
Followings are the short names used to denote folders used in this article.
  • %APACHE_HTTP_HOME%
    • denotes Apache HTTP Server installation directory. In our testing machine, it is "D:\ASF\Apache"
  • %CATALINA_HOME%
    • Tomcat installation directory; for example: "D:\ASF\Tomcat"

Connection Configurations

The connection between HTTP server and Tomcat will be done using Apache Tomcat Connector module named mod_jk which is configured inside Apache HTTP server. For that first download the correct version of connector from here.

For version 2.2.10 of Apache HTTP; correct connector version is 2.2.4 (available here). Make sure to download the correct version, as incorrect version may result in errors.

Mod_jk configuration

Apache HTTP ServerNow the downloaded mod_jk module must be configured in Apache HTTP server. First rename the above downloaded file say mod_jk-1.2.26-httpd-2.2.4.so to "mod_jk.so". In Apache server, module files are stored under %APACHE_HTTP_HOME%\modules folder. Now copy the mod_jk.so file into this folder.

Then open the HTTP server configuration file named httpd.conf. It's located in %APACHE_HTTP_HOME%\conf folder. This file has a number of lines used to load Modules starting with "LoadModule ". Add the following line below these lines.

LoadModule jk_module modules/mod_jk.so

Now the downloaded mod_jk module is added into Apache server.

Tomcat Server Information

We need a new properties file to store details about the Tomcat server. Create a new file named workers.properties inside %APACHE_HTTP_HOME%\conf folder with the following content.

worker.list=localTomcat
worker.localTomcat.host=localhost
worker.localTomcat.port=8009
worker.localTomcat.type=ajp13

Pay attention to the above port 8009; this is the port defined inside %CATALINA_HOME%\conf\server.xml file as follows.

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Tomcat ServerIf the port in AJP 1.3 connector section of Tomcat is different than 8009, change the above workers.properties file to match that.

Now we need to configure the workers.properties file in %APACHE_HTTP_HOME%\conf\httpd.conf file. For that add the following lines after the line that we added into httpd.conf in a previous step.

JkWorkersFile conf/workers.properties
JkShmFile logs/mod_jk.shm
JkLogFile logs/mod_jk.log

Now everything is setup. However still we have not yet defined the important part; which user requests to be forwarded to Tomcat server from Apache server.

URL Patterns

Following is the format of those configurations.

JkMount  <URL_PATTERN>  <WORKER>
  • <URL_PATTERN> - the url pattern that you need to forward to Tomcat
  • <WORKER> - the worker name defined in workers.properties file.
We can set up multiple url patterns as follows.

JkMount  /myProject/* localTomcat
JkMount /*.jsp localTomcat
JkMount /*.do localTomcat

First one will send all requests for a web application named "myProject" to Tomcat while the second will send all requests ending with ".jsp". The third line configures for all requests ending with ".do" (many use .do extension with Struts actions).

Add required configuration lines as above into httpd.conf (just after the lines we added in above steps).

Setup Completed

Now try the URLs of your application as follows (using Apache port number, rather than Tomcat port number). We assumed Apache HTTP server is running on port 80. Following as some sample urls for the above configurations.

http://localhost/myProject/
http://localhost/testApp/main/admin.jsp
http://localhost/strutsApp/login.do

Now Apache will direct all these request to Tomcat (running on a different port) and serve the client smoothly.

Related Article: How to change Tomcat port

Configuring Apache Web Server and PHP

[Update: You may download the MSI
package
of PHP instead of the ZIP one which would do all the set-up and
configuration itself and you wouldn’t have to do the following by yourself]


This is the crucial part guys, first I start off with giving the locations
of the configuration files of Apache and PHP.



Apache: [X:\…]\Apache Software Foundation\Apache2.2\conf\httpd.conf

PHP: [Y:]\PHP\php.ini-dist [renamed to php.ini later]



Now let’s start guys:


1. Start by backing up both the configuration files, you’d
need them in case you mess up the files.


2 .Rename the php.ini-dist file to php.ini


3. Look for a line like below

doc_root

and make it look like

doc_root =[X:\…]\Apache Software Foundation\Apache2.2\htdocs


be sure to change ’[X:\…]\’ with the location you installed
Apache server in. Save the file.


4. Open the folder you installed PHP in. There you’ll
see lots of DLL files, select them all and click copy. Now open your Windows
System directory and click paste. If any of the files already seem to be there
be sure to make a back up of them.



Location of Windows system folder for various versions of windows is listed
below:


Windows XP/Vista: c:\windows\system32

Windows NT/200: c:\winnt\system32



5. Now, copy the php.ini file (the one you edited) and paste
it in the system root directory.



Again the Location of Windows root directory for various versions of windows
is listed below:


Windows XP/Vista: c:\windows\

Windows NT/200: c:\winnt\



6. Now PHP itself is all set-up, its time now to tell Apache
to use it to interpret special files(.php). Open the httpd.conf (Apache configuration
file)


7. Seach for the word “Load Module” without the
quotes, you’d see many such lines, scroll to the end of that section and
paste or type these lines after the last Load Module statement:


#-----For PHP--------
LoadModule php5_module "x:/php/php5apache2_2.dll"

AddType application/x-httpd-php .php

# For syntax highlighted .phps files,
AddType application/x-httpd-php-source .phps

#-----/For PHP--------

Change the line “x:/php/php5apache2_2.dll” from ‘x’
to the drive you installed PHP in. Save the file.


Now we are all set-up, restart the Apache server for the changes to take effect.
Copy and paste the following in Notepad and save it with a ‘.php’
extension (Save as type: All Files) and put the file in the htdocs folder.


  <?php
phpinfo();
?>

Now open the browser and type http://localhost/[filename].php.


You should see something like below, if you have done everything correctly.


PHP info


If not replace both the configuration files by the backed up ones and start
all over again.


Best of luck!


Related Articles:


Installing Apache and PHP on your PC

So you want to install web server on your own computer and have downloaded
Apache and PHP. Please read

Installing
Web Server on your PC
first, if in doubt.


Installing Apache web server


Installing Apache is a no-brainer. It is just as you install any other software.
Run the set-up, feed the information in each step and click finish to install.
It is a 4-5 step installation and wouldn’t take much time to get installed.
Be sure not install it as a Windows Sevice (default option).


After installation, fire-up your favorite browser. Type in http://localhost
or http://127.0.0.1 (both refer top the local server) in the
address bar and press enter. You’ll see a welcome page stating that your
installation was successful. If it doesn’t show up, go to Start->Programs->Apache
HTTP Sever->Control Apache Sever->START
.


Installing PHP


Installation of PHP is too, straightforward but to integrate and configure
it to work with Apache is a bit cumbersome for a Windows user. Windows users
are more accustomed to GUI for configuring software whereas Apache and PHP like
most other Linux (UNIX) software requires manual configuration through configuration
files which needs to be edited.


PHP comes as a zip file as well as a MSI installation package. I don’t
know how it is to install PHP through MSI package; I myself did it with the
zip one. For the zip file you just have to unzip the package in a root folder
like C:/PHP, D:/PHP. It is recommended to unzip the package in a root folder
and not inside folders like C:/Program Files/PHP, which are deep inside root.


[Update: You may download the MSI
package
of PHP instead of the ZIP one which would do all the set-up and
configuration itself and you would have to do otherwise]


That’s it for this post, in the nest post I’m going to outline
the method of configuring Apache and PHP.


[Update: Read the next article Configuring
Apache Web Server and PHP
]


Related Articles:


Installing Web Server on your PC

Why install web server on your own PC?


Running C++ programs or finding error (syntax) is as easy as clicking on the
Compile button but that is not the case with PHP. You have to first upload the
script file to the server to run it or to find any errors. As with anybody,
peoples make lots of syntax mistakes in the beginning when they learn a new
language. So sending back and forth scripts from your PC to the server every
time you forget a ‘;’ here or a ‘,’ there via FTP is
not something many would like to do.


If you have a server set-up on your own PC then it’s as simple as copy
and paste, there would be no need of FTP or anything of that sort. Write the
script, put it in the special folder (created by the web server) and there you
are.


This is by far the main reason that makes people install web servers on their
home PC.


What does installing a web server do?


Nothing, in the sense that your PC runs the way it used to.


It installs various programs and services which listens to HTTP (and other
web requests) and serves content. It creates a special folder ‘htdocs’
whose contents are made public (or accessible) via the server. When you request
the local IP address http://127.0.0.1 or http://localhost,
the contents of that folder is served.


For example, my PC has Apache server installed at D:\Program Files\
and has the htdocs folder at D:\Program Files\Apache Software Foundation\Apache2.2\htdocs
so when I type the address 127.0.0.1, I see the contents of
the htdocs folder via the Apache server.


One thing to note here is that requesting that folder from HTTP request and
via My Computer (or anything) is totally different. When you request the IP
address of the local server, you are served content as per the configuration
of the server, it makes sure what and how contents are to be served.


Which web server should I Install?


There are many web servers available but the BEST and of course the most popular
(besides being free) is Apache server. So you shouldn’t give a second
thought to other servers.


Where to get Apache and PHP from?


You may download latest versions of Apache from Apache
Download
and PHP from PHP
Download
or you may use the direct download links for Apache
and PHP.


In the next post we’ll see how to install and configure Apache and PHP,
till then good bye to all and thanks for reading.


Related Articles:


Run BASIC Tour Video Posted

One big challenge we face in explaining Run BASIC to people is that people have a preconceived notion of what an easy programming system is. Ruby on Rails is supposed to be easy, right? Easy compared to what? Java server pages? Apache and Perl? For a certain class of problems we need the kind of easy that BASIC (and I don't mean Visual Basic) has always provided.

So, today I created a 20 minute video that walks through installation, startup, and gives a tour of the features of the Run BASIC programming software and several examples. This includes creation and hosting of a simple app. Visit the Run BASIC site and check out the video and I'm sure you'll agree that you've never seen anything easier.

Learn Web Programming

What does it mean to learn web programming (or web development) anyways? Most of the time it means that you need to struggle learning a programming language, a web server, and a bunch of frameworks. When I tell people that they can learn web programming with Run BASIC their automatic response (unless they're complete newbies) is, why not learn one of the popular web programming systems? Why learn Run BASIC?

I'll tell you why. Because you don't need a thick book with Run BASIC. Because you don't need to install a whole bunch of stuff. Because you can create a program and put it on the Internet in the blink of an eye with Run BASIC. Because you can think about programming instead of about trying to satisfy all the requirements of Apache+Perl, or PHP, or Ruby, or whatever.

http://www.runbasic.com/

Check out this stream