This are the basic steps that I follow while I’m deploying to Railway:
- Configure static files
Django app is terrible at handling static files and it needs whitenoise which radically simplified static file serving for Python web apps with a couple of lines of config WhiteNoise allows your web app to serve its own static files, making it a self-contained unit that can be deployed anywhere without relying on nginx, Amazon S3 or any other external service. Especially useful on Raiway, Heroku, OpenShift and other PaaS providers.
Assuming that your static files directory is set e.g.
STATIC_ROOT = BASE_DIR / "staticfiles"
#or
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
Install whitenoise by running:
pip install whitenoise
Edit your settings.py
file and add WhiteNoise to the MIDDLEWARE
list, above all other middleware apart from Django's SecurityMiddleware:
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# ...
]
also in your settings.py file add this for forever-cacheable files and compression support.
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
add whitenoise to your installed apps
INSTALLED_APPS = [
#..
"whitenoise.runserver_nostatic",
#..
]
Collect all static files by running
python manage.py collectstatic
2. Configure Django for Railway
Railway requires you to have this three files a procfile, runtime.txt and requirements.txt.
1. procfile
The procfile contains instructions for gunicorn which enable Serving Python WSGI HTTP to UNIX.
Install gunicorn
pip install gunicorn
create a procfile in the same folder as manage.py and add this to your procfile
web: gunicorn <project_name>.wsgi --log-file -
#or works good with external database
web: python manage.py migrate && gunicorn <project_name>.wsg
2. runtime.txt
In this file you write the python version you’re using on your machine.
run this command to get the python version
python --version
3. requirements.txt
This files contains the various python packages that you have used in your project. python makes it easy to create this file. Just run
pip freeze > requirements.txt
At this point you should have this
3. Prepare the environment variables
At this point you probably have a .env file that contains your environment variables copy all your variables to a secure place you’re gonna need them in a minute.
Create a.gitignore file in the same folder as your manage.py and add .env so that it can be ignored during the push to github.
4. Deploy to Github
Push your project to github
5. Deploy to Railway
To deploy you need to create an account on RAILWAY and link it to your github using the github option.
Click on create new project, select github as the source and navigate to the repository that contains your project.
After the project has finished uploading or still deploying click on variables
then click on raw editor and paste the environment variables you copied and click update variables.
You just deployed your app, got to settings get your domain and you can also set your custom domain name.
Originally published at https://blog.theaibunny.com.