在生产服务器上使用django日志记录时出现Apache WSGI权限错误
这里是一些软件信息 Django 1.8.1 Apache2 软呢帽21 日志输出错误在生产服务器上使用django日志记录时出现Apache WSGI权限错误,django,apache,wsgi,selinux,Django,Apache,Wsgi,Selinux,这里是一些软件信息 Django 1.8.1 Apache2 软呢帽21 日志输出错误 mod_wsgi (pid=8272): Target WSGI script '/var/www/anime/anime/wsgi.py' cannot be loaded as Python module. mod_wsgi (pid=8272): Exception occurred processing WSGI script '/var/www/anime/anime/wsgi.py'. Trace
mod_wsgi (pid=8272): Target WSGI script '/var/www/anime/anime/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=8272): Exception occurred processing WSGI script '/var/www/anime/anime/wsgi.py'.
Traceback (most recent call last):
File "/usr/lib64/python3.4/logging/config.py", line 557, in configure
handler = self.configure_handler(handlers[name])
File "/usr/lib64/python3.4/logging/config.py", line 725, in configure_handler
result = factory(**kwargs)
File "/usr/lib64/python3.4/logging/__init__.py", line 999, in __init__
StreamHandler.__init__(self, self._open())
File "/usr/lib64/python3.4/logging/__init__.py", line 1023, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: '/var/www/anime/log/info.log'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/www/anime/anime/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/opt/virtualenvs/django_project/lib64/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/opt/virtualenvs/django_project/lib64/python3.4/site-packages/django/__init__.py", line 17, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/opt/virtualenvs/django_project/lib64/python3.4/site-packages/django/utils/log.py", line 86, in configure_logging
logging_config_func(logging_settings)
File "/usr/lib64/python3.4/logging/config.py", line 789, in dictConfig
dictConfigClass(config).configure()
File "/usr/lib64/python3.4/logging/config.py", line 565, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '/var/www/anime/log/info.log'
我已经搜索了所有可能的解决方案,但没有一个有效。因此我怀疑它与SELinux设置有关?如果是,有人能告诉我需要将哪个标志设置为true吗?在阅读SELinux之后,我找到了此权限错误的解决方案。我希望它能帮助在RHEL linux下的生产服务器上部署时遇到类似情况的其他人 基本上运行ls-Z命令可以显示以下内容
drwxr-xr-x. apache apache unconfined_u:object_r:httpd_sys_content_t:s0 log
文件夹被标记为httpd\u sys\u content\t,这不允许httpd对文件夹具有写访问权限。因此,我们需要将此标签更改为httpd\u sys\u rw\u content\t
首先,我们需要向fcontext添加一个条目,以告知SELinux将在此文件夹中创建的文件的默认标签是什么
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/path/to/directory(/.*)?"
这将向fcontext文件(/etc/selinux/targeted/contexts/files/file_contexts.local)添加一个条目
接下来,我们需要使用restorecon更新文件夹中文件的所有标签
sudo restorecon -R -v /path/to/directory
现在,与django日志记录相关的权限错误将从httpd error_log=)中消失。可能在StackOverflow之外搜索。我已设法找出问题的原因。因此,当我设置了0时,它就工作了。当然,这不是真正的方法。所以我需要为apache设置httpd_sys_content_t rw。你知道做这件事的命令是什么吗?我以前见过这样的命令,但在网上找不到。
sudo restorecon -R -v /path/to/directory