我是網頁設計新手,我正在嘗試為一些 python 代碼設置 Apache Web 界面。我正在運行一個測試 python 腳本,該腳本僅在 /var/www/html/ 中打開一個名為 output.txt 的新文件并嘗試寫入它。當網頁加載時,我用來shell_exec('/usr/bin/python3 /var/www/html/ptest.py');運行我的 python 腳本。python 腳本確實運行并輸出一些打印消息,但嘗試打開 output.txt 失敗并顯示: Traceback (most最近的調用最后): File "/var/www/html/ptest.py", line 8, in f=open ("/var/www/html/output.txt", "w") PermissionError: [Errno 13] 權限被拒絕: '/var/www/html/output.txt'。如果文件已經存在并且我打開讀取,它也會失敗。我已經確認該腳本正在由 apache 運行getpass.getuser(),并且我已經為 /var/www/html/ 嘗試了盡可能多的不同權限組合,包括 777。我已將 apache 設置為每個目錄的組/var/ 到 html/。我已經嘗試使用 777 權限提前創建該文件。我已經檢查過 /var/ 到 html/ 具有組執行權限。我嘗試創建并使用另一個完全由 apache 擁有的文件夾。我查看了 apache 指令,看看是否有我需要的指令,但我還沒有找到。我在下面包含了 python 腳本和 php 頁面的代碼。編輯:我嘗試使用 apache 作為 apache 運行 ptest.py su -s /bin/bash apache。ptest.py 以這種方式成功運行,因此問題似乎不是與 apache 用戶/組關聯的權限。ptest.py#!/usr/bin/python3import sysimport getpassimport ossys.stderr = sys.stdoutprint(getpass.getuser())print(os.getgid())f = open("/var/www/html/output.txt", "w")f.write("banana")f.close()print("I wrote banana, which is a berry")香蕉.php<html> <head></head> <body> <h2>Welcome to the Program Test</h2> <?php echo "Created Command", "<br>"; $output = shell_exec('/usr/bin/python3 /var/www/html/ptest.py'); echo "Executed Command", "<br>"; echo $output, "<br>"; echo "End of output", "<br>"; $output = shell_exec('ls /var/www/html/'); echo $output; ?> </body></html>
- 1 回答
- 0 關注
- 120 瀏覽
添加回答
舉報
0/150
提交
取消