0

In my apache.conf file (see code below°), VirtualHost port 80 configuration works fine. However, in the port 443, the Alias /admin/media/ /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/ shows two issues:

  • my settings.py has : STATIC_URL = '/m/' and ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
  • my admin directory is : /home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/

When I put Alias /m/admin/ /home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/ it shows error 403 access forbidden.

When I add:

<Directory "/home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/">
Require all granted
</Directory>
<Directory "/home/user/project/">
<Files django.wsgi>
Require all granted
</Files>
</Directory>

It shows error 404 not found with error_log saying :[wsgi:error] Target WSGI script '/home/user/project/django.wsgi' does not contain WSGI application 'application'

Could you please help me configure my apache virtualhost port 443 to server django admin app?

°My apache.conf file is as below:

#The following two directories must be both readable and writable by apache
WSGISocketPrefix /var/run/apache2/wsgi

#WSGIPythonEggs /var/python/eggs

# the following directory must be readable by apache
WSGIPythonHome /home/user/project/virtual-environment/local/

# NOTE: all urs below will need to be adjusted if
# settings.FORUM_SCRIPT_ALIAS is anything other than empty string (e.g. = 'forum/')
# this allows "rooting" forum at http://domain-name/forum, if you like

#replace default ip with real IP address
<VirtualHost *:80>
    ServerAdmin you@domain-name
    DocumentRoot /home/user/project/
    ServerName domain-name

    # aliases to serve static media directly
    Alias /m/ /home/user/project/static/
    Alias /upfiles/ /home/user/project/askbot/upfiles/
    <DirectoryMatch "/home/user/project/askbot/skins/([^/]+)/media">
        Require all granted
    </DirectoryMatch>
    <Directory "/home/user/project/askbot/upfiles">
        Require all granted
    </Directory>
    <Directory "/home/user/project/ask-skins">
        Require all granted
     </Directory>

     <Directory "/home/user/project//static">
        Require all granted
     </Directory>
     #must be a distinct name within your apache configuration
    WSGIDaemonProcess askbot2 python-path=/home/user/project:/home/user/project/virtua-environment/lib/python2.7/site-packages
    WSGIProcessGroup askbot2

    WSGIScriptAlias / /home/user/project//django.wsgi

    <Directory "/home/user/project/">
        <Files django.wsgi>
            Require all granted
        </Files>
    </Directory>

    # make all admin stuff except media go through secure connection
    <LocationMatch "/admin(?!/media)">
    RewriteEngine on
        RewriteRule /admin(.*)$ https://domain-name/admin$1 [L,R=301]
        </LocationMatch>
    CustomLog /var/log/apache2/domain-name/access_log common
    ErrorLog /var/log/apache2/domain-name/error_log
    LogLevel debug
</VirtualHost>

#again, replace the IP address
<VirtualHost *:443>
    ServerAdmin you@domain-name
    DocumentRoot /home/user/project/
    ServerName domain-name
    <LocationMatch "^(?!/admin)">
        RewriteEngine on
        RewriteRule django.wsgi(.*)$ http://domain-name$1 [L,R=301]
    </LocationMatch>
     SSLEngine on
     #your SSL keys
     SSLCertificateFile /etc/httpd/ssl.crt/server.crt
     SSLCertificateKeyFile /etc/httpd/ssl.key/server.key

    Alias /admin/media/ /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/

    WSGIScriptAlias / /home/user/project/django.wsgi
     CustomLog /var/log/httpd/askbot/access_log common
     ErrorLog /var/log/httpd/askbot/error_log
</VirtualHost>

My django.wsgi file is as below :

import os
import sys
import time
import traceback
import signal
current_directory = os.path.dirname(__file__)
parent_directory = os.path.dirname(current_directory)
module_name = os.path.basename(current_directory)

sys.path.append(parent_directory)
sys.path.append(current_directory)
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % module_name

from django.core.wsgi import get_wsgi_application
try:
    application = get_wsgi_application()
    print 'WSGI without exception'
except Exception:
    print 'handling WSGI exception'
    # Error loading applications
    if 'mod_wsgi' in sys.modules:
        traceback.print_exc()
        os.kill(os.getpid(), signal.SIGINT)
        time.sleep(2.5)
Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
novis
  • 15
  • 6
  • Is there a reason you are not defining ``STATIC_ROOT`` and running ``python manage.py collectstatic`` to gather all static files into the one location and then referencing that with ``Alias``. You would not normally reference static files from inside of the Python install directories. See https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files – Graham Dumpleton Mar 16 '17 at 19:56
  • Regarding the admin static files, I have checked and they are located at "/home/user/project/static/admin/" but when I do "Alias /m/admin /home/user/project/static/admin" they aren't rendered. Please advice – novis Mar 17 '17 at 08:24
  • @GrahamDumpleton: I have been testing ""Alias /m/admin /home/user/project/static/admin" but the admin app continue to display without formatting. Shall I open a new question for this issue? – novis Mar 18 '17 at 09:08

1 Answers1

0

You have a number of things wrong or sub optimal with your configuration.

The first is that when using mod_wsgi daemon mode and have only the one application, it is recommended you force use of the main Python interpreter context. This avoids problems with some third party Python modules that do not work with Python sub interpreters. To do this, add:

WSGIApplicationGroup %{GLOBAL}

to both VirtualHost definitions.

The second is that your SSL VirtualHost is not delegating the WSGI application to run in the same mod_wsgi daemon process group as non SSL VirtualHost defines. You need to add to the SSL VirtualHost:

WSGIProcessGroup askbot2

There is no need to add a WSGIDaemonProcess directive to the SSL VirtualHost as it is piggy backing off that setup in non SSL VirtualHost to avoid multiple copies of application. Right now you have an even bigger problem in that the SSL variant is running in embedded mode, with is even less desirable scenario.

The third is that when setting up a Python virtual environment, you should use the python-home option to refer to the root of the Python virtual environment and not use python-path to refer to site-packages. For details on that see:

The fourth problem is how you are dealing with Django initialisation failing. There is no need to to use the try/except around creating the WSGI application object. What you should do if using mod_wsgi daemon mode is use a modern mod_wsgi version (not likely the ancient version your OS packages provide), and set the startup-timeout option on the WSGIDaemonProcess directive. That option will automatically force a restart of process if WSGI script file fails to load within a certain time. For details on that option see:

If you are not going to do that, you at least need to add a raise to the except part so that the exception is propagated back to mod_wsgi so it knows the WSGI script file couldn't be loaded. if you don't, mod_wsgi thinks the WSGI script file was loaded fine, but then when it looks for application can't find it and so generates a 404 response and the error your see.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • Regarding the third point above, as I have "WSGIPythonHome /home/user/project/virtual-environment/local/" set at the top of my apache.conf file, is it ok yo keep python-path? – novis Mar 18 '17 at 09:04
  • Either the ``WSGIPythonHome`` value was wrong, or the ``python-path`` value was wrong. One says virtual environment is at ``virtual-environment/local`` and the other at ``virtual-environment``. Best thing to do is get rid of ``WSGIPythonHome`` and replace it with ``WSGIRestrictEmbedded On`` to ensure Python doesn't get initialised in normal Apache child processes. Then, use ``python-home`` (not ``python-path`` and ``site-packages``) to reference virtual environment root. See the virtual environment docs linked. – Graham Dumpleton Mar 18 '17 at 09:35