centos 7 Apache wsgi写入文件权限被拒绝

时间:2016-04-03 14:07:46

标签: django apache wsgi permission-denied

在我的site2中,Django应用程序Apache wsgi没有对/ var / www / site2中文件的写入权限,即使:

  • 我已经知道apache来自/ var / www / site2目录下的所有文件
  • 我赋予所有权利(chmod -R 777)。
  • 且SELinux已停用。
  • 以下是简报状态:

    [root@nuc www]# httpd -v
    Server version: Apache/2.4.6 (CentOS)
    Server built:   Nov 19 2015 21:43:13
    
    [root@nuc www]# ls -l
    total 4
    drwxr-xr-x. 2 root   root      6 Nov 19 23:43 cgi-bin
    drwxr-xr-x. 2 root   root      6 Apr  2 15:02 html
    drwxrwxrwx. 6 apache apache 4096 Apr  3 16:14 site2
    
    [root@nuc site2]# ls -l
    ...
    drwxrwxrwx. 5 apache apache  4096 Apr  2 15:23 blog
    -rwxrwxrwx. 1 apache apache 95232 Apr  3 16:09 db.sqlite3
    drwxrwxrwx. 5 apache apache  4096 Apr  2 16:54 home
    -rwxrwxrwx. 1 apache apache   248 Mar 19 13:56 manage.py
    drwxrwxrwx. 5 apache apache  4096 Apr  3 16:13 pock
    drwxrwxrwx. 2 apache apache  4096 Apr  3 14:45 site2
    
    
    [root@nuc www]# sestatus
    SELinux status:                 disabled
    
    httpd.conf ...
    #-------------------------------------------------
    WSGIScriptAlias / /var/www/site2/site2/wsgi.py
    WSGIPythonPath /var/www/site2/
    
    Alias /static/ /var/www/site2/blog/static/
    Alias /static2/ /var/www/site2/home/static/
    Alias /static3/ /var/www/site2/pock/static/
    
    <Directory /var/www/site2/blog/static/>
    Require all granted
    </Directory>
    <Directory /var/www/site2/home/static/>
    Require all granted
    </Directory>
    <Directory /var/www/site2/pock/static/>
    Require all granted
    </Directory>
    
    <Directory /var/www/site2/site2/>
    <Files wsgi.py>
    #Order deny,allow
    Allow from all
    Require all granted
    </Files>
    </Directory>
    #-------------------------------------------------
    

    仍然在/ var / log / httpd / error_log:

    [Sun Apr 03 13:43:17.389404 2016] [:error] [pid 4753] /var/www/site2
    [Sun Apr 03 13:43:17.390560 2016] [:error] [pid 4753] Internal Server Error: /pock/test/
    [Sun Apr 03 13:43:17.390593 2016] [:error] [pid 4753] Traceback (most recent call last):
    [Sun Apr 03 13:43:17.390602 2016] [:error] [pid 4753]   File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    [Sun Apr 03 13:43:17.390609 2016] [:error] [pid 4753]     response = self.process_exception_by_middleware(e, request)
    [Sun Apr 03 13:43:17.390634 2016] [:error] [pid 4753]   File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    [Sun Apr 03 13:43:17.390643 2016] [:error] [pid 4753]     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    [Sun Apr 03 13:43:17.390650 2016] [:error] [pid 4753]   File "/var/www/site2/pock/views.py", line 57, in test
    [Sun Apr 03 13:43:17.390656 2016] [:error] [pid 4753]     f = open('test.txt', 'w')
    [Sun Apr 03 13:43:17.390662 2016] [:error] [pid 4753] IOError: [Errno 13] Permission denied: 'test.txt'
    

    解决

    感谢Volodymyr的建议我做了更多的挖掘工作,我找到了最终的方式。

    在/ home目录中移动项目对我来说不起作用:从Apache获取“Forbidden”。

    我试图写的文件需要绝对路径:

    f = open('/var/www/site2/test.txt', 'w')
    

    否则wsgi python会尝试在“/”根文件系统中编写它,所以解释了上面的错误

    最后我做了-R 744 / var / www / site2,它起作用,写入文件和数据库。

    似乎我已经做了其他错误,但无论如何,它现在有效。

    1 个答案:

    答案 0 :(得分:0)

    尝试:

    chown -R root:apache /Path/To/You/WebSitePackage
    

    我在Centos 7上使用httpd部署Django时出现此问题。我选择其他方式:在home / user /目录中创建WEBSITE Package,然后

    chown -R apache:apache Path/To/WEBSITEPackage
    chmod -R 770 Path/To/WEBSITEPackage
    

    您可以尝试:

    chown -R user:root Path/To/WEBSITEPackage
    chmod -R 770 Path/To/WEBSITEPackage
    
    chown -R apache:root Path/To/WEBSITEPackage
    chmod -R 770 Path/To/WEBSITEPackage
    

    其他词语apos on centos 7需要root privelegies