Django WebApp configuration on CentOS7 using Apache mod_wsgi - django

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.

Related

How to set the apache2.conf file for a django project?

I am now deploying a django test project on aws-ec2 and the AMI is Ubuntu18.04 with Python 3.6, Django 2.1, Apache2.
The project is under /var/www/Project and I am trying to add the setting to apache.conf.
The project is simply generated by django-admin startproject Project and I want make sure that when hit the public IP provided by the instance, it should show up the django default page.
WSGIDaemonProcess ubuntu processes=2 threads=12 python-path=/var/www/Project
WSGIProcessGroup ubuntu
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /var/www/Project/Project/wsgi.py
<Directory /var/www/Project/Project>
Require all granted
</Directory>
Now i got the internal server error.
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
I have tried this previously. And it seems like it only works when i use python2.7 with django 1.11.
WSGIScriptAlias / /var/www/Project/Project/wsgi.py
WSGIPythonPath /var/www/Project
<Directory /var/www/Project/Project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
what's wrong with my conf file?
I finally come up with the following solution.
If you are using Apache2, Python2 and Ubuntu18.04 LTS, you can change the apache2.conf file by adding the following:
WSGIScriptAlias / [path_to_wsgi.py]
WSGIPythonPath [path_to_project]
<Directory [path_to_project]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
If you want to use the latest Django 2+ version, change the /etc/apache2/sites-available/000-default.conf which is the settings for HTTP port 80. (000-default.conf file can't contains WSGIPythonPath in virtualhost)
<VirtualHost *:80>
ServerAdmin webmaster#localhost
Alias /static [path_to_static_folder]
<Directory [path_to_static_folder]>
Require all granted
</Directory>
<Directory [path_to_project]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess [name] python-home=[path_to_virtualenv] python-path=[path_to_project]
WSGIProcessGroup [name]
WSGIScriptAlias / [path_to_project_wsgi.py]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Deploying Django on Apache with mod_wsgi

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

Python Djangocms Install App WSGI vs 8000 port

I follow the tutorial http://docs.django-cms.org/en/develop/introduction/plugins.html, when I install the Polls App as indicated it is visible on 8000 port But not in WSGI/Apache mode (error message no module polls). To see the APP polls I need to copy Polls application files in the default root directory. Idem with the Aldryn Blog News. I guess I have to specify some more PATH in wsgi mode to help Python to find the modules. Where and how to do that in my Virtualenv, to be also effective when I deploy all the stuff on a remote platform ?
Thanks for any help
Thank you for your interest. IMHO I don't think the problem is on APACHE configuration, everything is OK and DjangoCMS works without polls. Herinafter the conf file, the domain is a local virtual one.
DocumentRoot "/var/www/djangocms"
WSGIScriptAlias / /var/www/djangocms/default/wsgi.py
ServerName djangocms.net
Alias /static/ /var/www/djangocms/default/static/
Options +ExecCGI
Order Allow,Deny
Allow from All
ErrorLog "logs/errordjangocms_log"
LogLevel error
Marcel
Here is a working config i use
<VirtualHost *:80>
ServerName iot.mydomain.com
ServerAlias iot
WSGIDaemonProcess iot.local python-path=/home/name/PycharmProjects/iotdata
WSGIProcessGroup iot.local
WSGIScriptAlias / /home/name/PycharmProjects/iotdata/iotdata/wsgi.py process-group=iot.local
#WSGIPythonPath /home/name/PycharmProjects/iotdata
Alias /static/ /home/name/PycharmProjects/iotdata/static/
<Directory /home/name/PycharmProjects/iotdata>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/name/PycharmProjects/iotdata/static>
Require all granted
</Directory>
</VirtualHost>

Apache doesn't load static folder for Django project

I have a Django 1.4.5 project called mp which I'm trying to run on my localhost using Apache 2.4. Following the official tutorial for Django with mod_wsgi
(How to use Django with Apache and mod_wsgi) I managed to display my Django page when I visit http://127.0.0.1:8801/.
My project folder mp is located in /opt/masterportal/mp, and the static files are located in /opt/masterportal/mp/mp/static.
This is my masterportal.conf file in /etc/apache2/sites-available:
Listen 8081
<VirtualHost *:8081>
ServerAdmin my#mail.adress
XSendFilePath /opt/masterportal/mp/mp/uploads/
<Files *.*>
XSendFile On
</Files>
WSGIDaemonProcess masterportal python-path=/opt/masterportal/mp:/opt/masterportal/mp/env/dev/lib/python2.7/site-packages
WSGIProcessGroup masterportal
WSGIScriptAlias / /opt/masterportal/mp/mp/apache/wsgi.py
Alias /static /opt/masterportal/mp/mp/static
<Directory /opt/masterportal/mp/mp/static>
Require all granted
</Directory>
<Location />
WSGIProcessGroup masterportal
Require all granted
</Location>
</VirtualHost>
However, the website at http://127.0.0.1:8801 can't find any of the static files. This is odd, because the exact same project works on the server of my university (where I don't have access to the apache configuration). So there must be something wrong with my Apache configuration, but I can't see what. I'm desperate for help.
Some general information: I'm using Django 1.4.5 (because this is the version on the university server), and Apache 2.4. The project runs in a virtualenv located here /opt/masterportal/mp/env. I also tried it with Alias /static/ instead of Alias /static, but that didn't work either. My apache2.conf is still original - I made no changes there.
Edit: Here's my configuration for the site in /etc/apache2/conf-available/:
<Location "/mp/2015/suse">
ProxyPass https://my-computername:8081/
ProxyPassReverse https://my-computername:8081/
RequestHeader set X-FORWARDED-PROTOCOL ssl
RequestHeader set X-FORWARDED-SSL on
</Location>
Try to move the lines
Alias /static /opt/masterportal/mp/mp/static
<Directory /opt/masterportal/mp/mp/static>
Require all granted
</Directory>
before
WSGIDaemonProcess masterportal python-path=/opt/masterportal/mp:/opt/masterportal/mp/env/dev/lib/python2.7/site-packages
WSGIProcessGroup masterportal
WSGIScriptAlias / /opt/masterportal/mp/mp/apache/wsgi.py
because the link with /static/some_static_files... might be forwared to the wsgi app instead of pointing to the static directory.

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