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
Related
I am trying to host a web app on a Ec2 instace (no GPU, t2.,medium or t2.large) with Ubuntu 20.04 AMI, using the framework Flask. I am able to run the code locally, however I have problems when launching it on the server. I am able to launch a simple form in the server, but whenever I uncomment the import tensforflow line, I have the following error:
AH00491: caught SIGTERM, shutting down
[mpm_event:notice] [pid 9376:tid 140217758919744] AH00489: Apache/2.4.41 (Ubuntu) mod_wsgi/4.6.8 Python/3.8 configured -- resuming normal operations
[core:notice] [pid 9376:tid 140217758919744] AH00094: Command line: '/usr/sbin/apache2'
I am in root, and when I open python3 in the terminal I am able to import tensorflow without problems, the issue happens with apache
/flaskapp$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> import cv2
I am using an instance with 2GB of RAM (also tried with a 4GB). I tried to do it within an environment but the same happened.
I put here the installations made in the instance:
sudo apt-get -y update
sudo apt -y install python3-virtualenv
sudo apt-get -y install python3-pip
sudo apt-get -y install apache2
sudo apt-get -y install libapache2-mod-wsgi-py3 (without -py3 it says installs python2.7)
sudo pip3 install tensorflow-cpu (had problems without -cpu)
sudo pip3 install opencv-python--headless (had problems without headless)
sudo pip3 install flask
I basically followed the steps in here, and then modify it to try to use a machine learning model.
mkdir ~/flaskapp
sudo ln -sT ~/flaskapp /var/www/html/flaskapp
sudo nano /etc/apache2/sites-enabled/000-default.conf
Add this in 000-default.conf
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
Add permission to a folder to be able to upload a file
sudo chown -R ubuntu /var/www/html/flaskapp/descargas
sudo chmod -R a+rwx /var/www/html/flaskapp/descargas
Restart apache and see error log
sudo service apache2 restart
sudo tail -f /var/log/apache2/error.log
I don't know if has to do with the python version used in mod_wsgi, although seems to be the same 3.8 version. I don't know what could I do to change it either.
Some info of my python:
~$ which python3
/usr/bin/python3
:~$ whereis python3
python3: /usr/bin/python3.8 /usr/bin/python3.8-config /usr/bin/python3 /usr/lib/python3.8 /usr/lib/python3.9 /usr/lib/python3 /etc/python3.8 /etc/python3 /usr/local/lib/python3.8 /usr/include/python3.8 /usr/share/python3 /usr/share/man/man1/python3.1.gz
-------------- UPDATE-----------
I was able to solve this with the mentioned here that requires an extra WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
WSGIApplicationGroup %{GLOBAL}
<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
I have created a django application and deployed it on the server.I have run the application through :-
python manage.py runserver 8000 &
and handle the requests on the apache server through proxy
ProxyPass "/" "http://www.example.com/"
ProxyPassReverse "/" "http://www.example.com/".
But there is a issue that I am facing while testing the api through JMeter, when i am running a test case for 10 users my python service over the server gets killed automatically.
What i am doing wrong or what i have to do more to resolve the above test scenario,please suggest?
First of all you need to deploy it on other server like apache. Below I am sharing how as usual configuration of apache2 to deploy a python django project.
Considering virtual environment is created else see how to create
virtual environment
Apache installation:
You need to install apache if not installed yet. Here I am showing the apache2 installation and some other installation which we need.
sudo apt-get update
sudo apt-get install apache2
sudo a2enmod wsgi
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
configuration of 000-default.conf file : Generally the apache2 is located on linux m at path /etc/apache2/sites-available/000-default.conf. And the configuration file may like that.
<VirtualHost *:80>
ServerName www.something.com
# this is the document root of your project
DocumentRoot /path/to/my-project
# The directory which you can access with base path EX: www.something.com/static/images/image.png
Alias /static /path/to/my-project/static
<Directory /path/to/my-project/static>
Require all granted
</Directory>
<Directory /path/to/my-project/my-project>
<Files wsgi.py>
Header set Access-Control-Allow-Origin "*"
Require all granted
</Files>
</Directory>
WSGIDaemonProcess my-project python-home=/path/to/my_env python-path=/path/to/my-project
WSGIProcessGroup my-project
WSGIScriptAlias / /path/to/my-project/my-project/wsgi.py
ErrorLog /path/to/my-project/logs/error.log
CustomLog /path/to/my-project/logs/access.log combined3
</VirtualHost>
Django project wsgi.py : The django project you created there have a wsgy.py and this file may look like.
python_home = '/path/to/my_env'
import sys
import site
sys.path.append('/path/to/my-project')
sys.path.append('/path/to/my_env/bin')
sys.path.append('/path/to/my_env/lib/python3.6/site-packages')
# Calculate path to site-packages directory.
python_version = '.'.join(map(str, sys.version_info[:2]))
site_packages = python_home + '/lib/python%s/site-packages' % python_version
# Add the site-packages directory.
site.addsitedir(site_packages)
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my-project.settings")
application = get_wsgi_application()
After that you can start you apache2 sever sudo service apache2 start
Also you need to give the permission of you project directory sudo chmod -R 777 /path/to/my-project
This is the basic configuration of apache2 with python django project. Hope this will help to configure any linux machine with your python django project
You cannot using python manage.py runserver 8000 on a server, it's for development ONLY.
You can see the documentation here https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
A basic conf for Apache would be :
<VirtualHost *:80>
ServerName yoursite.com
ServerAdmin your#site.com
Alias /media/ /path/to/django-app/media/
<Directory /path/to/django-app/media/>
Require all granted
</Directory>
WSGIScriptAlias / /path/to/django-app/conf/wsgi.py
<Directory /path/to/django-app/conf/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
You need to adapt this for your project.
If you need to install mod_wsgi : https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html
For me i would use https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/uwsgi/ it's more convenient (pip install uwsgi)
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
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.
I am trying to publish my site on an Amazon's EC2 Instance, and I keep getting a 500 error. I really don't know why.
//Log Files
[Sun Feb 17 23:12:48.066802 2013] mod_wsgi (pid=2102): Target WSGI script '/srv/www/app/poka/apache/wsgi.py' cannot be loaded as Python module.
[Sun Feb 17 23:12:48.066840 2013] mod_wsgi (pid=2102): Exception occurred processing WSGI script '/srv/www/app/poka/apache/wsgi.py'.
[Sun Feb 17 23:12:48.066864 2013] Traceback (most recent call last):
[Sun Feb 17 23:12:48.066889 2013] File "/srv/www/mysite/poka/apache/wsgi.py", line 26, in <module>
[Sun Feb 17 23:12:48.066920 2013] from django.core.wsgi import get_wsgi_application
[Sun Feb 17 23:12:48.066945 2013] ImportError: No module named django.core.wsgi
//Apache Config Files
WSGIScriptAlias / /srv/www/app/mysite/apache/wsgi.py
WSGIDaemonProcess mysite python-path=/srv/www/app/mysite:/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages
WSGIProcessGroup mysite
<Directory /srv/www/app/mysite/apache/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Directory /srv/www/app/mysite/apache/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ec2-user/app/mysite/static>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ec2-user/app/mysite/media>
Order deny,allow
Allow from all
</Directory>
//wsgi.py
import os
import sys
import site
site.addsitedir('/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
path = '/srv/www/app/mysite'
if path not in sys.path:
sys.path.append(path)
I know that this is an old thread but I've just bumped into the same issue and I don't think that this is caused by a missing package. As the Django core distribution contains the correct wsgi handler already.
The problem here is that when wsgi.py is executed it's missing the packages of the site-packages from your virtualenv. (If you have activated your virtualenv, and done pip install django then everything is fine. You have the necessary django packages).
As far as I'm concerned, I fixed the issue modifying the sys.path in my Path/to/Project/Project/wsgi.py file.
You have to append your project dir and your virtualenv site-packages to the sys.path List.
Here is my wsgi.py file contained in my project (Talking about the wsgi.py created with django-admin.py start-project)... that I had to modify in order to make it work with Apache
# =====================
# wsgi.py file begin
import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('<PATH_TO_MY_DJANGO_PROJECT>/hellodjango')
# add the virtualenv site-packages path to the sys.path
sys.path.append('<PATH_TO_VIRTUALENV>/Lib/site-packages')
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# wsgi.py file end
# ===================
Make sure:
you added mod_wsgi to the Apache modules dir
mod_wsgi must be compiled for the OS, Apache and Python version you have
added the load module command into your httpd.conf to load mod_wsgi module
LoadModule wsgi_module modules/mod_wsgi.so
configured Django specifics in your httpd.conf or any conf you include in your httpd.conf
Based on the documentation How to use Django with Apache and mod_wsgi
WSGIScriptAlias / <PATH_TO_PROJECT>/hellodjango/hellodjango/wsgi.py
WSGIPythonPath <PATH_TO_PROJECT>:<PATH_TO_VIRTUALENV>/Lib/site-packages
<Directory <PATH_TO_PROJECT>/hellodjango/hellodjango>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
Hope this helps. It worked for me.
I had the same issue. My libapache2-mod-wsgi was python 2.x and not python 3.x
found the solution here: https://stackoverflow.com/a/28118284/2489042
credits to #nima
$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3
Warning from #alxs before you copy/paste these commands:
If there are python 2 projects running on the server that use wsgi and apache, the above commands will effectively shut them down.
Add this to the Apache configuration file:
WSGIPythonHome /home/ec2-user/.virtualenvs/mysite-main
I had a similar error just now. It turns out that our Django code was developed on python 3.5, but for some reasons the people who deployed our server setup virtualEnv with python 2.7. We redeployed with python 3.5 and everything worked for us
Below was the error message I received:
$ python serviceStartup.py
Traceback (most recent call last):
File "serviceStartup.py", line 10, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
Hope this will help with anyone seeing a similar error message!
For me, this indicated Django wasn't installed on the sever. Fixed via
pip install Django
In general, make sure you installed requirements:
pip install -r requirements.txt
If using a virtual environment, activate it before installing requirements.
You've configured everything very well my friend, just need to give the apache user permission to access both project and virtualenv dirs.
Example:
sudo chown -R www-data:www-data /home/ubuntu/projects
sudo chown -R www-data:www-data /home/ubuntu/virtualenv
This solved my problem with ImportError: No module named django.core.wsgi (virtualenvs folders) and ImportError: No module named <project-name>.settings (projects folders)
At first glance, I am sorry for my English. I also faced this issue, and I have solved it by changing 'wsgi.py' file to:
import os
import django
from django.core.handlers.wsgi import WSGIHandler
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eisentask.settings.production")
django.setup(set_prefix=False)
application = WSGIHandler()
I am using Centos and the nginx and gunicorn for my project...
I had this problem...
wsgi file is not reachable maybe it's bcz of the your current directory
I changed my current directory and It worked...
my project name is coda so I typed
cd coda
and then
gunicorn --bind 0.0.0.0:8001 coda.wsgi
In case someone came here using AWS Lightsail bitnami..
The problem is that 'python install packages in a directory that apache do not see'.
so: packages get installed in '/home/bitnami/.local/lib/python3.8/site-packages/' and Apache looks in '/opt/bitnami/python/lib/python3.8/site-packages/'.
The temporary solution I followed is copying packages to Apache eyes folder with this command 'cp -r /home/bitnami/.local/lib/python3.8/site-packages/* /opt/bitnami/python/lib/python3.8/site-packages/'
Hi I got the same issue in Ubuntu 20.04 server. I fixed it by installing apache2-dev
sudo apt-get install apache2-dev
In mod_wsgi documentation its important to have both apache2 and apache2-dev installed.
Run these commands:
$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3
For me it was some variables that needed to be setted (for windows) :
set PYTHONHOME=F:\path\to\python
set PYTHONPATH=F:\path\to\python