How to define a variable in apache's httpd.conf file? - apache

I want to define a variable in Apache server's httpd.conf configuration file.
Ex: variable static_path = C:\codebase\snp_static
and I want to use this variable (static_path) in httpd.conf where ever required.
Please tell me how can define a variable in httpd.conf file ?

Within httpd.conf, declare your variable(s) with: Define (Preferably at the very first line)
Syntax: Define variable-name variable-value
In this manner:
#The line below creates the variable [static_path]
Define static_path C:/codebase/snp_static
You can later use this variable like so:
ServerRoot = ${static_path}
...
DocumentRoot = ${static_path}
...
<Directory ${static_path}>
...etc.
You can even combine multiple variables:
#Below, I am going to combine variables [server_space] and [static_path]
Define server_space c:/
Define static_path codebase/snp_static
...
ServerRoot = ${server_space}${static_path}
...
DocumentRoot = ${server_space}${static_path}
...
<Directory ${server_space}${static_path}>
...etc.
Documentation: http://httpd.apache.org/docs/2.4/mod/core.html#define

If all you want is simple variable substitution inside httpd.conf, then define an ordinary shell environment variable for the user that runs Apache, then use the ${ENVVAR} syntax to refer to it inside your httpd.conf file, see Apache docs

Apache2.4 I researched it out and here is what worked for me.
and tested using httpd_z.exe -t -D DUMP_RUN_CFG
RESULTS:::
ServerRoot: "C:/path/core/apache2"
Main DocumentRoot: "C:/path/apache/htdocs"
Main ErrorLog: "C:/path/core/apache2/logs/error.log"
Mutex rewrite-map: using_defaults
Mutex default: dir="C:/path/core/apache2/logs/" mechanism=default
PidFile: "C:/path/core/apache2/logs/httpd.pid"
Define: DUMP_RUN_CFG
Define: US_ROOTF=C:/path **THIS IS THE ROOT PATH VARIABLE I JUST MADE**
Define: php54
#<IfDefine TEST>
# Define servername test.example.com
#</IfDefine>
#<IfDefine !TEST>
# Define servername www.example.com
# Define SSL
#</IfDefine>
#DocumentRoot /var/www/${servername}/htdocs
<IfDefine US_ROOTF>
Define US_ROOTF C:/PATH **The path i want the variable for**
</IfDefine>
<IfDefine !US_ROOTF>
Define US_ROOTF C:/PATH **The path i want the variable for**
# Define SSL
</IfDefine>
#DocumentRoot /var/www/${servername}/htdocs OPTIONS ON HOW TO USE
EXAMPLE of use
ServerRoot = ${US_ROOTF}
<IfDefine php54>
LoadFile "${US_ROOTF}/core/php54/icudt53.dll"
PHPIniDir "${US_ROOTF}/core/php54/php_production.ini"
I was told never to use a direct HARD path to anything when serving something to the internet always use variables to help secure your system.
I found the hard way this is so true. Now I finally figured out how to set the variables for all services dealing with Apache i use them.
Hope it helps you too.

Late to the question but recently had this issue and fixed it like so:
DEFINE path "C:\path/to the/directory"
Then later use like so:
DocumentRoot ${path}
<Directory ${path}>
Note: In the path use \ after the drive letter

If your apache project is not taking system's environmental variables which we added to bashrc,
We can directly EXPORT variables to
/etc/apache2/envvars file
example: export ADMIN='Bibin'

Related

Apache httpd.conf using defined variable in if statement

I try to make a universal httpd.conf using an if statement to decide which port is used.
# In HOME the path of the home directory of the apache user is set
PassEnv HOME
Define HTTPServerPath "${HOME}/HTTPServer"
<If "%{HTTPServerPath} -strcmatch '*something*'">
Define ListenPort 1080
</If>
<Else>
Define ListenPort 1180
</Else>
This does not work, as the variable from the Define line seems not available. How can I implement such an universal config file?
I found no solution for that. The workaround is now to start the service with a different parameter.
Start of service:
httpd start -Dsomething
#httpd start -DsomethingElse.
httpd.conf
<IfDefine something>
...
</IfDefine>
<IfDefine somethingElse>
...
</IfDefine>

Apache VirtualHost conditional based on an environment variable

I'm trying to include a config file inside an Apache 2.4 <VirtualHost> based on the presence of an environment variable.
Inside the VirtualHost declaration, I set the VIEWMODE environment variable as such:
Define virtualhost_config "${virtualhost_path}/conf/virtualhost.conf"
<VirtualHost *:80>
SetEnv VIEWMODE demo
Include "${virtualhost_config}"
</VirtualHost>
Inside the included config file, I now have this conditional inside the <Directory> block:
<If "env('VIEWMODE') == 'demo'">
RewriteRule (.*) http://www.apple.com/ [L,R=302]
</If>
However, I can't seem to get this to work. The conditional RewriteRule is ignored.
What am I missing?
See the documentation for SetEnv:
The internal environment variables set by this directive are set after most early request processing directives are run, such as access control and URI-to-filename mapping. If the environment variable you're setting is meant as input into this early phase of processing such as the RewriteRule directive, you should instead set the environment variable with SetEnvIf.
And the note about environment variables within the functions section of the Apache Expresions documentation is also of interest:
Environment variable ordering
When environment variables are looked up within an <If> condition, it's important to consider how extremely early in request processing that this resolution occurs. As a guideline, any directive defined outside of virtual host context (directory, location, htaccess) is not likely to have yet had a chance to execute. SetEnvIf in virtual host scope is one directive that runs prior to this resolution
When reqenv is used outside of <If>, the resolution will generally occur later, but the exact timing depends on the directive the expression has been used within.
So you need to use SetEnvIf because SetEnv is not processed soon enough, and that fixed it for me when I tested here. Something like:
Define virtualhost_config "${virtualhost_path}/conf/virtualhost.conf"
<VirtualHost *:80>
SetEnvIf Request_URI ^ VIEWMODE=demo
Include "${virtualhost_config}"
</VirtualHost>
I solved this by setting an Apache variable, which then fed both the SetEnv directive as well as the <If> block. Unfortunately, the <If> block itself seemed to cause issues with the processing order of directives inside it (e.g. ServerAlias not allowed here), but <IfDefine> did not have this problem (using <IfDefine> only worked for me because VIEWMODE was binary). The final solution looked something like this:
Define environment production
Define viewmode demo
<VirtualHost *:80>
SetEnv ENVIRONMENT ${environment}
<IfDefine viewmode>
SetEnv VIEWMODE ${viewmode}
Include "${virtualhost_path}/conf/demo-configuration.conf"
</IfDefine>
</VirtualHost>
UnDefine environment
UnDefine viewmode
An important caveat is that Apache variables are global, so if the same variables might be used in subsequent Virtual Hosts, make sure to UnDefine them at the end of each config.

Apache Apache/2.4.7 (Ubuntu) ProxyPass

I'm trying to configure a simple ProxyPass using this file (as simple as possible) linked in /etc/apache2/sites-enable form sites-avalable (same file seems to work on apache 2.2 but may be i have done other configuration on previous installation and i canìt remember those configuration)
Note i use an IPADDRESS and not a Server Name but i think this is not the problem.
Note 2: i'm not an apache web server expert :)!
ServerName IP_SERVER_ADDRESS
# Redmine
ProxyPass /redmine http://IP_SERVER_ADDRESS:8555/redmine
# test1
ProxyPass "/test1" "http://IP_SERVER_ADDRESS:9180/test1/"
ProxyPassReverse "/test1" "http://IP_SERVER_ADDRESS:9180/test1/"
# test2
ProxyPass /test2 http://IP_SERVER_ADDRESS:8880/test2
ProxyPassReverse /test2 http://IP_SERVER_ADDRESS:8880/test2
DocumentRoot /var/www
But it didn't work, so the url
http://IP_SERVER_ADDRESS:8880/test2
work fine , but the proxied url
http://IP_SERVER_ADDRESS/test2
did not work , with the error
Not Found
The requested URL /redmine was not found on this server
Output of apache2ctl -S seems strange as apparently no VirtualHost configuration is used (?)
VirtualHost configuration:
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex proxy: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
Quite sure i'm missing some simple thing but i can't find it!
This answer is tested in Apache Apache/2.4.7 (Debian) ProxyPass.
See /etc/apache2/apache2.conf:
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
previous version was
# Include the virtual host configurations:
Include sites-enabled/
So basically in apache 2.4 (Debian and derivated) you must use file with a .conf extension to limit headache....
In Apache 2.2 the .conf extension in file name is not needed.
Other Details:
In sites-enabled configuration files (a soft link to sites-available) MUST be have an extension .conf so WHATEVER_YOU_WANT.conf
You MUST use a2ensite to enable a configuration, because if the file in sites-available did not have the "right" extension ".conf" it say "ERROR: Site WHATEVER_YOU_WANT does not exist!". So for example if you use a file named mysite and try to enable the configuration mysite with the command "a2ensite mysite" you receive an error. If you use a file named myfile.conf same command will work fine... If you manually create a soft link in sites-enabled to a conf file without the "conf" extension , the site will not work!!
You can create a soft link in sites-available with the right extension (.conf) to a file WITHOUT the right extension alredy peresent in sites-available. In such a case the configuration will work but if you use a2dissite to disable the site it will say "ERROR: Site WHATEVER_YOU_WANT does not exist!"
After finding the solution i found some link around
https://www.linode.com/docs/security/upgrading/updating-virtual-host-settings-from-apache-2-2-to-apache-2-4

apache vhost setup doesnt work

First things first .. im pretty new to apache configuration.
I'm trying to configure vhosts locally for an apache
(Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3 according to phpinfo()).
Its set up via XAMPP (xampp-win32-5.6.3-0).
Operating system is Windows 7 Home Premium SP1.
So i already read some articles about setting up vhosts (also some in this forum).
My host file looks as follows:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# ::1 localhost
127.0.0.1 localhost
127.0.0.1 test.local
My C:\xampp56\apache\conf\extra\httpd-vhosts.conf contains the following code:
<VirtualHost *:80>
ServerName test.local
ServerAlias test.local
DocumentRoot "C:/xampp56/htdocs/test.local"
DirectoryIndex app.php
</VirtualHost>
It is loaded in the C:\xampp56\apache\conf\httpd.conf file.
Include conf/extra/httpd-vhosts.conf
The Module mod_log_config.so is also loaded in the httpd.conf
LoadModule log_config_module modules/mod_log_config.so
(After all editing in the config files i saved them and restarted apache.)
When I type test.local (or http://test.local) in FireFox it always says "test.local was not found". When i call up localhost however its working.
I havn't found any answers yet except to check if the vhost and the host file are set up correctly (which seems to be the case here to me).
Sorry for the long post and I am sorry in case it's just some stupid mistake i made here.
Thx for any help!
EDIT:
Here is also the output from the httpd -S command:
VirtualHost configuration:
*:80 test.local (C:/xampp56/apache/conf/extra/httpd-vhosts.conf:44)
*:443 www.example.com (C:/xampp56/apache/conf/extra/httpd-ssl.conf:80)
ServerRoot: "C:/xampp56/apache"
Main DocumentRoot: "C:/xampp56/htdocs"
Main ErrorLog: "C:/xampp56/apache/logs/error.log"
Mutex ssl-cache: using_defaults
Mutex default: dir="C:/xampp56/apache/logs/" mechanism=default
Mutex rewrite-map: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
PidFile: "C:/xampp56/apache/logs/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
I found the solution.
It was Avira blocking access to my host file .. disabled that option in avira and vhosts work fine now.
Beside that i needed admin rights which were not set for the account i was working with ..

Apache ignore missing DocumentRoot

I would like to configure my apache different. Now, if one of DocumentRoots is missing, all server fail to start. That's annoying..
Is there any option, how to ignore this error?
Based on the assumption that your DocumentRoot directive is used synonomously with a VirtualHost, you could separate the VirtualHosts configuration into separate conf files, place these individually into their document roots and then include them with a directory wide include directive in a generic, serverwide conf. See below for an example:
Create a conf in the directory below:
/etc/apache/sites-enabled/sites.conf
add the following
Include /data/www/sites/
This will include any conf file in the above directory and any subdirectory
For a host with the following DocumentRoot
/data/www/sites/website-one
Create a VirtualHost conf file and place it in the above directory e.g:
/data/www/sites/website-one/website-one.conf
And your conf file will contain all the information about the VirtualHost including the DocumentRoot e.g
<VirtualHost *:80>
DocumentRoot /data/www/sites/website-one
ServerName www.website-one.com
# Other directives here
</VirtualHost>
If the DocumentRoot doesn't exist, then the conf file doesn't get loaded and the DocumentRoot directive never executed. For security reasons be careful that your conf files don't become dereferenceable. This should probably be disabled at a server level, but it is something to be aware of. Finally, the above pattern means that any bad conf files in the /data/www/sites directory and subdirectories will cause the server not to start.