Setting up Tomcat to process JSP's in DocumentRoot - apache

I've got Apache running on my server, and some VHosts set up for myself through this; however, the apache documentRoot, nor any VHost's documentRoot appear to be calling tomcat to process any JSP files - in order to run a JSP, I need to upload it as a war archive, and access it via domain:8080.
Would anyone be able to point me in the right direction on getting tomcat set up so that it can process JSP files as normal?

If I understand you correctly, you want to have JSP deployed outside of a war/webapps. Unfortunately, Tomcat does not work that way. You have to deploy your JSP as a WAR/folder-in-webapps. Unlike PHP or some such language plugin, Tomcat hosts Java apps. It does not process JSP files.
To get pass through, have you tried the mod_jk or as it is known now, the connector? http://tomcat.apache.org/connectors-doc/miscellaneous/faq.html

Answering my own question on this, as neither solution offered what I was looking for.
Firstly, I downloaded and compiled the apache mod jk_serv; following this, I configured a worker, under workers.properties with the following;
workers.tomcat_home=/opt/apache-tomcat-7.0.50
workers.java_home=/opt/jdk1.7.0_51/
ps=/
worker.list=ajp13_worker
worker.ajp13_worker.port=8009
worker.ajp13_worker.host=localhost
worker.ajp13_worker.type=ajp13
worker.ajp13_worker.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=ajp13_worker
Following this, under apache's conf.d, I set up a worker config to be loaded by apache, as follows;
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf/workers.properties
JkLogFile /var/log/httpd/mod_jk_log
JkShmFile /var/log/httpd/jk-runtime-status
JkWatchdogInterval 60
JkLogLevel info
JkMountCopy All
# all the contexts:
JkMount / worker1
JkMount /*.jsp worker1
Finally, I set up a script that would update tomcat's server.xml, to add any relevant vHosts that were added to httpd.conf.
With this, jsp files are now processed on-the-fly; without the need for uploading seperate WAR's.

You simply need to copy your WAR file into the $TOMCAT/webapps/ folder and define a context in server.xml (or context.xml) for it, so it points to the root. in server.xml, contexts are defined inside a <Host>.
I would also advise you to remove the ROOT folder under $TOMCAT/webapps/ if any has been created.
Below an example of context definition:
<Host name="192.168.0.251" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Alias>myhost.mydomain.com</Alias>
<Context docBase="myWarFileName" path="" reloadable="true" source="myWarFileName">
... Add any resource, etc.. for this context here, if any ...
</Context>
</Host>
Note the empty path value, pointing then to the root directory. In some cases I have had to use "/" instead of an empty string.

Related

How to setup glassfish 4.1.1 behind apache on Ubuntu 16.04 server

I want to setup Apache and Glassfish on Ubuntu 16.04 server.
I have installed
apache2
libapache2-mod-jk
glassfish
The following are the steps I have followed
Configuring the MPM module
Set MaxRequestWorkers to 400 in /etc/apache2/mods-available/mpm_event.conf
Configuring the JK Module
<IfModule mod_jk.c>
JkWorkersFile /usr/share/glassfish4/glassfish/domains/<domain-doamin1>/config/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMountCopy all
</IfModule>
JkMount /myapp/* ajp13
<Location "/myapp/WEB-INF/">
require all denied
</Location>
Create a workers.properties file in your GlassFish domain's config directory
worker.list=ajp13
worker.ajp13.type=ajp13
worker.ajp13.host=localhost
worker.ajp13.port=8009
# load balancing only: worker.ajp13.lbfactor=50
connection_pool_size=10
connection_pool_timeout=600
worker.ajp13.socket_keepalive=False
worker.ajp13.socket_timeout=30
Create the JK listener in GlassFish using these commands
asadmin create-http-listener --listenerport 8009 --listeneraddress 0.0.0.0 --defaultvs server jk-listener
asadmin set server-config.network-config.network-listeners.network-listener.jk-listener.jk-enabled=true
then I restarted glassfish domain successfully but when i try to restart apache2 with sudo /etc/init.d/apache2 restart I get the error below
[....] Restarting apache2 (via systemctl): apache2.serviceJob for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.
failed!
This error occurs when I edit the file jk.conf located under /etc/apache2/mods-available/jk.conf
Where am I going wrong. Is there a complete guide to accomplishing this? Finally the newer apache2 doesn't have the file httpd.conf and all the tutorials allover the Internet rely upon this file. Thanks in advance.
Since your objective is just to forward requests from Apache to GlassFish, not to loadbalance requests from Apache to multiple GlassFish servers, I would recommend avoiding mod_jk. You can certainly achieve your goal with it, but if you are new to the concepts involved, you will find it difficult to understand and maintain.
Instead you can use mod_proxy and, optionally, mod_proxy_ajp.
First, a definition:
AJP vs HTTP
AJP is a protocol like HTTP, but binary rather than text based. It has no secure/insecure options like HTTPS/HTTP since it is normally used behind a firewall and performs much better than HTTP for these scenarios. When you mark any GlassFish network listener as jk-enabled, you are enabling AJP communication, rather than HTTP.
You've installed Apache via the ubuntu apache2 package which has its own example structure to configuration which is different to the layout you would get if you downloaded and unzipped it. This has advantages, but we need to understand the Apache configuration file before getting to that.
Apache Configuration
Generally, you will see internet guides refer to httpd.conf as the configuration file to edit. This is just the default "parent" configuration file. In Debian/Ubuntu systems (and their derivations, like Linux Mint), the file to look for is apache2.conf.
This file is read, and its directives applied, from top to bottom, so if you have set the same property to two different values, the second will apply. (More accurately, they will both apply but the first will only apply until the second setting is read).
This file can also specifically "include" files and folders (where any *.conf file in an included folder will be included). These will be read in and merged with the main configuration at the point where the "include" statement is written. So the very last line in the main configuration file (if it is not specifying another file) will be the last line of configuration to be set, no matter what.
Debian config layout
I would highly recommend you read the opening comment in the apache2.conf file, since it will tell you all you need to know about the layout. Suffice it to say that keeping all the config in one file is very painful to maintain. The Debian package separates configuration into three categories:
sites
Sites are single configuration files for a website or web project. This could be anything: PHP, static HTML or a Java EE application deployed to an app server like GlassFish.
mods
Modules are subdivided into *.load files which load the actual libraries needed to run them, and *.conf files which have global configuration for the modules. Note that this configuration applies to every site that uses the module, so it is best to put any site/app specific module configuration in the appropriate site.conf file
conf
These files are just for any other general configuration which fits into a nice group. This could be SSL configuration like keystore and truststore locations.
When you look at the directory structure, you will see that each of these have 2 folders: *-available and *-enabled. This is because the Debian Apache package comes with 6 helper tools, a2ensite and a2dissite; a2enmod and a2dismod; a2enconf and a2disconf. The idea is that you follow these rules:
Never directly edit the apache2.conf file
only ever add or change files in the *-available folders
Use the helper tools to enable or disable sites/modules/conf files.
Answer
So to (finally) answer your question, I would do the following steps:
Enable mod_proxy_ajp
a2enmod mod_proxy_ajp
Create a new myApp.conf in sites-available. You can copy the default one, which is a good example. Assuming you have just want to forward all requests to GlassFish, you can use the default VirtualHost settings of ` which will process a request for any hostname on port 80. Use port 443 if you want to add HTTPS.
Add ProxyPass and ProxyPassReverse directives to the location of your server. If Apache and GlassFish are on the same server, it is likely you will want to use ajp://localhost:8080
ProxyPass / ajp://host_name:0000
ProxyPassReverse / ajp://host_name:0000
Note: This assumes you are using AJP. If that causes you problems, switch to HTTP by changing ajp to http above and disabling the jk-listener in GlassFish.
Once you have completed your myApp.conf configuration, remember to disable the default site:
a2dissite 000-default-site.conf
And enable your new site:
a2ensite myApp.conf
Those commands will appropriately modify the main apache2.conf and create the appropriate links in the sites-enabled folder.
That should be all you need. Now, everything that points to your hostname after the root / of the URL will be forwarded to the root context / of GlassFish.

Where put workers.properties for mod_jk.so Tomcat connector to Apache httpd (OS X)?

I'm trying to set up the Tomcat 7 connector mod_jk.so on OS X (10.8.3) so that calls to Tomcat will go through httpd from apache 2.2. The file mod_jk.so is in place. But where does workers.properties go? The instructions at http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html just don't seem to say.
I know you're now using mod_proxy but I'm answering this for the record, at least to include the info when your *nix packaging framework is involved.
In Debian, the package apache2 installs apache with the configuration directory /etc/apache2/mods-available and the seperate package libapache2-mod-jk places an apache config file in this directory called jk.conf, which gets pulled in by apache at start-up.
jk.conf has the JkWorkersFile directive.
The libapache2-mod-jk package also installs a workers.properties file under /etc/libapache2-mod-jk.
So for OS X, you put your workers.properties file anywhere, e.g. rationally in the same dir as the httpd.conf, and then you have to put the directive either in your httpd.conf file, or that might contain an Include to pull in everything in a directory, e.g. conf.d where you can put custom stuff in your own files that won't get interfered with at package upgrades.
Please put your workers.properties file according to your JkWorkersFile configuration:
JkWorkersFile /etc/httpd/conf/workers.properties
I had the same problem. But later I found that I should first look whether I am using mod_jk.so or mod_proxy_ajp in httpd.conf. I turned out to be tha later one. So instead of editing the worker.properties, which I don't have, I should edit the httpd.conf: Add a ProxyIOBufferSize directive to Apache httpd's configuration.
ProxyIOBufferSize 65536
Hope it helps.
Ref: Here at the bottom of the page.

dynamic configuration of mod_jk

I have configured mod_jk to allow load balance. I want to know if more workers could be dynamically added to workers.properties while apache is running?
No. workers.properties file is configured in some .conf files, to make change to .conf file take effect, you need to restart Apache.
See the thread about mod_jk configuration:
http://milestonenext.blogspot.de/2012/09/ssl-offloading-with-modjk-part-1.html

JSP with Apache/2.2.17 (Ubuntu)

I'm running a Ubuntu Sever with Apache/2.2.17. I can't seem to run JSP I just get the source code in the browser. What am I doing wrong?
Thanks
What am I doing wrong?
Expecting that Apache HTTPD magically supports JSP. It doesn't. You need Apache Tomcat instead.
See also:
Our JSP wiki page - at the bottom you can find several tutorial links to get started properly.
Try this
1)get the mod_jk.so
2)include this in httpd.conf file
LoadModule jk_module modules/mod_jk.so
3)Then in virtual host section of httpd.conf
JkMount /test/*.jsp loadbalancer
put your jsp file in webapp dir in the tomcat directory. Run as localhost:8080/your.jsp from your browser. Also make sure tomcat is set properly by running localhsot:8080 form your browser.

How do I configure Apache to forward some URLs to my servlet container regardless if the file exists

How do I configure Apache to forward a URLa of a certain extension, say *.htm to my Servlet container, in this case, Resin, without first checking for the file's existence.
Currently, if Apache can not find the requested file in the directory structure, it serves a 404, even though my web.xml Servlet mapping would handle the request if forwarded.
I've temporarily resorted to placing an empty file matching the requested file within my web structure (i.e. c:/dir/dir/index.htm) so that Apache forwards the request.
Resin's Apache configuration is a bit different than Tomcat's (below), however the problem seems to exist entirely within Apache since the request never makes it to Resin.
Help would be greatly appreciated.
LoadModule caucho_module c:/resin-pro/win32/apache-2.0/mod_caucho.dll
NameVirtualHost *
<VirtualHost *>
ServerName sub.domain.com
DocumentRoot c:/web
ResinConfigServer sub.domain.com 6802
</VirtualHost>
You need to read How the Plugins Dispatch to Resin and explicitly configure the URLs you want to be handled by Resin to be forwarded to Resin:
mod_caucho discovers its configuration by contacting the ResinConfigServer specified
in the httpd.conf or resin.ini. The ResinConfigServer can be any Resin server. When a
user requests a URL, mod_caucho uses the configuration it has determined from the
ResinConfigServer to determine whether Resin or Apache should handle the request.
That decision is based on the configuration in the ResinConfigServer's resin.conf.
This means if you want certain URLs to be handled by Resin, you need to configure it this way in your resin.conf.