Django MySQLdb issue on apache2 - django

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.

Related

Django files not found when served with Apache

I deployed my Django project on a Linux server using Linode. When I run it by the python manage.py runserver command on port 8000 everything looks fine. However, when I use apache2 I get the following error when loading the website:
FileNotFoundError at /
[Errno 2] No such file or directory: 'research_files/data.xlsx'
Request Method: GET
Request URL: http://453.33.13.202/
Django Version: 3.1.5
Exception Type: FileNotFoundError
Exception Value:
[Errno 2] No such file or directory: 'research_files/data.xlsx'
In /etc/apache2/sites-available I've created a django_project.conf file which has the following instructions:
Alias /research_files/ /home/alexa/django_project/research_files/
<Directory /home/alexa/django_project/django_project/research_files>
Require all granted
</Directory>
<Directory /home/alexa/django_project/django_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/alexa/django_project/django_project/wsgi.py>
WSGIDaemonProcess django_app python-path=/home/alexa/django_project python-home=/home/alexa/django_project/venv
WSGIProcessGroup django_app
Then I've enabled that file by running sudo a2ensite django_project and also given access to the folder by running:
sudo chown :www-data django_project/research_files
sudo chmod 664 django_project/research_files
What may be causing this error?
Maybe is the extra / in the first line after research_files, so instead /research_files/ it would be /research_files. This could be a guide also https://stackoverflow.com/a/5684257/15020017

Django rest API deploy on apache

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)

apache2 crashes when trying to dynamically load python module

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

Django Apache and Virtualenv ImportError: No module named site

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

Django, mod_wsgi, psycopg2 ImproperlyConfigured: Error loading psycopg2 module: No module named _psycopg

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.