I'm trying to use Python with AMPPS, which is an Apache / MySQL / etc. bundle for Windows. There's just one problem though; every script I try to run returns an 'Internal Server Error', even though it's correct code.
This is probably an issue that's not worth posting it here, but I googled for three hours and I just can't find any help.
My httpd.conf is over here: http://pastebin.com/5DMU2cUU
The single line of code I use is: print "Hello World!";
Notepad++ is set to EOL Conversion->UNIX format (before you ask).
This is all I know so far. Please don't hate me for posting this (I know you love to do that), I'm new and confused :3
mod_wsgi is already configured in AMPPS. You can see it in loaded modules of apache.
httpd.exe -D DUMP_MODULES
You just need to configure your script now.
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
You can modify the python.conf included in httpd.conf too. i.e for using mod_wsgi directives.
This is official hello world example from mod_wsgi. Hope this helps.
Follow below steps:
Open httpd.conf
Append .py to AddHandler cgi-script .cgi .pl under tag
Now AddHandler cgi-script .cgi .pl becomes AddHandler cgi-script .cgi .pl .py
Search for block and Add ExecCGI to Options FollowSymLinks Indexes.
Now its become Options FollowSymLinks Indexes ExecCGI
Save the httpd.conf file, stop the apache server and restart again.
If the apache server restarts without any problem process you have successfully setup ampps to run python, otherwise restore the conf file from backup. In ampps there is an option to restore default configuration.
Add python exe file link line to the top of the python file to direct to the Python interpreter.It might be #!C:\Ampps\python\python.exe
Add this line print("Content-type: text/html\n")
Add this line print("Hello world!")
Now execute your python file. http://127.0.0.1/test/test.py
wsgi_module must be installed for apache to run python script via execCGI. On ampps this is installed and added to conf file by default.
link:http://www.lakshman.com.np/run-python-on-ampps-windows/
Related
I have been trying to run a simple python script hello.py with CGI but am getting 500 Internal Server Error.
My python code.
#!/usr/bin/python2.7
print '<html>'
print '<head>'
print '<title>Hello World - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello World! This is my first CGI program</h2>'
print '</body>'
print '</html>'
The directory which i have the python script running is in /var/www/crunchworld.The conf file which i enabled is in `/etc/apache2/conf-available/crunchworld.conf
The conf file looks like
<Directory /var/www/crunchworld>
Options +ExecCGI
AddHandler cgi-script .cgi
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I have cgi enabled and given the necessary permission for the file hello.py but its still showing me internal server error.When i checked the logs i see
End of script output before headers: hello.py
I have researched about the error and give appropriate permissions for the file but it doesnt work.
Any help would be so much appreciated. Thanks in advance.
Further changes i have made.
I have added AddHandler cgi-script .cgi .py in my crunchworld.conf file.
2.I have given permission for the file hello.py
I have symlinked /etc/apache2/conf-available/crunchworld.conf in /etc/apache2/conf-enabled
4.I had already installed python2.7 on the path /usr/bin/python2.7 and i have also tried using #!/usr/bin/env python but still it doesnt work.
Upon checking the logs i found End of script output before headers: hello.py, referer: http://localhost/
Thank you for your recommendations but it is still showing 500 internal error.
Your CGI script must also output header information.
The minimum required is the Content-type header -- which in this case should be set to text/html.
Add this to the start of your CGI script (before you print anything else).
print 'Content-type: text/html\n'
Take note of the extra trailing newline -- its necessary to leave at least one blank line between the header(s) and the content itself.
Update:
For further troubleshooting, do the following:
Make sure your CGI script has the correct permissions set: chmod 0755 hello.py just to be sure.
Your script appears to be .py whereas your apache config appears to only specify .cgi files. Your AddHandler should be AddHandler cgi-script .cgi .py.
You should symlink your /etc/apache2/conf-available/crunchworld.conf file in /etc/apache2/conf-enabled if you haven't already done so. Do so by running the following: cd /etc/apache2/conf-enabled; sudo ln -s ../conf-available/crunchworld.conf.
Always remember to restart apache if you make any changes to your apache config: e.g. sudo service apache2 restart.
Check that your hashbang line is correct. Does /usr/bin/python2.7 exist? You can try setting instead to #!/usr/bin/env python or #!/usr/bin/env python2. (Or better yet, switch to python3 if possible on your system).
Check the apache error logs once again. E.g. tail -20 /var/log/apache2/error.log (or wherever your logs are).
You can attempt further debugging with the cgitb module (see https://docs.python.org/3/library/cgitb.html).
I have a ajax call being make to a python script.But instead of executing the code the call just prints the raw code.
I'm using xampp installed on mac.
What I did:
[edited the following line in httpd.conf]
AddHandler cgi-script .cgi .pl .asp .py
My python script present in htdocs is:
#/usr/local/opt/python/bin/python3.7
print("Hello World!")
I'm using python 2.7.13
At first the browser was showing the raw code.
what I did:
Edited httpd.conf
AddHandler cgi-script .cgi .pl .asp .py
At the top of all my scripts I added this:
#!j:/Installeds/Python/python
print "Content-type: text/html\n\n"
Now it's giving me Internal Server Error (500) and I have no idea what else to try... First time with python.
Obs: I think this may help> Apache>Error.log
[cgi:error] [pid 6364:tid 1620] (9)Bad file descriptor: [client ::1:51083] AH01222: don't know how to spawn child process: C:/Files and Installs/Xampp/htdocs/Test/main.py
AH02102: C:/Files and Installs/Xampp/htdocs/Test/main.py is not executable; ensure interpreted scripts have "#!" or "'!" first line
Run Python in XAMPP for Windows
Step 1: Download and Install Python
Download and install the latest version of Python from https://www.python.org/downloads.
Step 2: Configure XAMPP for Python
Open the Apache httpd.conf configuration file located at .../xampp/apache/conf/httpd.conf using a text editor of your choice.
The XAMPP GUI can also quickly access the httpd.conf file:
Copy and paste the following code at the end of the file:
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
Step 2.5: Add Python extension to default page locations (Optional)
Inside the httpd.conf file search for <IfModule dir_module> to add index.py among others to the list of default page locations.
<IfModule dir_module>
DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm index.py \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm default.py \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm home.py
</IfModule>
Step 3: Restart Apache / XAMPP
If Apache was running while editing, now is the time to restart it.
Step 4: Run Python from XAMPP
Create a folder and Python file in the XAMPP htdocs directory; e.g. .../xampp/htdocs/PythonProject/test.py.
At the beginning of your script, you first need to specify the directory of your Python executable. The default location of Python 3.10.0 is C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe, but in your case, it may be different, depending on the version and directory in which you've installed Python.
#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe
After that, you can create your Python script.
#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe
print("Content-Type: text/html\n")
print("Hello, World!")
Save the file and open localhost/PythonProject/test.py in your web browser. Your Python script should be running.
Im running ubuntu 16.04 so my answer might be a little different. I am using a google chrome browser with a python 3 file called test.py in /opt/lampp/htdocs/PythonProject:
#test.py
#!/usr/bin/env python3
print('Content-type: text/html\r\n\r')
print("<p>hello world!</p>")
print("I can view this in my browser yay!!")
I edited my httpd.conf file in /opt/lampp/etc/httpd.conf and I did not add
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
to the end of the file, instead I added .py to the end of the existing line
AddHandler cgi-script .cgi .pl
finally i made the file executable by chmod +x /opt/lampp/htdocs/PythonProject/test.py and then I just ran it through my browser:
http://localhost/PythonProject/test.py
OUTPUT:
hello world!
I can view this in my browser yay!!
Download python from here (https://www.python.org/downloads/) and install it
Open XAMPP control panel, click on config and go to httpd.conf file >> search for addhandler and add “.py” [without quotation ] just like in the screenshot (if it’s not added)
httpd.conf file
Restart the apache server
To run a python script:
Open any text editor and put this code
#!C:/Users/"Username"/AppData/Local/Programs/Python/Python37-32/python.exe
print("content-type: text/html\n\n" )
print("<br><B>hello python</B>")
In the first line, you have to type the location of the python.exe file after putting a shebang (#!)
“username” — username of your PC
This will differ from one user to another. You can find the python location from environment variables (see the screenshot below)
py environment variables
Then put the script in xampp>> htdocs folder
Open your browser and type localhost/”filename”.py (http://localhost/filename.py)
[ “filename”= script name]
You’ll see this output
output
Bad File descriptor means that a file is corrupt and it says it can not run the script so you probably have python incorrectly set up.
I'm trying to run a simple Python script using XAMPP in my MAC. I have followed this link. Anyway I'll tell what I've done in steps.
I'm running MAC OS X 10.9.5 and I have python 2.7.5 and python 3.4.1 both installed in this machine. Also I have successfully installed XAMPP 1.8.3-5 in this machine and tested with PHP and HTML pages.
Next thing I did was creating a directory named cgi-bin in the path /Volumes/Macintosh\ HD/Applications/XAMPP/xamppfiles/htdocs/ . Inside that directory I've created file name called first.py with the content with below.
#!/usr/bin/env python
print "Content-type: text/html\n"
print "Hello, world!"
Just for testing i run this file in Terminal to make sure I get the desired print out puts.
$ python /Volumes/Macintosh\ HD/Applications/XAMPP/xamppfiles/htdocs/cgi-bin/hello.py
Yes, it runs well. Then I changed the file permission as well as directory permisson.
drwxrwxrwx 5 anuja mygroup 170 Nov 14 17:40 cgi-bin/
and
-rwxr-x--x# 1 anuja mygroup 82 Nov 14 16:05 first.py*
Next thing was configure httpd.conf file. I load the XAMPP control panel then select Manage Services tab and select Apache Web Server and click on the Configure button. From the pop up window I clicked Open Conf File.
There I comment the following line
# ScriptAlias /cgi-bin/ "/Applications/XAMPP/xamppfiles/cgi-bin/"
Then add the following directory block
<Directory "/Volumes/Macintosh\ HD/Applications/XAMPP/htdocs/cgi-bin/">
AddHandler cgi-script .cgi .py
AllowOverride All
Options +Indexes FollowSymLinks +ExecCGI
Order allow,deny
Allow from all
</Directory>
There is a similar directory block there and I kept it as it is above the given block.
<Directory "/Applications/XAMPP/xamppfiles/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
After restarting Apache server from XAMPP panel I tried to load this it using my web browser
http://localhost/cgi-bin/first.py
Finally I ended up with "Object not found" error on the browser. I tried several changes but out of luck. How can I run this python script from the web browser I get Hello, world! as out put?
I have a standard Apache2 installation on an Ubuntu server. The default settings use ScriptAlias to refer /cgi-bin/ to /usr/lib/cgi-bin/. If I place my Python CGI script in /usr/lib/cgi-bin/ it executes.
I created /var/www/cgi-bin/ with appropriate permissions, removed the ScriptAlias line, changed the Directory entry in the default file for the site, moved the CGI file to /var/www/cgi-bin/ and restarted Apache2, but I was not able to get the script to run. It was appearing as a text file in the browser instead of being executed. The HTML file calling the script refers to /cgi-bin/SCRIPT, so I left that unchanged. I tried variations on /cgi-bin and /var/www/cgi-bin in the config files without success. How can I get a Python CGI file to run from /var/www/cgi-bin?
If you removed the ScriptAlias directive, you should probably add the following lines into the main config for the directory or into .htaccess file:
Options +ExecCGI
AddHandler cgi-script py
please make sure:
the file you are calling itself has enough permission
the file you are calling has an extension that is is in the .htaccess file (see newtover's answer)
specify how you want to run the script in the first line of the file. If you are calling foo.pl, make sure #!/usr/bin/perl is in the first line.