I am deploying a Django project on Apache. and after configuration, I open the "localhost" in the browser, and nothing showed up and the status bar just keep saying "Waiting for localhost". Here is some info.
1.Environment:
OS: Red Hat Enterprise Linux Server x86
Python: 2.7.2
Django: 1.3.1
Mod_wsgi: 3.3
Apache: 2.2.21
Django project: /var/www/html/server/video1
2.Apache Config file lines added:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / /var/www/html/server/video1/apache/django.wsgi
Alias /media/ /usr/local/apache2/htdocs/media/
Alias /static/ /usr/local/apache2/htdocs/static/
<Directory "/var/www/html/server/video">
Order Deny,Allow
Allow from all
</Directory>
<Directory "/usr/local/apache2/htdocs/media">
Order Deny,Allow
Allow from all
</Directory>
<Directory "/usr/local/apache2/htdocs/static">
Order Deny,Allow
Allow from all
</Directory>
3.Django.wsgi file:
import os
import os.path
import sys
sys.path.append('/var/www/html/server')
sys.path.append('/var/www/html/server/video')
os.environ['DJANGO_SETTINGS_MODULE'] = 'video1.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
4.settings.py lines added:
MEDIA_ROOT='/usr/local/apache2/htdocs/media/'
MEDIA_URL='/media/'
STATIC_ROOT='/usr/local/apache2/htdocs/static/'
STATIC_URL='/static/'
ADMIN_MEDIA_PREFIX='static/admin'
TEMPLATES_DIR = {
"/var/www/html/server/video1/templates"
}
INSTALLED_APPS = {
'django.contrib.admin'
}
5.restart apache
These are what I did, can someone help me to see if somewhere is wrong?
Thank you very much
The path '/var/www/html/server/video' doesn't match what you have in WSGIScriptAlias.
Besides that, did you actually try and deploy a WSGI hello world program first?
Also try setting LogLevel to info in Apache instead of warn. The Apache error log will then contain messages to say whether the WSGI script file is loaded. Right now not obvious whether your browser can't even contact server, or whether WSGI application loaded, but not responding.
Related
I have a Django app which is deployed in local network using Apache + mod_wsgi under Windows. When I run python manage.py runserver, everything works fine. But when I start the Apache Service, I cannot access the app. The only response I get from the access.log is the error code 408. Below is my httpd.conf:
LoadFile "c:/users/felix/appdata/local/programs/python/python37/python37.dll"
LoadModule wsgi_module "c:/users/felix/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/felix/appdata/local/programs/python/python37"
ServerName localhost
WSGIScriptAlias / "D:/dev/test_server/django/django/wsgi_windows.py"
Listen 8000
<VirtualHost *:8000>
WSGIPassAuthorization On
ErrorLog "logs/django.error.log"
CustomLog "logs/django.access.log" combined
Alias /static "D:/dev/test_server/staticfiles"
<Directory "D:/dev/test_server/staticfiles">
Require all granted
</Directory>
<Directory "D:/dev/test_server/django/django">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
And below is the wsgi_windows.py file:
# Activate the virtualenv
activate_this = 'D:/dev/test_server/.venv/Scripts/activate_this.py'
exec(open(activate_this).read(), dict(__file__=activate_this))
import os # noqa
import sys # noqa
import site # noqa
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('D:/dev/test_server/.venv/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('D:/dev/test_server/django')
sys.path.append('D:/dev/test_server/django/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django.settings")
from django.core.wsgi import get_wsgi_application # noqa
application = get_wsgi_application()
I'd appreciate any ideas or hints on the issue.
start apache at command line with 'httpd' and look for error messages. If Apache has a problem at startup there is no message in the log files.
check error.log
You can place 'print('xyz') even in the settings.py and elsewhere and this way by checking error.log see how your app is setup and how far a request is processed. If your app get stuck somewhere like this you find the code where it is stuck
I have the same error after install scipy library and use it in some scrip in django. I found that some libraries like "numpy" and "scipy" only work in the Python main interpreter and you have to force the WSGI to run in the global app group to run it. Adding this line in my conf file work for me.
WSGIApplicationGroup %{GLOBAL}
I'm running Django 2.0.4 with mod_wsgi 4.5.20.
I'm getting an error when I try to deploy a site to our dev environment at /parature. What's weird is that the site deployed at the root of the VirtualHost is responding as normal:
[Tue Apr 10 13:34:08.998704 2018] [wsgi:error] [pid 65245] [client xx.yy.zz:65390] Timeout when reading response headers from daemon process 'parature-develop-https': /var/django/html/parature-develop/config/wsgi.py
I can run the site via runserver with the virtualenv activated. It shouldn't be timing out, as I'm just trying to bring up the Django admin site.
<VirtualHost *:443>
SSLEngine On
ServerName wrds-pub1-dev.example.com
ErrorLog "|/usr/sbin/cronolog /var/log/httpd/errorlog/%Y/%Y-%m-wrds-pub1-dev-error.log"
LogLevel info
WSGIApplicationGroup %{GLOBAL}
# The site I'm adding, which isn't working
WSGIDaemonProcess parature-develop-https python-home=/var/django/virtualenvs/parature-develop request-timeout=600
WSGIProcessGroup parature-develop-https
WSGIScriptAlias /parature /var/django/html/parature-develop/config/wsgi.py process-group=parature-develop-https
<Directory /var/django/html/parature-develop/config>
Require all granted
</Directory>
Alias /parature/static/ /var/django/html/parature-develop/static/
<Directory /var/django/html/parature-develop/static>
Require all granted
</Directory>
# The site which has been and continues to work
WSGIDaemonProcess django-wrds-dev-https python-home=/var/django/virtualenvs/django-wrds-dev request-timeout=600
WSGIScriptAlias / /var/django/html/django-wrds-dev/config/wsgi.py process-group=django-wrds-dev-https
<Directory /var/django/html/django-wrds-dev/config>
Require all granted
</Directory>
Alias /static/ /var/django/html/django-wrds-dev/static/
<Directory /var/django/html/django-wrds-dev/static>
Require all granted
</Directory>
Alias /media/ /var/media/wrds-www/
<Directory /var/media/wrds-www>
Require all granted
</Directory>
</VirtualHost>
I feel like I'm missing something obvious, but can't see it. I've got a similar configuration in another VirtualHost with multiple Django projects under the same domain, and that is working fine, as long as the root site comes last.
The wsgi.py is almost exactly the same as the site that is working as well:
import os, sys, logging
from socket import gethostname
from django.core.wsgi import get_wsgi_application
# Since this powers Apache, let's route Python errors to the Apache
# log rather than STDOUT, where they'll never be seen.
logging.basicConfig(stream=sys.stderr)
# Figure out where we're at, and add the parent to the path
sys.path.append(os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))
# wrds-pub1-dev server
if 'wrds-pub1-dev' in gethostname():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
# wrds-pub* production servers.
elif 'wrds-pub' in gethostname():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
# else use dev settings.
else:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
application = get_wsgi_application()
Any ideas?
I figured it out - our dev server (since there's never a good time for a rebuild) is our only non-Ansiblized server, and still running mod_wsgi built against Python 3.5. The virtualenv was built against Python 3.6.
I rebuilt the virtualenv against Python 3.5, and everything works. Hopefully this saves someone hair-pulling in the future!
I am no expert on Django. I am able to run the app on my local machine well. I want to deploy this app on our internal server.
Versions I am using: Python: 2.7, Django 1.11, Apache: 2.4
Ports Open: 9991
When I access the port from any machine, it gives me no error. For e.g. xxx.xx.xxx.xxx:9991 shows Apache page.
But when I add the URL to the app
xxx.xx.xxx.xxx:9991/WebApp1Url
it gives error.
Below is my configuration of httpd.conf
Listen 9991
<VirtualHost *:9991>
Alias /static "C:/xxx/DjangoSite/WebApps/WebApp1/static"
<Directory "C:/xxx/DjangoSite/WebApps/WebApp1/static">
Require all granted
</Directory>
<Directory "C:/xxx/DjangoSite/WebApps/WebApps">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Below is my wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "WebApps.settings")
application = get_wsgi_application()
from WebApp1.wsgi import WebApp1
application = WebApp1(application)
settings.py contains:
WSGI_APPLICATION = 'WebApps.wsgi.application'
I can assure that the paths to static and wsgi.py are correct. I can't figure out what is not right. Can someone please provide pointers as to which step is missing? Any help is appreciated. Pardon any typos above. Thank you in advance.
The first stop for any problems, new user or experienced, should be Django's very detailed, extensive, up-to-date, versioned documentation. There's even a section for deploying a Django project with Apache HTTPD.
Some stuff quoted below, but broadly:
Install Apache HTTPD with mod_wsgi (installing the module on Windows is probably the hard part, the mod_wsgi docs link to this guide)
Add WSGI directives to a conf file
The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi. You’ll probably want to start with the installation and configuration documentation.
Basic configuration
Once you’ve got mod_wsgi installed and activated, edit your Apache server’s httpd.conf file and add the following. If you are using a version of Apache older than 2.4, replace Require all granted with Allow from all and also add the line Order deny,allow above it.
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonHome /path/to/venv
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
I've been trying to work out how I'll go about setting up a Django application on production, when it's ready for deployment. I'm using Django v1.11, and my EC2 is running Ubuntu 14.04. I have been attempting to refer to this guide as reference, however it is not specific to Ubuntu, so I've been experiencing a bit of difficulty in this regard. I've referred to several other resources, but much of what is out there seems to be outdated.
I have a host rule set up on my local machine, pointing www.example.com to my EC2 instance's public IP address.
I have a virtualenv set up which lives in /home/django/example.com/ENV. My Django project lives in /home/django/example.com directly. The project name is mysite, and was generated using django-admin startproject mysite, thus it has the default wsgi.py file inside the /home/django/example.com/mysite directory. The contents of wsgi.py look like:
"""
WSGI config for mysite 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
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I've tried adding VirtualHost rules such as the following:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory "/home/django/example.com/mysite">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Similarly, I've tried adding:
Include /etc/apache2/httpd.conf
to /etc/apache/apache2.conf and chucking the following:
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
WSGIPythonHome /home/django/example.com/ENV
WSGIPythonPath /home/django/example.com
<Directory /home/django/example.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
into httpd.conf.
In either case, I've restarted the Apache server directly afterwards.
I'm not getting any further than hitting "500 Internal Server Error" or hitting an "ERR_EMPTY_RESPONSE".
Anyone able to shed some light around a) where I'm going wrong, b) where I can refer to for up-to-date instructions, or c) how I can troubleshoot this?
After a lot of troubleshooting, and consulting with the resource mentioned in Graham's comment, here's what I established was required in my VirtualHost:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Alias /static /home/django/example.com/static
<Directory /home/django/example.com/static>
Require all granted
</Directory>
WSGIDaemonProcess mysite python-path=/home/django/example.com:/home/django/ENV/lib/python3.4/site-packages
WSGIProcessGroup mysite
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory /home/django/example.com/mysite>
Require all granted
</Directory>
</VirtualHost>
and here are the contents of wsgi.py that I settled on:
"""
WSGI config for mysite 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, sys
from django.core.wsgi import get_wsgi_application
path = '/home/django/example.com'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
And it is also worth noting (simply because it wasn't immediately obvious to me) that it is necessary to specify:
STATIC_ROOT = '/home/django/example.com/static'
in settings.py, and to run python manage.py collectstatic to collect static files from all applications into said STATIC_ROOT.
I hope this helps somebody else, in future!
I am trying to set up apache with an existing django project using the tutorial in django site here. My os is Ubuntu, and everything is installed (django apache2 libapache2-mod-wsgi)
My conf file
WSGIPythonPath /home/avlahop/development/django/rhombus2/rhombus/
<VirtualHost *:80>
ServerName myrhobmus.com
ServerAlias www.myrhombus.com
WSGIScriptAlias / /home/avlahop/development/django/rhombus2/rhombus/rhombus/wsgi.py
</VirtualHost>
After I created the conf file I ran the a2ensite Ubuntu command for enabling the site.
Putting WSGIPythonPath inside VirtualHost gives me an apache configtest failure
Files inside directive inside directory (as described in the example) gives me the same failure
If I go to www.myrhombus.com I get a Google chrome could not find the specified address message.
What am I doing wrong? Every tutorial on the Internet is using the old file.wsgi while now Django creates this for you but it is a python file with .py extension. How can I serve my django with apache? And If I wanted to go production at my own server where would you put django code and where would you put template files?
EDIT: I am only getting the Index of / page. Is there something I have to do with mysites location in terms of permissions? Could you give me a working example of an django site apache conf file?
I use Django 1.8 and I successfully deployed my project Apache server. I followed basic concept like you and enable Apache this module early.
enable module
You can my project structure this question.Refer this question to understand project structure
--------------this is my virtual host file----------------------------------
WSGIPythonPath /home/umayanga/Desktop/view_site/serialKey_gen_site:/home/umayanga/Desktop/view_site/serialKey_gen_site/myvenv/lib/python3.4/site$
<VirtualHost *:80>
ServerAdmin admin#key.com
ServerName key.com
ServerAlias www.key.com
Alias /templates/ /home/umayanga/Desktop/view_site/serialKey_gen_site/templates/
Alias /static/ /home/umayanga/Desktop/view_site/serialKey_gen_site/static/
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/static">
Require all granted
</Directory>
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/templates">
Require all granted
</Directory>
WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
-----------------wsgi.py---------------------------------------
"""
WSGI config for mysite 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.8/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I think this will be help to you.