(I am in way over my head) I am trying to deploy my django server on apache2. I have already buildt a quite large front-end application (that is currently deployed with apache2) and do not want to serve any views through django, rather I want to follow the backend as service principal.
I have followed the instructions here:
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi
After the modwsgi part I get this in my apache log file:
[Tue May 20 12:19:44 2014] [notice] Apache/2.2.22 (Debian) PHP/5.4.4-14+deb7u8 mod_wsgi/3.4 Python/2.7.3 configured -- resuming normal operations
After appending this to the apache config file:
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
WSGIScriptAlias / /var/www/PhotodiceServer/PhotodiceServer/wsgi.py
WSGIPythonPath /var/www/PhotodiceServer
<Directory /var/www/PhotodiceServer/PhotodiceServer>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
This is the content of the wsgi file:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PhotodiceServer.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This is the content of the urls.py file:
from django.conf.urls import patterns, include, url
from django.contrib.auth.models import User, Group
from rest_framework import viewsets, routers
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browseable API.
url(r'^admin/', include(admin.site.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
model = User
class GroupViewSet(viewsets.ModelViewSet):
model = Group
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)
This is the installed apps:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'photod',
)
previously when i entered the ip of the webserver (a raspberry pi) i was automagically routed to index.html that is stored in var/www/.
Now when i enter the ip of the webserve i get 404 page not found, do i have to explicetly say what url will load a static file somehow? and how will this play with the routing in angular? (I have used the angular-ui-router.js)?
(if i set debug =False i get Bad Request (400) instead)
I think you are missing a couple of things in your Apache config:
AddHandler wsgi-script .py
In <Directory xxx>
Options +ExecCGI
SetHandler wsgi-script
Try these, and see what it says then.
EDIT*** as your clearly not familiar with apache!!
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
AddHandler wsgi-script .py
WSGIScriptAlias / /var/www/PhotodiceServer/PhotodiceServer/wsgi.py
WSGIPythonPath /var/www/PhotodiceServer
<Directory /var/www/PhotodiceServer/PhotodiceServer>
Options +ExecCGI
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Another EDIT***
Here is the config I used, (although in the end I just ran uWSGI server and ProxyPass to it as it was a bit of a pain!!)
<VirtualHost *:80>
ServerName my.local.domain
SetEnv APPLICATION_ENV development
Alias /favicon.ico /path/to/project/favicon.ico
AliasMatch ^/static/(.*)$ /path/to/project/static/$1
AddHandler wsgi-script .py
WSGIScriptAlias / /path/to/project/project/wsgi.py
WSGIDaemonProcess _WSGI user=chazmead group=www-data processes=1 threads=5 python-path=/path/to/project/venv/lib/python2.7/site-packages:/path/to/project
WSGIProcessGroup _WSGI
<Directory "/path/to/project/project/">
SetHandler wsgi-script
Options Indexes FollowSymLinks ExecCGI
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
Related
I installed an linux server with apache where I want to run my Django app. So I set everything up and when I started the app everything was shown to me except for the pictures.
I couldn't find a error inside my site.conf
<VirtualHost *:80>
ServerName MYHOST
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Alias /robots.txt /var/www/mysite/static/robots.txt
Alias /favicon.ico /var/www/mysite/static/favicon.ico
Alias /static/ /var/www/mysite/static/
Alias /static/ /var/www/mysite/media/
<Directory /var/www/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite/static>
Require all granted
</Directory>
<Directory /var/www/mysite/media>
Require all granted
</Directory>
</VirtualHost>
or inside my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/mysite/static'
MEDIA_ROOT = '/var/www/mysite/media'
MEDIA_URL = '/media/'
What I did was setting debug = True and inside linux console I run python3 manage.py collectstatic after reloading apache the website was shown with all its css and js but for every image request I get an error 404.
Inside my templates/base.html I call {% load static %}. I tried to replace it with {% load staticfiles from static %} but that crashed my entire app (err 500)
I use Django version 3.x and apache2
[SOLVED]
I did the setups to substitute from runserver to apache. As you can notice below. The Django app isn't running over apache. Could you guide me, please?
mod_wsgi was installed with no issue and I could check it when I run index.py with the following code:
def application(environ, start_response):
start_response('200 OK',[('Content-type','text/html')])
return ['<html><body>Hello World!</body></html>']
I have a Django default application built over startproject, startapp, makemigrations, migrate and are running fine over python webserver (runserver). www.host.com:8000/admin. I mean, The Django Dev Server works perfectly.
I'd like to run Django over Apache.
httpd.conf
<Directory /var/www/html>
Options +ExecCGI
AddHandler wsgi-script .wsgi .py
</Directory>
<Directory /var/www/VEnvs/valoreiovenv/djangoproj/djangoproj>
Options +ExecCGI
AddHandler wsgi-script .wsgi .py
</Directory>
<VirtualHost *:80>
WSGIDaemonProcess contacts python-path=/var/www/VEnvs/valoreiovenv/djangoproj:/var/www/VEnvs/valoreiovenv/lib/python3.5/site-packages
WSGIProcessGroup contacts
WSGIScriptAlias /contacts /var/www/VEnvs/valoreiovenv/djangoproj/djangoproj/wsgi.py
ServerName contacts
<Directory /var/www/VEnvs/valoreiovenv/djangoproj/djangoproj>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /var/www/VEnvs/valoreiovenv/djangoproj/static
<Directory /var/www/VEnvs/valoreiovenv/djangoproj/static>
Require all granted
</Directory>
</VirtualHost>
/var/www/VEnvs/proj/djangoproj/djangoproj/wsgi.py file
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoproj.settings")
application = get_wsgi_application()
urls.py
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
When I ran: www.host.com/contacts I got:
404 Not Found
The requested URL /contacts was not found on this server.
Could you please help me on it?
Versions:
Python 3.5.1 over virtualenv 15.0.2;
Django 1.9.6;
Apache (httpd) 2.4.6 over RedHat.
mod_wsgi 3.4.12
EDIT:
I SOLVED THE ISSUE ABOUT:
The requested URL /contacts was not found on this server.
Thank you all!
so i'm about to lunch my first django website , i currently have a server that has been configured to host php websites and i've decided to test a simple empty project to get familiar with the process
so the python version in this server is bit old (2.6) so i couldn't install latest version of django , i installed 1.6 and since it's just a test that's not important (im going to upgrade python version when my website is ready to lunch )
so i've installed django and created a new project called testing in this dire
/home/sdfds34fre/public_html/
which you can see using this domain
http://novadmin20.com
and after reading documentation on django (unfortunately they have removed doc for 1.6 and i had to use 1.9) and wsgi i've updated my httpd.conf like this
<VirtualHost 111.111.111.111:80>
ServerName 111.111.111.111
DocumentRoot /usr/local/apache/htdocs
ServerAdmin somemeail#gmail.com
<IfModule mod_suphp.c>
suPHP_UserGroup nobody nobody
</IfModule>
<Directory /home/sdfds34fre/public_html/testing/testing>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess testing python-path=/home/sdfds34fre/public_html/testing:/usr/lib64/python2.6/site-packages/
WSGIProcessGroup testing
WSGIScriptAlias / /home/sdfds34fre/public_html/testing/testing/wsgi.py
</VirtualHost>
but even after restarting httpd service when i go to
http://novadmin20.com/testing/
all i see is directory list , am i missing something ?
here is my wsgi.py file
"""
WSGI config for testing 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.6/howto/deployment/wsgi/
"""
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testing.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
DocumentRoot directive is the main root of your problem. (ref)
try this config:
<VirtualHost 111.111.111.111*:80>
ServerName novadmin20.com
WSGIDaemonProcess testing python-path=/home/sdfds34fre/public_html/testing:/usr/lib64/python2.6/site-packages/
WSGIScriptAlias / /home/sdfds34fre/public_html/testing/testing/wsgi.py
<Directory /home/sdfds34fre/public_html/testing/testing>
<Files wsgi.py>
Order deny,allow
Require all granted
WSGIProcessGroup testing
</Files>
</Directory>
</VirtualHost>
I have a functioning Django application on the local development server, however when deyploying it to an Apache + mod_wsgi setup the urls don't seem to get resolved correctly, i.e. the url module within an installed app cannot be loaded. I have been debugging this for quite some time now and would greatly appreciate some help.
There is actually no real error message in the apache logs, so there might be some infinite loop going on, but I wouldn't know where or why. After a long time the request ends with error code 500 and log message: "Daemon process deadlock timer expired, stopping process 'Tianjin'." I found the url import to be the problem through painful debugging and print statements and realizing this is the point where the application gets stuck. The stack is something like this, where import(name) never returns with name being "Tianjin.urls".
import_module [__init__.py:37]
urlconf_module [urlresolvers.py:396]
url_patterns [urlresolvers.py:402]
resolve [urlresolvers.py:366]
get_response [base.py:119]
My setup:
Linux 4.2.0-27-generic #32-Ubuntu SMP x86_64 GNU/Linux
Apache/2.4.12 (Ubuntu) Server MPM: forked threaded
Python 2.7 in a virtual environment called env in the project folder
mod_wsgi-4.4.21, compiled from sources
Django 1.8.5
Directory structure in /home/TianjinAdmin/:
-IRIS
-env
-..
-Tianjin
- __init__.py
- celery.py
- settins.py
- urls.py
- wsgi.py
-TianjinUI
- __init__.py
- admin.py
- models.py
- tests.py
- urls.py
- views.py
-TianjinBE
- ...
- manage.py
Apache Virtual Host Configuration:
<VirtualHost *:80>
ServerAdmin bla#gmail.com
DocumentRoot /var/www/html
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/TianjinAdmin/IRIS/templates/TianjinUI/app
<Directory /home/TianjinAdmin/IRIS/templates/TianjinUI/app>
Require all granted
</Directory>
<Directory /home/TianjinAdmin/IRIS/Tianjin>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess Tianjin python-path=/home/TianjinAdmin/IRIS:/home/TianjinAdmin/IRIS/env/lib/python2.7/site-packages processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup Tianjin
WSGIScriptAlias / /home/TianjinAdmin/IRIS/Tianjin/wsgi.py
</VirtualHost>
Tianjin/wsgi.py
import os, sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Tianjin.settings")
#tried with and without the following two lines
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
print >> sys.stderr, sys.path
application = get_wsgi_application()
relevant Content of Tianjin/settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#'kombu.transport.django',
'kombu.transport.django.KombuAppConfig',
'djcelery',
'TianjinBE',
'TianjinUI',
)
ROOT_URLCONF = 'Tianjin.urls'
Tianjin/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^ui/', include('TianjinUI.urls')),
url(r'^oauth/', include('TianjinBE.urls')),
url(r'^admin/', include(admin.site.urls)),
]
Turns out, it had nothing to do with my configuration. Somewhere hidden in the logs and sometimes swallowed was the warning "Matplotlib is building the font cache using fc-list. This may take a moment.". So I had a 3rd party python library for matplot that blocked the application. Seemingly because of some permissions problem on caching files, see matplotlib taking time when being imported
I am new to working with apache and mod_wsgi. But I do have little experience in django, so from some tutorials I tried to run django app through apache webserver using mod_wsgi.
I created mysite in /var/www/
then in mysite/application I created application.wsgi ...
import os
import sys
sys.path.append('/var/www/mysite/application')
os.environ['PYTHON_EGG_CACHE'] = '/var/www/mysite/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
and in /etc/httpd/sites-available I created file named mysite.conf ...
<VirtualHost *:80>
ServerName mysite.com
ServerAdmin id#somewhere.com
ServerAlias mysite.com
DocumentRoot /var/www/mysite/
<Directory /var/www/mysite>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
</Directory>
WSGIDaemonProcess mysite processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup mysite
</Virtualhost>
Then I ran a2ensite mysite.conf, didn't showed any error.
Then in /etc/httpd/hosts/ I added one line my-ipddress mysite.com
I gave permission chmod 777 to all the above files and to folder /var/www/mysite. Now when I open mysite.com on browser I see apahce's default page nothing from django.
I am using fedora 21.
You haven't put in anything in that configuration to serve your Django site via WSGI: you're missing the WSGIScriptAlias line (see the documentation:
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Note that you shouldn't really be putting things in /var/www; and also you shouldn't need to create your own WSGI file, Django creates one for you when you create a project.