如何在Railway平台部署GitHub仓库子文件夹中的Django后端?
Let’s walk through the common issues that might be breaking your deployment, and fix them one by one—having both Django and Next.js in a single repo is totally feasible, so let’s get this sorted:
1. Fix the Procfile’s Working Directory & Path
Your current Procfile points to backend.myblog.wsgi, but most deployment platforms (like Heroku, Render, etc.) start running commands from the repo root by default. This means gunicorn can’t locate your Django project properly.
Update your Procfile to first navigate to the backend folder before starting the server:
web: cd backend && gunicorn myblog.wsgi --log-file -
Alternatively, use gunicorn’s --chdir flag to set the working directory directly:
web: gunicorn --chdir backend myblog.wsgi --log-file -
This ensures gunicorn looks for your Django project files inside the backend subfolder.
2. Confirm Dependencies in requirements.txt
Make sure gunicorn is included in your root-level requirements.txt—it’s the WSGI server required to run Django in production. If it’s missing, add it:
gunicorn>=20.1.0 # Add all your other Django dependencies here (Django, psycopg2-binary, etc.)
Double-check that all other required packages are listed correctly too—missing dependencies are a common deployment blocker.
3. Set the DJANGO_SETTINGS_MODULE Environment Variable
Your deployment platform needs to know where to find Django’s settings file. If your settings are in backend/myblog/settings.py, set this environment variable:
DJANGO_SETTINGS_MODULE=myblog.settings
How you set this varies by platform: for Heroku, run heroku config:set DJANGO_SETTINGS_MODULE=myblog.settings in your terminal; for Render, add it in the environment variables section of your deployment dashboard.
4. Update Django’s ALLOWED_HOSTS
In backend/myblog/settings.py, make sure you’ve added your deployment platform’s domain (or localhost for testing—avoid using * in production):
ALLOWED_HOSTS = ['your-deployed-app-domain.com', 'localhost', '127.0.0.1']
For platforms like Heroku, you can make this dynamic with:
import os ALLOWED_HOSTS = [os.environ.get('ALLOWED_HOSTS', 'localhost')]
Then set the ALLOWED_HOSTS environment variable to your app’s actual domain.
5. Add Deployment-Time Django Commands
Don’t forget to run critical setup steps like database migrations during deployment. Most platforms let you define a "release command"—for example, on Heroku, add this to your Procfile or dashboard:
release: cd backend && python manage.py migrate
This ensures your database schema is up to date every time you deploy.
6. Double-Check Your Folder Structure
Verify your repo matches the structure we’re assuming (adjust paths below if your setup is different):
repo-root/ ├── backend/ │ ├── myblog/ │ │ ├── wsgi.py │ │ ├── settings.py │ │ └── ... │ └── manage.py ├── frontend/ │ └── (Next.js project files) ├── requirements.txt ├── runtime.txt └── Procfile
If your Django project is nested deeper (e.g., backend/my-django-app/myblog/), tweak the paths in your Procfile and environment variables to match.
Once you’ve made these adjustments, redeploy your app—this should resolve most common issues with running Django from a subfolder in a monorepo.
内容的提问来源于stack exchange,提问作者Maryll Castelino

