I have a Django application, which has the following code init:
import importlib
def get_class(m):
module_name, class_name = m.rsplit('.', 1)
module = importlib.import_module(module_name)
return getattr(module, class_name)
Whenever I make a request which requires a call of this function, in the error.log I see child pid 27228 exit signal Segmentation fault (11), possible coredump in /etc/apache, and request fails with error code 500.
The strange thing is that if I call that function from the command line, everything works well. Seems like mod_wsgi does not allow to dynamically load a python module.
I tried to do the following with all combinations:
Use virtual environment
Downloaded sources of python 3.6, compiled and installed it,
Recompiled the mod_wsgi for both default python 3.4.3 and new python
3.6.
The error is always the same:
OS: Ubuntu Server 14.04
Apache: 2.4.7
Python: 3.4.3, 3.6.0
On my Ubuntu Desktop 14.04 everything works fine.
Apache is installed by sudo apt-get install apache2
in /etc/apache2/sites-enabled/000-default.conf
<Directory /my/django/app/appp>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myapp python-path=/my/django/app
WSGIProcessGroup myapp
WSGIScriptAlias / /my/django/app/app/wsgi.py
WSGIApplicationGroup %{GLOBAL}
UPDATE:
I have downloaded and compiled apache2 and recompiled mod_wsgi. Still the same problem.
UPDATE 2:
Not sure if this will help, but the OS is on a Azure VM with Ubuntu Server 14.04
Related
I wish to deploy multiple Django sites (say 2 for now - www.example.com, www.example2.com) using apache2 mod_wsgi. Here are the steps I followed in a fresh Ubuntu 16.04 installations.
I've set alias python=python3 in my .bashrc as I am working in python3
packages installed:
apache2 - sudo apt-get install apache2
mod_wsgi - sudo apt-get install libapache2-mod-wsgi-py3
Django(V1.11) - sudo apt-get install python3-django
Then I've created two projects inside
/var/www/
django-admin startproject example
django-admin startproject example2
then I did the followings for both the projects (I've mentioned only one, the other one is exactly the same except replacing example with example2)
/var/www/example/example/settings.py
ALLOWED_HOSTS = ['example.com', 'www.example.com']
/var/www/example/example/wsgi.py
#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings")
os.environ["DJANGO_SETTINGS_MODULE"] = "example.settings"
/etc/apache2/sites-available/example.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin admin#example.com
DocumentRoot /var/www/example
WSGIScriptAlias / /var/www/example/example/wsgi.py
ErrorLog /var/www/example/error.log
</VirtualHost>
Also, I've added
/etc/apache2/apache2.conf
WSGIPythonPath /var/www/example
I've called a2ensite for both example.conf and example2.conf
Once I restart apache I could now access www.example.com successfully, but www.example2.com gives an internal server error(500). I thought that's because I've only included one path for WSGIPythonPath in apache2.conf and changed it as
/etc/apache2/apache2.conf
WSGIPythonPath /var/www/example:/var/www/example2
But however, I now get an internal server error(500) for both.
in both cases, the error message is ImportError: No module named example
This is my first deployment by the way. Hense I have no prior experience.
Please direct me what I should do to get both sites up and running.
The error from apache after a 504 page
[info] mod_wsgi (pid=): Python home /var/venv/mybox.
[info] mod_wsgi (pid=): Initializing Python.
ImportError: No module named site
This is with a barely configured app.
<IfModule mod_wsgi.c>
WSGIDaemonProcess myapp python-home=/var/venv/mybox
WSGIProcessGroup myapp
WSGIScriptAlias / /var/www/html/web/myapp/wsgi.py
WSGISocketPrefix /var/run/wsgi
<Directory /var/www/html/web>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</IfModule>
Followed every post and tutorial I can. I am on CENTOS6 . using virutal env python 2.7 the default system env is 2.6
$ ldd /etc/httpd/modules/mod_wsgi.so
linux-vdso.so.1 => (0x00007ffc06174000)
mywsgi.py
import os,sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
sys.path.insert(0,'/var/www/html/web')
activate_this = '/var/venv/mybox/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
application = get_wsgi_application()
PYHTONHOME is not set
The documentation for using virtual environments with mod_wsgi can be found at:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
Most important in your case is the section:
Virtual Environment and Python Version
In that section it states:
When using a Python virtual environment with mod_wsgi, it is very important that it has been created using the same Python installation that mod_wsgi was originally compiled for. It is not possible to use a Python virtual environment to force mod_wsgi to use a different Python version, or even a different Python installation.
You cannot for example force mod_wsgi to use a Python virtual environment created using Python 3.5 when mod_wsgi was originally compiled for Python 2.7. This is because the Python library for the Python installation it was originally compiled against is linked directly into the mod_wsgi module.
So most likely what is happening is that mod_wsgi is compiled for Python 2.6. You cannot in this case force it to use a Python virtual environment created from Python 2.7. When you do this, you will get the error you see about site module being missing.
You will need to uninstall that mod_wsgi from system packages and install mod_wsgi from source code, compiling it against Python 2.7. The easiest way to do this might be to use the pip install method as described in:
https://pypi.python.org/pypi/mod_wsgi
Run pip install to install it in your virtual environment and then follow instructions in section 'Connecting into Apache installation' about configuring Apache to use it.
Solved in CentOS 7 with Apache 2.4.6
My whole server uses Python 2.7, but I've already installed Python 3.6 and my virtualenv is using Python 3.6.
After configured djang.conf (/etc/httpd/conf.d/django.conf) with this code:
<VirtualHost *:80>
WSGIDaemonProcess myProj python-home=/home/user/django-site/env python-path=/home/user/django-site
WSGIProcessGroup myProj
WSGIScriptAlias /myProj /home/user/django-site/my-project/wsgi.py
Alias /static /home/user/django-site/static
<Directory /home/user/django-site/static>
Require all granted
</Directory>
<Directory /home/user/django-site/my-project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
And restarted my apache
sudo systemctl restart httpd
I got this error a thousand lines (/var/log/httpd/error_log)
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
The solution
First:
sudo grep wsgi /var/log/httpd/error_log
I got this:
[mpm_prefork:notice] [pid 62324] AH00163: Apache/2.4.6 (CentOS) PHP/7.0.33 mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
Note the Python version (2.7.5). What I did to get mod_wsgi according to my Python 3.6 is using:
yum list *mod_wsgi*
Installed packages
mod_wsgi.x86_64 3.4-18.el7 #base
Disponible packages
python35u-mod_wsgi.x86_64 4.6.2-1.ius.centos7 ius
python36u-mod_wsgi.x86_64 4.6.2-1.ius.centos7 ius
and then I installed the package python36u-mod_wsgi.x86_64:
sudo yum install python36u-mod_wsgi.x86_64
Then I restarted Apache service:
sudo systemctl restart httpd
And got this new line from logs:
[Fri Mar 29 12:33:26.788716 2019] [mpm_prefork:notice] [pid 76317] AH00163: Apache/2.4.6 (CentOS) PHP/7.0.33 mod_wsgi/4.6.2 Python/3.6 configured -- resuming normal operations
And everything works! :-)
Hope it helps you. C ya!
this is taken fron the Documentation
write this:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
and this is specifically for the virtual env, you need to write the path to the site packeges of your python virtual env:
WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages
the problem may also be in the -
PYTHONHOME
Change the location of the standard Python libraries. By default, the
libraries are searched in prefix/lib/pythonversion and
exec_prefix/lib/pythonversion, where prefix and exec_prefix are
installation-dependent directories, both defaulting to /usr/local.
When PYTHONHOME is set to a single directory, its value replaces both
prefix and exec_prefix. To specify different values for these, set
PYTHONHOME to prefix:exec_prefix.
Try to clean up your PYTHONHOME:
user$ export PYTHONHOME=
On Ubuntu, if you want to use python3, you will have to uninstall libapache2-mod-wsgi-py and install libapache2-mod-wsgi-py3 instead if you obtained your mod_wsgi from the repository.
I was on centos6.X with python 2.7, reinstall of mod_wsgi has fixed the issue for me.
yum remove mod_wsgi.x86_64
yum install mod_wsgi.x86_64
/etc/init.d/httpd restart
i've installed on an ubuntu server django 1.9.4 and mysqlclient, i'm not under virtualenv.
If i run manage.py shell or runserver all goes right, but if i try through apache2 and wgi i got this error:
ImproperlyConfigured: Error loading MySQLdb module: this is MySQLdb version (1, 3, 7, 'final', 1), but _mysql is version (1, 2, 3, 'final', 0)
Python version 3.4
This is the apache2 vhost config:
WSGIPythonPath /usr/local/lib/python3.4/dist-packages/:/home/ubuntu/www/mysite
DocumentRoot /home/ubuntu/www/mysite
ErrorLog ${APACHE_LOG_DIR}/mysite.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIScriptAlias / /home/ubuntu/www/mysite/mysite/wsgi.py
<Directory /home/ubuntu/www/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Allow from all
Require all granted
</Files>
</Directory>
SOLUTION:
was a packages version error, with the following commands i solved it:
sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
sudo apt-get install libapache2-mod-wsgi-py3
It is clear from the error message that your mysql version on system differs from MySQL-python-1.3.7.
Try uninstall MySQL-python and install 1.2.3 version.
I created a droplet(cloud server) on DigitalOcean and with no-ip.com I gave it the hostname - project.ddns.net.By ssh(ing) into the droplet I installed pip and virtualenv.
Inside /var/www/ I created a virtualenv and cloned the repository from github of my project.The directory struture is -
project_revamped (root of the project)
->requirements
->base.txt
->dev.txt
->project (django project)
->static
->media
->apps (folder which contains apps)
->manage.py
->project
->urls.py
->settings
->base.py
->dev.py
I installed apache2 and mod_wsgi using -
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
I then installed mysql,created a database and installed all requirements
pip install -r base.txt
I created a virtualhost project.conf on the path -
/etc/apache2/sites-available/project.conf
the content of file is this -
<VirtualHost *:80>
ServerAdmin example#gmail.com
ServerName project.ddns.net
ServerName www.project.ddns.net
WSGIScriptAlias / /var/www/project_revamped/project/project/wsgi.py
<Directory /var/www/project_revamped/project/project>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Then I gave this command to activate this conf file -
a2ensite project.conf
The content of my wsgi.py in my django project is -
import os
import sys
import site
#Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/var/www/.virtualenvs/projectenv/local/lib/python2.7/site-packages')
#Add the app's directory to the python path
sys.path.append('/var/www/project_revamped/project')
sys.path.append('/var/www/project_revamped/project/project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings.dev'
#Activate your virtualenv
activate_env = os.path.expanduser('/var/www/.virtualenvs/projectenv/bin/activate_this.py' )
execfile(activate_env, dict(__file__=activate_env))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
After changing the files I finally gave the commands -
service apache2 reload
service apache2 restart
However after doing these things right the corresponding ip says there is some problem the server and sends 500 error.I guess the problem is somewhere in my configuration because apache server was responding working fine.After I include django project with it the problem starts.
Can anybody please help me here in the configuration? I am stuck in this for past 2 days and every different article on the internet tells the different story.
Have a look at the official documentation. I think you're missing the WSGIPythonPath-directive.
As #BurhanKhalid stated, this linked tutorial is complete and tested and should nearly exactly match your setup.
I have a Django 1.5, Python 2.7 site running under Apache with mod_wsgi on a CentOS 6.4 server.
I have rebuilt this site using Django 1.6 and Python 3.3. Deploying it to the same server and changing the paths in httpd.conf I get the subject error. This new install works as expected using ./manage.py runserver.
Here are the two WSGI definitions from httpd.conf:
WSGIScriptAlias / /home/ccdgen/CCDGEN2/apache/wsgi.py
WSGIPythonPath /home/ccdgen/CCDGEN2/ccdgen/ccdgen:/home/ccdgen/CCDGEN2/ccdgen:/home/ccdgen/CCDGEN2/lib/python3.3/site-packages
<Directory /home/ccdgen/CCDGEN2/ccdgen>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
#WSGIScriptAlias /ccdgen /home/ccdgen/CCDGEN/apache/wsgi.py
#WSGIPythonPath /home/ccdgen/CCDGEN/mlhim/ccdgen:/home/ccdgen/CCDGEN/mlhim:/home/ccdgen/CCDGEN/lib/python2.7/site-packages
#<Directory /home/ccdgen/CCDGEN/mlhim>
# <Files wsgi.py>
# Order allow,deny
# Allow from all
# </Files>
#</Directory>
The wsgi.py file is the same on both installations:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mlhim.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Any ideas? Obvious oversights? Thanks
I had this problem recently:
ImproperlyConfigured: Error loading psycopg2 module: No module named _psycopg
... and this command was the answer:
sudo apt-get install libapache2-mod-wsgi-py3
Reference:
http://python.6.x6.nabble.com/ImproperlyConfigured-Error-loading-psycopg2-module-No-module-named-psycopg-td5044145.html
The problem was that mod_wsgi was compiled for Python 2.7 and it needed to be recompiled for Python 3.3. I did not find a new mod_wsgi available anywhere as a package so I recompiled it.
Since Python 3 is an alternate install on CentOS 6.4 .configure had a difficult time producing a good Makefile, even passing the --with-python option. I needed to edit Makefile a bit after getting the information from python3.3-config --cflags and also with --ldflags options.
HTH someone in the future.
Try these commands Debian, Ubuntu
sudo apt-get install python-dev
sudo apt-get install libpq-dev
For RedHat Enterprise, Fedora, CentOS
sudo yum install python-devel
sudo yum install postgresql-libs
Then Install psycopg2
pip install psycopg2
As has been mentioned, mod_wsgi has not been compiled for python 3. One option is to use Software Collections and/or go with nginx, and "pip install uwsgi" which saves a lot of compiling and running custom packages.