first of all, I have tried my best to find a solution for my problem in stackoverflow and all over internet and over the past two days I haven't been able to find a solution.
I have a django project I want to deploy.
I am using apache2 and mod_wsgi for deployment. Now there are many problems here that I'm facing!
the first one is, the static files are not being served. no css, no image! I don't know that this is due to a permission problem, or to wrong apache config and whatever I've tried, has not worked yet!
for starters, here's my apache config:
<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>
I've gone over it many times and I haven't found anything odd in there, but I'm completely new at this, so if there is even anything wrong, please tell me.
Secondly, and I think the most important problem is here, my permissions are not set right! For deployment, I followed a tutorial on digital ocean, which said to set your project in /var/www/ and I did and this directory is owned by root so whatever I want to do I have to use sudo. I tried many things, I ran mod_wsgi in root mode like this :
sudo mod_wsgi-express start-server wsgi.py --user www-data --group www-data
then when it didn't work, I ran mod_wsgi in normal mode, again nothing. I tried changing the owner of the project folder to www-data by running :
sudo chown www-data:www-data
and again it didn't work! then I changed the owner to myself (ashkan), didn't work. then I changed the owner of the /www in /var/www/ to www-data, nothing happened again!
so here is a summary of my folders and files permissions:
directory : /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
directory : /freshcopy/django-oscar/sites/
drwxrwxr-x 12 www-data www-data 4096 May 26 00:31 sandbox
directory /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
thanks in advance.
Update 1:
I found the permissions for the previous version of db.sqlite file which was 644 and set that file to that permission and now i get an internal error on every page of the site, but the log is very clear now, every error for getting css files is because the db file is read-only:
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
update 2 : I moved the correct project files ( without permissions touched ) to another directory, with normal privileges, and I called it fresh copy, I changed all the settings for this directory change and I've updated my data. but still, whenever I enter setakshop.ir:8000 i get a server error and the log file shows what I've posted above, cannot write to a readonly database. I ran the project with
sudo mod_wsgi-express start-server wsgi.py --user www-data --group www-data
But another surprising fact is that I stopped apache but the site is still available at setakshop.ir:8000 . why is this happening?