Deploying Django on Apache with mod_wsgi - django

I'm currently trying to set up a Django application on Apache on a Centos server, with the use of mod_wsgi. This is set up to run on https. It is on virtualenv.
However, currently it gives a 504 Gateway Timeout error.
Error log:
(2)No such file or directory: mod_wsgi (pid=15007): Unable to stat Python home /bin/virtualenv:/var/www/django/mysite/mysite/lib/python3.6/site-packages. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
httpd.conf
<VirtualHost *:443>
ServerName mysite.example.com
ServerAlias www.mysite.example.com
# DocumentRoot /var/www/django/mysite
LogLevel info
ErrorLog /etc/httpd/logs/mysite_error.log
Alias /static /var/www/django/mysite/static
<Directory /var/www/django/mysite/static>
Require all granted
</Directory>
<Directory /var/www/django/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mysite python-home=/bin/virtualenv:/var/www/django/mysite/mysite/lib/python3.6/site-packages
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/django/mysite/mysite/wsgi.py
WSGIApplicationGroup %{GLOBAL}
SSLEngine on
SSLProtocol all -SSLv2
SSLCompression off
SSLCertificateFile /etc/pki/tls/certs/mysite.cert.pem
SSLCertificateKeyFile /etc/pki/tls/private/mysite.key.pem
SSLCertificateChainFile /etc/pki/tls/certs/mysite.chain.pem
</VirtualHost>

This is incorrect:
WSGIDaemonProcess mysite python-home=/bin/virtualenv:/var/www/django/mysite/mysite/lib/python3.6/site-packages
Review:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
The python-home value should be a single directory, which is the same as sys.prefix for Python when using virtual environment. Do not added site-packages directory.
Most important, mod_wsgi must be compiled for same Python version as was used to create the virtual environment. You can't use mod_wsgi compiled for Python 2.7 with virtual environment created with Python 3.6.
Check what version of Python mod_wsgi was compiled for using:
http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#python-installation-in-use

Related

Django Numpy ImportError with Apache but not on runserver

I've tried every other thread and their suggested solutions but without success so far.
I have a Django app with a virtualenv and Apache 2.4 and mod_wsgi, which i developed on other server. Now, I'm migrating (rebuilding from scratch) everything on another server.
While everything else works, when i tried to install pandas, the app served through Apache throws me an ImportError for Numpy.
Original error was: /optilab/env/lib64/python3.6/site-packages/numpy/core/_multiarray_umath.cpython-36m-x86_64-linux-gnu.so:
failed to map segment from shared object
Thing is, this doesn't happen while using runserver with manage.py . Everything is the same: Python Version (3.6.8), Python Executable (/optilab/env/bin/python) except the Python Path order.
For the testing runserver, this is the Python Path list:
['/optilab',
'/optilab/env/lib64/python3.6/site-packages',
'/usr/lib64/python36.zip',
'/usr/lib64/python3.6',
'/usr/lib64/python3.6/lib-dynload',
'/optilab/env/lib64/python3.6/site-packages',
'/optilab/env/lib/python3.6/site-packages',
'/home/appuser/.local/lib/python3.6/site-packages',
'/usr/local/lib64/python3.6/site-packages',
'/usr/local/lib/python3.6/site-packages',
'/usr/lib64/python3.6/site-packages',
'/usr/lib/python3.6/site-packages',
'/env/lib64/python3.6/site-packages']
But, for the app served through Apache, the Python Path is:
['/optilab',
'/optilab/core',
'/usr/lib64/python36.zip',
'/usr/lib64/python3.6',
'/usr/lib64/python3.6/lib-dynload',
'/optilab/env/lib64/python3.6/site-packages',
'/optilab/env/lib/python3.6/site-packages',
'/usr/local/lib64/python3.6/site-packages',
'/usr/local/lib/python3.6/site-packages',
'/usr/lib64/python3.6/site-packages',
'/usr/lib/python3.6/site-packages']
I suspect that the list order is the culprit for this problem, but it seems i'm not able to change it.
My file structure is the following:
/optilab
/core <-- startproject directory
/env <-- virtualenv files
/static
/media
... other apps
This is the first part of my apache config (BTW, I tried with python-path in WSGIDaemonProcess but didn't help):
WSGIPythonPath /optilab/env/lib64/python3.6/site-packages:/optilab/env/lib/python3.6/site-packages:/optilab:/optilab/core
<VirtualHost *:80>
ServerName optilab
WSGIDaemonProcess optilab python-home=/optilab/env
WSGIProcessGroup optilab
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /optilab/core/wsgi.py
<Directory /optilab>
Require all granted
</Directory>
<Directory /optilab/core>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
....
I tried everything but nothing worked.

Django WebApp configuration on CentOS7 using Apache mod_wsgi

I have developed a simple website using python django web framework. I am trying to deploy my website on apache mod_wsgi, but it is throwing 500 Internal server error.
My project environment details are here.
Python version: Python 3.6.8
Django version: 3.0.5
Database: mysql Ver 14.14 Distrib 5.7.30
Operating system: CentOS7
HTTPD version: Apache/2.4.6 (CentOS)
My Apache configuration file is below.
<VirtualHost *:80>
ServerName 192.168.25.128
ServerAlias nwaytech.co.in
Alias /favicon.ico /opt/pydjwebpro/nwayapp/static/images/favicon.ico
Alias /media/ /opt/pydjwebpro/nwayapp/media/
Alias /static/ /opt/pydjwebpro/nwayapp/static/
Alias /static/ /opt/pydjwebpro/nwayapp/temlpates/
<Directory /opt/pydjwebpro/nwayapp/templates>
Require all granted
</Directory>
<Directory /opt/pydjwebpro/nwayapp/static>
Require all granted
</Directory>
<Directory /opt/pydjwebpro/nwayapp/media>
Require all granted
</Directory>
WSGIScriptAlias / /opt/pydjwebpro/nwayapp/nwayapp/wsgi.py
WSGIDaemonProcess nwayapp python-home=/opt/pydjwebpro/ python-path= /opt/pydjwebpro/lib/python3.6/site-packages
WSGIProcessGroup nwayapp
<Directory /opt/pydjwebpro/nwayapp/nwayapp/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
I have installed mod_wsgi using pip in my virtual environment, I have to install httpd-devel packages.
Could you anyone help, Please.
Thanks in advance.

WSGIDaemonProcess causes 'You don't have permission error'

Hello I am trying to deploy my django application on digital ocean server. I did run the application inside a virtual environment and it all worked fine. But I want my domain to point to my application and followed this tutorial. But the problem is when the apache conf file is edited with the following code i get the error.
Forbidden You don't have permission to access / on this server.
This is how my directly looks
+root
+myproject
+myproject
settings.py
urls.py
wsgi.py
+static
manage.py
+myprojectenv
Here is my WSGI.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_wsgi_application()
Here is my Apache Default conf file 000-default.conf
<VirtualHost *:80>
#ServerAdmin webmaster#localhost
#DocumentRoot /var/www/html
Alias /static /home/user/alpha/static
<Directory /home/user/alpha/static>
Require all granted
</Directory>
Alias /static /home/root/myproject/static
<Directory /home/root/myproject/static>
Require all granted
</Directory>
<Directory /home/root/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-home=/home/root/myproject/myprojectenv python-path=/home/root/myproject
WSGIProcessGroup myproject
WSGIScriptAlias / /home/root/myproject/myproject/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
I am not experience enough to find out what is causing the error. If i remove below lines in the apache conf file. I get Ubuntu home page when i try to access my server
WSGIDaemonProcess myproject python-home=/home/root/myproject/myprojectenv python-path=/home/root/myproject
WSGIProcessGroup myproject
WSGIScriptAlias / /home/root/myproject/myproject/wsgi.py
Your Python code will run as the Apache user. As a result it likely will not have access to files under home directories as they don't have permissions on them that allows access by others.
You should move stuff from out of the home directories and put it under somewhere like /var/www/project.
See:
http://modwsgi.readthedocs.io/en/develop/user-guides/application-issues.html#access-rights-of-apache-user

How to deploy Django with mod_wsgi on Debian machine?

I have installed apache2, mod_wsgi on my Debian machine and added this on my apache2.conf file:
WSGIScriptAlias /home/zurelsoft/Documents/workspace/genalytics/genalytics/wsgi.py
WSGIPythonPath /home/zurelsoft/Documents/workspace/genalytics
<Directory /home/zurelsoft/Documents/workspace/genalytics/genalytics>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
My project name is genalytics. I am using Django 1.5. There's already wsgi.py available. What should I do run the django with mod_wsgi and where should I give the path of my static files. Thanks
Edit
I have this on my apache.conf file:
Listen 8000
Alias /static/ /home/zurelsoft/Documents/workspace/genalytics/fileupload/static
<Directory /home/zurelsoft/Documents/workspace/genalytics/fileupload/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /home/zurelsoft/Documents/workspace/genalytics/fileupload/static
<Directory /home/zurelsoft/Documents/workspace/genalytics/fileupload/static>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
But when I run try to start apache I get this error:
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
Presuming that you have set up everything correctly, you don't have much left to do.
In your application root, create a file named django.wsgi and write the following code.
import os
import sys
sys.path.append('/path/to/your/app')
os.environ['PYTHON_EGG_CACHE'] = '/path/to/your/app/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Now, add a virtual host in your apache configuration for serving static and other files and add the following lines:
WSGIScriptAlias / /path/to/your/app/django.wsgi
<Directory /path/to/your/app>
Order allow,deny
Allow from all
</Directory>
Alias /robots.txt /path/to/your/app/robots.txt
Alias /favicon.ico /path/to/your/app/favicon.ico
Alias /images /path/to/your/app/images
Alias /static /path/to/your/app/static
ErrorLog /path/to/your/app/logs/error.log
CustomLog /path/to/your/app/access.log combined
Remember to restart apache.
You can check this and this links for complete information. Also, if you need to know how to add virtual host, check this out.
Hope that helps.
There are a number of howtos on the web, most of which work with current Django versions, but I was unhappy with their lack of conformance with Django's current docs and found the easiest path to follow these instructions:
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/
There it says: 'As of Django version 1.4, startproject will have created wsgi.py for you' - which looks like this:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
So now it's wsgi.py that connects to mod_wsgi, which you installed with aptitude, and django.wsgi is deprecated.
Now we want to honor the debian method of configuring apache sites, so instead of putting the following code into httpd.conf, as django-docs propose, we create a dj-myapp file in /etc/apache2/sites-available, activate it with a2ensite dj-myapp and disable default with a2dissite default.
The WSGI-directives are written before the virtualhost section:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<VirtualHost *:80>
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
This is for apache 2.2x, for 2.4+ use Require all granted instead of Allow from all.
Finally configure the static file serving, as described in the django docs. The directives are also placed in dj-myapp. For the admin static files this line worked for me:
Alias /static/admin /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin
create file called app.conf in /etc/apache2/sites-available.The app.conf:
WSGIPassAuthorization On
WSGIPythonPath /home/brms/manage/web/brms
WSGIDaemonProcess pyramid user=brms group=brms threads=4 \
python-path=/usr/local/lib/python3.4/dist-packages/
<VirtualHost *:80>
<Directory /home/brms/manage/>
<Files wsgi.py>
WSGIProcessGroup pyramid
Require all granted
</Files>
</Directory>
Alias /meetingApp /var/www/meetingApp
</VirtualHost>
WSGIScriptAlias / /home/brms/manage/wsgi.py
Enable this site:sudo a2ensite app.conf
Restart Apache: sudo service apache2 restart

Multiple Django applications using virtualenv on Apache 2 on Ubuntu 11

I've successfully setup one Django application using virtualenv on Ubuntu and Apache 2, using the WSGIPythonHome directive pointing to my virtualenv location. Now I am in need to create a separate Django application, that is going to run on Apache on a different port on the same Ubuntu server. I am wondering if there's a way to have Apache run multiple WSGIPythonHome instances? Currently with WSGIPythonHome being set to one virtualenv root, there's a problem with imports on the second Django app…
The best way to do this, I've discovered about a year ago, is to use WSGI as a daemon and set the python path in the daemon directive. Example is below
<VirtualHost *:80>
ServerName yourhost.com
<Directory />
Order deny,allow
#Require all granted
</Directory>
#Alias /static /opt/yourhost/static
WSGIScriptAlias / /opt/yourhost/wsgi.py
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess yourhost.com python-path=/opt/yourhost:/opt/yourhost/venv/lib/python2.7/site-packages processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup yourhost.com
</VirtualHost>
WSGISocketPrefix /var/run/wsgi
You should do this with separate virtual hosts in Apache. Each one can listen to a particular port, and can have its own separate WSGI directives.