how to make django render the default home page instead of apache - django

As is known, after issuing a request with a sole IP or server name through web explorer, apache will return its default static home page to the explorer. I'm using django and apache with mod_wsgi to deploy my site(i.e. django app on apache web server). How to route the processing to django mapping rules defined in views.py to dynamically produce a home page(i.e. index page) for that site when I issue a request to that server running apache? Thanks in advance!
I added the following configuration in the main Apache 'httpd.conf':
<VirtualHost 127.0.0.1:80>
ServerName 127.0.0.1
ServerAlias example.com
ServerAdmin webmaster#example.com
DocumentRoot /usr/local/www/documents
Alias /robots.txt /usr/local/www/documents/robots.txt
Alias /favicon.ico /usr/local/www/documents/favicon.ico
Alias /media/ /usr/local/www/documents/media/
<Directory /usr/local/www/documents>
Require all granted
</Directory>
WSGIDaemonProcess 127.0.0.1 processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup 127.0.0.1
WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi
<Directory /usr/local/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Code in myapp.wsgi:
def application(environ, start_response):
status = '200 OK'
output = b'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Then I issued 127.0.0.1, the explorer displayed 'It works!', which is the default page produced by apache, instead of which I wanted 'Hello world!' returned in this case. I just want that when just issued an IP or site address, the work of generating the default home page is directly delegated to django to dynamically handle rather than to return a static index.html by Apache. Although I configured WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi , this seems still not work.

Virtual hosts don't work in that way for 127.0.0.1 as it is treated a bit special. What is going to happen, unless that is the only VirtualHost in your configuration, is that Apache will fall back to using the first VirtualHost definition it found, usually the default site.
So normally you would have:
<VirtualHost *:80>
ServerName my.fqdn.host.name
...
</VirtualHost>
That is, no IP in VirtualHost directive and ServerName is the actual hostname that appears in the URL, not an IP address.
Was there a prior VirtualHost in httpd.conf or was it including including extra/httpd-vhosts.conf, that would result in a VirtualHost superseding this one?

Related

Django Apache and name-based VirtualHost

I have recently locally deployed Django project on Apache server Fedora 36. Everything work good when accessing site by ip. The issue that could not access it by hostname. I am getting "Bad Request (400)" error.
here my httpd.conf
<VirtualHost *:80>
ServerName calljournal.local
alias /static /var/www/django_project/call_journal/static
<Directory /var/www/django_project/call_journal/static>
Require all granted
</Directory>
<Directory /var/www/django_project/call_journal>
Require all granted
</Directory>
WSGIDaemonProcess calljournal.local python-path=/var/www/django_project/virt/lib/python3.8/site-packages
WSGIProcessGroup calljournal.local
WSGIScriptAlias / /var/www/django_project/call_journal/call_journal/wsgi.py
redirect / https://192.168.1.109
</VirtualHost>
and my hosts file
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.109 calljournal.local
You need to enter the hostname in ALLOWED_HOSTS settings.

Why POST requests are turned into GET in django with wsgi?

I have a Django 1.9.4 site.
In localhost, I could easily submit the form and have the results that I want.
But when it's on the server, the POST request turns into a GET request.
On local, I use python manage.py runserver but on server I use Apache2, WSGI
Local:
django 1.9.4
python 2.7.10
Ubuntu 15.10
Server:
same django
python 2.7.6
Server version: Apache/2.4.7 (Ubuntu)
Ubuntu 14.04
Server example.com.conf:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin email#gmail.com
Alias /static /var/www/example.com/static
<Directory /var/www/example.com/static>
Require all granted
</Directory>
Alias /media /var/www/example.com/media
<Directory /var/www/example.com/media>
Require all granted
</Directory>
<Directory /var/www/example.com/wh>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess webhistory python-path=/var/www/example.com:/var/www/example.com/env/lib/python2.7/site-packages
WSGIProcessGroup webhistory
WSGIScriptAlias / /var/www/example.com/wh/wsgi.py
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
Alias /robots.txt /var/www/example.com/robots.txt
Alias /favicon.ico /var/www/example.com/favicon.ico
</VirtualHost>
And the wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wh.settings")
application = get_wsgi_application()
And the craziest part is that even my admin app doesn't work properly.
All the POST submitted forms turn into GET ones.
Here is the error:
request <WSGIRequest: GET '/someurl/'>
What's the problem?
Is it related to APPEND_SLASH = True?
And here's what's realley bothering me... The form submission worked once! why isn't it working again?

going to www version of site url receives 404 on django site

I can get to the route of my site without issues like this: http://example.com
When I go to http://www.example.com I get a 404 error.
How can I handle going to www and delivering the user to the route of the site?
I don't think it is related, but here is my httpd.conf:
WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>
WSGIDaemonProcess example.com display-name=%{GROUP}
WSGIProcessGroup example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /example
WSGIScriptAlias / /example/wsgi.py
</VirtualHost>
Alias /static/ /example
<Directory /example>
Order deny,allow
Allow from all
</Directory>
WSGIPythonPath /example
<Directory /example>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Change ALLOWED_HOSTS in the settings.py to something like this
ALLOWED_HOSTS = ['www.example.com', 'example.com']
According to Django Documentation, ALLOWED_HOSTS defines
A list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent an attacker from poisoning caches and triggering password reset emails with links to malicious hosts by submitting requests with a fake HTTP Host header, which is possible even under many seemingly-safe web server configurations.
For more ALLOWED_HOSTS

Domain url redirecting to localhost in apache2

I am facing a problem while mapping my domain name with my hosted django application running in 8000 port. I have seen a lot of posts regarding this issue but I think I am missing out something. I have tried many ways but all failed. My domain name is coachingfunda.com which is mapped with my ec2 public ip address in Godaddy. My 000-default.conf file is
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
LoadModule deflate_module /usr/lib/apache2/modules/mod_deflate.so
<VirtualHost *:80>
WSGIScriptAlias /wsgi/ /home/ubuntu/public_wsgi/
#ProxyPreserveHost On
#ProxyPass / http://nrollin.com:8080/nrollin
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
#ProxyPassMatch ^(.*)$ http://localhost:8000/$1
#ProxyPass / http://coachingfunda.com/
# ProxyPassReverse / http://127.0.0.1:8000/
ServerAlias www.coachingfunda.com
ServerName coachingfunda.com
#AliasMatch ^/(.*) http://www.coachingfunda.com:8000/$1
Redirect permanent http://coachingfunda.com http://www.coachingfunda.com:8000/
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
You can see that this url is working http://coachingfunda.com:8000/ which means my app is properly hosted but http://coachingfunda.com is redirecting to localhost:8000.
The problem seems stupid but I am stuck for about 2 days. Please help me here. My coachingfunda.conf is
<VirtualHost *:8000>
ServerAdmin webmaster#mydomain.com
ServerName coachingfunda.com
ServerAlias www.coachingfunda.com
WSGIScriptAlias / /var/www/coachingfunda/index.wsgi
Alias /static/ /home/ubuntu/coachingfunda/static/
<Location "/static/">
Options -Indexes
</Location>
<Directory "/home/ubuntu/coachingfunda/static/">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Please help me. Any suggestion may work.
Take a backup of your default conf and copy the conf in your site into the default conf and reload and restart the server.

Django Based framework virtual host setup https on Apache

I am using a django based framework and have successfully figured Apache settings for http mode. Basically I have done the setting correctly on <VirtualHost *:80> ... </VirtualHost> and when I do, http://mysite.domain.com I get routed correctly to my site and the site pages and the skins get render correctly.
I have setup https://mysite.domain.com to work with shibboleth, shibboleth is working and when use the https I get routed to login credential page via shibboleth server, and after successful login I get redirect to https://mysite.domain.com but site doesn't get rendered correctly and skins don't show up as same as http://mysite.domain.com.
Here is my Apache settings, I am trying to understand what I am doing wrong here
<VirtualHost *:443>
ServerAdmin myname#mydomain.com
DocumentRoot /code/vEnviornment/mysite
ServerName mydomain.com
#<LocationMatch "^(?!/admin)">
#<LocationMatch "^(?!/m)">
# RewriteEngine on
# RewriteRule django.wsgi(.*)$ https://mydomain.com:443$1 [L,R=301]
#</LocationMatch>
SSLEngine on
#your SSL keys
#I have removed this wasn't comfortable putting SSL key info
#Alias /admin/media/ /usr/local/lib/python2.6/site-packages/django/contrib/admin/media/
Alias /admin/media/ /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/
WSGIScriptAlias /m/ /code/vEnviornment/mysite/django.wsgi
<Directory "/">
AuthType shibboleth
ShibRequestSetting requireSession 1
Require valid-user
</Directory>
Alias /Shibboleth.sso /tmp
# CustomLog /var/log/httpd/mysite/access_log common
# ErrorLog /var/log/httpd/mysite/error_log
CustomLog /var/log/apache2/mysite/access_log common
ErrorLog /var/log/apache2/mysite/error_log
</VirtualHost>
And here is how I have hetup http:
<VirtualHost *:80>
ServerAdmin myname#mydomain.com
DocumentRoot /code/vEnviornment/mysite
ServerName mysite.mydomain.com
#aliases to serve static media directly
#will probably need adjustment
Alias /m/ /code/vEnviornment/mysite/static/
Alias /upfiles/ /code/vEnviornment/mysite/myframework/upfiles/
<DirectoryMatch "/code/vEnviornment/mysite/myframework/skins/([^/]+)/media">
Order deny,allow
Allow from all
</DirectoryMatch>
<Directory "/code/vEnviornment/mysite/myframework/upfiles">
Order deny,allow
Allow from all
</Directory>
#must be a distinct name within your apache configuration
WSGIDaemonProcess mysite2
WSGIProcessGroup mysite2
WSGIScriptAlias / /code/vEnviornment/mysite/django.wsgi
#make all admin stuff except media go through secure connection
<LocationMatch "/admin(?!/media)">
RewriteEngine on
RewriteRule /admin(.*)$ https://128.101.35.71/admin$1 [L,R=301]
</LocationMatch>
# CustomLog /var/log/httpd/mysite/access_log common
# ErrorLog /var/log/httpd/mysite/error_log
CustomLog /var/log/apache2/mysite/access_log common
ErrorLog /var/log/apache2/mysite/error_log
LogLevel debug
</VirtualHost>
What am I doing wrong here to render the site incorrectly via https?
Alias /m/ /code/vEnviornment/mysite/static/
Alias /upfiles/ /code/vEnviornment/mysite/myframework/upfiles/
These two lines are missing in https virual host
and
your WSGIScriptAlias should point to / not /m/