【发布时间】:2020-04-21 00:03:30
【问题描述】:
先说几个细节
所以我用 Flask 做了一个小的 Web 应用程序。 从理论上讲,只要有人请求或访问该网站,它就应该获取 IP。 我已经完成了所有工作(在 Windows 上我的代码运行完美),但我安装了 Flask 并将我的项目转移到我安装了 Apache2 的 Linux 服务器上。我已经配置了 Apache,以便它处理 Flask Web 应用程序的请求。 一切都很好,就像我的模板加载得很好,但是记录 ip 的部分不起作用。 我认为获取IP没有问题,将它存储在一个json文件中。 每次我尝试运行时,我的网站上都会出现 500 错误。
Apache Error Log : [Errno 13] Permission denied '/opt/iplogs/iplog.json'
Python 代码
def writeToJSONFile(path, fileName, data):
filePathNameWExt = path + fileName + '.json'
with open(filePathNameWExt, 'a') as fp:
json.dump(data, fp, indent=2)
fp.close()
@app.route("/")
def getIP():
visit = {}
ip_visit = request.remote_addr
now = datetime.now()
request_time = now.strftime("%d/%m/%Y %H:%M:%S")
visit["IP"] = str(ip_visit)
visit["date"] = str(request_time)
writeToJSONFile("/opt/iplogs/", "iplog", visit) # WHEN i comment this function out there is no 500 error
return render_template("home.html")
主要问题
因此,在开发环境中的 Windows 中它工作正常,但在 linux 中,当我让 Flask 运行而没有 apache 处理其请求时 只有当我通过 Apache 运行网站时,我才会收到错误“权限被拒绝” 所以它必须与apache及其写入权限有关?
请注意,我的烧瓶(python 代码)所在的文件夹与记录 ips 的位置完全不同 + 我使用的是 Ubuntu,我没有更改任何关于文件权限的东西,我什至通过 root 运行(我知道我不应该这样做,但它只是为了测试一个非常小的项目)
我能给你们的就这些了
感谢大家的回复
【问题讨论】:
-
你是如何配置Apache2的?是简单的 ProxyPass 还是其他?描述您如何让您的应用运行。
-
我刚刚用 mod_wsgi 配置了它,这样我的 python 脚本就可以执行了。 000-default.conf (apache) 文件:pastebin.com/GCwGjkHg;和 flask.wsgi 文件:pastebin.com/xVxvsxqv– bot_diyar 7 分钟前删除
标签: json python-3.x linux flask apache2