Apache2没有写文件的权限[Errno 13]权限被Flask Python拒绝了

问题描述 投票:0回答:1

首先有一些详细信息

所以我用Flask做了一个小的Web应用程序。从理论上讲,只要有人请求或访问该网站,它都应该获取IP。我已经完成了所有工作(在Windows上我的代码可以完美运行),但是我安装了Flask并将我的项目移到装有Apache2的Linux服务器上。 Ive配置了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及其权限进行写操作?

注意我的flask(python代码)所在的文件夹与ips的记录完全不同+我使用Ubuntu,并且我没有更改任何有关文件权限的内容,甚至直接通过root运行(我知道我不应该这样做,但仅用于测试非常小的项目)

这就是我能给你们的一切

感谢所有回复

json python-3.x linux flask apache2
1个回答
0
投票

尝试一下:

sudo chown -R www-data:www-data /opt/iplogs/

Apache2用户www-data不允许操作此文件。

© www.soinside.com 2019 - 2024. All rights reserved.