Django Deployment on Apache's virtualhost - django

I have a wordpress (/var/www/cb), which I wish to run as root (www.cb.com) and one Django app (/vc/cb/cb) as a subdirectory (cb.com/launch).
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin info#cb.com
ServerName cb.com
DocumentRoot /var/www/cb
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/cb>
Options Indexes FollowSymLinks MultiViews
AllowOverride FileInfo Indexes
Order allow,deny
allow from all
</Directory>
WSGIScriptAlias /launch /vc/cb/cb/wsgi.py
<Directory "/vc/cb/cb">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
</IfModule>
A similar question has been asked here, however that was pre-Django-1.4, where you had to create your wsgi file manually.
In the documentation it says the wsgi.py is created automatically for you. The main difference I see is the file extension, in the link above they refer to the wsgi file as xx.wsgi while django 1.4 documentation points to wsgi.py. I don't know if this is an issue, but I get a 404 when I do a https://cb.com/launch
The content of wsgi.py:
import os
import sys
sys.path.append('/vc/cb')
sys.path.append('/vc/cb/cb')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cb.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
What am I missing?

Related

What am I missing here? Apache keeps serving default page

I have configured my Django site but Apache keeps serving the default apache's page. What am I missing here? My setup is Django 1.10 on CentOS server with python3.5.
Project structure
/home/sbsadmin/web/sbsportal
requirements.txt
src
manage.py
media
sbsportal
__init__.py
__pycache__
settings.py
urls.py
wsgi.py
static
templates
venv
/etc/httpd/conf.d/sbsportal.conf
LoadModule wsgi_module modules/mod_wsgi.so
<VirtualHost *:80>
ServerAdmin admin#email.com
ServerName sbsportal
ServerAlias sbsportal.com
DocumentRoot /home/sbsadmin/web/sbsportal/src/
WSGIDaemonProcess sbsportal python-path=/home/sbsadmin/web/sbsportal/src:/home/sbsadmin/web/sbsportal/venv/lib/python3.5/site-packages
WSGIApplicationGroup sbsportal
WSGIScriptAlias / /home/sbsadmin/web/sbsportal/src/sbsportal/wsgi.py process-group=djangoproj
<Directory /home/sbsadmin/web/sbsportal/src>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride None
Order allow,deny
Require all granted
</Directory>
<Directory /home/sbsadmin/web/sbsportal/src/sbsportal>
<Files wsgi.py>
Order deny,allow
Allow from all
Require all granted
</Files>
</Directory>
Alias /static /home/sbsadmin/web/sbsportal/src/static
<Directory /home/sbsadmin/web/sbsportal/src/static>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride None
Order allow,deny
Require all granted
</Directory>
Alias /static /home/sbsadmin/web/sbsportal/src/templates
<Directory /home/sbsadmin/web/sbsportal/src/templates>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride None
Order allow,deny
Require all granted
</Directory>
Alias /media /home/sbsadmin/web/sbsportal/src/media
<Directory /home/sbsadmin/web/sbsportal/src/media>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride None
Order allow,deny
Require all granted
</Directory>
</VirtualHost>
wsgi.py
"""
WSGI config for sbsportal 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.10/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sbsportal.settings")
application = get_wsgi_application()
Settings.py
.
.
.
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
.
.
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I have resolved pretty much all the problems. Only thin is that now I am seeing 403 forbidden error.
Did you added selinux context in your project diretory ? if not do the following :
semanage fcontext -a -t httpd_sys_content_t '/home/sbsadmin/myproject/(/.*)'
restorecon -Rv /home/sbsadmin/myproject/
Or try disabling selinux setenforce 0

Django templates not updating

I'm running a small django website with wsgi and apache on an ubuntu server.
I made a minor change to one of my template files in my Django app, and the change is not showing up on my server. However, if I run python manage.py runserver, and curl -l 127.0.0.1:8000, I can see the change. I've tried restarting apache, with no luck.
My urls.py for the page is just
url(r'^page/', TemplateView.as_view(template_name="page.html")),
I've verified that the change is not present by checking on other devices and using incognito mode.
My wsgi.py contains
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_site.settings")
application = get_wsgi_application()
And my /etc/apache2/sites-enabled/000-default.conf contains
<VirtualHost *:80>
ServerName www.site.com
ServerAlias site.com
ServerAdmin webmaster#site.com
Alias /static /opt/django_site/django_site/static
<Directory /opt/django_site/django_site/static>
Require all granted
</Directory>
<Directory /opt/django_site/django_site/django_site>
<Files wsgi.py>
Alias /static /opt/django_site/django_site/static
<Directory /opt/django_site/django_site/static>
Require all granted
</Directory>
<Directory /opt/django_site/django_site/django_site>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess django_site processes=2 threads=15 display-name=%{GROUP} python-path=/opt/django_site:/opt/django_site/django_site/lib/python2.7/site-packages
WSGIProcessGroup django_site
WSGIScriptAlias / /opt/django_site/django_site/django_site/wsgi.py
</VirtualHost>

Django - permission denied

I'm trying to setup Django (Django 1.9.6) on Apache (Apache 2.4.10) in my vps (ubuntu server 15.04).
I followed this guide: HERE
Unfortunately when I try to visit my website, the server return this error (403 Forbidden):`
[authz_core:error] AH01630: client denied by server configuration: /home/user/proj/proj/wsgi.py
I searched everywhere for a solution but everything I tried didn't work.
this is my /etc/apache2/sites-available/000-default.conf file:
Alias /static /home/user/proj/Gestione/static
<Directory /home/user/proj/Gestione/static>
Order deny,allow
Allow from all
</Directory>
<Directory /home/user/proj/proj>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
WSGIDaemonProcess proj python-path=/home/user/proj:/home/user/.local/lib/python3.4/site-p$
WSGIProcessGroup proj
WSGIScriptAlias / /home/user/proj/proj/wsgi.py
Then i found this code on /etc/apache2/apache2.conf, I don't know if this could create problems:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
This is my wsgy.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")
application = get_wsgi_application()
I'm NOT using virtualenv
Thanks for help
In Apache 2.4, you should use Require all granted instead of Order deny,allow and Allow from all.
It looks like you are using the old style in two places in 000-default.conf. Try updating it to:
<Directory /home/user/proj/Gestione/static>
Require all granted
</Directory>
<Directory /home/user/proj/proj>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

how can i resolve the default apache page for django with apache

im new at deploying and i've been trying to deploy my django app and i can only seem to get the default :"it works" page to work. i have followed a few tutorials to no luck.
my '/etc/apache2/sites-available/37.***.22.**' Virtual host:
`<VirtualHost *:80>
ServerAdmin webmaster#37.***.22.**
ServerName 37.***.22.**
DocumentRoot /var/www/37.***.22.**/public_html
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/37.***.22.**/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
WSGI file:
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/.virtualenvs/myprojectenv/local/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('~/MyProject/django-bookmarks')
sys.path.append('~/MyProject/django-bookmarks/django_bookmarks')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_bookmarks.settings'
# Activate your virtual env
activate_env=os.path.expanduser("~/.virtualenvs/myprojectenv/bin/activate_this.$
execfile(activate_env, dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
i had SCP'ed my files for my djagno app into the MyProject dir on the server.
i also ran the a2ensite 37.***.22.25 and restarted apache, but even with that i still get the "it works" default page. how can i resolve this?
i had also installed Postfgress. i really want to deploy this app asap and have been struggling, and learning alot also at the same time! any advise would be appreciated.
thanks in advance.
Make sure that you have mod_wsgi installed and enabled for Apache.
You will also want to ensure that you have defined both WSGIProcessGroup and WSGIScriptAlias (configuration directives for mod_wsgi) for the virtual host.

Deploy a Django site and a PHP site on the same server with Apache and mod_wsgi

I currently have a Django site working at cinepass.com.ec , I would like to deploy an additional PHP site to the same server at mobile.cinepass.com.ec
My current httpd.conf (from DjangoFoo) :
<Directory "/home/ec2-user/cinepass/media">
Order deny,allow
Allow from all
</Directory>
<Directory "/home/ec2-user/cinepass/cinepass">
AllowOverride All
Order deny,allow
Allow from all
</Directory>
Alias /media/ /home/ec2-user/cinepass/media/
ServerAdmin smansfield#palapa.com.ec
ErrorLog "logs/cinepass.com-error_log"
CustomLog "logs/cinepass.com-access_log" common
# mod_wsgi configuration is here
# we are running as user/group 'deamon', if you don't have those you need to change or create.
WSGIDaemonProcess cinepass python-path=/home/ec2-user/cinepass:/home/ec2-user/cinepass/venv/lib/python2.6/site-packages user=daemon group=daemon processes=2 threads=25
WSGIProcessGroup cinepass
# this is our WSGI file.
WSGIScriptAlias / /home/ec2-user/cinepass/cinepass/wsgi.py
My current wsgi.py :
import os, sys
sys.path.append('/home/')
sys.path.append('/home/ec2-user/cinepass/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cinepass.settings_production.py")
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
How would I edit my Apache configuration so that I can also run a php site at mobile.cinepass.com.ec?
Using apache´s virtualhosts, here I put an example of something similar in a server of mine, in which I have a djangp app in the main domain and a joomla in a subdomain. Both files are located in /etc/apache2/sites-enabled
Joomla´s apache conf file (named /etc/apache2/sites-enabled/manual.domain.com):
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin dsanabria#domain.com
ServerName manual.domain.com
DocumentRoot "/home/ubuntu/manual/"
<Directory /home/ubuntu/manual/>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/manual.domain-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/manual.domain-access.log combined
</VirtualHost>
And the django app (named /etc/apache2/sites-enabled/www.domain.co):
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin diego#diegue.us
ServerName domain.co
ServerAlias machete.anotherdomain.com
Alias /admin/media/ /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/
Alias /media/ /home/ubuntu/webapps/machete/machete/media/
Alias /static/ /home/ubuntu/webapps/machete/machete/collected/
<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/django/contrib/admin/media/ >
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/machete/media/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/machete/collected/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptReloading On
WSGIDaemonProcess machete python-path=/home/ubuntu/webapps/machete/lib/python2.7/site-packages
WSGIProcessGroup machete
WSGIApplicationGroup machete
WSGIPassAuthorization On
WSGIScriptAlias / /home/ubuntu/webapps/machete/machete/machete/wsgi.py
ErrorLog /var/log/apache2/machete-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/machete-access.log combined
</VirtualHost>
The first, tells to apache, that if the user gets to manual.domain.com, just response with a php application (joomla). The second file says to apache, that if the user calls the server with www.domain.com response with a python wsgy, (django).
This is in a ubuntu server, redhat/centos/fedora locates the folder sites-enabled in another location that I can´t remember, but anyway you can use virtualhosts.
Generraly, I avoid to mess with the httpd.conf file and prefer use virtualhosts.