WSGI Django 403 Forbidden - django

When I run
sudo service apache2 restart
I get an error
unable to resolve host hostname
When I look at the error.txt it says it's my WSGI configuration.
Here is my WSGI script.
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings'
sys.executable = '/usr/lib/python2.7/dist-packages'
application = WSGIHandler()
django.setup()
EDIT1:
Here is my apache2 file. I did change the WSGI file to /user/app/app_settings/wsgi.py
<VirtualHost website:80>
ServerName website.com
ServerAlias www.website.com
ServerAdmin email#website.com
DocumentRoot /home/user/app/static
WSGIScriptAlias / /home/user/app/app_settings/wsgi.py
<Directory /home/user/app/>
Order allow,deny
Allow from all
Require all granted
</Directory>
#Alias /robots.txt /var/www/user/app/robots.txt
#Alias /favicon.ico /var/www/user/app/favicon.ico
#Alias /images /home/user/app/static/images
#Alias /static /home/user/app/static/
ErrorLog /var/www/website/error.log
#CustomLog /var/www/website/access.log combined
</VirtualHost>

Related

Django APPS.PY ignored by Apache2

I have a function in apps.py set to run at Apache boot/restart, however it doesn't and it only works after I pull up the index page.
However, if I use Django's development environment it works perfectly.
APPS.PY
from django.apps import AppConfig
class GpioAppConfig(AppConfig):
name = 'gpio_app'
verbose_name = "My Application"
def ready(self):
from apscheduler.schedulers.background import BackgroundScheduler
from gpio_app.models import Status, ApsScheduler
import gpio_app.scheduler as sched
import logging
logging.basicConfig()
logging.getLogger('apscheduler').setLevel(logging.DEBUG)
sched.cancel_day_schedule()
sched.get_schedule()
sched.daily_sched_update()
sched.add_status_db()
MOD_WSIG 000-default.conf is as follows:
<VirtualHost *:80>
ServerName 127.0.0.1
ServerAlias localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
Alias /static /home/pi/poolprojectdir/static
<Directory /home/pi/poolprojectdir/static>
Require all granted
</Directory>
<Directory /home/pi/poolprojectdir/poolproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess poolproject python- home=/home/pi/poolprojectdir/venv python-path=/home/pi/poolprojectdir
WSGIProcessGroup poolproject
WSGIScriptAlias / /home/pi/poolprojectdir/poolproject/wsgi.py
Any ideas as to how I get apps.py recognised by Apache2?
The problem is with apache loading your wsgi.py lazily - only after the first request arrives.
See this answer on how to fix this.

'404 Not Found. The requested url / was not found on this server' Django deployment with apache2

I'm developing a django website and am currently working on deploying. I'm following this tutorial. And im in the last stage of the tutorial (1:08:00 in the video). After he finished configuration th django-project.conf file he saves and everything is working, but me on the other hand, i get an:
Not Found
The requested URL / was not found on this server.
Apache/2.4.34 (Ubuntu) Server at **** Port 80
This is my projects .conf file:
<VirtualHost *:80>
ServerName _
Redirect 404 /
Alias /static /home/user/project/static
<Directory /home/user/project/static>
Require all granted
</Directory>
<Directory /home/user/project/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/user/project/project/wsgi.py
WSGIDaemonProcess project_app python-path=/home/user/project python-home=/home/user/project/venv
WSGIProcessGroup project_app
</VirtualHost>
This is my urls.py:
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.homepage),
path('rh/', views.rh, name="rh"),
path('gvt/', views.gvt, name="gvt"),
path('fth/', views.fth, name="fth"),
path('pages/', include('pages.urls')),
]
This is my wsgi.py
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'excelsite.settings')
application = get_wsgi_application()
sys.path.append('/home/alexholst/excelsite')
This is the error.log
(Couldnt copy, so i have this screenshot instead)
here

ImportError: No module named site deploying Django with mod-wsgi and httpd

I'm trying to deploy a django project on CentOS 7, but I keep getting ImportError: No module named site on my httpd error log.
This is my config file for httpd located at /etc/httpd/conf.d:
<VirtualHost *:8008>
WSGIProcessGroup programa_registos
WSGIDaemonProcess programa_registos python-home=/var/www/html/programa_registos/.env python-path=/var/www/html/programa_registos
Alias /static /var/www/html/programa_registos/static
<Directory /var/www/html/programa_registos/static>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/html/programa_registos/programa_registos/wsgi.py
<Directory /var/www/html/programa_registos/programa_registos>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
My project is located at /var/www/html/programa_registos and my virtualenv at /var/www/html/programa_registos/.env.
Here is my wsgi.py file, located at /var/www/html/programa_registos/programa_registos:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "programa_registos.settings")
application = get_wsgi_application()

django 1.4 with apache virtualhost - path problems

I'm trying to upgrade my application from django 1.2 to 1.4, which I have tested successfully with the inbuilt webserver.
However, I am having problems with deploying it as a VirtualHost with Apache (on Ubuntu).
my sites-available/default contains:
<VirtualHost *:80>
ServerName myapplication
WSGIScriptAlias / /usr/share/myapplication/wsgi.py
WSGIDaemonProcess myapplication python-path=/usr/share/myapplication:/usr/lib/python2.6/dist-packages
<Directory /usr/share/myapplication>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
the file /usr/share/myapplication/wsgi.py contains the standard:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapplication.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Apache will start ok, but when I go to http://myapplication I get '500: Internal Server Error' and the apache logs show:
ImportError: Could not import settings 'WCReporter.settings' (Is it on sys.path?): No module named WCReporter.settings
Am I using WSGIDaemonProcess correctly? The django docs aren't clear.
Thanks
Solved this with the following, from http://rc98.net/django_wsgi.
sites-available/default:
<VirtualHost *:80>
ServerName myapplication
WSGIDaemonProcess myapplication
WSGIProcessGroup myapplication
WSGIScriptAlias / /usr/share/myapplication/wsgi.py
<Directory /usr/share/myapplication>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
and wsgi.py:
import os,sys
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if path not in sys.path:
sys.path.append(path)
path = '/usr/share/myapplication'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapplication.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Could you print your path at runtime:
#init.py
import sys
print sys.path
have you got an init file in the project base dir?
does it work if you import settings instead of myapp.settings?

Problem with Django setup using mod_wsgi and apache on centos 5.4

I am trying to setup apache with mod_wsgi to serve Django through a nginx proxy on CentOS 5.4. I want to start by configuring Apache with Wsgi to serve on a local port, but I get a 403 error when I run curl localhost:8080
# Httpd config:
Listen 127.0.0.1:8080
NameVirtualHost 127.0.0.1:8080
<VirtualHost 127.0.0.1:8080>
ServerName staging.example.com
LogLevel warn
ErrorLog /home/django/project_name/log/apache_error.log
CustomLog /home/django/project_name/log/apache_access.log combined
DocumentRoot /home/django/project_name/django_project_dir/
WSGIDaemonProcess staging.example.com user=apache group=apache threads=25
WSGIProcessGroup staging.example.com
WSGIScriptAlias / /home/django/project_name/django_project_dir/django.wsgi
<Directory /home/django/project_name/django_project_dir>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
And my wsgi script:
#/home/django/project_name/django_project_dir/django.wsgi
ALLDIRS = ['/home/django/project_dir/virtualenv/lib/python2.4/site-packages']
import os
import sys
import site
prev_sys_path = list(sys.path)
for directory in ALLDIRS:
site.addsitedir(directory)
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
sys.path.append('/home/django/project_name/')
os.environ['PYTHON_EGG_CACHE'] = '/home/django/.python-eggs'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
What is the problem with this configuration?