0

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?

Ashkan Kazemi
  • 1,077
  • 8
  • 26
  • You might have a dangling connection to the database file (sqlite) which is causing the issue. Check to see if all processes are closed and try again.. – karthikr May 24 '15 at 18:12
  • i have closed the mod_wsgi process many times and nothing changed, once i even restarted the vps. I don't think that's the problem. what do you suggest i open and close? @karthikr – Ashkan Kazemi May 24 '15 at 18:14
  • and `apache reload` ? – karthikr May 24 '15 at 18:15
  • @karthikr i actually use sudo service apache2 restart but i suppose they are the same thing – Ashkan Kazemi May 24 '15 at 18:16
  • Yes, the same thing .. Also, did you see this post: http://stackoverflow.com/questions/21054245/attempt-to-write-a-readonly-database-django-w-selinux-error may be that is the issue ? – karthikr May 24 '15 at 18:20
  • @karthikr yes indeed i have, but this issue does not have an accepted answer, and i did try the solution suggested but it made the website completely unavailable, so i dont think that works! – Ashkan Kazemi May 24 '15 at 18:26
  • 1. What error do you get in the browser when trying to access a static file (not found, forbidden, etc.)? 2. Do you have anything relevant in the Apache logs (access.log and error.log)? – mimo May 24 '15 at 18:49
  • @Tony i don't get an error, just that there is no css and image loaded, and when i try to open them from the source i get a 404! also, there is nothing useful in apache logs, but i will check again, and if there were any, i would post it, but i have checked it many times and i doubt there is any – Ashkan Kazemi May 24 '15 at 18:52
  • Why the heck do you have Python libraries and the wsgi.py file in /var/www? /var/www/ should only contain things that may be handed on directly to the client (static files and media files). – mimo May 24 '15 at 18:53
  • @Tony btw, you can visit the site at setakshop.ir:8000 to see for yourself – Ashkan Kazemi May 24 '15 at 18:53
  • @Tony I know that that'z somehow the problem here, but blame it on digital ocean guys for their tutorial, here : https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps – Ashkan Kazemi May 24 '15 at 18:55
  • In the 8080 version of the site, you are missing trailing slashes in the Alias directives. See http://httpd.apache.org/docs/2.2/mod/mod_alias.html for more details. Does that solve the problem? – mimo May 24 '15 at 19:10
  • @Tony I added some new info and a log if you are interested – Ashkan Kazemi May 24 '15 at 19:10
  • @Tony i added the trailing but it did not solve the problem. – Ashkan Kazemi May 24 '15 at 19:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78646/discussion-between-tony-and-ashkan-kzme). – mimo May 24 '15 at 19:21
  • The directory a SQLite database is in needs to be writable to user that the application runs as, not just the database file. – Graham Dumpleton May 25 '15 at 09:10
  • @GrahamDumpleton I actually chmoded the directory to 777. but the problem persists. ofcourse, the directory before the sqllite directory is owned by root, like every other directory before, in and outside these directory zone, since it is in /var/www/ ! – Ashkan Kazemi May 25 '15 at 17:03
  • Are you running on a system which has SELinux enabled? The profile for Apache may prevent write access to that part of the file system – Graham Dumpleton May 25 '15 at 20:51
  • @GrahamDumpleton I don't have selinux sir. but i have updated my question, so please read the updates – Ashkan Kazemi May 26 '15 at 04:02
  • You have to get it straight as to whether you intend to use the main Apache installation or mod_wsgi-express. They are different things and if you are trying to have both running they will interfere with each other if trying to use the same port. Which one are you trying to use? – Graham Dumpleton May 26 '15 at 08:54
  • @GrahamDumpleton I want apache with mod_wsgi. I read about other things for deploying django like nginx and gunicorn and i thought that since mod_wsgi is the application server ( i suppose! ) so i should run it with some command like i did with gunicorn. So please help me to run it with apache and mod_wsgi as the application server, thanks – Ashkan Kazemi May 26 '15 at 10:34
  • You are missing the distinction, they are both Apache with mod_wsgi. In one case you are using the system Apache and configuration and the second (which is mod_wsgi-express) is allowing you to run it up on the command line with an auto generated configuration. If you want to use the main system Apache, completely ignore mod_wsgi-express. Ignoring mod_wsgi-express entirely, go back to your main system Apache and rerun it properly and state clearly your current problem with it. – Graham Dumpleton May 26 '15 at 11:19
  • @GrahamDumpleton thank you so much. there was not much info about mod_wsgi-express online, but this made it crystal clear. – Ashkan Kazemi May 26 '15 at 11:49
  • You have read my blog posts about it and the PyPi page? – Graham Dumpleton May 26 '15 at 22:00
  • @GrahamDumpleton i have not read your blog post, but i read the pypi age. I will post my problem into a new question. thank you for your help. – Ashkan Kazemi May 27 '15 at 04:57
  • @GrahamDumpleton here is the description of my new problem: http://stackoverflow.com/questions/30473634/sqlite-error-attempt-to-write-to-a-read-only-database – Ashkan Kazemi May 27 '15 at 05:10

0 Answers0