简体   繁体   中英

Environment variables in Ubuntu Tomcat, Apache and Java

I have a java web app running in Windows enviroment and I want to move it to Ubuntu system. In windows I use an environment variable to store the main path:

MAIN_PATH=C:\test

This variable is used in many configuration files and also by application, for example:

  • Apache and Tomcat configuration file
  • Application logging configuration (log4j)
  • Java application itself by System.getEnv(...)

examples:

tomcat config

 <Host name="localhost"  appBase="${MAIN_PATH}/webapps" unpackWARs="true" autoDeploy="true">

apache virtualhost

<VirtualHost *:80>
    DocumentRoot "${MAIN_PATH}/www"
    ServerName testmain
    <Directory "${MAIN_PATH}/www">
...

I can not find some way to set it in ubuntu, I tried this places:

/etc/enviroment
/etc/profile

export MAIN_PATH=/opt/test in /etc/init.d/tomcat7.sh

All this solution didn't work or works just in one of my requirements (only in server configuration but not in application or vice versa).

Is nothing like windows global environment variable to use for all this cases?

Did you check ubuntu/linux documentation? It's fairly simple.

Refer to: https://help.ubuntu.com/community/EnvironmentVariables

This worked perfectly for me.

  • $ nano /etc/environment (or whatever file editor you want to use)
  • add 'export MAIN_PATH=/opt/test' to the file and save
  • $ source /etc/environment
  • $ echo $MAIN_PATH
  • Output: /opt/test

I would recommend you keep the environment variables separate for each layer of your application. For example it might seem like putting everything in one place makes sense, but I don't feel it's good design to share a configuration directory for Apache and Tomcat. Both have standard locations for config files (eg, $CATALINA_HOME/conf ).

I wouldn't think you're using log4j on Apache so why put it in a custom directory that's shared with Apache. Put it in a conf folder inside your webapp, or in the previously mentioned top level conf directory. Maybe you'll have more than one webapp on Tomcat -- in that case it makes even more sense to have logging config files within the particular webapp directory.

It's also cleaner to have the environment variables contained within the process. They don't gum up the global environment variables. For example when you start Tomcat, the setenv.sh script runs and sets all the enviroment variables just for the java process that's running Tomcat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM