Apache httpd.conf using defined variable in if statement - apache

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>

Related

Set Apache directive based on external environment variable with default

I would like to do something like this with my Apache configuration:
<VirtualHost *:443>
DocumentRoot /var/www
TimeOut ${APACHE_TIME_OUT}
...
</VirtualHost>
where APACHE_TIME_OUT is an environment variable set in the OS before Apache starts. However, I would like Apache to use a suitable default if the environment variable APACHE_TIME_OUT is not defined.
I tried this without success:
<If "-n env('APACHE_TIME_OUT')" >
TimeOut ${APACHE_TIME_OUT}
</If>
The above generated the complaint "Config variable ${APACHE_TIME_OUT} is not defined".
This could be solved by always being sure that APACHE_TIME_OUT is defined before starting Apache, but I want to handle the case where APACHE_TIME_OUT might not be defined.

Can you use Apache's <If> to conditionally set the Document Root (without using mod_rewrite)?

I need to set Apache's DocumentRoot directive inside a <VirtualHost> according to whether a particular remote IP address is calling it. I could use mod_rewrite for this but once I've "made the switch" I subsequently have a very complicated .htaccess file in the two destination folders themselves. I figure I need to do this inside httpd.conf. Is there a way to conditionally switch the entire Virtual Host directory based on my IP?
I am close with the following code, but it doesn't change based on my IP.
<VirtualHost _default_:443>
<If "-R '1.2.3.4'">
Define mydocumentroot "/var/www/just_for_me"
</If>
<Else>
Define mydocumentroot "/var/www/everyone_else"
</Else>
DocumentRoot ${mydocumentroot}
</VirtualHost>
The DocumentRoot variable correctly gets set to the mydocumentroot variable, but this always evaluates to the <Else> clause, no matter what I try.
Thanks!
After endless experiments and research it seems that doing any kind of switching in httpd.conf or .htaccess is slow and unnecessarily taxing for the server. I ended up working it out using a combination of mod_rewrite and soft links.

How to enable mod_info in Apache?

I've gone through the Apache guide to enable to mod_info.
As per doc:
To configure mod_info, add the following to your httpd.conf file.
<Location /server-info>
SetHandler server-info
</Location>
You may wish to use mod_access inside the <Location> directive to limit access to your server configuration information:
<Location /server-info>
SetHandler server-info
Order deny,allow
Deny from all
Allow from yourcompany.com
</Location>
Once configured, the server information is obtained by accessing
http://your.host.dom/server-info
In my case this link is not giving any info. Is there anything I need to install as mod_info.c or something? Is there anything I need to put as AddModule or something?
There should be a mod_info.so that must be on a path Apache 2 can find. For example, I have:
kdp#darwin ccl $ locate mod_info.so
/usr/lib64/apache2/modules/mod_info.so
Then, I have these in my httpd.conf:
ServerRoot "/usr/lib64/apache2"
LoadModule info_module modules/mod_info.so
This is made available by a snippet in /etc/apache2/modules.d/00_mod_info.conf:
<IfDefine INFO>
# Allow remote server configuration reports, with the URL of
# http://servername/server-info
<Location /server-info>
SetHandler server-info
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
</IfDefine>
(The IfDefine is only needed because of the way stuff is set up on Gentoo.)
Check if info_module is loaded.
% /usr/local/sbin/httpd -t -D DUMP_MODULES | grep info_module
If it is not loaded, add following line to httpd.conf. Note that path/to must be replaced with appropriate path.
LoadModule info_module path/to/mod_info.so
I got the solution.
When I check ./apachectl -l, mod_info has not been installed.
To install the Mod_info or any module, First Compile and install Apache by ./configure --enable-info make make install
More info for install module : http://publib.boulder.ibm.com/httpserv/manual60/install.html
Check ./apachectl -l, mod_info would be there in the list
Enable server-info by updating httpd.conf
invoke url http://your.host.dom/server-info
One thing I am not sure why apache mod_info not installed default installation.

suPHP setting user/group to virtual host and using docroot

I am trying to get suPHP working, i followed tutorials and seem to have it installed correctly.
now my issue is this
1) i set docroot in the suphp.conf file, but its set to /var/www by default. The problem I have with this is that my domains are stored under another directory as well. So for instance i have my subdomains in one directory and my regular domains in another. So I get an error thrown to my browser telling me that a domain is resolving to a directory not in the configuration.
2) I would like to know how to use suPHP_UserGroup to set the user/group of a specific domain set under my VirtualHost configuration.
This is an example VirtualHost that I have to better show my issues.
<VirtualHost *:80>
ServerAdmin admin#domain.com
ServerAlias mail.domain.com
DocumentRoot /web/users/domain.com/subdomains/mail/webmail
<IfModule mod_suphp.c>
suPHP_UserGroup mail mail
</IfModule>
<Directory "/web/users/domain.com/subdomains/mail/webmail">
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>
When I reload apache I get an error:
Invalid command 'suPHP_UserGroup', perhaps misspelled or defined by a module not included in the server configuration
I configured suPHP with this:
./configure --prefix=/usr --sysconfdir=/etc --with-apr=/usr/bin/apr-1-config --with-apxs=/usr/sbin/apxs --with-apache-user=apache --with-setid-mode=owner --with-php=/usr/bin/php-cgi --with-logfile=/var/log/httpd/suphp_log --enable-SUPHP_USE_USERGROUP=yes
http://www.suphp.org/DocumentationView.html?file=apache/CONFIG
suPHP_UserGroup (expects user- and groupname)
Only supported when compiled with setid-mode "force" or "paranoid" *
Specify the user- and groupname to run PHP-scripts with. This setting
can only be used within a or context.
Example: suPHP_UserGroup foouser bargroup
./configure --prefix=/usr --sysconfdir=/etc --with-apr=/usr/bin/apr-1-config --with-apxs=/usr/sbin/apxs --with-apache-user=apache --with-setid-mode=paranoid --with-php=/usr/bin/php-cgi --with-logfile=/var/log/httpd/suphp_log --enable-SUPHP_USE_USERGROUP=yes
I add the following four lines to my vhost definitions. Line two is relevant to your question.
suPHP_Engine on
suPHP_UserGroup vhost_owner vhost_group
suPHP_AddHandler x-httpd-php
AddHandler x-httpd-php .php .php3 .php4 .php5

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

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'