Django/Production
The following is an attempt at detailing steps to push a Django project into production.
Note that this page makes references to several system root folders, particularly srv
and opt
. For details on what root folders are generally meant for, see https://help.ubuntu.com/community/LinuxFilesystemTreeOverview.
However, having said that, these folders are not set in stone and if you have reason to change directory paths, that's okay as long as you change all references to such.
Pre-Setup
If not yet done, create an appropriate database instance for your project. Note that SqLite is not sufficient, as it's unlikely to be able to handle throughput necessary for a server. MySQL/ PostgreSQL/Oracle/etc are designed for this.
Base Project Setup
Project Location
First, create directories and clone your project to the desired location on the server. The recommended location is somewhere like:
/src/web_serve/django/<project_name>/
Initial Production Settings Values
Then edit your project settings for bare minimum production values to get it up and running. This likely includes things like:
- Connection settings for the production database.
- Static file serve locations.
- Allowed Hosts setting.
- Secret Key setting.
- Any possible custom project settings, required to get the project running.
While you're editing the settings file(s), it's recommended to set these as well, but technically it can wait until the end of the project setup.
- Email settings.
- Security settings.
- Any possible custom project settings, required for long-term stability & security
Installation
If your project has any custom installation scripts, now might be a good time to run them. Otherwise, install system dependencies (such as Apt-Get Packages) and then run the standard manage.py
setup commands.
Apt Package Installation
Assuming use of an Ubuntu server, common system packages for production are:
- The desired Python version to serve your project.
- Apache packages, for a standard Django install (alternatively, can use Nginx if you'd prefer).
- Nginx packages, if using Django Channels.
- Redis packages, also if using Django Channels.
Note that these are the bare minimum for a standard Django install. Depending on your project, you might need other things such as NPM packages to manage Programming/JavaScript files.
Setup Python Environment
After installing system packages, you can create a Python Virtual Environment to serve your project from.
The recommended location is something like:
/opt/env/<environment_name>/
Once created, remember to load the environment to your console, and then install project packages. (ex: pip install -r requirements.txt
from project root).
Manage.py Setup Commands
Once all system packages are installed and the environment is setup, we can finally run our manage.py commands.
Depending on your project, you may have additional custom commands to install. But the standard default ones are (in order):
python manage.py migrate
python manage.py collectstatic
python manage.py check
python manage.py makemigrations
in production. This is because migration files should always be created and committed in development, and then pushed up after thorough testing.
Post-Installation Notes
At this point, your project should be set up enough to run via python manage.py runserver
and project UnitTests should pass when run.
While we definitely don't want to serve the project as it is now (and you shouldn't be able to load pages in a web browser at this point), these act as a good check to make sure everything is handling correctly so far.
If you get any errors at this stage, go back and troubleshoot it before proceeding.