I am trying to run my django project on an apache server. I am not running the django project on a virtualenv. I have the following piece of code in views.py where I get an import error saying No module named startInsight every time. The same if I run with python manage.py runserver 0.0.0.0:80 it works prefectly fine.
Error point in views.py:
sys.path.insert(0,"/Insight/scripts")
import startInsight
The following are the configurations I made:-
/etc/apache2/sites-available/000-default.conf:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /web/mysite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /web/mysite>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
Alias /static /web/mysite/mysite/static
<Directory /web/mysite/mysite/static>
Require all granted
</Directory>
<Directory /web/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mysite python-path=/usr/bin/python:/web/mysite
WSGIProcessGroup mysite
WSGIScriptAlias / /web/mysite/mysite/wsgi.py
</VirtualHost>
/web/mysite/mysite/wsgi.py:
import os
'''
I tried adding these two lines below. Did not make a difference.
import sys
sys.path.insert(0,"/Insight/scripts")
'''
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I am not sure what I am doing wrong and would need some guidance.
Related
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.
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
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()
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>
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?