I setup apache2 on a mac (big sur), and it works for an html page. But I installed python3 on the machine, yet if I try to load a .py file in the home directory my web browser the system treats it like I'm running a local program and opens it in an editor.
I can't find any doc on how to get python3 to run as cgi in the apache server. Any help?
UPDATE: I added to the httpd.conf file:
Options +ExecCGI
AddHandler cgi-script .py
Now it spits back the file as just plain text, even though it ends in ".py" but still won't execute.
UPDATE: I removed the # from the load cgi lines. Now it responds "Forbidden: you do not have permission to access this resource". The file is 755, and all other files in the home directory are accessible.
I had to put
<Directory "/...the directory/">
Options +ExecCGI
</Directory>
At the bottom of the httpd.conf file. Then I had further problems with a "malformed header". I checked the error log and it was seeing the actual content of the python script, so it must have been running the script. I added
print("Content-Type: text/html\r\n\r\n")
as the first output in the program and that fixed it. The \r\n\r\n tells it that i am done with headers and onto the body.
Related
My perl code has a error on the first line #!/usr/bin/perl when deploying on linux server. But it work properly in my local b/c I used this line #!C:\xampp\perl\bin\perl.exe.
Can somebody tell me the reason?
Ah I have solved this problem by enable mod cgi in my web server. In my case I use apache2 so I added some configs like:
Explicitly using Options to permit CGI execution
You could explicitly use the Options directive, inside your main server configuration file, to specify that CGI execution was permitted in a particular directory:
<Directory "/var/www/mypro">
Options ExecCGI
</Directory>
The above directive tells Apache to permit the execution of CGI files. You will also need to tell the server what files are CGI files. The following AddHandler directive tells the server to treat all files with the cgi or pl extension as CGI programs:
AddHandler cgi-script .cgi .pl
After that load cgi module:
a2enmod cgi
Restart my apache server. It worked!
If you use another web server like nginx you can find some way to enable cgi in your config file.
I am trying to run a simple cgi script which resides on my iMac (Yosemite installed) from a Windows 10 computer.
Here is the say_test01 script (note that I tried with .cgi at the end but it also didn't work)
#!/bin/bash
echo -e "Content-type: text/html\n"
cat << junk
<html>
<head>
<title>
saying
</title>
</head>
<body>
junk
#-----------------------
osascript -e "set volume 8"
say -v Whisper This is just wrong!
osascript -e "set volume 4"
say -v Whisper This is OK!
#-----------------------
cat << junk
</body>
</html>
junk
On the iMac I am running Apache v2.4.16.
On the iMac, from the terminal, I can enter ./say_test01 and I can hear the voice say the phrase.
Again, on the iMac, but from a browser, when I enter the localhost/cgi-bin/say_test01 all I see are the contents of the file.
I understand, from reading other threads, that this is because the system doesn't know which application is associated with this file.
When I run from a browser on my Win10 PC the command 192.168.1.55/cgi-bin/saysomething I either see the contents of the file displayed or I get the error message "This site can't be reached".
So I am looking for some help in figuring this out...
In the httpd.conf here are some excerpts:
These lines have the '#' removed:
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule authz_core_module libexec/apache2/mod_authz_core.so
LoadModule cgi_module libexec/apache2/mod_cgi.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule alias_module libexec/apache2/mod_alias.so
Here is the Directory section:
<Directory "/Library/WebServer/CGI-Executables">
AddHandler cgi-script .cgi
Options FollowSymLinks +ExecCGI
AllowOverride None
Require all granted
</Directory>
And finally this line has the '#' removed
Include /private/etc/apache2/extra/httpd-userdir.conf
In the httpd-userdir.conf file, the following line has the '#' removed
Include /private/etc/apache2/users/*.conf
In the /etc/apache2/users directory I have the file Name.conf which has the following code
<Directory "/Users/Name/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>
I have not touched the virtual host file because I don't think (?) it applies here.
My ultimate goal is to pass parameters from another computer (raspberry pi) perhaps using python code to a cgi script so that the iMac can 'say' what I want... (e.g. today's weather, lights have just turned on...etc)
Any suggestions on how I can get this working?
Edit:
Using the suggestion of looking at the error log I saw that my httpd.conf had an error. The line Options FollowSymLinks +ExecCGI gave me an error message saying that I needed to have the '+' in front of all options or no options! So I changed the line to Options +FollowSymLinks +ExecCGI. It now works well.
I now send a http GET statement from my ISY994i (Insteon controller) to the Raspberry pi3. On the pi I have setup a HTTPServer which waits for the GET statement. Once received, it triggers a python script on the pi which parses a weather xml file (which I previously created for my iRule app, using wunderground.com as the source) and extracts the current day's weather forecast, which is a text descriptive paragraph; it then re-formats the text forecast so that the iMac can say it properly; and finally, using urllib2, it triggers the iMac to 'say' the weather. I am still encountering a problem, on the pi, setting up the HTTPServer to run automatically on a reboot but it works well when I start it manually. I will open a new question to get help resolving this.
The first thing to point out is that scripts run through the web server are unlikely to have permissions to make things happen like having the computer speak. The web server user won't have permission to access that part of the system. You would be better off doing this through ssh.
However, in order to get you being able to run scripts through the web server, as asked, here goes:
You need to add the .cgi extension to your script. You have your handler set up for the .cgi extension:
AddHandler cgi-script .cgi
So it's only going to work for scripts that have that extension. Currently you have none. I know you say you tried it, but it's definitely not going to work without it. So put it back in, and see if it works now. If it doesn't, don't take it off again. It's needed. Let me know how you get on.
I have difficulties making my .htacces work on my Ubuntu 14.04 LTS. I know it is a hidden file and all; I've searched everywhere and people seem to have the same problem. I've made a virtual host on my machine and all. Can someone please help me ?
ErrorDocument 404 /pages/error.php?code=404
ErrorDocument 403 /pages/error.php?code=403
You need to make sure the following things are true:
You need to check what DocumentRoot is set to. When an url beginning with a slash is detected, it will try to load that file relative to the document root. (docs) The file <documentroot>/pages/error.php must thus exist.
You need to make sure that .htaccess files are allowed by the main Apache configuration. The easiest way to test this is by entering garbage into your .htaccess file, saving it and reloading a page. You'll get an internal server error if Apache reads the .htaccess file. Otherwise the page loads as expected. To enable .htaccess files, the AllowOverride directive must allow something (see next bullet point). Additionally, check in httpd.conf if AccessFileName is set to something different than .htaccess. Change it as necessary, then RESTART APACHE. (docs)
For ErrorDocument, AllowOverride must be at least set to FileInfo. See the docs. Go to your main config file (httpd.conf), probably apache/Apachex.y.z/conf/httpd.conf. Search for the <Directory ...> block that corresponds to your http root, and look around if it contains an AllowOverride directive. Add FileInfo as one of the arguments, save the file and RESTART APACHE.
I'm trying to get cakephp working locally on my mac - yosemite. I have apache and php working locally in my /~user/Sites directory... I can hit a php ini file ok and have an install of wordpress in another directory.
I've hooked it all up apart from getting the urls rewriting... I followed the steps on the cake site:
http://book.cakephp.org/2.0/en/installation/url-rewriting.html
Now all I get is 404 not found!.
The three .htaccess files have the default content with an additional RewriteBase pointing to the folder its installed in, in my Sites folder.
Any ideas what I'm doing wrong? I've read the other posts on this and none of them helped!
Thanks
First, confirm mod_rewrite is enabled by (on command line):
# a2enmod rewrite
If it just got enabled using the command above, restart apache:
# /etc/init.d/apache2 restart
These types of errors should hit your error.log .. while trying to load the site, hold the error log open:
# tail -f /var/log/apache/error.log
and try to access your page again.
If this doesn't help you solve the problem, paste in your rewrite rules so we can take a peak.
Apache configuration files all appear correct; I've added the ExecCGI to the RootDirectory, although this file resides in the ScriptAlias directory so that shouldn't even be necessary. I put a .htaccess file out there which is also probably not necessary.
This is a simple counter.cgi file - runs fine from the linux command line; runs fine called as a "method post" from an html file. When I call it using "!exec cgi" it's as if it never happened - it's completely ignored. There are no errors in any log files - the file is not executed.
I figured this out - my SSI configuration wouldn't process .html files. Changed the httpd.conf file so that SSI includes were processed from .html files. :)