简体   繁体   English

Ubuntu Tomcat,Apache和Java中的环境变量

[英]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. 我有一个在Windows环境中运行的Java Web应用程序,我想将其移至Ubuntu系统。 In windows I use an environment variable to store the main path: 在Windows中,我使用环境变量存储主路径:

MAIN_PATH=C:\test

This variable is used in many configuration files and also by application, for example: 此变量在许多配置文件和应用程序中使用,例如:

  • Apache and Tomcat configuration file Apache和Tomcat配置文件
  • Application logging configuration (log4j) 应用程序日志记录配置(log4j)
  • Java application itself by System.getEnv(...) Java应用程序本身由System.getEnv(...)

examples: 例子:

tomcat config Tomcat配置

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

apache virtualhost apache虚拟主机

<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: 我找不到在ubuntu中进行设置的方法,我尝试了以下方法:

/etc/enviroment
/etc/profile

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

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? 有没有像Windows全局环境变量可用于所有这些情况的?

Did you check ubuntu/linux documentation? 您是否检查过ubuntu / linux文档? It's fairly simple. 这很简单。

Refer to: https://help.ubuntu.com/community/EnvironmentVariables 请参阅: https//help.ubuntu.com/community/EnvironmentVariables

This worked perfectly for me. 这对我来说非常有效。

  • $ nano /etc/environment (or whatever file editor you want to use) $ nano / etc / environment(或您要使用的任何文件编辑器)
  • add 'export MAIN_PATH=/opt/test' to the file and save 在文件中添加“ export MAIN_PATH = / opt / test”并保存
  • $ source /etc/environment $ source / etc / environment
  • $ echo $MAIN_PATH $ echo $ MAIN_PATH
  • Output: /opt/test 输出:/ 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. 例如,将所有内容放在一个地方似乎很有意义,但我认为共享Apache和Tomcat的配置目录并不是一个好设计。 Both have standard locations for config files (eg, $CATALINA_HOME/conf ). 两者都有配置文件的标准位置(例如$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. 我不认为您在Apache上使用log4j,为什么还要将其放在与Apache共享的自定义目录中。 Put it in a conf folder inside your webapp, or in the previously mentioned top level conf directory. 将其放在您的Web应用程序内的conf文件夹中,或放在前面提到的顶级conf目录中。 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. 也许您将在Tomcat上拥有多个web应用程序-在这种情况下,在特定的webapp目录中记录配置文件更为有意义。

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. 例如,当您启动Tomcat时,setenv.sh脚本将运行并仅针对运行Tomcat的Java进程设置所有环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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