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
Related
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
I have an Apache server with PHP support. I also installed Python with mod_wsgi and with mysql-connector. Besides I installed Django. Now, I want to try to use PHP and Python simultaneously at the server side. The catch is, I worked with PHP for a couple of years and I see that it is becoming less and less popular, so I plan to port some of my PHP-code to Python-code, or just to try it, to see how they work together. So, I now have a site located at C:\Apache\htdocs and I created a first Django project at C:\WebPython\djsite. Inside djsite I have djsite folder and four files _init_.py, settings.py, urls.py and wsgi.py. In my site I want to address both to PHP handlers (or scripts) and to Python scripts, so, I guess, the problem is in how to config httpd.conf. I looked through many forum threads here at stackoverflow and outside, but still I can't make it work. Now, my httpd.conf looks like this:
...
ServerName localhost
<Directory "c:/Apache/htdocs">
Options Indexes FollowSymLinks
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.htm index.php
</IfModule>
...
You should see this question then:
PHP script inside Django template
It has a link to this:
http://animuchan.net/django_php/
Running PHP with Django would be a mess though.
Hello Like Every Body Else Said Its A terrible idea but Refer To Django Documentation adding this to http.conf on your apache2 and tweek
the wsgi.py file will work
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
and change
If multiple Django sites are run in a single mod_wsgi process, all of them will use the settings of whichever one happens to run first. This can be solved by changing:
in wsgi.py, to:
os.environ["DJANGO_SETTINGS_MODULE"] = "{{ project_name }}.settings"
or by using mod_wsgi daemon mode and ensuring that each site runs in its own daemon process.
Fixing UnicodeEncodeError for file uploads
If you get a UnicodeEncodeError when uploading files with file names that contain non-ASCII characters, make sure Apache is configured to accept non-ASCII file names:
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
A common location to put this configuration is /etc/apache2/envvars.
See the Files section of the Unicode reference guide for details.
See More At https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/
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.
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
First off let me start by saying that yes I have searched for this in google and in stackoverflow specifically, I have found many answers and tried them all. At this point I believe my only resource is posting the question myself, even if the scenario sounds repeated please be so kind as to try to help.
The situation is quite basic, on Ubuntu desktop 10.04 I set up apache via Synaptic and Php5 according to this guide and its spin-off here.
At this point if I go on command line and call a php script it works, for example:
php test.php
outputs my hello world without any problem. But if I go to firefox and point to test.php it will show the 403 error Forbidden...
I have changed ownership on /var/, /var/www/ and /var/www/test.php to every variable I can think of (www-data [apache runs as this user], purefan [my regular user], root) it makes no difference, I have also changed permissions several times 777, 0777 (just to be safe), 644, 755, no change.
from CLI I got the phpinfo into a file and added it here.
If Im not mistaken the problem is happening when Apache calls the php interpreter, as when I go to http://localhost/index.php it shows apache's default "It Works!" page, but if I add php content to that file it simply gets ignored, no error is shown though (also checked error log and syslog).
So please, if you have any suggestion let me know, this is not a life or dead thing but would really like to set up using worker instead of prefork.
Thank you for your time
I had exactly this same problem.. first I installed apache2 with worker mpm and php5 with fastcgi successfully under a virtual machine.. but when I tried it (using exactly the same process) on my production host, it gave me forbidden errors.
After a lot of search, I finally got it working. Here you can find the steps I've done to get it working:
These are the packages I've installed to get apache2 with mpm-worker & php5 with fastcgi:
apache2
apache2-mpm-worker
php5-cli
php5-cgi
php5-common
libapache2-mod-fcgid
Then you need to create a file to tell apache how to use php files. I've created one under the /etc/apache2/conf.d directory named php.conf. This should be the content:
<Directory /usr/share>
AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
Options ExecCGI FollowSymlinks Indexes
</Directory>
<Files ~ (\.php)>
AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
Options ExecCGI
allow from all
</Files>
And that's all. Obviously, ensure that the user, group and permissions for the files are the correct one (www-data for user and group and 644 & 755 for files and folders).
My failure was not to add the <Files></Files> tag. After adding it I finally could access the site. Before having the php configuration under the <Files> tag I was adding those lines in the virtual-host, under the <Directory> tag of my host. Like this:
<Directory /var/www/website/>
AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
Options ExecCGI Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
But this wasn't working! In my virtual machine I have exactly this lines and it works perfectly... so, maybe my solution works for you too.
I hope this will help somebody :)
Edit: This is the forum thread that saved my life: http://forum.parallels.com/showthread.php?t=85413
YOu get the PHP only forbidden error, if you have not enabled ExecCGI in your apache httpd.conf
Options Indexes FollowSymLinks **ExecCGI**
AllowOverride None
Require all granted
hmm have you installed the Apache2 PHP module?
It sounds a bit that you have installed php5-cli but not the apache2 module.
On this page you found a short example. Perhaps i could help you.
It puzzles me really a lot. Had exactly same problem.
Solved it that way:
I added .htaccess file with following content in it:
# Follow symbolic links in this directory.
Options +FollowSymLinks
And everything under that folder suddenly started to work properly.
Adding Indexes and FollowSymLinks to Files tag solved my problem:
<Files ~ "\.php$>"
AddHandler fcgid-script .php
FcgidWrapper "c:/php/php-cgi.exe" .php
Options ExecCGI Indexes FollowSymLinks
allow from all
</Files>