Serving static content for your Django project

This procedure will configure your site to serve your Django applications static assets directly from Opalstack's frontend Nginx server via a separate static application. This configuration improves the overall performance of your application in a couple of ways:

  • Since your backend uWSGI server isn't servicing requests for static media, it's able to service dynamic requests more efficiently.
  • Nginx serves static content more efficiently than uWSGI, so your static content will load faster for your visitors.
1

Create a "Nginx Static Only" application and make a note of the application name.

The remainder of these steps will refer to the application as "mystatic".

2

Edit your site to add a site route serving the mystatic application on the URI path /static.

3

SSH into your app's server as the app's shell user.

4

Edit your project's settings.py to set the following variables:

STATIC_ROOT = '/home/username/apps/mystatic'
STATIC_URL = '/static/'
5

Go into your app directory and activate your Python environment:

cd ~/apps/myapp
source env/bin/activate
6

Go into your project directory and run the collectstatic management command. This copies your app's static files into your static app directory.

python manage.py collectstatic
7

Run the following commands to restart your Django instance:

/home/username/apps/appname/stop
/home/username/apps/appname/start