简体   繁体   English

从另一台计算机在优胜美地/ Apache上运行CGI脚本

[英]Running a CGI script on Yosemite/Apache from another computer

I am trying to run a simple cgi script which resides on my iMac (Yosemite installed) from a Windows 10 computer. 我正在尝试从Windows 10计算机上运行一个简单的cgi脚本,该脚本位于我的iMac(已安装优胜美地)上。

Here is the say_test01 script (note that I tried with .cgi at the end but it also didn't work) 这是say_test01脚本(请注意,我在末尾使用.cgi尝试过,但也没有用)

  #!/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. 在iMac上,我正在运行Apache v2.4.16。

On the iMac, from the terminal, I can enter ./say_test01 and I can hear the voice say the phrase. 在iMac上,从终端上,我可以输入./say_test01 ,我可以听到语音说出该短语。

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. 同样,在iMac上,但是从浏览器,当我输入localhost / cgi-bin / say_test01时,我看到的只是文件的内容。

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". 当我从Win10 PC上的浏览器运行命令192.168.1.55/cgi-bin/saysomething时,我要么看到显示的文件内容,要么收到错误消息“无法访问此站点”。

So I am looking for some help in figuring this out... 所以我正在寻找一些帮助来解决这个问题...

In the httpd.conf here are some excerpts: httpd.conf中 ,有一些摘录:

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 包括/private/etc/apache2/extra/httpd-userdir.conf

In the httpd-userdir.conf file, the following line has the '#' removed httpd-userdir.conf文件中,以下行删除“#”

Include /private/etc/apache2/users/*.conf 包括/private/etc/apache2/users/*.conf

In the /etc/apache2/users directory I have the file Name.conf which has the following code / etc / apache2 / users目录中,我有文件Name.conf ,它具有以下代码

<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... (eg today's weather, lights have just turned on...etc) 我的最终目标是将可能使用python代码的另一台计算机(树莓派)中的参数传递给cgi脚本,以便iMac可以“说出”我想要的...(例如,今天的天气,灯光刚刚打开...等)

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. 使用查看错误日志的建议,我看到我的httpd.conf出现错误。 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! 选项FollowSymLinks + ExecCGI ”行给我一个错误消息,提示我需要在所有选项前面加“ +”,否则就没有选项! So I changed the line to Options +FollowSymLinks +ExecCGI . 所以我将行更改为Options + FollowSymLinks + ExecCGI It now works well. 现在运行良好。

I now send a http GET statement from my ISY994i (Insteon controller) to the Raspberry pi3. 现在,我从ISY994i(Insteon控制器)向Raspberry pi3发送一个http GET语句。 On the pi I have setup a HTTPServer which waits for the GET statement. 在pi上,我设置了一个HTTPServer,等待GET语句。 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; 收到后,它会在pi上触发一个python脚本,该脚本会解析一个天气xml文件(我以前是使用wunderground.com为我的iRule应用程序创建的,使用wunderground.com作为源)并提取了当天的天气预报,这是一段文字说明性的段落; it then re-formats the text forecast so that the iMac can say it properly; 然后重新格式化文本预测,以便iMac可以正确说出; and finally, using urllib2, it triggers the iMac to 'say' the weather. 最后,使用urllib2,它触发iMac“说”天气。 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. 我在pi上仍然遇到问题,将HTTPServer设置为在重新启动时自动运行,但是当我手动启动它时效果很好。 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. 首先要指出的是,通过Web服务器运行的脚本不太可能拥有使计算机讲话之类的事情发生的权限。 The web server user won't have permission to access that part of the system. Web服务器用户将无权访问系统的该部分。 You would be better off doing this through ssh. 您最好通过ssh执行此操作。

However, in order to get you being able to run scripts through the web server, as asked, here goes: 但是,为了使您能够按照要求通过Web服务器运行脚本,请按以下步骤进行:

You need to add the .cgi extension to your script. 您需要将.cgi扩展名添加到脚本中。 You have your handler set up for the .cgi extension: 您已为.cgi扩展名设置了处理程序:

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. 让我知道你是怎么办的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM