0

I am running the saleor django app under a virtualenv with apache as a non-privileged user.

Actually getting everything going was fairly straightforward, but one bit is confusing me.

The /static/admin/ portion of the site is not being served.

I have looked at the deployment docs and other advice but have found nothing that helps, so far.

My /static/ directory is being served just fine. I'm sure it's something very obvious to the non-django-noob, but that's what I am. I'm not certain that it's copacetic to alias a sub-directory in another aliased directory. I'd rather not resort to symlinks.

# dev-site.conf

WSGIPythonPath/home/admin/project/saleor:/home/admin/project/venv/lib/python2.7/site-packages

<VirtualHost *:80>
        ServerName example.com
        ServerAdmin email@example.com
        DocumentRoot "/home/admin/project"

        WSGIDaemonProcess example.com python-path=/home/admin/project/saleor:/home/admin/project/venv/lib/python2.7/site-packages
        WSGIProcessGroup example.com
        WSGIScriptAlias / /home/admin/project/saleor/wsgi.py

        Alias /media/ /home/admin/project/media/
        <Directory /home/admin/project/media/>
                Require all granted
        </Directory>

        Alias /favicon.ico /home/admin/project/saleor/static/images/favicon.ico
        Alias /robots.txt /home/admin/project/saleor/static/robots.txt
        Alias /static/ /home/admin/project/saleor/static/
        <Directory /home/admin/project/saleor/static/>
                Require all granted
        </Directory>

        Alias /static/admin/ /home/admin/project/venv/lib/python2.7/site-packages/django/contrib/admin/static/admin/
        <Directory /home/admin/project/venv/lib/python2.7/site-packages/django/contrib/admin/static/admin/>
                Require all granted
        </Directory>

        <Directory /home/admin/project>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I include my saleor/wsgi.py for smarts.

# wsgi.py

python

import os
import sys
import site

site.addsitedir("/home/admin/project/venv/lib/python2.7/site-packages")
os.environ.setdefault("SECRET_KEY", "br549")
os.environ.setdefault("MYSQL_DB_URL", "mysql://dbuser:doublesecret@dbuser-dev.example.com:3306/saleor")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "saleor.settings")

project = "/home/admin/project/saleor"
workspace = os.path.dirname(project)
sys.path.append(workspace)

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Community
  • 1
  • 1
lysdexia
  • 1,786
  • 18
  • 29

2 Answers2

0

You shouldn't attempt to get Apache to serve directly from the static directory of either your own apps or the admin app. Instead, follow the documentation and use the collectstatic command to collect the static files for all apps in one place and serve from there.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I read that portion of the [docs](https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/#serving-the-admin-files), too. Unfortunately, I could not get it to work. I'm missing something. – lysdexia Dec 11 '15 at 22:08
  • [This](https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#django-admin-collectstatic) is the bit of the docs you should be following. If it's not working, post any error, and the relevant parts of your settings.py. – Daniel Roseman Dec 11 '15 at 22:15
  • I think I finally have my head around what is *supposed* to happen. I didn't realize one had to do "django-admin collectstatic" (which is refusing to see my DJANGO_SETTINGS_MODULE=saleor.static, so I'm guessing there is some extra gymkhana I'm going to have to go through to make that work in a virtual env) to copy everything somewhere magical for django to use. I'm going to have to let this rest before I impress my co-workers with my command of profanity. – lysdexia Dec 11 '15 at 22:52
  • Apparently I'm having troubles running collectstatic because I don't have django globally installed and it can't find my DJANGO_SETTINGS_MODULE setting without doing a relative import. This seems like a hell of a lot of precursors just to set a path. – lysdexia Dec 11 '15 at 23:02
  • No, you don't need (and shouldn't have) Django installed globally. Use `./manage.py collectstatic` from inside your project. – Daniel Roseman Dec 12 '15 at 09:12
  • Thank you for clarifying that I need to be running manage.py, rather than django-admin like the docs describe. I am not hip to this platforms shibboleths yet. – lysdexia Dec 12 '15 at 18:42
0

I did not understand the documentation on serving the admin files or django-admin collectstatic.

The documentation for serving the admin files indicated that if one has "django.contrib.staticfiles" enabled in the INSTALLED_APPS portion of settings.py, then it is recommended the STATICFILES_DIRS list in settings.py shall contain the paths to the "static" directory (django/contrib/admin/static/admin) provided by django for it's development server.

One is told to use the collectstatic management command which will physically copy the files to a "static" directory in what you will configure as ServerRoot in your sitename.conf file for apache to serve.

My problem was pretty much a noob error. I followed the instructions without knowing that there is some sort of received wisdom about what actually is used to run the "collectstatic" command.

Rather than

django-admin collecstatic

one needs to (in my case, ymmv)

python manage.py collectstatic

When invoking collectstatic with django-admin, I got the maddening error:

ImportError: No module named saleor.settings

which was very confusing to me, as I had set that enivronment variable in both my .env file and in my wsgi.py file. I figured this might be some sort of quirk with virtualenv. Setting this variable on the command line resulted in:

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Which led me down a very disgusting rabbit-hole vis. my virtualenv setup and every config file I had ever edited. They were fine, fortunately. The actual invocation was with

$ python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

    /home/admin/project/static

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel:

Which then proceeds to magically copy what I need (I hope!) into my project/static directory.

My noob mistake was not reading django-admin and manage.py first, thus discovering that the convenience script has a convenience script. RIMSHOT

Another bit of confusion (for me, anyway) is the assertion that one should not serve static files from the "app" or the "admin app". A simpler way for me to think about it is: "one must run a script that appears either wrap or replicate rsync, thus copying static files from myapp/static and ~/project/venv/lib/python2.7/contrib/admin so that apache will not be able to play silly buggers with important files". This does NOT mean that one does not put a path to /static/ in the sitename.conf file. There is no magic here, just copying static files.

Therefore:

WSGIPythonPath /home/admin/project/saleor:/home/admin/project/venv/lib/python2.7/site-packages

<VirtualHost *:80>
        ServerName example.com
        ServerAdmin email@example.com
        DocumentRoot "/home/admin/project"

        Alias /media/ /home/admin/project/media/
        <Directory /home/admin/project/media/>
                Require all granted
        </Directory>

        Alias /static/ /home/admin/project/static/
        <Directory /home/admin/project/static/>
                Require all granted
        </Directory>

        WSGIDaemonProcess example.com python-path=/home/admin/project/saleor:/home/admin/project/venv/lib/python2.7/site-packages
        WSGIProcessGroup example.com
        WSGIScriptAlias / /home/admin/project/saleor/wsgi.py

        <Directory /home/admin/project>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I now have static files being served from ~/project/static/ directory!

/me wipes away sweat.

Thanks Daniel!

lysdexia
  • 1,786
  • 18
  • 29