'Django - Serving production using Apache2
Days I struggle with this.
I have a django webapp written in Python 3.10 that works fine when launched using pipenv shell then python3.10 manage.py runserver. Also, doing python3.10 app/wsgi.py quits without any error.
Now I want it to be served by Apache2. Here is my conf file:
Listen 8000
<VirtualHost *:8000>
ServerAdmin webmaster@localhost
DocumentRoot /opt/app
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
LogLevel Debug
Alias /static /opt/app/static
<Directory /opt/app/static>
Require all granted
</Directory>
<Directory /opt/app/app>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /opt/app/app/wsgi.py
WSGIDaemonProcess app python-home=/opt/app python-path=/opt/app/app:/usr/local/lib/python3.10:/usr/local/lib/python3.10/lib-dynload:/usr/local/lib/python3.10/site-packages
WSGIProcessGroup app
</VirtualHost>
Results in HTTP 500 when navigating to my website.
Apache2 error logs:
[core:notice] AH00094: Command line: '/usr/sbin/apache2'
[wsgi:info] mod_wsgi (pid=25232): Attach interpreter ''.
[wsgi:info] mod_wsgi (pid=25232): Adding '/opt/app/app' to path.
[wsgi:info] mod_wsgi (pid=25232): Adding '/usr/local/lib/python3.10' to path.
[wsgi:info] mod_wsgi (pid=25232): Adding '/usr/local/lib/python3.10/lib-dynload' to path.
[wsgi:info] mod_wsgi (pid=25232): Adding '/usr/local/lib/python3.10/site-packages' to path.
[wsgi:info] [remote 192.168.3.110:37296] mod_wsgi (pid=25232, process='app', application=''): Loading Python script file '/opt/app/app/wsgi.py'.
[wsgi:error] [remote 192.168.3.110:37296] mod_wsgi (pid=25232): Failed to exec Python script file '/opt/app/app/wsgi.py'.
[wsgi:error] [remote 192.168.3.110:37296] mod_wsgi (pid=25232): Exception occurred processing WSGI script '/opt/app/app/wsgi.py'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error] File "/opt/app/app/wsgi.py", line 13, in <module>
[wsgi:error] from django.core.wsgi import get_wsgi_application
[wsgi:error] File "/usr/local/lib/python3.10/site-packages/django/__init__.py", line 1, in <module>
[wsgi:error] from django.utils.version import get_version
[wsgi:error] File "/usr/local/lib/python3.10/site-packages/django/utils/version.py", line 1, in <module>
[wsgi:error] import datetime
[wsgi:error] File "/usr/local/lib/python3.10/datetime.py", line 1185
[wsgi:error] def __new__(cls, year, week, weekday, /):
[wsgi:error] ^
[wsgi:error] SyntaxError: invalid syntax
Content of my wsgi.py file :
import sys
import os
from django.core.wsgi import get_wsgi_application
sys.path.append('/opt/app')
sys.path.append('/opt/app/app')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'vpo.settings')
application = get_wsgi_application()
Any help would be welcome.
Solution 1:[1]
The python parameter syntax with "/"
def __new__(cls, year, week, weekday, /):
is new since Python 3.8: https://docs.python.org/3/whatsnew/3.8.html#positional-only-parameters
You need to use the same python version to compile mod_wsgi for Apache that you use in the virt.environment. Please post the message that you get in error.log when you start Apache. Most certainly it is not Python 3.10.
update:
- First Possibility:
this is the way I installed mod_wsgi with Python3.10 on Ubuntu:
$ cd /var/www/django_project/
$ python3.10 -m venv env
$ source env/bin/activate
$ pip install mod_wsgi # this installed automatically the right version
$ mod_wsgi-express module-config # console output to be added to /etc/apache2/apache2.conf
LoadModule wsgi_module "/var/www/django_project/env/lib/python3.10/site-packages/mod_wsgi/server/mod_wsgi-py310.cpython-310-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/django_project/env"
so far Apache is running well in dev environment! I never tried that in production with Python 3.10
- Second Possibility: Check if you really need Python 3.10 for your project.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
