apache不在django项目上提供静态文件

时间:2015-05-24 17:42:39

标签: django apache mod-wsgi

首先,我已经尽力在stackoverflow和整个互联网上找到我的问题的解决方案,过去两天我一直无法找到解决方案。

我有一个我想要部署的django项目。

我正在使用apache2和mod_wsgi进行部署。现在我面临很多问题!

第一个是,静态文件没有被提供。没有CSS,没有图像!我不知道这是由于权限问题,还是错误的apache配置以及我尝试过的任何问题,还没有成功!

对于初学者来说,这是我的apache配置:

<VirtualHost *:80>
    WSGIDaemonProcess main python-path=/var/www/setak/setakenv/main/ashop/ashop:/var/www/setak/setakenv/lib/python2.7/site-packages
    WSGIProcessGroup main
    WSGIScriptAlias / /var/www/setak/setakenv/main/ashop/ashop/ashop/wsgi.py
    ServerAdmin admin@setakshop.ir
    ServerName www.setakshop.ir

    Alias /media/ /var/www/setak/setakenv/main/ashop/ashop/static/media/
    Alias /static/ /var/www/setak/setakenv/main/ashop/ashop/static/

    <Directory /var/www/setak/setakenv/main/ashop/ashop/static>
        Order allow,deny
        allow from all
    </Directory>

    <Directory /var/www/setak/setakenv/main/ashop/ashop/static/media>
        Order allow,deny
        allow from all
    </Directory>    

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>


<VirtualHost *:8080>
    WSGIDaemonProcess setak python-path=/home/ashkan/freshcopy/django-oscar/sites/sandbox:/usr/local/lib/python2.7/site-packages 
    WSGIProcessGroup setak 
    WSGIScriptAlias / /home/ashkan/freshcopy/django-oscar/sites/sandbox/wsgi.py 
    ServerAdmin admin@setakshop.ir 
    ServerName shop.setakshop.ir 
    DocumentRoot /var/www/ 

    Alias /media/ /home/ashkan/freshcopy/django-oscar/sites/sandbox/public/media/ 
    Alias /static/ /home/ashkan/freshcopy/django-oscar/sites/sandbox/public/static/ 

    <Directory /home/ashkan/freshcopy/django-oscar/sites/sandbox/public/static>
        Order allow,deny
        allow from all
    </Directory>

    <Directory /home/ashkan/freshcopy/django-oscar/sites/sandbox/public/media>
        Order allow,deny
        allow from all
    </Directory>    

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

我已经多次过去了,我在那里找不到任何奇怪的东西,但我对此完全是新的,所以如果有任何错误,请告诉我。

其次,我认为最重要的问题在这里,我的权限设置不正确!为了部署,我遵循了数字海洋的教程,它说在/ var / www /中设置你的项目,我做了,这个目录由root拥有,所以无论我想做什么,我都必须使用sudo。我尝试了很多东西,我在root模式下运行mod_wsgi,如下所示:

sudo mod_wsgi-express start-server wsgi.py --user www-data --group www-data

然后当它不起作用时,我在正常模式下运行mod_wsgi,再没有。我尝试通过运行:

将项目文件夹的所有者更改为www-data
sudo chown www-data:www-data

再一次没用!然后我改变了主人自己(ashkan),没有工作。然后我将/ var / www /中的/ www的所有者更改为www-data,再没有发生任何事情!

所以这里是我的文件夹和文件权限的摘要:

目录:/ freshcopy / django-oscar / sites / sandbox

-rw-rw-r-- 1 ashkan   ashkan      382 Jan 30 11:54 README.rst
-rw-rw-r-- 1 ashkan   ashkan        0 Jan 30 11:54 __init__.py
drwxrwxr-x 4 ashkan   ashkan     4096 Feb  6 15:15 apps
-rw-rw-r-- 1 www-data www-data 446464 May 26 00:31 db.sqlite
drwxrwxr-x 7 ashkan   ashkan     4096 Jan 30 11:54 deploy
drwxrwxr-x 2 ashkan   ashkan     4096 Jan 30 11:54 fixtures
drwxrwxr-x 2 ashkan   ashkan     4096 Feb 22 00:14 i18n
drwxrwxr-x 3 ashkan   ashkan     4096 Feb 23 21:45 locale
drwxr-xr-x 2 ashkan   ashkan     4096 May 26 08:33 logs
-rwxrwxr-x 1 ashkan   ashkan      242 May 26 00:22 manage.py
lrwxrwxrwx 1 ashkan   ashkan       10 May 25 23:15 oscar -> i18n/oscar
drwxrwxr-x 4 ashkan   ashkan     4096 Jan 30 21:53 public
-rw-rw-r-- 1 ashkan   ashkan    14306 May 26 00:07 settings.py
-rw-r--r-- 1 root     root      10230 May 26 00:23 settings.pyc
-rw-rw-r-- 1 ashkan   ashkan    14876 Feb  6 14:26 settings.py~
-rw-rw-r-- 1 ashkan   ashkan      293 Jan 30 11:54 settings_mysql.py
-rw-rw-r-- 1 ashkan   ashkan      266 May 26 00:16 settings_postgres.py
-rw-rw-r-- 1 ashkan   ashkan      162 Jan 30 11:54 settings_sphinx.py
drwxrwxr-x 2 ashkan   ashkan     4096 Feb 23 21:45 static
drwxrwxr-x 3 ashkan   ashkan     4096 Jan 30 11:54 templates
-rwxrwxr-x 1 ashkan   ashkan     1114 Jan 30 11:54 test_migrations.sh
-rwxrwxr-x 1 ashkan   ashkan     1138 Jan 30 11:54 update_latest.sh
-rw-rw-r-- 1 ashkan   ashkan     1573 Jan 30 11:54 urls.py
-rw-rw-r-- 1 ashkan   ashkan     1427 Jan 30 21:53 urls.pyc
drwxr-xr-x 2 ashkan   ashkan     4096 Jan 30 20:58 whoosh_index
-rw-rw-r-- 1 ashkan   ashkan      778 May 26 00:23 wsgi.py

目录:/ freshcopy / django-oscar / sites /

drwxrwxr-x 12 www-data www-data    4096 May 26 00:31 sandbox

目录/ freshcopy

drwxrwxr-x 8 ashkan   ashkan       4096 May  7 19:38 django-oscar
-rw-rw-r-- 1 ashkan   ashkan   43499005 May 25 23:15 django-oscar.zip

提前感谢。

更新1:

我找到了以前版本的db.sqlite文件的权限,该文件是644并将该文件设置为该权限,现在我在网站的每个页面上都收到内部错误,但现在日志非常清楚,每个错误获取css文件是因为db文件是只读的:

ERROR 2015-05-24 20:03:06,774 base Internal Server Error: /static/oscar/css/1829574bee5b.css
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 87, in get_response
    response = middleware_method(request)
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/locale.py", line 31, in process_request
    request, check_path=check_path)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 188, in get_language_from_request
    return _trans.get_language_from_request(request, check_path)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 496, in get_language_from_request
    lang_code = request.session.get(LANGUAGE_SESSION_KEY, request.session.get('django_language'))
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py", line 60, in get
    return self._session.get(key, default)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py", line 175, in _get_session
    self._session_cache = self.load()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/db.py", line 29, in load
    self.create()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/db.py", line 41, in create
    self.save(must_create=True)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/db.py", line 64, in save
    obj.save(force_insert=must_create, using=using)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 589, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 617, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 698, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 731, in _do_insert
    using=using, raw=raw)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 921, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 921, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 485, in execute
    return Database.Cursor.execute(self, query, params)
OperationalError: attempt to write a readonly database

更新2:我将正确的项目文件(没有权限触及)移动到另一个目录,具有正常权限,我将其称为新副本,我更改了此目录更改的所有设置,我我已经更新了我的数据。但是,每当我输入setakshop.ir:8000时,我收到服务器错误,日志文件显示我上面发布的内容,无法写入只读数据库。我用

运行了这个项目
sudo mod_wsgi-express start-server wsgi.py --user www-data --group www-data

但另一个令人惊讶的事实是我停止了apache但该网站仍然可以在setakshop.ir:8000上找到。为什么会这样?

0 个答案:

没有答案