Learning Django: Internal error 500, Invalid command 'PythonHandler' - python

I am trying to run a Django project "strona".
I did create a folder structure in ~/apps, created symbolic links and added in .httpd.conf of ~/apps:
SetHandler None
And in .httpd.conf of my public_html:
SetHandler python-program
PythonHandler django.core.handlers.wsgi # also tried modpython modpython
SetEnv DJANGO_SETTINGS_MODULE strona.settings
PythonPath "['/usr/local/lib/python2.6', '/home/USERNAME/apps'] + sys.path"
PythonDebug On
#PythonDebug Off
In error.log I get this:
[Date] [alert] [client [...]] /[...]/public_html/.htaccess: Invalid command 'PythonHandler', Perhaps misspelled or defined by a module not included in the server configuration
I do have Python and Djano, I can import wsgi module.
Why is it not working?

I don't know where you got the instruction to use PythonHandler, but it is completely wrong. That's only used for mod_python, which has been deprecated for years and is no longer supported by Django.
However, if you are just learning Django, you should not be trying to run it with Apache. That's for deployment: for learning, you should use the built-in development server, as described in the tutorial. When you are ready to set up Apache for real, you should read the very comprehensive deployment documentation to learn how to configure mod_wsgi, which is what you should actually be using.

Related

500 Error running Django, Apache2, PostGreSQL Ubuntu 18.04.3 LTS

Sorry if I seem lost, but I have very little experience deploying web applications and searching online isn't helping. As the title states, I am running Ubuntu, Apache2, PostgreSQL, and Django , -- and I repeatedly get error messages, most of which are either vague and/or have no clear solutions. Plus my ignorance on the specific interactions quicksands any attempt at finding a solution, so i will be very specific.
Installations:
apache2 libapache2-mod-wsgi-py3 -- for python 3
I can't remember if I installed django or if it automatically packaged with my PyCharm package.
PostgreSQL - works fine.
The Application:
It is a cloud computing platform, so it needs to receive files, store files, and render files.
Works fine with Django's web server attached.
has static files and needs to write to a media folder
settings.py:
- DEBUG = True
- WSGI_APPLICATION = 'example.wsgi.application'
-DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'example',
'USER': 'example',
'PASSWORD': 'example',
'HOST': 'localhost',
'PORT': '',
000-default.conf
<VirtualHost *:80>
ServerName FireAnts.localhost
ServerAlias www.FireAnts.localhost
DocumentRoot /var/www/FireAnts
<Directory /var/www/FireAnts/FireAnts>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess FireAnts python-path=/var/www/FireAnts python-home=/var/www/FireAnts/venv
WSGIProcessGroup FireAnts
WSGIScriptAlias / /var/www/FireAnts/FireAnts/wsgi.py
</VirtualHost>
ERRORS, per the log
Currently, i'm getting "No module named 'django'".
Frequently i get "populate" is not reentrant -- an issue which seems to have a million possible solutions.
Target WSGI cannot be loaded as Python module.
I temporarily got another script to work by deleting all lines writing to an error.txt file -- a file that exists for both packages and generates syntax errors. For some unexplained reason, it broke again with the "No module name django" failure. Also, i moved the entire package from an outside directory to the '/var/www/' (and modified the 000-default.conf file). that shouldn't generate any issues because the virtual environment traveled with the package and the database should still connect from the outside. But I assume this generated the 500 error before by writing to a file that no longer has write permissions. Could my database be generating the error some how (by blocking the connection)? If a user uploads a package later on, it will write to the media folder temporarily. Even if it didn't write yet, could this break it? Do any more modification need to be made when moving the package to the /var/www directory? I have no clue what could be generating these errors. It must be an apache configuraton error because it works with Django's web server, and only breaks with apache2...
Sorry for the mess and lack of organization; i just have no clue where I could have messed up. - but every attempted fix just breaks it again. I would appreciate any and all help.
You maybe missing out providing the WSGIDaemonProcess correctly in your default conf. Try giving as
WSGIDaemonProcess FireAnts python-path=/var/www/FireAnts/venv/path/to/bin/python:/var/www/FireAnts/FireAnts
Where replace <path/to/> with correct path to your venv. You have to give complete path of the python executable.
Edit the wsgi.py file of your project
/var/www/FireAnts/FireAnts/wsgi.py:
import os
import sys
from django.core.wsgi import get_wsgi_application
path = '/var/www/FireAnts'
if path not in sys.path:
sys.path.insert(0, path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "FireAnts.settings")
application = get_wsgi_application()
Okay, so i figured out the answer.
Everything was configured the correct way, but there was a missing installation with an unclear error message.
I located the file that generates the "populate() isn't reentrant" message, found the line that provides the error message, and instructed it to continue by replacing "raise RuntimeError("populate() isn't reentrant")" with "self.app_configs = {}". Instead of receiving a vague, unclear answer, the error log reported a missing "psycopg2" import. I activated the venv directory and installed psycopg2. After that, it worked.

What is the point of a django virtualenv, if when deploying with apache, wsgi.py is executed on the server NOT in the virtualenv?

I have my apache config set up to point to my virtualenv, but when i load the page and look at the error log it gives the following error:
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
apache2 000-default.conf file:
WSGIDaemonProcess project python-home=/home/django/config/env python-path=/usr/local/bfx/Databases/project
WSGIProcessGroup project
WSGIScriptAlias / /usr/local/bfx/Databases/project/project/wsgi.py
Basically, it will work if I install django, and all my django packages on my server, but what is the point of the virtual env?
Thanks
Virtualenv allows to create isolated environments. So you could create and run multiple projects with different versions of the same library without conflicts, for example.
You have to install all libraries in the server too. Virtualenv do not create a bundle nor deploy it.

Django in apache with mod_wsgi confused by os.environ mysite.settings and settings.py

Is settings.py the same as mysite.settings from this example here:
http://django.readthedocs.io/en/1.2.X/howto/deployment/modwsgi.html
It seems ambiguous to me if it is the same thing or not, my app has a settings.py in it but it seems like this isn't the same as mysite.settings spoken of in the docs. I do have my app running with$ python manage.py runserver just fine on centos, and even see apaches test page on the same box so i know apache is running. Was going through the doc above to try to get apache to server the django app and thats where I got confused as to what it is really asking for...
(also the apache folder didn't exist so I added that and created a django.wsgi with the code below that I think was the right thing to do).
Almost looks like I have to setup a sys.path somewhere?
https://code.google.com/archive/p/modwsgi/wikis/IntegrationWithDjango.wiki
When you define:
DJANGO_SETTINGS_MODULE = 'mysite.settings'
Django looks up for settings.py file in mysite folder. mysite folder has to be on your PYTHONPATH. Make sure you set your PYTHONPATH to something like:
export PYTHONPATH=/home/work/project:/home/work/project/project:$PYTHONPATH

How to host Django1.3.1 in Apache2.2?

I am Using python 2.7.2,Django 1.3.1, Apache 2.2.22 on WindowsXP(win32). By the documentation i found here i did the step by step, but when the directory section is given
`Alias /media/ C:/Programs/TestDjango/mysite/media/
<Directory C:/Programs/TestDjango/mysite/media/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / C:/Programs/TestDjango/mysite/apache/django.wsgi
<Directory C:/Programs/TestDjango/mysite/apache>
Order deny,allow
Allow from all
</Directory>`
and restarted the Apache, On opening localhost/mysite i get a Microsoft Visual C++ Library runtime error, and the Apache error log shows "Caught ImproperlyConfigured while rendering: Error loading pyodbc module: DLL load failed: A dynamic link library (DLL) initialization routine failed."....My Django app run in WAMP but wish to know where did i go wrong using Apache2.2.22 alone. Followed many Django documentation but still the same, Please to help me find where did i go wrong. thanks
(identation was fixed by guettli)
I got it solved, it was the version problem, as i worked with Apache 2.2.21 instead of Apache 2.2.22, its working. i followed the step in this link.
Install Python 2.7.2, Django 1.3.1 and Apache2.2.21
Install the modwsgi module.
The module file will be named something like mod_wsgi-win32-ap22py26-2.6.so get mod_wsgi.
Copy it to the modules directory of the Apache installation. E.g., C:/Program Files/Apache Software Foundation/Apache2.2/modules.
Rename it to mod_wsgi.so. Right click--> properties click Unblock and apply
Open Apache's http.conf file.
Add the line LoadModule wsgi_module modules/mod_wsgi.so before all the other LoadModule entries.
Configure Apache for your Django project by adding the following to end of http.conf:
# Static content
Alias /media/ C:/Programs/TestDjango/mysite/media/
<Directory C:/Programs/TestDjango/mysite/media/>
Order deny,allow
Allow from all
</Directory>
# Django dynamic content
WSGIScriptAlias / C:/Programs/TestDjango/mysite/apache/django.wsgi
<Directory C:/Programs/TestDjango/mysite/apache>
Order deny,allow
Allow from all
</Directory>`
Where icardtest is the Django project root. The paths below icardtest will be specific to your project. This configuration serves all static media via the URL space /media/ and all the rest via WSGI and Django.
Create a file django.wsgi and add the following to it:
` import os
import sys
sys.path.append('C:/Programs/TestDjango')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()`
Restart Apache.
Your error has to do with your python setup, not Apache.
Error loading pyodbc module: DLL load failed: A dynamic link library (DLL) initialization routine failed.
This means that the Python you are using with Apache cannot load the pyodbc module. Once you fix this error, you can proceed.

Invalid command 'WSGIReloadMechanism' in my Apache site's config file

I am trying to deploy a Django project and am using Apache2 with mod_wsgi. Here are the relevant lines in my Apache conf file:
WSGIScriptReloading On
WSGIDaemonProcess myprojectcom
WSGIReloadMechanism Process
WSGIProcessGroup myprojectcom
WSGIApplicationGroup myprojectcom
WSGIPassAuthorization On
WSGIScriptAlias / /home/myproject/myproject/deploy/deploy.wsgi
I've used a very similar conf file for many other deployments, but this is the first time that I'm getting the following error:
/etc/apache2/sites-available$ sudo /etc/init.d/apache2 restart
Syntax error on line 8 of /etc/apache2/sites-enabled/myproject.com:
Invalid command 'WSGIReloadMechanism', perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' failed.
The Apache error log may have more information.
...fail!
I don't see any syntax error, though. I'm on Ubuntu, using the libapache2-mod-wsgi package. What could be wrong?
Remove the whole line:
WSGIReloadMechanism Process
It isn't needed any more and the directive was removed completely in mod_wsgi 3.X.
You should preferable not rely on old blog posts for how to set up mod_wsgi and use the actual mod_wsgi documentation on the mod_wsgi site instead.

Categories

Resources