Ubuntu

執行 python CGI 報告時的 Lighttpd - 403 禁止 - Ubuntu

  • April 27, 2020

我正在我的 ubuntu 中啟動一個 lighttpd 服務。我的想法是通過 CGI 提供一個 python 腳本。但是,當我這樣做時,我得到 403 禁止?請幫助如何調試以及我在這裡缺少什麼?

vi /etc/lighttpd/lighttpd.conf
server.document-root  "/home/httpd"

在 lighttpd 中啟用 CGI。

vi /etc/lighttpd/lighttpd.conf

server.modules = (
   "mod_access",
   "mod_alias",
   "mod_compress",
   "mod_redirect",
   "mod_cgi",
   "mod_rewrite",
)

為了讓 lighttpd 能夠辨識任何 python 腳本,我們需要在文件末尾添加以下新部分。

$HTTP["url"] =~ "^/cgi-bin/" {
 cgi.assign = (".py" => "/usr/bin/python")
}

授予根文件夾適當的權限。

# chown www-data /home/httpd/cgi-bin
# chgrp www-data /home/httpd/cgi-bin

現在,編寫 hello.py

vi /home/httpd/cgi-bin/hello.py

#! /usr/bin/python
#
print "Content-Type: text/html\n\n"
print '<html> <head> <meta-content="text/html; charset=UTF-8"/>'
print '<title> Raspberry  Pi </title> 
<p> 
for count in range(1,100)
print'Hello&nbspWorld...'
print "/p> <body> </html>
Finally, restart the lighttpd service.

服務 lighttpd 重啟

 But, when I try to access the page it says- 

403 forbidden

這是我的具有/home/httpd 權限的文件夾。

/home/httpd$ ls -l
total 8
drwxr-xr-x 2 www-data www-data 4096 Apr  3 17:56 cgi-bin
drwxr-xr-x 2 www-data root     4096 Apr  3 16:41 html

這是hello.py

/home/httpd/cgi-bin$ ls -l 
total 4
-rwxrwxrwx 1 root www-data 244 Apr  3 17:56 hello.py

日誌說它仍在尋找 php、html 文件而不是我的 python 二進製文件?

read(7, "GET / HTTP/1.1\r\nHost: 10.0.2.15\r"..., 4159) = 328
stat("/home/httpd/", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/home/httpd/index.php", 0x7ffee411b9f0) = -1 ENOENT (No such file or directory)
stat("/home/httpd/index.html", 0x7ffee411b9f0) = -1 ENOENT (No such file or directory)
stat("/home/httpd/index.lighttpd.html", 0x7ffee411b9f0) = -1 ENOENT (No such file or directory)
setsockopt(7, SOL_TCP, TCP_CORK, [1], 4) = 0
writev(7, [{iov_base="HTTP/1.1 403 Forbidden\r\nContent-"..., iov_len=134}, {iov_base="<?xml version=\"1.0\" encoding=\"is"..., iov_len=345}], 2) = 479
setsockopt(7, SOL_TCP, TCP_CORK, [0], 4) = 0
ioctl(7, FIONREAD, [0])                 = 0
read(7, 0x5606cf6ceaf0, 4159)           = -1 EAGAIN (Resource temporarily unavailable)
epoll_ctl(6, EPOLL_CTL_ADD, 7, {EPOLLIN|EPOLLERR|EPOLLHUP, {u32=7, u64=7}}) = 0
accept4(4, 0x7ffee411bc10, [112], SOCK_CLOEXEC|SOCK_NONBLOCK) = -1 EAGAIN (Resource temporarily unavailable)

檢查 lighttpd 錯誤日誌以查看是否有任何跟踪。

檢查您的 SELinux 設置,並可能暫時禁用強制測試。

這是一個可能相關的舊問題: https ://serverfault.com/questions/335571/selinux-causes-permission-denied-when-starting-lighttpdfastcgi-via-upstart

如果您對更詳細的細節感興趣,您還可以strace使用 lighttpd 程序來查看在您發出請求時哪些系統呼叫失敗了。

引用自:https://unix.stackexchange.com/questions/579318