Python Forum

Full Version: Trouble deploying Django (project folder, wsgi.py and libapache2-mod-wsgi)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I’m having some issues deploying Django.

Here is my site: https://daniel496.agency/

I’m running a Digital Ocean Droplet with Ubuntu 14.04 and Python v3.4 with Django 2.0.2 installed.

In this post first I share what errors I am encountering, then what I think is the issue is and then what I’ve tried to resolve the issue. At the end of this post are my configuration files like ssl.conf and wsgi.py.

Before I begin, I think the issue could be that my project folder on my local machine is named: first_project_attemp. But on my remote server, the project folder is named, cel2fah. I copied the contents locally to my remote host and now trying to iron out the issues.

Here I go. If you access my site linked to above, right now it is showing:

Quote:Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Apache/2.4.7 (Ubuntu) Server at daniel496.agency Port 443

Here are the last ~50 lines of my apache error log file: https://pastebin.com/UPsTmf28

I’m thinking that there could be a potential conflict between libapache2-mod-wsgi and libapache2-mod-wsgi-py3. I encountered a similar issue a few weeks ago. There are apparently still traces of libapache2-mod-wsgi (for Python 2.7) still flagged as active on my system given that $ sudo dpkg -s libapache2-mod-wsgi prints:

Quote:Package: libapache2-mod-wsgi
Status: deinstall ok config-files
Priority: optional
Section: httpd
Installed-Size: 242
Maintainer: Ubuntu Developers <[email protected]>
Architecture: amd64
Source: mod-wsgi
Version: 3.4-4ubuntu2.1.14.04.2
Config-Version: 3.4-4ubuntu2.1.14.04.2
Provides: httpd-wsgi
Depends: libc6 (>= 2.14), libpython2.7 (>= 2.7), apache2-api-20120211, python (>= 2.7), python (<< 2.8)
Conffiles:
/etc/apache2/mods-available/wsgi.load 06d2b4d2c95b28720f324bd650b7cbd6 obsolete
/etc/apache2/mods-available/wsgi.conf c4ca5be35d0820b5d5cc2892097b476b obsolete
Description: Python WSGI adapter module for Apache
The mod_wsgi adapter is an Apache module that provides a WSGI (Web Server
Gateway Interface, a standard interface between web server software and
web applications written in Python) compliant interface for hosting Python
based web applications within Apache. The adapter provides significantly
better performance than using existing WSGI adapters for mod_python or CGI.
.
This package provides module for Python 2.X.
Homepage: http://www.modwsgi.org/
Original-Maintainer: Debian Python Modules Team <[email protected]>

However when I go to remove it, it says it’s not installed:

Error:
$ sudo apt remove libapache2-mod-wsgi Reading package lists... Done Building dependency tree Reading state information... Done Package 'libapache2-mod-wsgi' is not installed, so not removed 0 upgraded, 0 newly installed, 0 to remove and 44 not upgraded. $
More to the point, when invoking this as root:

Quote:apache2ctl -t -D DUMP_MODULES

...it lists what modules apache2 has loaded. And libapache2-mod-wsgi (for Python version 2) is included. So I Google ‘how to remove apache modules’ which turns up this SO post: https://stackoverflow.com/questions/9288...he2-module
So I set out to rename my libapache2-mod-wsgi.so (for Python version 2) from my apache2 modules directory inside /usr/lib. I see a symlink mod_wsgi.so which refers to mod_wsgi.so-3.4:

Quote:<user>@<host>:/usr/lib/apache2/modules$ ls -la mod_wsgi*
lrwxrwxrwx 1 root root 15 Nov 18 2014 mod_wsgi.so -> mod_wsgi.so-3.4
-rw-r--r-- 1 root root 174448 Nov 18 2014 mod_wsgi.so-3.4
<user>@<host>:/usr/lib/apache2/modules$

So is my Apache serving the libapache2-mod-wsgi module for python2 or just for python3 or what?

What else can you people determine could be the issue here?

Here is my wsgi.py:

"""
WSGI config for first_project_attempt project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""

import os
import sys
from django.core.wsgi import get_wsgi_application

sys.path.append('/home/tranq/cel2fah/cel2fah')
# NEW:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cel2fah.settings")

# OLD:
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "first_project_attempt.settings")

application = get_wsgi_application()
And my ssl.conf:

Quote:<IfModule mod_ssl.c>
<VirtualHost *:443>

ServerAdmin [email protected]
ServerName daniel496.agency
ServerAlias www.daniel496.agency
# DocumentRoot /var/www/html/daniel496.agency/public_html

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

# Django project
Alias /static /home/tranq/cel2fah/static
<Directory /home/tranq/cel2fah/static>
Require all granted
</Directory>

<Directory /home/tranq/cel2fah/cel2fah>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

WSGIDaemonProcess cel2fah2 python-home=/home/tranq/cel2fah/venv
WSGIProcessGroup cel2fah2
WSGIScriptAlias / /home/tranq/cel2fah/cel2fah/wsgi.py

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/daniel496.agency/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/daniel496.agency/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/daniel496.agency/chain.pem

</VirtualHost>
</IfModule>

Thanks for your attention.
Quote:Before I begin, I think the issue could be that my project folder on my local machine is named: first_project_attemp. But on my remote server, the project folder is named, cel2fah. I copied the contents locally to my remote host and now trying to iron out the issues.

With that piece of information, I believe you are correct.

If you have simply copied the project from your local machine to the remote server, but changed the name of the project I would suggest creating a new project instead. Look inside your settings.py and you might discover references to the old project name, search for things like WSGI_APPLICATION and ROOT_URLCONF
these would all need to be changed and even then there may be references in other files of your project to the old name.

you could using something like grep or the newer https://github.com/ggreer/the_silver_searcher
to search for occurrences, and then use an editor like vim to replace these.
Hi @knackwurstbagel!

Thanks for your reply. I took your advice by taking a completely different approach.

I deleted both my remote Django project and local Django project. This time I started a fresh with a new Django project from scratch on my local machine. It runs and behaves beautifully in my development environment. Then I mirrored my new local Django project onto my remote server using rsync.

I changed my apache sites-available ssl configuration file to reflect my new project directory and file names. My new ssl config looks like this:
Quote:<IfModule mod_ssl.c>
<VirtualHost *:443>

ServerAdmin <email>
ServerName daniel496.agency
ServerAlias www.daniel496.agency
# DocumentRoot /var/www/html/daniel496.agency/public_html

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

# Django project
Alias /static /home/<user>/TheGreatWork/static
<Directory /home/<user>/TheGreatWork/static>
Require all granted
</Directory>

<Directory /home/<user>/TheGreatWork/TheGreatWork>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

WSGIDaemonProcess TheGreatWork python-home=/home/<user>/TheGreatWork/venv
WSGIProcessGroup TheGreatWork
WSGIScriptAlias / /home/<user>/TheGreatWork/TheGreatWork/wsgi.py

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/daniel496.agency/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/daniel496.agency/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/daniel496.agency/chain.pem

</VirtualHost>
</IfModule>

At this point I’ve ruled out the possibility that the issue has something to do with the Python version 2.7-based libapache2-mod-wsgi because I purged my system of that library. All that exists now is libapache2-mod-wsgi-py3.

My settings.py includes declaration of ALLOWED_HOSTS as [‘daniel496.agency’,’www.daniel496.agency’,]

Here is my site:
https://daniel496.agency/

Still showing:

Quote:Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.

The last 6 lines of my apache error log are as follows:
Quote:[Thu Mar 15 14:54:23.203156 2018] [:error] [pid 17841] [remote 50.101.151.249:20263] mod_wsgi (pid=17841): Target WSGI script '/home/<user>/TheGreatWork/TheGreatWork/wsgi.py' cannot be loaded as Python module.
[Thu Mar 15 14:54:23.203261 2018] [:error] [pid 17841] [remote 50.101.151.249:20263] mod_wsgi (pid=17841): Exception occurred processing WSGI script '/home/<user>/TheGreatWork/TheGreatWork/wsgi.py'.
[Thu Mar 15 14:54:23.203349 2018] [:error] [pid 17841] [remote 50.101.151.249:20263] Traceback (most recent call last):
[Thu Mar 15 14:54:23.203472 2018] [:error] [pid 17841] [remote 50.101.151.249:20263] File "/home/<user>/TheGreatWork/TheGreatWork/wsgi.py", line 12, in <module>
[Thu Mar 15 14:54:23.203492 2018] [:error] [pid 17841] [remote 50.101.151.249:20263] from django.core.wsgi import get_wsgi_application
[Thu Mar 15 14:54:23.203551 2018] [:error] [pid 17841] [remote 50.101.151.249:20263] ImportError: No module named 'django'

So the issue has to do with line 12 of my wsgi.py configuration. Here is wsgi.py and pay particular attention to line 12:

"""
WSGI config for TheGreatWork project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""

import os
import sys
from django.core.wsgi import get_wsgi_application

sys.path.append('/home/<user>/TheGreatWork/TheGreatWork')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "TheGreatWork.settings")

application = get_wsgi_application()
I’m not sure how line 12 could be improved because I only really partially understand what is going on here. All I understand is that line 22 is referring to this file /usr/local/lib/python3.4/dist-packages/django/core/wsgi.py on my system and then from this file, calling the get_wsgi_application function. Here are the complete contents of this small wsgi.py file:

import django
from django.core.handlers.wsgi import WSGIHandler


def get_wsgi_application():
    """
    The public interface to Django's WSGI support. Return a WSGI callable.

    Avoids making django.core.handlers.WSGIHandler a public API, in case the
    internal WSGI implementation changes or moves in the future.
    """
    django.setup(set_prefix=False)
    return WSGIHandler()
I’m lost. The Apache web server error log is pointing to this function inside the wsgi module. What is the problem here? If the Apache error log is referring to wsgi.py as it appears on my root system instead of where it should be, inside my venv, then could the problem be the configuration of my daniel496.agency-le-ssl.conf? I have it listed above and it is not clear to me what the issue is.

I have consulted this guide to help with deploying Django.

Thanks for your attention.
Quote:[Thu Mar 15 14:54:23.203551 2018] [:error] [pid 17841] [remote 50.101.151.249:20263] ImportError: No module named 'django'

If you source /home/<user>/TheGreatWork/venv/bin/activate
what does the command python -m django --version say?

When you rsynced everything, did you also rsync the venv?
Quote:I’m not sure how line 12 could be improved because I only really partially understand what is going on here. All I understand is that line 22 is referring to this file /usr/local/lib/python3.4/dist-packages/django/core/wsgi.py on my system and then from this file, calling the get_wsgi_application function. Here are the complete contents of this small wsgi.py file:

That location would indicate a system wide django module, is it installed within the venv. Sounds like you might have forgoten to install django to the venv.
Quote:When you rsynced everything, did you also rsync the venv?

I did exclude my venv when I rsync’ed my local project to my remote server. Here is the command I invoked:

$ rsync -vaz --exclude='venv/' --stats /home/<user>/Dropbox/TECH/python/2018/TheGreatWork/ <user>@theinitiate.agency:/home/<user>/TheGreatWork/

I previously already created a virtual environment from inside my project directory on my remote server. But I did forget to actually install Django. Ha Ha. Thanks for pointing that out, @knackwurstbagel. So I installed Django moments ago with my virtual environment activated.

Quote:If you source /home/<user>/TheGreatWork/venv/bin/activate, what does the command python -m django --version say?

Now it says:

(venv) <user>@<hostname>:~/TheGreatWork$ python -m django --version
2.0.3

But my site still shows: “Internal Server Error”. See here: https://daniel496.agency/

Now my apache2 error log shows some new messages:
Error:
[Fri Mar 16 20:41:29.023194 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] mod_wsgi (pid=2426): Target WSGI script '/home/<user>/TheGreatWork/TheGreatWork/wsgi.py' cannot be loaded as Python module. [Fri Mar 16 20:41:29.023348 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] mod_wsgi (pid=2426): Exception occurred processing WSGI script '/home/<user>/TheGreatWork/TheGreatWork/wsgi.py'. [Fri Mar 16 20:41:29.023443 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] Traceback (most recent call last): [Fri Mar 16 20:41:29.023572 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "/home/<user>/TheGreatWork/TheGreatWork/wsgi.py", line 17, in <module> [Fri Mar 16 20:41:29.023589 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] application = get_wsgi_application() [Fri Mar 16 20:41:29.023666 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "/home/<user>/TheGreatWork/venv/lib/python3.4/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application [Fri Mar 16 20:41:29.023682 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] django.setup(set_prefix=False) [Fri Mar 16 20:41:29.023754 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "/home/<user>/TheGreatWork/venv/lib/python3.4/site-packages/django/__init__.py", line 19, in setup [Fri Mar 16 20:41:29.023779 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) [Fri Mar 16 20:41:29.023855 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "/home/<user>/TheGreatWork/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 56, in __getattr__ [Fri Mar 16 20:41:29.023871 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] self._setup(name) [Fri Mar 16 20:41:29.023939 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "/home/<user>/TheGreatWork/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 43, in _setup [Fri Mar 16 20:41:29.023955 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] self._wrapped = Settings(settings_module) [Fri Mar 16 20:41:29.024024 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "/home/<user>/TheGreatWork/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 106, in __init__ [Fri Mar 16 20:41:29.024040 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] mod = importlib.import_module(self.SETTINGS_MODULE) [Fri Mar 16 20:41:29.024116 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "/home/<user>/TheGreatWork/venv/lib/python3.4/importlib/__init__.py", line 109, in import_module [Fri Mar 16 20:41:29.024132 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] return _bootstrap._gcd_import(name[level:], package, level) [Fri Mar 16 20:41:29.024181 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "<frozen importlib._bootstrap>", line 2254, in _gcd_import [Fri Mar 16 20:41:29.024231 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "<frozen importlib._bootstrap>", line 2237, in _find_and_load [Fri Mar 16 20:41:29.024280 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked [Fri Mar 16 20:41:29.024330 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed [Fri Mar 16 20:41:29.024378 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "<frozen importlib._bootstrap>", line 2254, in _gcd_import [Fri Mar 16 20:41:29.024427 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "<frozen importlib._bootstrap>", line 2237, in _find_and_load [Fri Mar 16 20:41:29.024476 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked [Fri Mar 16 20:41:29.024538 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] ImportError: No module named 'TheGreatWork'
What is Apache trying to say here?

Thanks again.
I think
Quote:[Fri Mar 16 20:41:29.024538 2018] [:error] [pid 2426] [remote 174.95.101.30:54727] ImportError: No module named 'TheGreatWork'
is saying that its looking for the module 'TheGreatWork' but its not in the sys path

I seem to recal solving this problem once by adjusting my sys.path.append to include the project directory. I dont have a current install to directly reference but you can try changing

""" this """
sys.path.append('/home/<user>/TheGreatWork/TheGreatWork')
""" To this """
sys.path.append('/home/<user>/TheGreatWork')
Whats probably causing this is the interpreter hitting

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "TheGreatWork.settings")
So It says ok I need "TheGreatWork.settings" and then it looks on the sys path for it.
os.environ deals with a string that represents your environment variables.
As per your suggestion, @knackwurstbagel, I adjusted my sys.path.append to include the project directory. I removed the second level project directory, so at that line it says 'TheGreatWork/' instead of 'TheGreatWork/TheGreatWork/'. The Apache Internal Server Error is now gone! Thank you, my friend.

Here is my site: https://daniel496.agency/
As you can see, now Django is throwing a new error message.

It is pointing towards the fact that it is looking for my templates folder but can't find it. Originally it was looking for the default inside my venv, but right away I noticed the 'templates' variable was missing from my inside my INSTALLED_APPS list inside settings.py. So I appended 'templates' to that list there but I am still getting the same error.

If it helps, here is a screenshot of my project tree:

[Image: fSoTmOP.jpg]
I have a few ideas of what might be happening, as mentioned I don't currently have a working project to test or compare to but what I can do is make two suggestions:

First, try moving the templates folder to within the innermost TheGreatWork folder like so:

cd /home/<user>/TheGreatWork
mv templates ./TheGreatWork/
A different suggestion would be to change settings.py

TEMPLATES = [
    {
        """This"""
        'APP_DIRS': True,
        """To This"""
        'APP_DIRS': False,
    },
]
My reasoning behind this is you currently are not using any apps, you are just using the projects folder for everything. That setting would tell Django weather to look under each app folder. See Django Documentation on Templates

Let me know if either of these two suggestions makes any difference. Id suggest the first one before the second.
Thanks for your advice.
I moved the templates folder down the next level from TheGreatWork/ to TheGreatWork/TheGreatWork/.
I also flipped the switch for APP_DIRS from True to False.
Still getting Debug errors: https://daniel496.agency/
Any other ideas?
Thanks again.

Here is part of the debug traceback:

Python Path:	
['/home/<user>/TheGreatWork/venv/lib/python3.4',
 '/home/<user>/TheGreatWork/venv/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/<user>/TheGreatWork/venv/lib/python3.4/lib-dynload',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/<user>/TheGreatWork/venv/lib/python3.4/site-packages',
 '/home/<user>/TheGreatWork/']
Looks like Django is pointing to Python inside my venv properly most of the time. There are two instances where Python is being referred to the root system, like /usr/lib/python3.4. Is this still an issue?