Apache and mod_wsgi WSGIDaemonProcess config error - django

I'm having a lot of difficulty with apache(Apache/2.4.7 (Ubuntu 14.04)) and mod_wsgi and my django site. My configuration has been working for a long time, then I'm not sure what I did (possibly because I moved the virtualenv, and didn't reflect this accurately in the config, or possibly just a server upgrade, I'm not sure), but now I get errors like
#/var/log/apache2/error.log
[Mon Dec 22 08:47:04.693620 2014] [:info] [pid 21082:tid 139779033159552] mod_wsgi (pid=21082): Starting process 'mysite.com' with uid=999, gid=999 and threads=4.
[Mon Dec 22 08:47:04.693744 2014] [:alert] [pid 21084:tid 139779033159552] (2)No such file or directory: mod_wsgi (pid=21084): Unable to change working directory to '/home/djangoUser'.
[Mon Dec 22 08:47:04.693769 2014] [:alert] [pid 21084:tid 139779033159552] mod_wsgi (pid=21084): Failure to configure the daemon process correctly and process left in unspecified state. Restarting daemon process after delay.
and
#/srv/apache/log/mysite_error.log
[Mon Dec 22 08:49:31.295810 2014] [core:error] [pid 13999:tid 139778670376704] [client 1.169.91.80:2836] Script timed out before returning headers: mysite.com_wsgi.py
I tried reinstalling and re-enabling mod_wsgi:
sudo aptitude install libapache2-mod-wsgi
a2enmod wsgi
service apache2 restart
and this did not help.
My wsgi file for django looks like:
#/srv/apache/mysite.com_wsgi.py (perms are www-data.www-data 644 on file, 755 dir above)
#!/usr/bin/env python
settings_module = "settings"
os.environ["DJANGO_SETTINGS_MODULE"] = settings_module
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
and my apache conf looks like:
#/etc/apache2/sites-enabled/mysite.com.conf:
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin me#gmail.com
ServerName www.mysite.com
ServerAlias mysite.com
WSGIScriptAlias / /srv/apache/mysite.com_wsgi.py
# Log file locations
LogLevel info
ErrorLog /srv/apache/log/mysite.com_error.log
CustomLog /srv/apache/log/mysite.com_access.log combined
WSGIDaemonProcess mysite.com processes=4 threads=4 display-name=%{GROUP} user=djangoUser group=djangoUser python-path=/srv/mysite:/srv/mysite/venv/lib/python2.7/site-packages
WSGIProcessGroup: mysite.com
<Directory "/srv/apache">
Require all granted
</Directory>
<Directory "/srv/www/mysite.com/media">
Require all granted
</Directory>
<Directory "/srv/www/mysite.com/static">
Require all granted
</Directory>
</VirtualHost>
Some dir structure notes:
The django site files are at /srv/mysite which is djangoUser.djangoUser 755 (and all files/dirs below are owned by djangoUser too). The virtual env is at /srv/mysite/venv and again all owned by djangoUser with same perms pip install gave them. Other files inside /srv/mysite/ are also owned by djangoUser with dir 500, files 400.
The wsgi file is at /srv/apache and it and the apache dir are www-data.www-data with the wsgi file 644 and dir above 755.
The static files are at /srv/www/mysite.com/static with everything owned by www-data, and dir .....I can actually access say the favico or robots file without problem...www.mysite.com/favicon.ico shows it without error.
Has anyone got any ideas what the problem could be? I thought at first it must be permissions, but I cannot see any issues.
Finally /etc/passwd shows the djangoUser as
djangoUser:x:999:999::/home/djangoUser:/bin/false

Related

404 error. The requested URL was not found on this server

I'm trying to host my flask application on apache2 running on ubuntu 20.04. I have wsgi already enabled as well as the site. I believe the error might be because of wrong configuration but the error log suggests something about no python frame. Here's a copy of the error log from /var/log/apache2/error.log `
Current thread 0x00007f281d459c40 (most recent call first):
<no Python frame>
[Sat Jul 02 14:10:24.169227 2022] [wsgi:warn] [pid 23151:tid 139810266520640] mod_wsgi (pid=23151): Python home /home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9 is not a directory. Python interpreter may not be able to be initialized correctly. Verify the supplied path.
Python path configuration:
PYTHONHOME = '/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9'
PYTHONPATH = (not set)
program name = 'python3'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = '/usr/bin/python3'
sys.base_prefix = '/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9'
sys.base_exec_prefix = '/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9'
sys.executable = '/usr/bin/python3'
sys.prefix = '/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9'
sys.exec_prefix = '/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9'
sys.path = [
'/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9/lib/python38.zip',
'/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9/lib/python3.8',
'/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9/lib/python3.8/lib-dynload',
]
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'
`
I have a package structure for my flask app allowing me to run it with python3 run.py but when importing flask_app I use from attend import app as application as shown in my wsgi file below.
import sys
import logging
logging.basicConfig(level=logging.DEBUG, filename='/var/www/html/SCHEYE/logs/SCHEYE.log', format='%(asctime)s %(message)s')
sys.path.insert(0, '/var/www/html/SCHEYE')
sys.path.insert(0, '/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9')
from attend import app as application
Here's a copy of my .conf file
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
ServerName 192.168.0.105
ServerAlias 192.168.0.105
DocumentRoot /var/www/html/SCHEYE
WSGIDaemonProcess SCHEYE threads=5 python-home=/home/dancungerald/.local/share/virtualenvs/SCHEYE-zGV6jY25/bin/python3.9
WSGIScriptAlias / /var/html/SCHEYE/SCHEYE.wsgi
WSGIApplicationGroup %{GLOBAL}
<Directory /var/www/html/SCHEYE>
WSGIProcessGroup SCHEYE
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
I have not yet registered a domain name for my site and I would like to ask if I can use my ip address as the ServerName and ServerAlias or could I simply enter an unregistered domain name? Sorry if the question is too obvious or redundant as I'm quite new to deployment this being my first. Thank you in advance.

Deploying a Flask using mod_wsgi: I get an error "Unable to Open Database File" from error.log

I have installed mod_wsgi and am currently working to deploy a test app. The issue stems from the application unable to read from the database. If I do not use mod_wsgi the test app works.
Here is my testapp.wsgi
#! /usr/bin/python3.7
import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/var/www/html/TestApp/TestApp/')
from testapp import app as application
application.secret_key = "anything you wish"
Here is my TestApp.conf file
<VirtualHost *:80>
ServerName 172.28.103.40
WSGIDaemonProcess testapp user=www-data group=www-data threads=5
WSGIScriptAlias /testApp/var/www/html/TestApp/TestApp$
<Directory /var/www/html/TestApp/TestApp>
WSGIProcessGroup testapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
my database directory
drwxrwxrwx 2 www-data www-data 4096 Nov 1 11:23 db
my database file
-rwxrwxrwx 1 www-data www-data 237568 Nov 1 11:23 shieldcampus.db
I realize that full 777 access is not required but I wanted to see if setting the permissions to 777 would work and it did not.
The exact error in the error log is
[Thu Nov 14 10:53:35.501047 2019] [wsgi:error] [pid 3760] [client 172.19.90.16:60233] con = sqlite3.connect("db/testapp.db")
[Thu Nov 14 10:53:35.501062 2019] [wsgi:error] [pid 3760] [client 172.19.90.16:60233] OperationalError: unable to open database file
One thing I am noticing is the IP address from the error log is not the IP address I am using to access my site.
My site IP is 172.28.103.40
The IP being used with WSGI for this call is 172.19.90.16:60233
Could this be a clue to what is going on? If not is there a disconnect I have missed. Being new to mod_wsgi it's possible I may have missed something when it comes to database setup with mod_wsgi. I didn't see any on the documentation.
Also this is a local server, not public. Not sure if that makes a difference.
Edit: flask application code
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello world!"
if __name__ == "__main__":
app.run()

Apache and wsgi config for Django

I'm struggling to get my Django stuff to appear on my web browser. If I use python3 manage.py runserver 192.168.0.10:8000, I can see the Django install and admin page, but I'd like to see it without specifying a port number, and I'm aware that using run server is the development side, rather than deployment. Also worth noting that I have not used any form of VirtualEnv as this is a dedicate Pi/Project, and I was getting confused with the libraries, local python install, virtual python install etc.
I have Django / MySQL / Apache2 up-to-date (as far as I can tell) and there is Django data in the correct db/table.
When navigating to 192.168.0.10/ or 192.168.0.10/admin, I get no error page or default apache page. It sits there for about 5 minutes, then gives me 'can't find page'.
Trying to narrow things down, I think I've got one of three problems:
Apache isn't configured correctly
Permissions/ownership issue of MySQL (e.g. apache not reading from table)
Permissions/ownership issue of dir's (e.g. /var/www)
I posted a similar question not too long ago, I've revisited that and the links that came with it, but it's not helped. I did have PHP and a VirtualEnv installed that time, and this is a fresh installation without either. I'm looking to have php install to have a bit of a gui to manage the MySQL should I need to, so I might do then after posting this and see if it helps.
Django's settings.py: I'll provide the important bits:
DEBUG = True
ALLOWED_HOSTS = ['192.168.0.10', '127.0.0.1']
.....
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'init_command': 'SET sql_mode=`STRICT_TRANS_TABLES`',
'read_default_file': '/etc/mysql/my.cnf',
},
}
}
......
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MySQL/my.cnf =
# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
# Import all .cnf files from configuration directory
#!includedir /etc/mysql/conf.d/
#!includedir /etc/mysql/mariadb.conf.d/
[client]
database = blog_data
host = localhost
user = root
password = xxx
default-character-set = utf8
#SET sql_mode='STRICT_TRANS_TABLES'
/etc/apache2/sites-available/000-default.conf =
...
Alias /static /home/pi/test_blog_app/blog/static
<Directory /home/pi/test_blog_app/blog/static>
Require all granted
</Directory>
<Directory /home/pi/test_blog_app/blog/blog>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess testblogapp python-path=/home/pi/test_blog_app python-home=/usr/lib/python3/dist-packages
WSGIProcessGroup testblogapp
WSGIScriptAlias / /home/pi/test_blog_app/blog/blog/wsgi.py
Output of sudo nano var/log/apache2/error.log (last few lines) =
Current thread 0xb6fa8e40 (most recent call first):
[Sun Oct 01 16:11:20.397787 2017] [core:notice] [pid 2390:tid 3069873728] AH00052: child pid 2476 exit signal Aborted (6)
[Sun Oct 01 16:11:20.398262 2017] [core:notice] [pid 2390:tid 3069873728] AH00052: child pid 2477 exit signal Aborted (6)
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Current thread 0xb6fa8e40 (most recent call first):
VERSIONS
O/S - Raspbian 9.1
Apache - 2.4.25
Django - 1.11.5
Python - 3.5.3
MySQL - (Maria) 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0
PHP - 7.0.19-1
EDIT I've attempted to re-install mod_wsgi using
sudo apt-get purge --auto-remove libapache2-mod-wsgi-py3 &
sudo apt-get install libapache2-mod-wsgi-py3
However, no result

Mod_wsgi fails to load django.core.handlers.wsgi

Ok, after 5-6 hours of trying, I give up. I have searched the web, tried all solutions suggested, but nothing is solving my problem.
Goal: Set up Django on my Ubuntu 12.04 VPS.
Problem: Exception occurred processing WSGI script [...] ImportError: No module named django.core.handlers.wsgi in /etc/log/apache2/error.log.
Solutions tried: 1) Appending the site-packages directory to the sys path in Djangos' wsgi.py file, 2) re-installing mod_wsgi, 3) making sure mod_wsgi is compiled for the same Python version as Django is installed with, 4) chmod 777 for the site-packages directory.
Environment: Ubuntu 12.04 VPS, Django installation in virtualenv, Python version 2.7.3, Django version 1.6.1, mod_wsgi built from mod_wsgi-3.4.tar.gz.
Full error message:
mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
Traceback (most recent call last):
File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
mod_wsgi (pid=23691): Target WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
Traceback (most recent call last):
File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
Conf file from sites-available:
<VirtualHost *:80>
ServerAdmin admin#mysite.com
ServerName mysite.com
DocumentRoot /var/www/mysite.com/djangoSite
WSGIDaemonProcess djangoSite python-path=/var/www/mysite.com/djangoSite:~/Envs/myEnv/lib/python2.7/site-packages
WSGIProcessGroup djangoSite
WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py
Alias /static/ /var/www/mysite.com/djangoSite/static/
<Directory /var/www/mysite.com/djangoSite/static>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/mysite.com/djangoSite/djangoSite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
wsgi.py:
import os
import sys
sys.path.append('/var/www/mysite.com/djangoSite/')
sys.path.append('/var/www/mysite.com/djangoSite/djangoSite/')
activate_this = '/root/Envs/myenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoSite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Site structure
/var/www/
-- mysite.com
-- djangoSite
-- manage.py
-- djangoSite
-- settings.py, wsgi.py etc.
In your wsgi.py, try adding this to activate virtual env:
import os
import sys
sys.path.append('/Path_To/Virtual_Env/Project_Dir/')
#This is important if multiple apps are running (instead of setdefault)
os.environ["DJANGO_SETTINGS_MODULE"] = "app_name.settings"
activate_this = '/Path_To/Virtual_Env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
In you apache config, you mainly need only(No Deamon Process required):
WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py
Alias /static/ /var/www/mysite.com/djangoSite/static/
See from the Create Virtual Host section of this link
Make sure you are using absolute paths in your Apache2 config file. I assume you have been able to run python manage.py shell to get a working Django shell? I would tackle the Apache2 config file; I'm sure your issue is there.
~/Envs/myEnv/lib/python2.7/site-packages
I think that's your problem right there: the ~ is, if it is expanded at all, expanded to the home of the www-data user. You can validate that by doing something like this:
import sys
print >> sys.stderr, sys.path
in your wsgi file; then check your error log. If it doesn't end up in the main error log, add an ErrorLog to your vhost to get per-vhost error logging.
Also, /root/ is not readable for the www-data user, you need to move your virtual env somewhere accessible.

Apache Django "client denied by server configuration" error

I found a similar question here, but didn't help me.
I basically have apache setup on my OS X. I also have Django installed.
However, when i try to load the page through the browser locally, i get:
Forbidden
You don't have permission to access / on this server.
I have an original httpd.conf with the only modification of enabling vhosts in which I have:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/Dropbox/project/www/"
ServerName touch.loc
# ServerAlias www.dummy-host.example.com
ErrorLog "/Dropbox/project/www/log/error.log"
CustomLog "/Dropbox/project/www/log/access.log" common
</VirtualHost>
And the error.log file gives:
[Tue May 03 20:22:56 2011] [error] [client 127.0.0.1] Directory index forbidden by Options directive: /Dropbox/project/www/
I read around and it looks like i have to add the following to the httpd.conf:
<Directory /Dropbox/project/www >
Order deny,allow
Allow from all
</Directory>
In which case i get:
[Tue May 03 20:27:55 2011] [error] [client 127.0.0.1] client denied by server configuration: /Dropbox/project/www/
Can someone help me fix this annoyance? How can I further Investigate it?
Does it have to do with users/groups?
UPDATE:
I then added the Options +Indexes and the permissions opened. However when i try to load the page, file structure appears instead of the wsgi file to pickup and load the website. What would be the reason for that?
here is my .wsgi file:
import os
import sys
sys.stdout = sys.stderr
# Add the virtual Python environment site-packages directory to the path
import site
site.addsitedir('/usr/lib/python2.6/dist-packages')
#If your project is not on your PYTHONPATH by default you can add the following
sys.path.append('/Dropbox/project/www/')
sys.path.append('/Dropbox/project/www/project')
# Avoid ``[Errno 13] Permission denied: '/var/www/.python-eggs'`` messages
os.environ['PYTHON_EGG_CACHE'] = '/Dropbox/project/www/mod_wsgi/egg-cache'
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
You seem to be missing anything in your Apache configuration that actually serves the Django application. You need to read the Django mod_wsgi documentation - the first line of code there is what you're missing.
Note that your code should not live under the DocumentRoot.
The problem I see is in Dropbox folder
I have made so:
chmod o+x /home/your_name/Dropbox
That resolves permissions