You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Philipp Kraus <ph...@tu-clausthal.de> on 2013/11/30 20:29:39 UTC

install war with xml and define environment values

Hello,

I have got a war file and I would like to install it with a XML file under Catalina/localhost. The content is

<Context docBase="/usr/share/myWar/mywar-0.0.1.war" privileged="true" allowLinking="true" crossContext="true">
    <Environment name="HOME" value="/home/mywar/" type="java.lang.String"/>
</Context>

The war uses always ${user.home}, how can I change this in mywar.xml ?
At next I would like to get access of this servlet with https://myserver/mywar, at the moment I
must use https://myserver/mywar-0.0.1 how can I change the URL?

Thanks a lot

Phil

Re: install war with xml and define environment values

Posted by Daniel Mikusa <dm...@gopivotal.com>.
On Nov 30, 2013, at 2:29 PM, Philipp Kraus <ph...@tu-clausthal.de> wrote:

> Hello,
> 
> I have got a war file and I would like to install it with a XML file under Catalina/localhost. The content is
> 
> <Context docBase="/usr/share/myWar/mywar-0.0.1.war" privileged="true" allowLinking="true" crossContext="true">
>    <Environment name="HOME" value="/home/mywar/" type="java.lang.String"/>
> </Context>
> 
> The war uses always ${user.home}, how can I change this in mywar.xml ?

What exactly do you mean?  How is the app using this?  

Setting an <Environment /> tag here is just going to expose the value through JNDI.  Your application will need to be looking for the value through JNDI, to pick this up.  

> At next I would like to get access of this servlet with https://myserver/mywar, at the moment I
> must use https://myserver/mywar-0.0.1 how can I change the URL?

The name of the context path is determined by the name of your WAR file or XML deployment descriptor.  So rename the appropriate file (war file or xml file).  

Based on your example above, if you have the file "conf/Catalina/localhost/mywar.xml", you should have the application available at https://<server>:<port>/mywar.

Dan

> 
> Thanks a lot
> 
> Phil


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: install war with xml and define environment values

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Mark,

On 12/2/13, 11:23 AM, Mark Eggers wrote:
> On 12/1/2013 10:49 PM, Philipp Kraus wrote:
>> Hello,
>> 
>> Am 01.12.2013 um 23:40 schrieb Mark Eggers
>> <it...@yahoo.com>:
>> 
>>> Run Tomcat as an unprivileged user.
>>> 
>>> If you need to have Tomcat serve on port 80, use jsvc, iptables
>>> to map port 80 to port 8080, or place an Apache HTTPD server in
>>> front of Tomcat using mod_proxy_http, mod_proxy_ajp, or
>>> mod_jk.
>>> 
>>> You could also unpack the WAR file, change the param value,
>>> and repackage the WAR file. Of course, the user Tomcat is
>>> running under will need to have privileges to the directory you
>>> change the param value to.
>> 
>> On my test system Tomcat 7 runs with root access, but on my 
>> production system it runs with an unprivileged user on port 9090
>> and a Nginx works like a proxy for https. This works fine, but on
>> the Tomcat runs Jenkins and a project planning system. My
>> Jenkins installation is configures by a XML file in the
>> /etc/tomcat7 directory with this content:
>> 
>> <Context docBase="/usr/share/jenkins/jenkins.war"
>> privileged="true" allowLinking="true" crossContext="true"
>> autoDeploy="true" > <Environment name="JENKINS_HOME"
>> value="/home/jenkins/" type="java.lang.String"/> </Context>
>> 
>> With the value JENKINS_HOME I can change the data directory of 
>> Jenkins.
>> 
>> The project planning system uses only ${user.home}, so I would
>> like to redefine this environment value for this war only
>> (because the backup system runs over the /home dir). I'm working
>> the first time with tomcat but not with java.
>> 
>> Thanks Phil
> 
> I don't know about your project planning system, but my
> installation of Jenkins doesn't need crossContext, privileged, or
> allowLinking. I'm not sure what autoDeploy accomplishes as a
> Context attribute (nothing?).

It produces a lovely warning message.

> Do other people use your machine, or are you the sole user?
> 
> If you're the sole user, set up Tomcat within your home directory,
> take the defaults, drop the WAR files in $CATALINA_BASE/webapps,
> and you're done.
> 
> If not, then set up your system much like your production system.
> That way others can access it on port 80, the unprivileged user
> gets everything backed up (provided the account is in
> /home/unprivileged), and again you can just take the defaults.
> 
> This really is a Java issue and not a Tomcat issue. The application
> is using the environment variable user.home. Setting aside whether
> this is a good idea or not, I think you have a few alternatives (if
> you don't set things up as above).

+1

<Environment> doesn't do what the OP thinks it does. OP wants to
redefine a *system property* when running a particular web
application, but not affect that value for other applications running
in the same JVM. This is simply not possible.

Phillipp, you've mentioned 3 different ways to configure things:

1. Using web.xml <init-param>
2. Using context.xml <Environment>
3. Using ${user.home} system property

It's unclear to me which of these you actually want to use and for
what. You have mentioned both Jenkins (which ought to be configurable
in a number of ways, given the wide range of environments in which it
is probably run) and an otherwise unspecified "project planning system".

Is Jenkins simply a red herring?

If your project planning system (that's the one you want to
reconfigure, right?) reads the system property "user.home", then you
are simply going to have to change it to read something else unless
you want to redefine the value of "user.home" for the entire JVM.

> 1. Unpack the WAR, change the param, repack the WAR, run the
> altered WAR
> 
> This may or may not work, and depends on whether the application
> uses System.getenv("user.home") elsewhere to write to the
> directory.

The most straightforward to code would be to read <init-param> from
web.xml using ServletContext.getInitParameter. But, as you've
mentioned, changing an init-param's value within a WAR is kind of a
PITA. If you use <Environment> in your webapp's META-INF/context.xml
(or CATALINA_BASE/conf/[Engine]/[Host]/[webapp].xml) then that *can*
be easier to configure (if you put your file in conf/ as shown above)
but then the code for fetching values from JNDI is semi-awkward.

> 2. Set the environment variable user.home
> 
> I don't know what other impacts this will have. You can do this by
> the following:
> 
> a. create a file called setenv.sh in $CATALINA_BASE/bin b. in that
> file put something like the following:
> 
> CATALINA_OPTS="-Duser.home=some-location" export CATALINA_OPTS

Phillip could also do something crazy like inventing a *new* system
property that only his application cares about. How about
project.planning.user.home, or whatever makes more sense for you?

CATALINA_OPTS="-Dproject.planning.user.home=some-location"
export CATALINA_OPTS

Now modify your code to read the system property
"project.planning.user.home" and be done with it. There's no need to
try to figure out how to make "user.home" look different to multiple
webapps.

This is a fairly clear case of holding a nail and looking everywhere
for a hammer when you clearly need to decide that a screw is more
appropriate and use the screwdriver you already have in your other hand.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.15 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJSnQkPAAoJEBzwKT+lPKRYspkP/iF4gPfTmj+bQL1knPx/a0FX
/v460Tna/FVIqfADkMdrJqPidWtoPNwK4rlnf36e3qxEJ34OG4zj/JJeGTXVDEsv
AsQSzg+Csuc1K9XycCQT8rs2DtCAuuhtVhtCCjBV/LjZi6SetVdeZFNKmX3u6OyO
HyMbCt1gsVfNPSdpnyTZL8fAZwDzg7qLPjPSShJ/gSJ0FXUD7aVXcWuBQXPZCoO+
Chy9n7fEmw8HZ4zqVKlyhmkuywiDHLsDwRZNnqiJALR2f1kUrmkYcu/CcOqzcGsV
ee/sCMO4StdNBbFiDCNPU6oPFB+MnLfC94rRcqFWkttOJruulW/CkVm528weDFbf
p1hxXO0qDk0eWRsJb+4bDlPCgpA4Gel0h3tDxMXLyTc4NEoNt24tBPlPCbj/80Af
AKTyM9LeiV710NIUxeEXso6MLrwhf8oEzpMj8L6GAlFNMTPbV4+MyG+tvMrqo/gR
/NEe1Qr0OTmjp5E5S/o2kTEu8hlyyUGZ0nIzn1Fnb139LYOELkeRlvDq3bVLBeGh
PEvXJObrSdd3kzzR2op1beqIYmw97id6T7xGnr5Og/wUueUnWY6kbSx7wtPOsrkR
yC0loVVrKYiZoMVQsyh9ddEsdok7nNli8uXvOhX/bgae35WNonAFf2rlBc+oz66y
L4HN+rcZo9KDLsxBi/Zp
=0LRF
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: install war with xml and define environment values

Posted by Mark Eggers <it...@yahoo.com>.
On 12/1/2013 10:49 PM, Philipp Kraus wrote:
> Hello,
>
> Am 01.12.2013 um 23:40 schrieb Mark Eggers <it...@yahoo.com>:
>
>> Run Tomcat as an unprivileged user.
>>
>> If you need to have Tomcat serve on port 80, use jsvc, iptables to
>> map port 80 to port 8080, or place an Apache HTTPD server in front
>> of Tomcat using mod_proxy_http, mod_proxy_ajp, or mod_jk.
>>
>> You could also unpack the WAR file, change the param value, and
>> repackage the WAR file. Of course, the user Tomcat is running under
>> will need to have privileges to the directory you change the param
>> value to.
>
> On my test system Tomcat 7 runs with root access, but on my
> production system it runs with an unprivileged user on port 9090 and
> a Nginx works like a proxy for https. This works fine, but on the
> Tomcat runs Jenkins and a project planning system. My Jenkins
> installation is configures by a XML file in the /etc/tomcat7
> directory with this content:
>
> <Context docBase="/usr/share/jenkins/jenkins.war" privileged="true"
> allowLinking="true" crossContext="true" autoDeploy="true" >
> <Environment name="JENKINS_HOME" value="/home/jenkins/"
> type="java.lang.String"/> </Context>
>
> With the value JENKINS_HOME I can change the data directory of
> Jenkins.
>
> The project planning system uses only ${user.home}, so I would like
> to redefine this environment value for this war only (because the
> backup system runs over the /home dir). I'm working the first time
> with tomcat but not with java.
>
> Thanks Phil

I don't know about your project planning system, but my installation of 
Jenkins doesn't need crossContext, privileged, or allowLinking. I'm not 
sure what autoDeploy accomplishes as a Context attribute (nothing?).

Do other people use your machine, or are you the sole user?

If you're the sole user, set up Tomcat within your home directory, take 
the defaults, drop the WAR files in $CATALINA_BASE/webapps, and you're done.

If not, then set up your system much like your production system. That 
way others can access it on port 80, the unprivileged user gets 
everything backed up (provided the account is in /home/unprivileged), 
and again you can just take the defaults.

This really is a Java issue and not a Tomcat issue. The application is 
using the environment variable user.home. Setting aside whether this is 
a good idea or not, I think you have a few alternatives (if you don't 
set things up as above).

1. Unpack the WAR, change the param, repack the WAR, run the altered WAR

This may or may not work, and depends on whether the application uses 
System.getenv("user.home") elsewhere to write to the directory.

2. Set the environment variable user.home

I don't know what other impacts this will have. You can do this by the 
following:

a. create a file called setenv.sh in $CATALINA_BASE/bin
b. in that file put something like the following:

CATALINA_OPTS="-Duser.home=some-location"
export CATALINA_OPTS

This should set user.home for Tomcat, but leave it set to the account's 
default for all other processes. You may actually have to set JAVA_OPTS 
rather than CATALINA_OPTS . . . depends on how user.home is used.

$CATALINA_BASE is where you're running Tomcat from. $CATALINA_HOME is 
where Tomcat is installed (roughly). Read RUNNING.txt for a more 
complete explanation and usage.

Since it looks like you're using an OS-repackaged Tomcat, I have no idea 
where the above directories are, or where you should place setenv.sh.

As most people on this mailing list will recommend, I recommend the 
following.

1. create an unprivileged account
2. unpack the tar.gz file for the desired Tomcat in this account
    a. get the file from tomcat.apache.org
    b. I use RUNNING.txt to run multiple Tomcats, you may not
3. use the defaults
    a. start out with running WAR files in $CATALINA_HOME/webapps
    b. don't add crossContext, privileged, linking unless needed

If you need the Tomcat instance to start up at boot time, either write a 
script, or run it using jsvc. The nice thing about running it under jsvc 
is that you can then run Tomcat on a privileged port in Linux / UNIX. 
Other ways to get Tomcat to serve content on port 80 include fronting it 
with a web server or using iptables.

. . . just my two cents
/mde/

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: install war with xml and define environment values

Posted by Philipp Kraus <ph...@tu-clausthal.de>.
Hello,

Am 01.12.2013 um 23:40 schrieb Mark Eggers <it...@yahoo.com>:

> Run Tomcat as an unprivileged user.
> 
> If you need to have Tomcat serve on port 80, use jsvc, iptables to map port 80 to port 8080, or place an Apache HTTPD server in front of Tomcat using mod_proxy_http, mod_proxy_ajp, or mod_jk.
> 
> You could also unpack the WAR file, change the param value, and repackage the WAR file. Of course, the user Tomcat is running under will need to have privileges to the directory you change the param value to.

On my test system Tomcat 7 runs with root access, but on my production system it runs with an unprivileged user on port 9090 and a Nginx works like a proxy for https. This works fine, but
on the Tomcat runs Jenkins and a project planning system. 
My Jenkins installation is configures by a XML file in the /etc/tomcat7 directory with this content:

<Context docBase="/usr/share/jenkins/jenkins.war" privileged="true" allowLinking="true" crossContext="true" autoDeploy="true" >
    <Environment name="JENKINS_HOME" value="/home/jenkins/" type="java.lang.String"/>
</Context>

With the value JENKINS_HOME I can change the data directory of Jenkins.

The project planning system uses only ${user.home}, so I would like to redefine this environment value for this war only (because the backup system runs over the /home dir). 
I'm working the first time with tomcat but not with java.

Thanks Phil
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: install war with xml and define environment values

Posted by Mark Eggers <it...@yahoo.com>.
On 12/1/2013 1:55 PM, Philipp Kraus wrote:
> Hello,
> I have got a precomiled war file and in the web.xml exists some
> values e.g.:
>
> <init-param> <param-name>database.location</param-name>
> <param-value>${user.home}/sysdata</param-value> </init-param>
>
> The Tomcat runs under root, so it creates under /root/sysdata the
> data files, but I would like to change the directory, the data should
> be stored in /home/mywar/sysdata
>
> Phil
>
>

Run Tomcat as an unprivileged user.

If you need to have Tomcat serve on port 80, use jsvc, iptables to map 
port 80 to port 8080, or place an Apache HTTPD server in front of Tomcat 
using mod_proxy_http, mod_proxy_ajp, or mod_jk.

You could also unpack the WAR file, change the param value, and 
repackage the WAR file. Of course, the user Tomcat is running under will 
need to have privileges to the directory you change the param value to.

. . . just my two cents.
/mde/

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: install war with xml and define environment values

Posted by Philipp Kraus <ph...@tu-clausthal.de>.
Hello,

Am 01.12.2013 um 15:31 schrieb Martin Gainty <mg...@hotmail.com>:
> MG> I was going to suggest using ln but since your docBase is based on the value of base folder ${user.home}
> MG>any change to user.home will automatically rebase your war after context redeploy


I have got a precomiled war file and in the web.xml exists some values e.g.:

 <init-param>
<param-name>database.location</param-name>
<param-value>${user.home}/sysdata</param-value>
</init-param>

The Tomcat runs under root, so it creates under /root/sysdata the data files, but I would like to change the directory, the data should be stored in
/home/mywar/sysdata

Phil