2012-07-09 205 views
6

我在一個Centos 6.2服務器上運行Django 1.4應用程序(運行apache,mysql,php),使用mod_wsgi與我的項目部署在一個虛擬ENV。該應用程序本身就是我在託管服務上使用了好幾年的應用程序,現在我正在部署在自己的rackspace雲服務器上。該應用的確切版本在其他地方運行良好,所以這個問題是關於我如何部署它的。這是我的第一個Python/Django的部署 - 我已經設置了數十個LAMP網站的前面,所以這是我的缺乏與Django的部署,這顯然是拖我熟悉的。Django/mod_wsgi OSError:[Errno 13] Permission denied:'static'when DEBUG = OFF

該應用在我的服務器上工作正常,在項目的settings.py文件中爲DEBUG = TRUE,但是當我將其更改爲FALSE時,網站的前端會產生[500]內部服務器錯誤。

我知道,在DEBUG設置爲OFF的情況下,apache現在通過mod_wsgi(mod_wsgi工作正常)提供我的靜態文件,這導致我相信我的配置中的某些內容阻止了這一點。我已經運行其填充在/myproject文件夾中的/static目錄./manage.py collectstatic命令。

我一直在這幾個星期,現在,閱讀儘可能多的部署指南,我能找到,但至今沒有喜悅。非常感謝您的幫助。

下面是我的項目的settings.py文件相關聲明:

############ settings.py ############# 

SITE_ROOT = os.path.realpath(os.path.dirname(__file__)) 
MEDIA_ROOT = '/opt/virtual/myproject/static/localtv/media/' 
MEDIA_URL = 'http://example.org/static/localtv/media/' 
STATIC_ROOT = '/opt/virtual/myproject/static/' 
STATIC_URL = 'http://example.org/static/' 

這裏是wsgi.py文件:在httpd.conf

############# wsgi.py ################# 
import os 
import sys 
import site 
site.addsitedir('/opt/virtual/myapp/lib/python2.7/site-packages') 

apache_configuration= os.path.dirname(__file__) 
project = os.path.dirname(apache_configuration) 

sys.path.append('/opt/virtual') 
sys.path.append('/opt/virtual/myproject') 

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' 

import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 

我的虛擬主機聲明看起來像這樣的:

############ virtual host declaration in httpd.conf ############## 
<VirtualHost *:80> 
ServerName example.org 
ServerAlias www.example.org 
DocumentRoot /opt/virtual/myproject 

Alias /robots.txt /opt/virtual/myproject/static/robots.txt 
Alias /favicon.ico /opt/virtual/myproject/static/favicon.ico 

AliasMatch ^/([^/]*\.css) /opt/virtual/myproject/static/styles/$1 

Alias /static/ /opt/virtual/myproject/static/ 
Alias /media/ /opt/virtual/myproject/static/media 
Alias /images /opt/virtual/myproject/static/images 

<Directory /opt/virtual/myproject/static> 
Order deny,allow 
Allow from all 
</Directory> 

<Directory /opt/virtual/myproject/static/media> 
Order deny,allow 
Allow from all 
</Directory> 

WSGIDaemonProcess example.org python-path=/opt/virtual/myapp/lib/python2.7/site-packages 
WSGIProcessGroup example.org 

WSGIScriptAlias//opt/virtual/myproject/application/wsgi.py 

<Directory /opt/virtual/myproject> 
<Files wsgi.py> 
Order allow,deny 
Allow from all 
</Files> 
</Directory> 

在/ ROOT我的.bashrc文件看起來是這樣的:

########### .bashrC################## 

# User specific aliases and functions 

alias rm='rm -i' 
alias cp='cp -i' 
alias mv='mv -i' 

# Source global definitions 
if [ -f /etc/bashrc ]; then 
    . /etc/bashrc 
fi 

# User specific aliases and functions 
alias python='/opt/bin/python2.7' 

export PYTHONPATH=/opt/virtual/myapp/lib/python2.7/site-packages:$PYTHONPATH 

最後,我error_log中顯示了回溯:

############### error_log ############### 
    [Mon Jul 09 09:21:13 2012] [error] <WSGIRequest 
    [Mon Jul 09 09:21:13 2012] [error] path:/, 
    [Mon Jul 09 09:21:13 2012] [error] GET:<QueryDict: {}>, 
    [Mon Jul 09 09:21:13 2012] [error] POST:<QueryDict: {}>, 
    [Mon Jul 09 09:21:13 2012] [error] 'DOCUMENT_ROOT': '/opt/virtual/myproject', 
    [Mon Jul 09 09:21:13 2012] [error] 'GATEWAY_INTERFACE': 'CGI/1.1', 
    [Mon Jul 09 09:21:13 2012] [error] 'HTTP_ACCEPT': "*/*", 
    [Mon Jul 09 09:21:13 2012] [error] 'HTTP_HOST': 'example.org', 
    [Mon Jul 09 09:21:13 2012] [error] 'HTTP_USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 
    [Mon Jul 09 09:21:13 2012] [error] 'PATH_INFO': u'/', 
    [Mon Jul 09 09:21:13 2012] [error] 'PATH_TRANSLATED': '/opt/virtual/myproject/application/wsgi.py/', 
    [Mon Jul 09 09:21:13 2012] [error] 'QUERY_STRING': '', 
    [Mon Jul 09 09:21:13 2012] [error] 'REMOTE_ADDR': '99.99.99.99', 
    [Mon Jul 09 09:21:13 2012] [error] 'REMOTE_PORT': '99999', 
    [Mon Jul 09 09:21:13 2012] [error] 'REQUEST_METHOD': 'GET', 
    [Mon Jul 09 09:21:13 2012] [error] 'REQUEST_URI': '/', 
    [Mon Jul 09 09:21:13 2012] [error] 'SCRIPT_FILENAME': '/opt/virtual/myproject/application/wsgi.py', 
    [Mon Jul 09 09:21:13 2012] [error] 'SCRIPT_NAME': u'', 
    [Mon Jul 09 09:21:13 2012] [error] 'SERVER_ADDR': '111.111.111.111', 
    [Mon Jul 09 09:21:13 2012] [error] 'SERVER_ADMIN': '[email protected]', 
    [Mon Jul 09 09:21:13 2012] [error] 'SERVER_NAME': 'example.org', 
    [Mon Jul 09 09:21:13 2012] [error] 'SERVER_PORT': '80', 
    [Mon Jul 09 09:21:13 2012] [error] 'SERVER_PROTOCOL': 'HTTP/1.0', 
    [Mon Jul 09 09:21:13 2012] [error] 'SERVER_SIGNATURE': '<address>Apache/2.2.15 (CentOS) Server at example.org Port 80</address>\\n', 
    [Mon Jul 09 09:21:13 2012] [error] 'SERVER_SOFTWARE': 'Apache/2.2.15 (CentOS)', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.application_group': 'example.org|', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.callable_object': 'application', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.handler_script': '', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.input_chunked': '0', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.listener_host': '', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.listener_port': '80', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.process_group': 'example.org', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.request_handler': 'wsgi-script', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.script_reloading': '1', 
    [Mon Jul 09 09:21:13 2012] [error] 'mod_wsgi.version': (3, 3), 
    [Mon Jul 09 09:21:13 2012] [error] 'wsgi.errors': <mod_wsgi.Log object at 0x7f34321aa530>, 
    [Mon Jul 09 09:21:13 2012] [error] 'wsgi.file_wrapper': <built-in method file_wrapper of mod_wsgi.Adapter object at 0x7f34320e4e40>, 
    [Mon Jul 09 09:21:13 2012] [error] 'wsgi.input': <mod_wsgi.Input object at 0x7f34320e02b0>, 
    [Mon Jul 09 09:21:13 2012] [error] 'wsgi.multiprocess': False, 
    [Mon Jul 09 09:21:13 2012] [error] 'wsgi.multithread': True, 
    [Mon Jul 09 09:21:13 2012] [error] 'wsgi.run_once': False, 
    [Mon Jul 09 09:21:13 2012] [error] 'wsgi.url_scheme': 'http', 
    [Mon Jul 09 09:21:13 2012] [error] 'wsgi.version': (1, 1)}> 
    [Mon Jul 09 09:21:13 2012] [error] ------------------------------------------------------------------------------- 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] mod_wsgi (pid=21520): Exception occurred processing WSGI script '/opt/virtual/myproject/application/wsgi.py'. 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] Traceback (most recent call last): 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__ 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  response = self.get_response(request) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/handlers/base.py", line 179, in get_response 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  response = self.handle_uncaught_exception(request, resolver, sys.exc_info()) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return callback(request, **param_dict) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  response = view_func(request, *args, **kwargs) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/views/defaults.py", line 33, in server_error 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return http.HttpResponseServerError(t.render(Context({}))) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 140, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return self._render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 134, in _render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return self.nodelist.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  bit = self.render_node(node, context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return node.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return compiled_parent._render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 134, in _render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return self.nodelist.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  bit = self.render_node(node, context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return node.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  result = block.nodelist.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  bit = self.render_node(node, context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  response = view_func(request, *args, **kwargs) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/views/defaults.py", line 33, in server_error 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return http.HttpResponseServerError(t.render(Context({}))) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 140, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return self._render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 134, in _render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return self.nodelist.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  bit = self.render_node(node, context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return node.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return compiled_parent._render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 134, in _render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return self.nodelist.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  bit = self.render_node(node, context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return node.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  result = block.nodelist.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  bit = self.render_node(node, context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return node.render(context) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/templatetags/compress.py", line 91, in render 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  rendered_output = compressor.output(self.mode, forced=forced) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/css.py", line 53, in output 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  ret.append(subnode.output(*args, **kwargs)) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/css.py", line 55, in output 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return super(CssCompressor, self).output(*args, **kwargs) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/base.py", line 221, in output 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  finished_content = self.handle_output(mode, filtered_content, forced) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/base.py", line 233, in handle_output 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  return output_func(mode, content, forced) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/base.py", line 245, in output_file 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  self.storage.save(new_filepath, ContentFile(content)) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/files/storage.py", line 45, in save 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  name = self._save(name, content) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/files/storage.py", line 168, in _save 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  os.makedirs(directory) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] File "/opt/virtual/myapp/lib/python2.7/os.py", line 157, in makedirs 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]  mkdir(name, mode) 
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] OSError: [Errno 13] Permission denied: '/opt/virtual/myproject/static/CACHE/css' 
+0

粗魯的方式:[chmod -R 777/opt/virtual/myproject/static /] – pinkdawn 2012-07-09 09:35:45

+1

您需要爲運行Django的用戶創建靜態目錄。 – 2012-07-09 09:35:59

+0

你的目錄應該可以被Apache用戶運行的用戶訪問/寫入,最可能的是www-data – Rohan 2012-07-09 09:43:27

回答

7

我一直在幾乎相同的條件下接收相同的錯誤,你(我正在運行Ubuntu,而不是CentOS)。

正如你注意到沒有,有DEBUG = FALSE運行的時候,你其實都是通過WSGI運行。這意味着,DEBUG = TRUE運行的時候,你實際上是使用用戶的權限,用DEBUG = FALSE,使用Apache的用戶權限運行時,運行時。 Apache使用的用戶是www-data

www-data既不是所有者也不是組擁有/var/www用戶英寸這意味着www-data被視爲other並且具有爲其他人設置的權限。

BAD解決方案,這將是做:

sudo chmod -R 777 /var/www/ 

這將使每個人都完全訪問一切/var/www/,這顯然是一個非常糟糕的主意。

另一個BAD的解決辦法是要做到:

sudo chown -R www-data /var/www/ 

這將所有者更改爲www-data,打開安全漏洞。

GOOD的解決辦法是:

sudo groupadd varwwwusers 
sudo adduser www-data varwwwusers 
sudo chgrp -R varwwwusers /var/www/ 
sudo chmod -R 770 /var/www/ 

這增加www-datavarwwwusers組,然後將其設置爲組/var/www/及其所有子文件夾。 chmod將爲所有者和組提供讀取,寫入,執行權限,同時阻止任何其他用戶訪問它。

+0

在** GOOD **解決方案中,不應該使用'''sudo usermod -a -G varwwwusers www-data'''而不是''sudo adduser www-data varwwwusers'''嗎? – maciek 2015-04-29 12:15:28

相關問題