How to override PHP configuration when running in CGI mode - php

There are some tutorials out there telling me how to override PHP configuration when it is running in CGI mode. But I'm still confused because lots of them assume that the server is running on Linux. While I need to do that also on Windows.
My hosting is indeed using Linux but my local development computer is using Windows XP with Xampp 1.7.3. So I need to do that in my local computer first, then I want to change the configuration on hosting server.
The PHP in my hosting server is already run as CGI while in my local computer still run as Apache module.
At this point, the processes that I understand are:
Change PHP to work in CGI mode. I did this by commenting these two line in "httpd-xampp.conf":
# LoadFile "C:/xampp/php/php5ts.dll"
# LoadModule php5_module modules/php5apache2_2.dll
My PHP is now running as CGI. I checked this with phpinfo(). It tells me that the Server API is now CGI/FastCGI. Now I want to override php configuration.
Create "cgi-bin" directory in DocumentRoot. My DocumentRoot is in "D:\www\" (I'm using apache with virtual host). So it is now "D:\www\cgi-bin".
Change the default "cgi-bin" directory settings from "C:/xampp/cgi-bin/" to "D:\www\cgi-bin":
ScriptAlias /cgi-bin/ "D:/www/cgi-bin/"
<Directory "D:\www\cgi-bin">
Options MultiViews Indexes SymLinksIfOwnerMatch Includes ExecCGI
AllowOverride All
Allow from All
</Directory>
Copy 'php.ini' file to "D:\www\cgi-bin" and modify upload_max_filesize setting from 128M to 10M.
Create 'php.cgi' file in "D:\www\cgi-bin" and put these code inside the file:
#!/bin/sh
/usr/local/cpanel/cgi-sys/php5 -c /home/user/public_html/cgi-bin/
That's it. I'm stuck at this point. All of tutorials tell me to create 'php.cgi' file and put shell code inside the file.
How to do the 6th step on Windows? I know the next step is to create handler in .htaccess file to load that 'php.cgi'.
And also, because I will also need to change PHP configuration on my hosting server (Linux), is the 6th step above right? Some tutorial tells to insert these lines instead of above:
#!/bin/sh
export PHPRC=/site/ini/1
exec /cgi-bin/php5.cgi
I'm sorry if my question is not clear. I'm a new member and this is my first question in this site.
Thank you.

If your server is already running PHP as cgi, and you do not need to run multiple PHP configurations, steps 5 and 6 are not necessary. Just change the default php.ini

Related

.htaccess not working in linux server

Actually my project is in WordPress. I'm changing my server from windows to Linux.
After changing this I'm facing this error!
Why isn't my .htaccess supporting the Linux server?
If you're using Apache, you should configure it in order to allow .htaccess files to be executed.
To do so, you can
Create a virtual host, that configures specifically one site. Using a virtual host you can set a domain name, document root, server alias, etc. Also, you can set Allowoverride to All (or other). See AllowOverride
Set your configuration in /etc/apache2/apache2.conf (or /etc/apache2/httpd.conf in some versions). In these files, there is a <Directory "/var/www/" that points to your /var/www/ directory. Inside this Directory tag, you can set AllowOverride to All. Using this configuration, every site on your server will be allowed to use .htaccess.
I recommend to use the Virtual Host that allows a easier and cleaner configuration.
Checkout the comment in .htaccess file, if it starts with // then change it to #
For me this solved the problem, I wish for you too.

PHP Fastcgi won't work if PHP5 mod enabled

I just installed multiple PHP versions on my server using FastCGI. Although I can only use them when the module PHP5 is disabled. When the latter is enabled, this configuration of my virtual host seems to be ignored :
<Directory /var/www>
AddHandler php-cgi .php
Action php-cgi /cgi-bin-php/php-cgi-5.5.17
</Directory>
Everything works fine when PHP5 is disabled but everything is executed using the module PHP5 when it is available. Would you have any idea?
Thank you
Ps: I am obviously on Apache (2.4) on Debian 7
After a moment of reflection, it makes sense that when the PHP module for Apache is available, Apache2 uses it, since it is part of it. My main issue was actually to prevent having to specify a handler for each website I have on the server, and if none is defined, use a default PHP version with FastCGI.
To get this working, I eventually added something like this in my fastcgi.conf:
<Directory /var/www>
AddHandler php-cgi .php
Action php-cgi /cgi-bin-php/php-cgi-5.5.17
</Directory>
With this I was sure to have at least PHP5.5 running for each site, without having to specify the handle in the virtual host. Problem was, what about Phpmyadmin that runs under /usr/share/phpmyadmin? Then I removed the <Directory> tag and it works.
I don't know if that is the best solution but please let me know if the above is correct or if there would be a better way to perform what I need.
Thank you

Using PHP 5.3 on old apache install

I completed a php/mysql project for a client and yesterday went about to deploy it onto the client's box - and promptly failed. To be honest, it's my fault I didn't do enough checking before I started - but the project is fully self-contained and I didn't need anything currently on the client's server.
I developed the project using php 5.3 and PDO for mysql connection. When I got access to the client's server, turned out that they have VERY old installation that was last upgraded in 2008 (yes, seriously). They have Apache 2.0 and php 5.2, which is configured/compiled without PDO and with minimum everything.
I do have root access to the server, but I can't go and blindly start upgrading things. They've got a whole live site, with live shopping cart, etc. on that server and any general upgrade of php would require thorough testing. Therefore I'm looking at ways to run my new project using a separate install of php 5.3, which I compiled from sources with all required stuff included and installed into a separate directory (/opt/php53).
I am thinking along the lines of enabling fastcgi on the server and then using that to run php form my separate install. If I do this inside apache <Directory> where my project is install, it should only affect that one directory - and should leave everything else as is. Things are made more complicated because I don't have any kind of test server where I could play/test configurations - I have to work on the live server directly.
I'm wondering whether this fastcgi approach is valid or maybe there are some other options I should consider.
You can try to use different ports on fastcgi for the various PHP versions.
Another approach: http://linuxplayer.org/2011/05/intall-multiple-version-of-php-on-one-server
I've done some further digging/experimenting (taking advantage of the fact that I'm in UK and the client and its customers are in the USA - so it's still night there). The following setup seems to have worked - this utilises php-cgi without recourse to fastcgi - and I still get a good response time.
1) Leave pretty much everything intact in apache httpd.conf file - that includes existing libphp5.so that utilises existing 5.2 installation.
2) Install php 5.3 into a separate directory - in my case /opt/php53.
3) In httpd.conf, add the following (or ensure it's already there)
ScriptAlias /cgi-bin/ /var/www/mainsite/cgi-bin
AddHandler cgi-script .cgi
<Directory /var/www/mainsite/newapp>
Options -Indexes FollowSymLinks +ExecCGI
AllowOverride AuthConfig FileInfo
AddHandler php5-cgi .php
Action php5-cgi /cgi-bin/php.cgi
DirectoryIndex index.php index.html
Order allow,deny
Allow from all
</Directory>
4) In directory /var/www/mainsite/cgi-bin/ create file php.cgi with the following content:
#!/bin/bash
PHP_CGI=/opt/php53/bin/php-cgi
PHP_FCGI_CHILDREN=5
PHP_FCGI_MAX_REQUESTS=1000
### no editing below ###
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGI
5) Restart apache: /etc/init.d/httpd restart
After this, I placed a file with <?php phpinfo(); ?> into the root of the web server and into my newapp directory. Executed from the root of the server it produced (among other things):
PHP Version 5.2.5
Build Date Dec 11 2008 19:04:47
Server API Apache 2.0 Handler
When executed from the newapp directory, it produced:
PHP Version 5.3.28
Build Date Feb 13 2014 17:00:43
Server API CGI/FastCGI
Which is exactly what I want.

Running PHP file outside of documentroot (cgi-bin folder)

I am working with a colleague to set up their local environment on a MAC in XAMPP, on windows my vhost looks like the one below.
When I access a URL like http://domain.local/cgi-bin/handler.php the web server processes the PHP correctly but on his we get a 500 server error and this message...
The server encountered an internal error and was unable to complete your request.
Error message:
Premature end of script headers:
We tried changing the name of the cgi-bin folder to something else as I noticed there was another alias in httpd.conf but this had no effect...so it seems to me like the issue is permissions related.
We believe the alias is setup ok as accessing http://domain.local/cgi-bin/filenothere.php which doesn't exist throws a 404 as expected, any .html/.pl files fail to execute.
The permissions that exist on the cgi-bin folder are...
rwxrwxrwx dave staff
and is owned by the user and group....
dave staff
Vhost
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName www.domain.local
ServerAlias domain.local
ServerAlias api.domain.local
# Indexes + Directory Root.
DirectoryIndex index.php
DocumentRoot E:/home/www/www.domain.co.uk/htdocs/
# CGI Directory
ScriptAlias /cgi-bin/ E:/home/www/www.domain.co.uk/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
# Logfiles
ErrorLog E:/home/www/www.domain.co.uk/logs/error.log
CustomLog E:/home/www/www.domain.co.uk/logs/access.log combined
</VirtualHost>
Any idea what is causing this PHP file to not be executed?
UPDATE
Tried adding a header to say that the content is PHP to the start of the PHP file and this now simply outputs the PHP code.
It's as if any path specified in as an Alias is accessible but the server doesn't know how to execute the content, this is for HTML as well as PHP
I think you need a section for your cgi-bin.
The fact that your server can show you the script sources means the server has at least read permissions on /file/system/path/to/cgi-bin and IIRC that clashes with ScriptAlias b/c ScriptAlias expects /file/system/path/to/cgi-bin to be unaccessible for security reasons. I think your solution should look something along the lines of:
<Directory /file/system/path/to/cgi-bin>
Options ExecCGI
SetHandler cgi-script
</Directory
There is (very) rarely a need to run PHP scripts as CGIs given that the PHP module for Apache can execute them directly. Try adding this to your Apache config:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Afterwards simply place the PHP scripts into the document root for the site and see if they work. You'll want to remove the /cgi-bin/ part of the URL.
You say you're setting XAMMP on a Mac, but you have a drive letter (E:) prefixing your paths. OS X does not have drive letters like Windows, and this may also be causing (part of) your issue.
I don't know much about settings used. But I think you should go through following links. Might get some help.
http://www.sean-barton.co.uk/2009/02/setting-up-a-phpmysql-local-development-environment-on-a-mac-doing-it-properly/
http://docs.joomlabamboo.com/using-joomla/setting-up-a-quick-start-package-on-your-local-server-xampp-pc
http://www.adobe.com/devnet/dreamweaver/articles/setup_php.html
How to create Mac OS X dev environment for legacy PHP app?
Assuming that PHP is running in Safe Mode you may need to "open" your cgi-bin directory, as the execution of user (PHP) scripts is limited to the DocumentRoot and it's subfolders.
For all I know you could do that in two ways
1. Edit your php.ini
Locate the line containing open_basedir. If there's a comment at the beginning of the line - a semicolon - remove it. Then add your cgi-bin directory.
open_basedir = "E:\home\www\www.domain.co.uk\cgi-bin\"
If you need to open more than one directories you can use semicolon ; as a separator. On Linux based server, use a colon :
open_basedir = "E:\home\www\www.domain.co.uk\cgi-bin\;E:\home\www\www.domain.co.uk\another_dir\"
In cases like mine, where your server is hosted by third party, you'd need the second option (well sort of)
2. Edit your VirtualHost
Add the following to your VirtualHost, i.e. after DocumentRoot:
php_admin_value open_basedir "E:\home\www\www.domain.co.uk\cgi-bin\"
Same rules apply here for multiple directories and difference between Linux and Windows systems as above.
Hope that helps
Do you know whether PHP is running as a CGI program or as a webserver module? You should be able to find this out if you can get a phpinfo() page working (maybe from a regular folder inside the website root). If you're running as a webserver module then you should have a section near the top with a heading of Server API which says Apache 2.0 Handler (or equivalent).
From these pages:
https://bugs.php.net/bug.php?id=13316
http://php.net/manual/en/install.unix.commandline.php
http://gallery.menalto.com/node/8955
... it seems that it may be either due to PHP running as a CGI script, or else a conflict between PHP and another CGI handler.
One of the posters on the third linked page found that their similar-sounding end of script headers issue was resolved by removing / commenting out the Options +ExecCGI line in their .htconfig / vhosts file.
Might be worth having a read through the above links to see if your problem is related.

Running Python scripts like PHP

There are Apache 2 + mod_wsgi + Python 2.6 on server.
I would like to run scripts from Python like PHP scripts. The idea may seem silly, but I'm so accustomed (at least at first learning Python).
Example:
PHP - http://example.com/script.php
Python - http://example.com/script.py
P.S. I know about mod_rewrite and other similar tricks that can do that. But it is only a disguise, and not a direct run.
UPD: My .htaccess file. index.py works, but other python scripts get 404 error.
<Files *.py>
SetHandler wsgi-script
Options ExecCGI FollowSymLinks
</Files>
DirectoryIndex index.py
That doesn't look as cool as having a wsgi app running, so I recommend that you use the flask framework which is as simple as can be a sane framework.
Here's a link describing the install procedure on mod_wsgi.
Later on, you might want to consider a cool framework like Django, Pyramid, Grok ...
If you really want to use mod_wsgi like mod_php check Graham Dumpleton's great answer.
Technically what you are doing should work, but see AddHandler method for configuring mod_wsgi in:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive
That way you don't have to fiddle with Files directive.
SetHandler does similar thing but all files in context are treated as WSGI script files even if they may be static HTML or PHP files. You got away with it because qualified with Files, but better to just use AddHandler.
Do note that code reloading will not work like you are used to with PHP. See:
http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
I run Python scripts like PHP using mod_cgi
Here is a tutorial by Apache. And the cgi interface to use is here by Python.org
A second good tutorial that I used is here once your up and running.
I would add that there is a simiplier way to configure Apache.
Step 1: The first step is not mentioned in the guides above is to enable CGI processing in apache.
sudo a2enmod cgi
This will automatically enable mod_cgid if your server is configured with a multi-threaded MPM, which was the case for me.
Step 2: Edit your httpd.conf or whatever it is named in /etc/apache2/sites-enabled
in Linux Mint 19.2.
Enable a script for / with an index.py
<VirtualHost *:80>
DocumentRoot /your/www/html
DirectoryIndex index.py
</VirtualHost>
Step 3: Enable other python scripts so they can also run in the same folder or in subdirectories.
<Directory "/your/www/html/*">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
AddHandler cgi-script .py
AddHandler default-handler .jpg .png .gif .css .js .ico
</Directory>
ScriptAlias / /your/www/html/
There are two caveats that I have encountered that must be adhered to to run python scripts successfully.
When running in linux, make sure the line endings of each python file.py are
unix line endings. Otherwise the python script will not run. For example,
Notepad++ has Edit, EOL Conversion, Linux (LF) in its menu,tool bar.
Ensure that the permission of each python file.py has execute permissions.
In Linux Mint 19.2 I right click the file, go to Properties, go to Permissions,
then check the checkbox at Execute: Allow executing program as file. Or just
run the command:
chmod a+x python_script.py

Categories