I have a Django wagtail application. I can run it in debug and I can run it on a django server but if I try to run it on my Ubuntu 18.04 server using Apache2 WSGI I get "Internal Server Error". Apache log:
[Mon Jan 18 23:29:23.673557 2021] [mpm_event:notice] [pid 92324:tid 140613127453760] AH00491: caught SIGTERM, shutting down
Exception ignored in: <function BaseEventLoop.__del__ at 0x7fe301c1fc10>
Traceback (most recent call last):
File "/usr/lib/python3.8/asyncio/base_events.py", line 654, in __del__
NameError: name 'ResourceWarning' is not defined
Exception ignored in: <function Local.__del__ at 0x7fe301b95040>
Traceback (most recent call last):
File "/home/user/wow/lib/python3.8/site-packages/asgiref/local.py", line 96, in __del__
NameError: name 'TypeError' is not defined
Exception ignored in: <function Local.__del__ at 0x7fe301b95040>
Traceback (most recent call last):
File "/home/user/wow/lib/python3.8/site-packages/asgiref/local.py", line 96, in __del__
NameError: name 'TypeError' is not defined
Exception ignored in: <function Local.__del__ at 0x7fe301b95040>
Traceback (most recent call last):
File "/home/user/wow/lib/python3.8/site-packages/asgiref/local.py", line 96, in __del__
NameError: name 'TypeError' is not defined
[Mon Jan 18 23:29:24.194636 2021] [mpm_event:notice] [pid 92749:tid 139932982541376] AH00489: Apache/2.4.41 (Ubuntu) OpenSSL/1.1.1f mod_wsgi/4.6.8 Python/3.8 configured -- resuming normal operations
[Mon Jan 18 23:29:24.194764 2021] [core:notice] [pid 92749:tid 139932982541376] AH00094: Command line: '/usr/sbin/apache2'
[Mon Jan 18 23:29:31.468972 2021] [wsgi:error] [pid 92750:tid 139932932445952] WSGI without exception
my wsgi file:
import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "americanss.settings.production")
application = get_wsgi_application()
try:
application = get_wsgi_application()
print('WSGI without exception')
except Exception:
print('handing WSGI exception')
# Error loading applications
if 'mod_wsgi' in sys.modules:
traceback.print_exc()
os.kill(os.getpid(), signal.SIGINT)
time.sleep(2.5)
from what I have found, the exceptions raised happen and nobody seems to know why but most of the time it doesn't stop the app from running. In my case I get a 500 error and I can't tell if that is related to the traceback because no one else reports a 500 with these errors.
apactctl -M:
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 143.110.226.199. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
core_module (static)
so_module (static)
watchdog_module (static)
http_module (static)
log_config_module (static)
logio_module (static)
version_module (static)
unixd_module (static)
access_compat_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_core_module (shared)
authn_file_module (shared)
authz_core_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
filter_module (shared)
mime_module (shared)
mpm_event_module (shared)
negotiation_module (shared)
reqtimeout_module (shared)
rewrite_module (shared)
setenvif_module (shared)
socache_shmcb_module (shared)
ssl_module (shared)
status_module (shared)
wsgi_module (shared)
Related
Was able to create a working Django application and works fine in development (runserver). However, I'm currently stuck with my production build using Apache as the web server. Hope someone can help.
Have added the following lines in my httpd.conf file:
# ServerName localhost:80 # use this if you're running this on a VirtualBox VM or PC
ServerName localhost:8000
# Django Project
LoadFile "C:/Users/Anton/AppData/Local/Programs/Python/Python310/python310.dll"
LoadModule wsgi_module "C:/Users/Anton/Documents/Dad/PythonVirtual/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/Users/Anton/Documents/Dad/PythonVirtual"
WSGIPythonPath "C:/Users/Anton/Documents/Dad/Complere"
WSGIScriptAlias / "C:/Users/Anton/Documents/Dad/Complere/Complere/wsgi.py"
<Directory "C:/Users/Anton/Documents/Dad/Complere/Complere/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ "C:/Users/Anton/Documents/Dad/Complere/static/"
<Directory "C:/Users/Anton/Documents/Dad/Complere/static/">
Require all granted
`
Without the additional lines mentioned above, I can see the apache service running in background. However, once above lines have been added, the apache server is in stopped status. Below is the log file content.
Starting the 'Apache2.4' service
The 'Apache2.4' service is running.
pm_winnt:notice] [pid 8692:tid 408] AH00455: Apache/2.4.54 (Win64) mod_wsgi/4.9.4 Python/3.10 configured -- resuming normal operations
[Mon Nov 07 22:01:09.941909 2022] [mpm_winnt:notice] [pid 8692:tid 408] AH00456: Apache Lounge VS16 Server built: Jun 22 2022 09:58:15
[Mon Nov 07 22:01:09.941909 2022] [core:notice] [pid 8692:tid 408] AH00094: Command line: 'C:\\Apache24\\bin\\httpd.exe -d C:/Apache24'
[Mon Nov 07 22:01:09.945903 2022] [mpm_winnt:notice] [pid 8692:tid 408] AH00418: Parent: Created child process 6552
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'python'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\Apache24\\bin\\httpd.exe'
sys.base_prefix = 'C:\\Users\\Anton\\AppData\\Local\\Programs\\Python\\Python310'
sys.base_exec_prefix = 'C:\\Users\\Anton\\AppData\\Local\\Programs\\Python\\Python310'
sys.platlibdir = 'lib'
sys.executable = 'C:\\Apache24\\bin\\httpd.exe'
sys.prefix = 'C:\\Users\\Anton\\AppData\\Local\\Programs\\Python\\Python310'
sys.exec_prefix = 'C:\\Users\\Anton\\AppData\\Local\\Programs\\Python\\Python310'
sys.path = [
'C:\\Users\\Anton\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'.\\DLLs',
'.\\lib',
'C:\\Apache24\\bin',`
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x0000156c (most recent call first):
[Mon Nov 07 22:01:10.411286 2022] [mpm_winnt:crit] [pid 8692:tid 408] AH00419: master_main: create child process failed. Exiting.
`
``
I followed this guide exactly: https://www.codementor.io/#aswinmurugesh/deploying-a-django-application-in-windows-with-apache-and-mod_wsgi-uhl2xq09e
I am trying to deploy my django application for the first time ever using mod_wsgi and apache on windows 10.
As mentioned in the guide, I changed a few things:
wsgi_windows.py
activate_this = 'C:/pythonvenv/djangoproj/Scripts/activate'
# execfile(activate_this, dict(__file__=activate_this))
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('C:/pythonvenv/djangoproj/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('C:/pythonstuff/djangoproj')
sys.path.append('C:/pythonstuff/djangoproj/djangoproj')
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoproj.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoprog.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
httpd.conf i added the following two lines as given by: mod_wsgi-express module-config
LoadModule wsgi_module "c:/pythonvenv/djangoproj/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win32.pyd"
WSGIPythonHome "c:/pythonvenv/djangoproj"
httpd-vhosts.conf
# Virtual Hosts
#
# virtual SupervisionTool
<VirtualHost *:80>
ServerName localhost
WSGIPassAuthorization On
ErrorLog "logs/my_application.error.log"
CustomLog "logs/my_application.access.log" combined
WSGIScriptAlias / "C:\pythonstuff\djangoproj\djangoproj\wsgi_windows.py"
<Directory "C:\pythonstuff\djangoproj\djangoproj">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
Alias /static "C:/pythonstuff/djangoproj/djangoproj/static"
<Directory "C:/pythonstuff/djangoproj/djangoproj/static">
Require all granted
</Directory>
</VirtualHost>
# end virtual SupervisionTool
Everytime i try to run httpd.exe to start my webserver, it gives the following error and then doesn't start. The error is found in the logs.
[Mon Jul 06 19:31:33.542403 2020] [mpm_winnt:notice] [pid 19432:tid 748] AH00455: Apache/2.4.41 (Win32) PHP/7.3.12 mod_wsgi/4.7.1 Python/3.8 configured -- resuming normal operations
[Mon Jul 06 19:31:33.542403 2020] [mpm_winnt:notice] [pid 19432:tid 748] AH00456: Apache Lounge VS16 Server built: Aug 9 2019 16:32:28
[Mon Jul 06 19:31:33.542403 2020] [core:notice] [pid 19432:tid 748] AH00094: Command line: 'httpd.exe -d C:/wamp/bin/apache/apache2.4.41'
[Mon Jul 06 19:31:33.546388 2020] [mpm_winnt:notice] [pid 19432:tid 748] AH00418: Parent: Created child process 17612
Python path configuration:
PYTHONHOME = 'c:\pythonvenv\djangoproj'
PYTHONPATH = (not set)
program name = 'python'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\wamp\\bin\\apache\\apache2.4.41\\bin\\httpd.exe'
sys.base_prefix = 'c:\\pythonvenv\\djangoproj'
sys.base_exec_prefix = 'c:\\pythonvenv\\djangoproj'
sys.executable = 'C:\\wamp\\bin\\apache\\apache2.4.41\\bin\\httpd.exe'
sys.prefix = 'c:\\pythonvenv\\djangoproj'
sys.exec_prefix = 'c:\\pythonvenv\\djangoproj'
sys.path = [
'C:\\pythonvenv\\djangoproj\\Scripts\\python38.zip',
'c:\\pythonvenv\\djangoproj\\DLLs',
'c:\\pythonvenv\\djangoproj\\lib',
'C:\\wamp\\bin\\apache\\apache2.4.41\\bin',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00004a24 (most recent call first):
<no Python frame>
[Mon Jul 06 19:31:35.480078 2020] [mpm_winnt:crit] [pid 19432:tid 748] AH00419: master_main: create child process failed. Exiting.
I am not sure what went wrong... can anyone please advise? If not, is there any more hassle-free ways to deploy django on windows 10?
I am trying to install the mod_wsgi module in apache to deploy a django project.
I am using the pre-installed apache2 in ubuntu.
After I installed the module, I was asked to verify if apache is loading properly the module by executing apache2ctl -M:
comon#mylocal:~$ apache2ctl -M
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
core_module (static)
so_module (static)
watchdog_module (static)
http_module (static)
log_config_module (static)
logio_module (static)
version_module (static)
unixd_module (static)
access_compat_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_core_module (shared)
authn_file_module (shared)
authz_core_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
filter_module (shared)
mime_module (shared)
mpm_event_module (shared)
negotiation_module (shared)
setenvif_module (shared)
status_module (shared)
And it seems that there is no mod_wsgi so I found that I need to add LoadModule wsgi_module modules/mod_wsgi.so in the httpd.conf configure file in apache.
To know where this file is located, I ran: /usr/sbin/apache2 -V | grep SERVER_CONFIG_FILE AND I go:
comon#mylocal:~$ /usr/sbin/apache2 -V | grep SERVER_CONFIG_FILE
[Mon Jan 25 12:04:39.169687 2016] [core:warn] [pid 7614] AH00111: Config variable ${APACHE_LOCK_DIR} is not defined
[Mon Jan 25 12:04:39.169748 2016] [core:warn] [pid 7614] AH00111: Config variable ${APACHE_PID_FILE} is not defined
[Mon Jan 25 12:04:39.169766 2016] [core:warn] [pid 7614] AH00111: Config variable ${APACHE_RUN_USER} is not defined
[Mon Jan 25 12:04:39.169773 2016] [core:warn] [pid 7614] AH00111: Config variable ${APACHE_RUN_GROUP} is not defined
[Mon Jan 25 12:04:39.169790 2016] [core:warn] [pid 7614] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
[Mon Jan 25 12:04:39.171891 2016] [core:warn] [pid 7614:tid 139649371690880] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
[Mon Jan 25 12:04:39.172022 2016] [core:warn] [pid 7614:tid 139649371690880] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
[Mon Jan 25 12:04:39.172037 2016] [core:warn] [pid 7614:tid 139649371690880] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
AH00526: Syntax error on line 74 of /etc/apache2/apache2.conf:
Invalid Mutex directory in argument file:${APACHE_LOCK_DIR}
It seems that nothing is conigured in my apache2!
What I am doing wrong?
Use apachectl, not apache2/httpd executables directly, to pick up the customized environment variables sourced by apachectl on your distribution of apache.
I have some small django projects working on the same server with apache and mod_wsgi. I installed another one project with virtualenvironment and set settings for it in Apache conf file:
<VirtualHost XXX.XXX.XXX.XXX:81 >
ServerName mywebsite.com
CustomLog /var/www/httpd-logs/mywebsite.com.access.log combined
DocumentRoot /var/www/empirik/data/www/mywebsite.com
ErrorLog /var/www/httpd-logs/mywebsite.com.error.log
ServerAlias www.mywebsite.com
SuexecUserGroup empirik empirik
WSGIScriptAlias / /var/www/empirik/data/www/mywebsite.com/myproject/wsgi.py
WSGIDaemonProcess mywebsite.com python-path=/var/www/empirik/data/www/mywebsite.com/env/lib/python2.7/site-packages
<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
Order deny,allow
Allow from All
</Directory>
<Directory /var/www/empirik/data/www/mywebsite.com/myproject/static>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
When I try to load website server returns 500 error or another website's content in about 50% times and there are some strange errors in apache log files that I can't understand because it tries to load another website's project settings:
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] mod_wsgi (pid=32361): Exception occurred processing WSGI script '/var/www/empirik/data/www/mywebsite.com/myproject/wsgi.py'.
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] Traceback (most recent call last):
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] self.load_middleware()
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] self._setup()
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] self._wrapped = Settings(settings_module)
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] ImportError: Could not import settings 'anotherproject.settings' (Is it on sys.path?): No module named anotherproject.settings
Another question is why it still uses default system python but not from virtualenv? Please help!
You cannot use different python executeables under the same apache due to the fact that WSGIPythonHome is set for all virtual hosts globally (see docs). If WSGIPythonHome is not set - mod_wsgi will use system python.
It seems like you have not set up wsgi.py correctly. Your goal in wsgi.py is to manipulate sys.path correctly so that all that your project needs is available. Here's what helped me when I had same problems: apache server not using proper virtualenv with WSGI setting.
Hope that helps.
Your configuration is missing:
WSGIProcessGroup mywebsite.com
Without that, your WSGI application isn't even going to be delegated to the daemon process group. You can check with:
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Embedded_Or_Daemon_Mode
Once you have daemon mode being used properly, so long as you are using mod_wsgi 3.4, you can set Python home for that specific daemon process group using:
WSGIDaemonProcess mywebsite.com python-home=/var/www/empirik/data/www/mywebsite.com/env
That way you can simply point at the root of the Python virtual environment and it will be picked up.
You still need though to specify the python-path option as well to the parent directory of your Django project so it and the settings module can be found, which is the problem you are having. Thus likely you want:
WSGIDaemonProcess mywebsite.com \
python-home=/var/www/empirik/data/www/mywebsite.com/env \
python-path=/var/www/empirik/data/www/mywebsite.com
Now if running only that site in the daemon process group, set:
WSGIApplicationGroup %{GLOBAL}
to avoid issues with C extension modules that don't work in sub interpreters.
To be safer, instead of:
<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
Order deny,allow
Allow from All
</Directory>
you should use:
<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
<Files wsgi.py>
Order deny,allow
Allow from All
</Files>
</Directory>
That way if screw up Apache configuration, less risk of someone downloading your code and settings file.
And where you have:
<Directory /var/www/empirik/data/www/mywebsite.com/myproject/static>
Order deny,allow
Allow from all
</Directory>
you would appear to be missing the corresponding:
Alias /media /var/www/empirik/data/www/mywebsite.com/myproject/static
If you don't have an Alias for static media directory, Apache will not serve up files there.
Finally, since you want to force daemon mode and want to avoid embedded mode, set:
WSGIRestrictedEmbedded On
That way if you screw up configuration and things run in embedded mode by mistake as you currently are, you will get an error.
I created a virtualenv named 'pyapps' and installed pinax and django in it.I've installed apache 2 and mod_wsgi.I created a directory named 'apache' inside my django project(testproject) and put 'django.wsgi' file inside that directory.Here is the content of my wsgi file:
import os
import sys
# put the Django project on sys.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
os.environ['DJANGO_SETTINGS_MODULE'] = 'textpisodes.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Then I created a directory '/check/www' and put my project folder and pyapps folder inside '/check/www'.I chmoded 777 on /check/www.
Finally I created a virtual host,it's content is as follows:
<VirtualHost *:80>
ServerAdmin myemail
ServerName djangoserver
WSGIDaemonProcess textpisodes user=rajat threads=10 python-path=/check/www
WSGIProcessGroup textpisodes
WSGIScriptAlias / /check/www/textpisodes/apache/django.wsgi
<Directory /check/www/textpisodes/apache>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
Now when I try to access 'http://djangoserver',all I'm getting is Internal Server Error.Here are the contents of my apache log file.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] mod_wsgi (pid=4890): Target WSGI script '/check/www/textpisodes/apache/django.wsgi' cannot be loaded as Python module.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] mod_wsgi (pid=4890): Exception occurred processing WSGI script '/check/www/textpisodes/apache/django.wsgi'.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] File "/check/www/textpisodes/apache/django.wsgi", line 9, in <module>
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
You need to add your virtual environment path so that apache/python can find modules there.
Either put this in your apache configuration (outside of VirtualHost entry)
WSGIPythonPath /home/me/virtualenv/env1/lib/python2.7/site-packages
Or put it in the sys.path in your wsgi file.