Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django tutorial on remote server: how to view in my browser?

Tags:

django

I'm getting started with a Django tutorial, and I've run into a snag. Having created the sample "mysite" on my usual domain, I want to be able to display it in my browser. The tutorial points me to http://127.0.0.1:8000. However, that's not going to work, as I'm doing this remotely.

[background information]

What I have done, apparently successfully, is django-admin.py startproject mysite (created mysite directory containing four files) python manage.py runserver (Validating models... 0 errors found, etc.)

The absolute path is /home/toewsweb/public_html/pythonlab/mysite What URL should I be able to use to bring this up in my browser?

I also put mysite at /home/toewsweb/mysite (since it's not supposed to go in a publicly accessible directory) What URL should I be able to use in this case?

This is a virtual private server, so I have access to httpd.conf. I have downloaded and installed mod_wsgi and have added it to the Apache configuration. I actually did set a subdomain with a DocumentRoot of /home/toewsweb/public_html/pythonlab/mysite; however, when I point the browser to that subdomain, I just get the directory listing.

[/background information]

Right now, I just want to know how to view what I'm working on in my browser.

Thanks!

like image 219
Rick Avatar asked Feb 23 '11 22:02

Rick


2 Answers

For development purposes, there's no need to mess about with configuring WSGI (although it's useful to know, as you will need to do it for production). Just start the dev server so that it listens to an external port:

./manage.py runserver 0:8000

This binds to the external IP address, so now you can access your Django site via port 8000 on that server:

http://whatever.my.ip.is:8000
like image 107
Daniel Roseman Avatar answered Oct 26 '22 03:10

Daniel Roseman


You need to setup the apache WSGIScriptAlias directive in your VirtualHost to properly load python and your site. Django's docs have a great explanation on what you need to do.

Basic configuration

Once you’ve got mod_wsgi installed and activated, edit your httpd.conf file and add:

WSGIScriptAlias / /path/to/mysite/apache/django.wsgi

The first bit above is the url you want to be serving your application at (/ indicates the root url), and the second is the location of a "WSGI file" -- see below -- on your system, usually inside of your project. This tells Apache to serve any request below the given URL using the WSGI application defined by that file.

Next we'll need to actually create this WSGI application, so create the file mentioned in the second part of WSGIScriptAlias and add:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

If your project is not on your PYTHONPATH by default you can add:

path = '/path/to/mysite'
if path not in sys.path:
    sys.path.append(path)

just below the import sys line to place your project on the path. Remember to replace 'mysite.settings' with your correct settings file, and '/path/to/mysite' with your own project's location.

OR

The other option is to run the dev server so it's accesible externally like so:

python manage.py runserver 0.0.0.0:80

though please DO NOT use this in production. The dev server is single-threaded, and has not been auditing for security.

like image 27
Sam Dolan Avatar answered Oct 26 '22 02:10

Sam Dolan