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

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.

Related

Fatal Python error: init_fs_encoding when running Apache with mod_wsgi to deploy Flask

I am currently attempting to deploy a Flask application using mod_wsgi and Apache. I am using Python 3.10 on a Windows 10 machine and I believe to have configured everything properly. Below is my flask_app.conf:
WSGIPythonHome "C:/../../venv"
<VirtualHost *:80>
ServerName XX.XX.XX.XX:80
WSGIScriptAlias / C:/../app/flask_app.wsgi
<Directory C:/../flask_app>
Require all granted
</Directory>
</VirtualHost>
and here is the relevant code added to the Apache httpd.conf file:
ServerName XX.XX.XX.XX:80
LoadFile <primary python3.10 dll>
LoadModule wsgi_module "venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/../venv"
Include conf/flask_app.conf
Strangely it appears that the server functions if I simply run the command
httpd.exe
However, it does stall on the command prompt, as if processing a continuous function.
Also, if I try to start Apache2.4 in Windows Services or use the command:
httpd.exe -k start
An error occurs on startup giving the following relevant snippet in the error.log for Apache:
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'
This has been a hard problem to debug and any guidance would be appreciated. Please let me know if any more information would be helpful.

Flask app running on apache resulting in 404 error

I'm using mac os x and trying to get the official flask tutorial to work with Apache, but when I attempt to open the site in localhost on my browser it results in a 404: The requested URL / was not found on this server. I followed these directions for configuring mod_wsgi for flask. I've run the app using the built in server with flask so I know the app itself works.
The following is the resulting error in the Apache error log
[negotiation:error] [pid 7563] [client ::1:50509] AH00687: Negotiation: discovered file(s) matching request: /Library/WebServer/Documents/flaskr/flaskr.wsgi (None could be negotiated).
Here is what I have in my flaskr.wsgi file located in /Library/WebServer/Documents/flaskr/flaskr.wsgi
import sys
sys.path.insert(0, '/Library/WebServer/Documents/flaskr')
from flaskr import app as application
I have the following in my httpd.conf file
Listen 80
<VirtualHost *>
ServerName localhost
WSGIDaemonProcess flaskr user=_www group=_www threads=5
WSGIScriptAlias / /Library/WebServer/Documents/flaskr/flaskr.wsgi
<Directory /Library/WebServer/Documents/flaskr>
WSGIProcessGroup flaskr
WSGIApplicationGroup %{GLOBAL}
Require all granted # httpd 2.4 syntax as noted in the guide
</Directory>
</VirtualHost>
LoadModule wsgi_module /usr/local/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-darwin.so
WSGIPythonHome /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6
From terminal when I run sudo apachectl -M it displays wsgi_module (shared). If I comment out the code in my httpd.conf file that I've listed above, the wsgi_module goes away from the enabled modules list, so unless there's something I'm not aware of mod_wsgi seems to be enabled successfully, but as I've mentioned I get a 404 message when trying to view the site.
EDIT
I don't think there's anything wrong with my Apache configuration. I tried this tutorial for getting flask up and running on Apache and it worked. I'll update once I figure out what I did wrong with the "flaskr" app.
Edit - found the problem
Turns out my flaskr.wsgi file was in the wrong directory. It was supposed to be in /Library/WebServer/Documents/flaskr/flaskr/flaskr.wsgi.
The official Flask tutorial I referenced uses the following directory structure
flaskr <----Initially put my `flaskr.wsgi` file in this directory
init.py
flaskr <----Changed to this directory and it worked
flaskr.py
flaskr.db
flaskr.wsgi
Also changed the system path to sys.path.append('/Library/WebServer/Documents/flaskr/flaskr')

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

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.

mod_wsgi configuration with flask

I have a problem with mod_wsgi and flask Im not sure what it is after almost 48 hours of trying I've finally given up and need some help. Everything seems fine and working properly I know this because my www.example.com domain routes to example.com/login if the user is not logged in.
When I access example.com the routing does happen and I can see the url change to example.com/login
Here is my configuration:
killerapp.wsgi:
from main import app as application
and the apache virtual host:
NameVirtualHost *:8080
<VirtualHost *:8080>
ServerName example.com
WSGIDaemonProcess killerapp user=apache group=apache threads=5
WSGIScriptAlias / /var/www/wsgi/killerapp.wsgi
<Directory /var/www/wsgi>
WSGIProcessGroup killerapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I was getting the following error all along
ImportError: No module named app
when I did
chmod 755 app
on the app folder I got the following in the log file indicating no errors:
[Wed Nov 06 17:25:29 2013] [info] [client xx.xx.x.xxx] mod_wsgi (pid=3823, process='killerapp', application=''): Loading WSGI script '/var/www/wsgi/killerapp.wsgi
But i still get the following error when I access the page:
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
I suspect its a ownership/security issue but that's just my guess I wouldn't know at this point. If its of any help Im running python 2.6.6 on Centos 6.4 and mod_wsgi 3.2.3
Any help would really be appreciated.
Thank you
With many thanks to Mark Hildreth for showing me the 'way' I managed to finally see what the problem was. It was a permission issue
In my python app I turned on debugging after initializing the flask app like so:
app = Flask(__name__)
app.debug = True
This allowed Flask to display the error messages in the apache error log. In my case the problem was Jinja was unable to access the templates directory because the apache user was not set as owner of that directory so the following fixed it:
chown apache:apache templates
Once again credit and thanks goes to Mark Hildreth for showing me the light!

django, apache, mod_wsgi and python py-scrypt not working together

I am running a ec2 instance to host some Django websites. The websites are being served by Apache with the use of mod_wsgi
Since a few days I am trying to deploy a new webplatform we are developing but I am running into a problem that seems impossible for me to solve. For security reasons we use scrypt 0.4 to secure the users personal information like passwords.
On the development server everything works like a charm but when we deploy to our live server we get a 500 Internal Server error. The apache log gives me the following message
Premature end of script headers: socialmarketingplatform.wsgi, referer:
When I uncomment the line where the scrypt module is used everything works fine. Also when I run the server delivered with Django with scrypt enabled everything works on the live server. So it is a combination of mod_wsgi, django and scrypt that generates the error.
I am using the following wsgi file:
import os
import sys
path = '/var/www/vhosts/[sub_domain]'
if path not in sys.path:
sys.path.append(path)
sys.path.append(path + '/socialmarketingplatform')
os.environ['DJANGO_SETTINGS_MODULE'] = 'socialmarketingplatform.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
And the following virtualhost config:
<VirtualHost *:80>
#Basic setup
ServerAdmin [removed email]
ServerName luxdevelopment.net
ServerAlias [sub domain]
DocumentRoot /var/www/vhosts/[sub domain]/socialmarketingplatform/
Alias /media/admin /usr/lib/python2.6/site-packages/Django-1.3-py2.6.egg/django/contrib/admin/media
Alias /media /var/www/vhosts/[sub domain]/socialmarketingplatform/media
<Location media="">
SetHandler None
</Location>
LogLevel warn
ErrorLog /var/log/httpd/smp_error.log
CustomLog /var/log/httpd/smp_access.log combined
WSGIDaemonProcess luxdevelopment.net user=apache group=apache threads=25
WSGIProcessGroup luxdevelopment.net
WSGIScriptAlias / /var/www/cgi-bin/socialmarketingplatform.wsgi
</VirtualHost>
I hope someone can help me with this problem. If there are any further question let me know.
See:
http://code.google.com/p/modwsgi/wiki/FrequentlyAskedQuestions#Apache_Process_Crashes
The message 'premature end of script headers' is usually indicative of your code crashing the daemon process. You can verify this by looking for segmentation fault or similar message in main Apache error log file. If you enable 'LogLevel info' in main Apache config and VirtualHost then mod_wsgi will log more about daemon process restarts.
A quick remedy if running only application in that daemon process group is to add:
WSGIApplicationGroup %{GLOBAL}
This will work around crashes caused by broken third party extension modules for Python which aren't written properly to work in sub interpreters.
Other than that, can be shared library version mismatches as described in the FAQ.

Categories

Resources