Django EC2 mod_wsgi configuration with Apache not working - django

I think my apache2 server is close to being set up but it's still returning a 500 error. Here is my .wsgi file:
import os, sys
#path to directory of the .wsgi file ('apache/')
wsgi_dir = os.path.abspath(os.path.dirname(__file__))
#path to project root directory (parent of 'apache/')
project_dir = os.path.dirname(wsgi_dir)
sys.path.append(project_dir)
project_settings = os.path.join(project_dir, 'settings')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ecomstore.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Here is my virtualhost file:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin admin#site.com
ServerName www.site.com
ServerAlias site.com
Alias /static /home/ecomstore/static
DocumentRoot /home/ecomstore
WSGIScriptAlias / /home/ecomstore/apache/ecomstore.wsgi
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
Here is my server restart:
sudo /etc/init.d/apache2 restart
* Restarting web server apache2
[Sun Apr 08 02:47:31 2012] [warn] module wsgi_module is already loaded, skipping
... waiting ..........[Sun Apr 08 02:47:42 2012] [warn] module wsgi_module is already loaded, skipping
...done.
However, even though I get no errors with mod-wsgi configuration on the restart, I still get the 500 Internal Server error mentioned before. Is there anything I'm missing?

You would need to look at the Apache error logs from when you made the request and not when you did the restart.
One source of problems is that sys.path does not include the parent directory of the project directory which would be necessary because of what you set DJANGO_SETTINGS_MODULE to. Go read about this requirement in:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
and fix it.
Beyond that, you need to work out whether it is an Apache 500 page or a Django one. If it is a Django one, then turn on DEBUG in Django settings file and restart Apache so you get a full error shown in browser. Turn off DEBUG when you have resolved.

Related

Deploying Django restfule service using WAMP and mod_wsgi

I am trying to deploy an API built on Django rest-framework using mod_wsgi and WAMP.
When I created a new Django project using 'django-admin startproject Predictor' and deployed it on WAMP, it was working fine as I can see the default Django window.
Now I created an app in the project using 'python manage.py startapp Predictor'
I built an API which accepts GET call and tested it using 'python manage.py runserver' and it was working fine.
Now I started WAMP service again and try to go to 'localhost:8000' it keeps loading. And as soon as I stop WAMP service it crashes saying Unable to connect. Not sure what's happening. Can someone help what's wrong I am doing? FYI I am on windows and I have created and virtual environment for my Django project. This is how my .conf and wsgi files look like
wsgi_windows.py
activate_this = 'C:/Users/DELL/Envs/predenv/Scripts/activate_this.py'
# execfile(activate_this, dict(__file__=activate_this))
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('C:/Users/DELL/Envs/predenv/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('C:/Users/DELL/Envs/predenv')
sys.path.append('C:/Users/DELL/Envs/predenv/Predictor')
os.environ['DJANGO_SETTINGS_MODULE'] = 'Predictor.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Predictor.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
httpd.conf
Listen localhost:8000
Listen [::0]:80
ServerName localhost:8000
LoadFile "c:/python376/python37.dll"
LoadModule wsgi_module "c:/python376/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/python376"
WSGIPythonPath "C:/Users/DELL/Envs/predenv/Predictor"
httpd-vhosts.conf
WSGIPythonPath "C:/Users/DELL/Envs/predenv/Predictor"
<VirtualHost *:80>
WSGIPassAuthorization On
ErrorLog "logs/predictor.error.log"
CustomLog "logs/predictor.access.log" combined
DocumentRoot "C:/Users/DELL/Envs/predenv/Predictor"
WSGIScriptAlias / "C:\Users\DELL\Envs\predenv\Predictor\Predictor\wsgi_windows.py"
<Directory "C:\Users\DELL\Envs\predenv\Predictor\Predictor">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
The issue was it was not able to 'import pandas' in my views.py
To resolve this I have to add this line in my apache 'httpd-vhosts.conf' file
WSGIApplicationGroup %{GLOBAL}

Can't Deploy Mezzanine CMS using Apache/Ubuntu

I'm trying to become better with deploying a website from scratch so I setup a new Nanode from Linode using the latest Ubuntu (19.04 I think).
I followed the instructions to install Mezzanine CMS (Django Based) and it all went fine, I was able to run the dev server to test the website fine on port 8000.
I did install UFW and right now only activity on port 80 is not rejected. Also I have mysql database running with Mezzanine not Djangos default SQLlite.
I installed Apache and WSGI. At first Apache would not restart due to some configuration issue which I think I've since worked out.
I have a domain alexmerced which is on godaddy, but I have a DNS "a" records which is pointing to my linodes IP address "mezz.alexmerced.com"
Mezzanine is still a fresh install so I haven't really changed anything outside of the WSGI and settings.py.
I currently get a forbidden error when I type the URL or IP address, the permissions of the application directory is 755.
below are my configuration file settings to see if I made any mistakes.
django_project.conf (the only conf file enabled in the /sites-available/ directory
<VirtualHost *:80>
ServerName mezz.alexmerced.com
ServerAdmin mezzanine#localhost
DocumentRoot /mymezz
WSGIScriptAlias / /mymezz/mymezz/wsgi.py
Alias /static /mymezz/static/
<Directory /mymezz/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
here is the wsgi.py file
"""
WSGI config for mymezz project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
from mezzanine.utils.conf import real_project_name
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"%s.settings" % real_project_name("mymezz"))
application = get_wsgi_application()
other stuff I read on asked me to add the following to my settings.py
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(PROJECT_ROOT, ".."))
settings_module = "%s.settings" % PROJECT_ROOT.split(os.sep)[-1]
os.environ["DJANGO_SETTINGS_MODULE"] = settings_module
and
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I never did figure this out with Apache, but I did successfully deploy this blog using Nginx and Uwsgi, just be careful regarding the path your virtual environment once completed I realized that was the problem I was having apache.

Django not running: ModuleNotFoundError: No module named 'encodings'

I have installed django website in a new Ubuntu 17.10 server. It is based on python3. It works fine when i run the website in development mode using
python manage.py runserver
But when i tried to configure it via apache2 webserver in port 80 by adding wsgi in apache by editing default config file as shown..
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/usr/mygpsproject/gps_restapi/gps_restapi>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess gps_restapi python-path=/home/usr/mygpsproject/gps_restapi/ python-home=/home/usr/mygpsproject/venv/bin/
WSGIProcessGroup gps_restapi
WSGIScriptAlias / /home/usr/mygpsproject/gps_restapi/gps_restapi/wsgi.py
</VirtualHost>
but Site is not working. When I checked the apache error log to find this error...
[Sat Apr 14 16:16:09.201739 2018] [core:notice] [pid 5089:tid 140258267696064] AH00051: child pid 7500 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007f906c390bc0 (most recent call first):
I am not able to find what is this encodings module. it worked fine in development mode. so python is not missing any libraries for sure.
What could be the problem with my configuration.
Instead of:
python-home=/home/usr/mygpsproject/venv/bin/
you should be using:
python-home=/home/usr/mygpsproject/venv
Review the documentation on using virtual environments. You are supposed to point to the root of the virtual environment as given by sys.prefix, not the bin directory.
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
Also ensure that the virtual environment is created from the same Python version that mod_wsgi was compiled for.
Finally, depending on the Linux distribution used, you also shouldn't place your code under your personal home directory as the permissions on the home directory can be such that the user that runs your code as under Apache can't access the home directory.

Configureing mod_wsgi on Apache2 for Django project

I am having some issues in configuring the various elements of a mod_wsgi setup. This is my fist time using mod_wsgi so I have been following several tutorials, the main one being a YouTube video Install Django on Apache with mod_wsgi on Linux . From what I understand after following the below steps I should at least see the Django 'It Works' page.
Setup
Ubunto 12.04
Apache 2.2.22
Python 2.7.3
Django 1.6
I have created a WSGI script file firstweb.wsgi in my home directory
/home/firstweb.wsgi
Its contents are
import os
import sys
sys.path = [‘/var/www/firstweb’] + sys.path
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘firstweb.settings’
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I then created a host file for the project located at
/etc/apache2/sites-available/firstweb.conf
Its contents are
<VirtualHost *:80>
ServerAdmin webmaster#firstweb.com
ServerName www.firstweb.com
ServerAlias firstweb.com
WSGIScriptAlias / /home/firstweb.wsgi
Alias /static /var/www/firstweb/static/
<Directory /var/www/firstweb/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I then enabled the configuration file with the command
a2ensite firstweb.conf
I then started my django project.
I went to /var/www/ and used the Django start project command
django-admin.py startproject firstweb
I then restarted my server
sudo service apache2 restart
Lastly I then reconfigured my Apache hosts file to point the domain firstweb.com to my new, local, Django project
/etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu
134.226.38.147 firstweb.com
So with all of this done when I visit www.firstweb.com I should see the Django "It Works!" page
Can anyone tell me what I'm doing wrong?
You haven't listed 'www.firstweb.com' in your /etc/hosts file. Both hosts listed under ServerName and ServerAlias must have a resolvable IP address.

Django ImportError: Could not import settings 'settings' - No module named csrf

I am just starting to fiddle around with django. First I made a small app on my windows machine and verified it worked fine
Then, I zipped the entire project, and opened the zip on a linux machine.
The linux machine was installed with mod_wsgi and django 1.1.1, of course.
I created the following dirs:
/usr/local/bin/ROOT - contains only one file, django.wsgi
/usr/local/bin/ROOT/myapp - root dir of django app
Per the instructions here, I added to httpd.conf:
<VirtualHost *:80>
ServerName server
ServerAlias server
ServerAdmin webmaster#example.com
WSGIScriptAlias /myapp /usr/local/bin/ROOT/django.wsgi
<Directory /usr/local/bin/ROOT/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now per the instructions here I put in /usr/local/bin/ROOT/django.wsgi:
import os
import sys
path = '/usr/local/bin/ROOT'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If it is of any importance: there is nothing set on PYTHOHPATH before the application starts.
After that I did a graceful restart to apache, and went to server/myapp. I got a 500 error.
Lookin in the log I see:
[Sun Dec 05 12:24:17 2010] [error] [client XXXX] ImproperlyConfigured: Error importing middleware django.middleware.csrf: "No module named csrf"
What am I doing wrong? all other threads I found about this always either end up with a conclusion that it's an old version of django (but mine's 1.1.1) or that there are several apps running, but I have only one...
Help?
"django.middleware.csrf" is the package in Django 1.2.x
For Django 1.1.x CSRF settings read the appropriate docs Here
The package in 1.1.x was "django.contrib.csrf.middleware.CsrfMiddleware"
1.1.1 is an old version of Django (the current version is 1.2.3), and that's almost certainly the cause of your problem. There is no django.middleware.csrf in 1.1.1.