Report this

What is the reason for this report?

Setting up Python/Django project to run www.blah.com/directory

Posted on April 16, 2015

Hey Guys,

I’ve been digging around stackoverflow, DO, and reddit for an answer. Most of my attempts have failed. I am looking to run a Python/Django via either a subdomain or directory (which ever is easier).

How do I accomplish this? (LAMP Ubuntu image, Django running on it as well)

Thus far it only runs via www.blah.com:port



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

There are a few ways that you could do this. If you are already running Apache, then using mod_wsgi might be a good approach. This tutorial takes you through its installation and configuration. It should point you in the right direction:

To set it up in a sub-domain, you’ll need to use a separate VirtualHost.

Let us know how it goes, and if you run into any problems.

Heya,

To configure a Python/Django project to run under a specific directory path like www.blah.com/directory, you’ll need to set up your web server (e.g., Nginx or Apache) to route requests to that path to your Django application. Here’s a step-by-step guide on how to do this:

  1. Prepare Your Django Project:

Make sure your Django project is set up and functioning correctly. You should be able to access your Django application at the root URL (e.g., www.blah.com) before configuring it to run under a subdirectory.

  1. Configure Django Settings:

Update your Django project’s settings to include the subdirectory in the ALLOWED_HOSTS and FORCE_SCRIPT_NAME settings.

In your settings.py:

ALLOWED_HOSTS = ['www.blah.com']  # Add your domain here
FORCE_SCRIPT_NAME = '/directory'

The FORCE_SCRIPT_NAME setting adds /directory to the beginning of URLs generated by Django.

  1. Set Up the Web Server:

The configuration steps will vary depending on the web server you are using. Below are examples for both Nginx and Apache.

For Nginx:

Create an Nginx configuration file for your site or edit the existing one. In the location block, specify the path where you want your Django project to run (e.g., /directory). Here’s a basic Nginx configuration:

server {
    listen 80;
    server_name www.blah.com;

    location /directory/ {
        alias /path/to/your/django/project/static/files;  # Set the path to your static files
        proxy_pass http://127.0.0.1:8000;  # Adjust the port if needed
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # ... other server configuration ...
}

For Apache:

Edit your Apache virtual host configuration file. Use the Alias directive to map the subdirectory path to your Django project’s static directory, and use WSGIScriptAlias to specify the WSGI script for your Django project. Here’s a basic Apache configuration:

<VirtualHost *:80>
    ServerName www.blah.com
    DocumentRoot /path/to/your/django/project/static/files

    Alias /directory/static /path/to/your/django/project/static
    Alias /directory /path/to/your/django/project/directory/wsgi.py  # Adjust the path as needed

    <Directory /path/to/your/django/project/static/files>
        Require all granted
    </Directory>

    WSGIDaemonProcess directory_project python-path=/path/to/your/django/project
    WSGIProcessGroup directory_project

    WSGIScriptAlias /directory /path/to/your/django/project/directory/wsgi.py  # Adjust the path as needed
</VirtualHost>
  1. Restart the Web Server:

After making these configuration changes, restart your web server to apply the new settings. For Nginx:

sudo systemctl restart nginx

For Apache:

sudo systemctl restart apache2

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.