Deploy Django on Apache virtual host: 404 - django

I know there are a lot of articles talking about this issue, but I keep getting a 404 error when deploying a Django site through Apache virtual host. Here is my .conf file:
Listen 8001
<VirtualHost *:8001>
ServerName www.myhostname.com/basic
ServerAdmin caisj#example.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
DocumentRoot /home/browser/BASIC/basic
Alias /static /home/browser/BASIC/basic/static
Alias /media /home/browser/BASIC/_py/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/contrib/admin/media
WSGIScriptAlias /basic /home/browser/BASIC/basic/basic.wsgi
ErrorLog /home/browser/BASIC/basic/log/basic.error.log
LogLevel info
CustomLog /home/browser/BASIC/basic/log/basic.access.log combined
</VirtualHost>
I have tried different combinations of the "ServerName" and other parameters, but when visiting www.myhostname.com/basic, I keep getting a 404 error. The Apache restarts successfully and the log file contains no clues.
Or could anybody help to tell where to find errors? Thanks.

You have set this up to listen on port 8001 and rooted at /basic, so you need to access www.myhostname.com:8001/basic.
Note that you shouldn't set DocumentRoot to the location of your Django files. It isn't necessary to serve a Django app, and it could potentially be a security risk - a misconfiguration could mean that your code files, including any db passwords, might be served.

Related

Adding a Django site breaks my virtual hosts on CentOS 7

I have a CentOS 7 server with about 20 domains all managed automatically with a vhosts file in /etc/httpd/conf.d.
I am trying to learn Django and wanted to set up a single domain using it, so I added a specific virtualhost to the .conf file.
The result is that the Django domain works fine but all the other sites are broken (try to use Django but cause errors).
The Django domain has two folders: djangodomain.com for static files and djangodomain.app for the Python files.
How can I integrate a single Django domain into my existing many-domain virtual host setup?
Existing vhosts.conf:
<VirtualHost *:80>
serveradmin username#domain.com
serveradmin username#domain.com
usecanonicalname off
# www.site.com ยป site.com
rewriteengine on
rewritecond %{HTTP_HOST} ^www\.(.*)$ [nc]
rewriterule ^(.*)$ http://%1 [r=301,l]
# file locations
virtualdocumentroot "/home/username/%0"
<directory "/home/username/*">
allowoverride all
require all granted
options indexes followsymlinks
options +execcgi
options +includes
</directory>
</VirtualHost>
What I added that broke all but the Django site:
<VirtualHost djangodomain.com:80>
ServerName djangodomain.com
ServerAlias www.djangodomain.com
ServerAdmin user#domain.com
DocumentRoot /home/username/djangodomain.com
WSGIScriptAlias / /home/username/djangodomain.app/django.wsgi
<Directory /home/username/djangodomain.app>
Order allow,deny
Allow from all
</Directory>
Alias /robots.txt /home/username/djangodomain.com/robots.txt
Alias /favicon.ico /home/username/djangodomain.com/favicon.ico
Alias /images /home/username/djangodomain.com/images
Alias /static /home/username/djangodomain.com/static
ErrorLog /home/username/djangodomain.logs/error.log
CustomLog /home/username/djangodomain.logs/access.log combined
</VirtualHost>
Crossposted to unix.stackexchange.com
When Apache gets an HTTP request, it needs to know what virtual host is supposed to process this request. Now, the way it works is. Apache picks up the IP address that the client request came into (the server IP, not the client) and it goes through the list of virtual host definitions to find any virtual hosts defined for that IP. If there is more than one, it will look at the Host header and try to match it to a particular ServerName or ServerAlias directive. If it still cannot find one, it will look for a default virtual host.
You have two virtual host definitions. One if the default vhost on port 80 <VirtualHost *:80>. It is the default because it has * for IP. The other is specific for the IP address <VirtualHost djangodomain.com:80>. The IP it uses is whatever djangodomain.com resolves to.
So any request that comes to that IP will be handled by the IP specific config before it might be allowed to drop into default.
To fix this, you need to replace your django virtual host directive with <VirtualHost *:80>, same as your other vhosts. This should put them all at the same parsing priority and it will just use the Host header to figure out the rest.
The said, if you are running a web server with 20+ vhosts, you really need to have better understanding on how it works. Read up on HTTP and web hosting when you have the time.

How to configure apache2 on ubuntu for django restful services

I have django restful services on my ubuntu server which are running on port 84.
When the request is send, it come through the apache2 server which is running on port 80.
Now let say my server ip is "xx.xx.xx.10" and when i call with this url http://xx.xx.xx.10/user where user is the rest service running on django rest framework on port 84. Then the request should go to my django rest service through apache2 and return the desired output.
I tried by using the below configuration in the apache2 :-
<VirtualHost *:80>
WSGIScriptAlias / /myproject/myapp/test/wsgi.py
<Directory "/myproject/myapp/test/">
<Files "wsgi.py">
Require all granted
</Files>
</Directory>
</VirtualHost>
but this is giving internal error saying that test.settings does not exits.
is this the right way to configure the restful services running on different port on apache server.
Did you specify a python path? If you don't, import mysite will not work.
WSGIPythonPath /path/to/mysite.com
For more info see: Django docs modWSGI
I have solved this issued by using the below configurations :-
<VirtualHost *:80>
ServerName mydomain
ServerAlias *.mydomain
ProxyRequests off
ProxyPass / http://localhost:84/
ProxyPassReverse / http://localhost:84/
</VirtualHost>

How to keep Bottle routes handling images from gridfs in mod_wsgi?

Desired Behaviour
Allow Bottle routes to handle requests for images in the format:
<img src="/gridfs/img/my_image.jpg">
On the server, this is being handled by a Bottle route and returning the image as expected:
#route('/gridfs/img/<filename>')
def server_gridfs_img(filename):
# get the image
# return it
Current Behaviour
In a local environment however, with mod_wsgi set up, I am getting the error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Apache/2.4.7 (Ubuntu) Server at localhost Port 80
What I've Tried
This is my local mod_wsgi configuration:
WSGIPythonHome /var/www/html/ENV
WSGIPythonPath /var/www/html:/var/www/html/ENV/lib/python2.7/site-packages:/var/www/html/wsgi
<VirtualHost *:80>
# for all content in static folder - css, js, img, fonts
Alias /static/ /var/www/html/wsgi/static/
# for rockmongo administration
Alias /rockmongo /var/www/html/rockmongo
<Directory /var/www/html/rockmongo>
Order deny,allow
Allow from all
</Directory>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
WSGIScriptAlias / /var/www/html/wsgi/application
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This is my /var/www/html/wsgi/application:
from mybottleapp import application
Directory
Question
The rest of the routes in my application seem to be being honored.
It doesn't seem that Bottle is handling the requests for images like /gridfs/img/my_image.jpg though.
How can I make that happen?
UPDATE
Looked at Apache error log and saw this:
CorruptGridFile: no chunk #1
I am troubleshooting that now...
I posted the solution to the updated situation mentioned above here:
How to mongodump from OpenShift and mongorestore locally on MongoDB 2.4.9?
The data corruption was caused from exporting and importing with RockMongo - apparently sometimes there can be issues with this approach and larger data sets.

403 forbidden when Django folder is located in /root

I'm trying to deploy my local Django site on my Ubuntu 12.04 server. I followed a tutorial, and everything seems to work fine, except Apache won't allow me to store my Django project under /root. More specifically, I get a 403 Forbidden when trying to access my site.
I suspect I need to configure my virtual host in a different manner. Any ideas?
Here is /etc/apache2/sites-available/mysite
<VirtualHost *:80>
ServerAdmin me#mysite.com
ServerName mysite.com
ServerAlias www.mysite.com
WSGIScriptAlias / /root/me/index.wsgi
Alias /static/ /root/me/static/
<Location "/static/">
Options -Indexes
</Location>
ErrorLog /var/log/mysite/error.log
</VirtualHost>
Thanks for the comments. I'll mark this as the correct answer for future reference.
I ended up moving the whole Django project to /home/anotheruser, and defined DocumentRoot as per Aamir Adnans suggestion. After service apache2 restart it started working.

Nginx/Apache serving wrong website

I have nginx as a reverse proxy to apache serving static files for django sites. I have 3 django sites on the server that are working (and have been working for over a year now) but when I try to add a new one either nginx or apache is serving one of the ones that already exist.
For example site A.com & B.com are already on the server. I am attempting to add C.com. I copied over the nginx/apache config files, linked them properly and changed the settings in them properly, restarted nginx & apache sucessfully with no errors but when I try to go to C.com it loads A.com! The DNS has been confirmed as being accurate by Rackspace. I have looked in the log files with no obvious luck. I am assuming this is apache since it is actually rendering the django code for A.com or could nginx be be giving apache the wrong directive?
Also I attempted to unlink one of the working sites that was pointed to the new django code ("C.com", and working!) and then relink (add to sites-enabled) in both nginx & apache and it too now will not work! It just redirects or defaults to A.com.
I've been digging around google with no luck other than apache/nginx defaults but nothing on how to fix this! Any help or direction would be appreciated!
Here are example nginx & apache config files:
nginx
server {
listen 111.111.111.111:80;
server_name mynewdomain.com;
rewrite ^/(.*) http://www.mynewdomain.com/$1 permanent;
}
server {
listen 111.111.111.111:80;
server_name www.mynewdomain.com;
location / {
proxy_pass http://127.0.0.1:8080/;
include /etc/nginx/proxy.conf;
}
location /media {
root /home/django/mynewdomain;
expires 24h;
}
}
apache:
<VirtualHost 127.0.0.1:8080>
ServerName www.mynewdomain.com
ServerAlias mynewdomain.com *.mynewdomain.com
<Directory /home/django/mynewdomain/>
Order deny,allow
Allow from all
</Directory>
Loglevel warn
CustomLog /var/log/apache2/mynewdomain.com.access.log combined
ErrorLog /var/log/apache2/mynewdomain.com.error.log
WSGIDaemonProcess mynewdomain.com user=www-data group=www-data threads=25
WSGIProcessGroup mynewdomain.com
WSGIScriptAlias / /home/django/mynewdomain/apache/django.wsgi
</VirtualHost>
Thanks in advance.
You are missing a line like this in your nginx config, you can place it anywhere in the server block:
proxy_set_header Host $host;