Set PATH for Apache on per-directory basis? - apache

How to set PATH for Apache server on a per-directory basis?
Problem: I have a system in which I can not change the
system installed python in /usr/bin/.
I have a bunch of python cgi scripts that all have
#!/usr/bin/env python
as their first line. I strongly prefer not to change
these scripts for logistical reasons.
But these scripts require a different version of Python
than the system installed one. I have the right version
installed in /opt/python/bin/. So if I could put this
directory at the front of PATH, but only for this particular
cgi directory, it would solve my problem.
That is, if the following would work, it would do what I want.
ScriptAlias /mydir/cgi/ /home/me/devel/cgi/
<Directory /home/me/devel/cgi>
SetEnv PATH /opt/python/bin:/usr/bin:/bin
...other stuff...
</Directory>
But it seems SetEnv does not work on PATH (at least on Apache 2.2).
Is there some other way to changing PATH for just one
particular directory in Apache?
(P.S., I also tried SetEnvIf without any joy.)

Related

How do I get apache to follow symlinks to windows drive on WSL

I'm trying to make apache run on Linux Subsystem for Windows.
So I just installed apache and changed DocumentRoot from "/var/www/html" to "/var/www" in /etc/apache2/sites-available/000-default.conf.
This has exactly the effect I wanted it to have, namely to list all folders inside when browsing to "localhost".
My projects are located on my Windows drive in "C:/Projekte", so I created a symlink like so:
sudo ln -s /mnt/c/Projekte /var/www/projects
Now my problem is, when I browse to localhost and click on projects it runs into a loop trying to call an index.html, so the url before it breaks looks like this:
http://localhost/projects/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/
I would want to see the directory listing of my folders instead.
I know about the Options Indexes and FollowSymLinks which are both set, but there seems to be something else I have to set for symlinks to work as I expect.
If I create a folder instead in "/var/www" it works fine.
Does anybody know how to set this up correctly?
By accident I found out, that my issue here is related to this issue on BashOnWindows. Problem is that I used tab to autocomplete the path which automatically added a trailing slash to the end of the path. So the actual command I used to create the symlink was
sudo ln -s /mnt/c/Projekte/ /var/www/projects
Creating the symlink without trailing slash, just like in my question it works just fine.

Loading ImageMagick from Apache (XAMPP)

I wrote an image manipulating program with PHP/imagick. The server is installed under XAMPP, which resides in a flash drive.
Now everything works fine except that ImageMagick needs to be installed on every machine I use.
To solve the problem and make it easier, I copied the ImageMagick folder to the flash drive, removed ImageMagick from Windows Environment Variables, and modified Apache config (tried both httpd.conf and extra/httpd-xampp.conf).
httpd.conf
SetEnv MAGICK_HOME "/xmapp/ImageMagick"
extra/httpd-xampp.conf
<IfModule env_module> <----- enabled
SetEnv MIBDIRs "xampp/php/extras/mibs"
SetEnv MAGICK_HOME "/xampp/ImageMagick" <-----
SetEnv MySQL_HOME "/xmapp/mysql/bin"
However, when I started Apache, I got a error message saying "Cannot find CORE_RL_wand_.dll". I'm pretty sure the path is correct and no files are missing. It seems that Apache just can't load ImageMagick.
Here are the versions I'm using:
XAMPP 1.8.2 (Apache 2.4.4 / PHP 5.4.19)
ImageMagick-6.8.7-1-Q16-x86-dll
php_imagick_ts.dll for PHP 5.4 (from Mikko)
Does anyone have any experience with portable ImageMagick/XAMPP?
Is this the correct way to load ImageMagick from Apache?
Try to add to the system path the Imagick's path. Maybe you also have to add as environment variable the MAGICK_HOME. That solved my problem.

Deploying Trac 1.0 with Apache - 'chrome' and 'site' folders MIA

I just installed a copy of Trac 1.0 using easy_install-2.6, but am having trouble deciphering the "Deploy Trac" section of the Install documentation.
After issuing easy_install-2.6 Trac=1.0, I then created my environment as so:
trac-admin /www/virtualhosts/trac initenv
Next, I set permissions:
chown -R apache.apache /www/virtualhosts/trac/
My /etc/httpd/conf.d/trac.conf file looks like this:
<Location /trac>
SetHandler mod_python
PythonHandler trac.web.modpython_frontend
# "/www/virtualhosts/svn/trac" is the folder you gave to trac-admin initenv earlier
PythonOption TracEnv /www/virtualhosts/trac
# "/trac" is the same as the Location above
PythonOption TracUriRoot /trac
# "/tmp" should be some writable temporary directory
SetEnv PYTHON_EGG_CACHE /tmp
# "trac" can be any string, but must be the same for all
# Trac instances on the same Apache install
PythonInterpreter trac
</Location>
<Location /trac/login>
AuthType Basic
AuthName "Trac User"
AuthUserFile /www/virtualhosts/trac/conf/trac.htpasswd
Require valid-user
</Location>
I reloaded the httpd config with:
service httpd reload
Seeing no errors, I opened up the Trac installation by pointing my browser to http://my.site/trac/. The Trac application loads OK.
In the documentation, there is a section on Deploying Trac. This is where I am a little fuzzy. Is my installation of trac not already deployed? Or is this an additional step?
Second, under Mapping Static Resources, the documentation explains how to alias two directories: chrome, and site. At the beginning of the Apache ScriptAlias example the docs provide this command:
trac-admin /var/trac/env deploy /path/to/trac/htdocs/common
I do not have /var/trac/env on my system. I cannot find where this directory was created, unless it was supposed to have been set up during initial installation of Trac 1.0.
Can someone explain what needs to happen to get the chrome/site directories created? I understand these are aliased with Apache, but I'm not getting the command I quoted right above, where trac-admin uses something in /var/trac/env. That directory doesn't exist, so I am unable to issue that deploy command.
I will need the chrome/site aliases working in order to install a plugin.
For one thing, don't use mod_python. It's not being maintained any longer and it has some known issues. Setting up your server to use WSGI will give you a much better experience.
Regarding the deploy command in particular, the Trac documentation could indeed be a bit more clear. I believe both of those directory paths are just placeholders and don't have anything to do with the paths used in the documentation up to this point. They're merely there to match up with the paths used in the config file examples that follow it.
Based on the information you gave and my notes from when I set up my Trac system, try this command instead:
trac-admin /www/virtualhosts/trac deploy /www/virtualhosts/trac/deploy
That should create a deploy subdirectory in your Trac directory and populate it with "cgi-bin" and "htdocs" subfolders. From there, simply modify your Apache config files depending on what type of deployment you are doing (more details on the FastCGI and WSGI pages).
/var/trac/env was meant /path/to/trac-env, that translates in your case to /www/virtualhosts/svn/trac - the real place of your "Trac enviroment" on disc.
The deploy directive actually creates new directories and extracts static content into it, Trac's als well as Trac plugin resources as you already know. You may call it repeatedly, whenever you update trac/htdocs content. And you can even use one such trac/htdocs/common for multiple Trac envs served by the same Trac version. Just make sure to check directory permissions to ensure read access from the web-server process.
chrome/site is not a real directory at all, just a reference to directory <trac-env>/htdocs.
Still there are some plugins, that need to get unpacked to work correctly, so you'll need to setup a cache directory too.

Declaring global variable with php.ini

Is it possible to keep variables in php.ini file. Like that we do with the web.config in .net. I like to keep a flag type variable in the php.ini and use it to different projects.
It's not possible to set user-level variables within a plain php.ini file (or the .htaccess equivilents). There are some PECL modules that do allow that, such as hidef (http://pecl.php.net/package/hidef) - though these would need to be installed on every installation you use.
Including (or pre-including) a file with auto_prepend_file is quite possible - though that would be on every PHP request.
What is frequently done is setting an environment variable as part of the webserver process, which can be read from PHP. In Apache this is quite easy, with the SetEnv module.
SetEnv PRODUCTION_SERVER 1
And accessing it in PHP:
if ($_ENV['PRODUCTION_SERVER']) {...} // or getenv('PRODUCTION_SERVER')
Have you looked at get_cfg_var()?
I needed to do something similar, and this was able to do it for me.
Nope.
You could use the auto_prepend_file directive to automatically include a file that said, although as it uses the include_path, you'd need to specify the full path.
However, it's probably more transparent just to explicitly include/require the relevant file.
One technique that I have found useful for passing a limited number of global variables to a bootstrap script is to take advantage of the SetEnv directive in an .htaccess file. The advantage is that the variable you set will be made available to any script in that directory, plus any scripts in child directories under it.
You could use a SetEnv varibale with the location of a configuration file, such as:
in .htaccess:
SetEnv init_path /home/hendepher/TestApp/init/init.php
In your .php scipt:
<?php
if(!getenv('init_path')) throw new Exception('Must set init_path in .htaccess');
require_once getenv('init_path');
.
.
.
?>
If you have a test directory that requires different initialization o global variables, simply add another .htaccess file in your test directory:
SetEnv init_path /home/hendepher/TestApp/init/testing_init.php
Doing it this way, as opposed to using the 'auto_prepend_file' directive, is that your global configuration script is not run by all the php applications on your server: some may not need it.
The accepted answere also worked for me, with one change.
I didn't test this on earlier versions, but in my environment (php 5.4.22) this doesn't show up in $_ENV, but rather in $_SERVER.
In my .htacess file:
SetEnv PRODUCTION_SERVER 0.
My php code:
$production="PRODUCTION";
if (!isset($_SERVER['PRODUCTION_SERVER']) || $_SERVER['PRODUCTION_SERVER'] != 1){
$production="DEVELOPMENT";
}
I don't think that's a good place to store variables. php.ini is for storing configuration for PHP itself not your applications. You should consider putting the shared variables into a .inc file and including that instead.
Have you considered hidef?
Allow definition of user defined constants in simple ini files,
which are then processed like internal constants, without any
of the usual performance penalties.
Complementing #Ascherer answer, use get_cfg_var() to save custom variables in custom php.ini (variable created by you, not an official PHP ini directive). For example:
In php.ini: custom_variable = "abcde"
In any php script: get_cfg_var('custom_variable') returns abcde
I use this in in a small project in local dev. As I run the local server via php -S localhost:8000 -c php.ini (not running an Apache server locally), it's a good option to call some configuration constants. In production, these constants are set in .htaccess.

Configure Apache to run ELF executables

I've searched for this for a while but have not been able to find a solution. How can I configure Apache to run any ELF executable in the web root as a CGI program? For example, if I write and compile a C program and place it as /var/www/something, I want to be able to visit http://localhost/something and have Apache run the program, outputting the result, instead of prompting me to download the binary.
Edit: I know how to run CGI programs, and those outside of cgi-bin, I just want to find out how to run ELF executables with no extension such as .cgi, possibly using Apache to detect the magic of the file.
How about using the files directive to whitelist your executable names?
<files something>
SetHandler cgi-script
</files>
Or better, can you put all your executables into a single known subdirectory?
<location /exec>
SetHandler cgi-script
</location>