By kaiserhase
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!
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:
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.
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.
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>
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.