This is a starter pack for quickly developing a Django project locally with Vagrant, which is then easily deployed into production with Ansible. It installs and configures all the services you'll need:
- Vagrant VM based on Ubuntu 18.04
- Python 3.6 virtualenv for your Django app and its dependencies
- Nginx to serve static files and proxy to Gunicorn
- Supervisor to automatically start/restart Gunicorn
- PostgreSQL with database and initial Django migrations
- Minimal Django configuration with Django Admin and Debug Toolbar
- Node.js for installing front-end components (optional)
- Disposable environment is fully self-contained within the Vagrant VM
- Ansible playbooks for both local development and production
- Develop with Django dev server, then test/deploy with Gunicorn/Nginx
- Easily add Ansible galaxy roles (ansible/requirements.yml)
- Activates Python virtualenv on login
- Installs Python packages from
requirements.txt
- Installs Node packages from
package.json
- Configure environment-specific Django settings in
settings_local.py
activate
- activate the virtualenvdeactivate
- deactivate the virtualenvmake run
- run Django development servermake migrate
- makemigrations and migratemake load
- load fixturesmake collect
- collect static filesmake restart
- start/restart Gunicorn & Nginxmake provision-dev
- run playbook for dev (must deactivate virtualenv)make provision-prod
- run playbook production (must deactivate virtualenv)
-
Install Vagrant
-
Clone this repo as your project name: (This is important, the project folder name will be used for configuring database name, hostname, etc.)
git clone [email protected]:paste/dvang.git my-project-name
-
Build your Vagrant VM:
vagrant up
-
Log into the VM via SSH:
vagrant ssh
-
Start Django development server:
cd my-project-name make run
-
Modify your computer's local
/etc/hosts
:192.168.33.55 my-project-name.local
-
Visit your app:
http://my-project-name.local
-
Login to Django Admin with user/pass: admin/admin:
http://my-project-name.local/admin
-
Profit ✔️
-
You'll need a remote user with
sudo
privileges to run Ansible. -
Edit the
host_name
and other settings inansible/prod.yml
as necessary. -
Clone your project onto the server in your remote user's home folder, e.g.
~/my-project-name
-
Install Ansible with the included script:
cd ~/my-project-name sudo ansible/install.sh
-
Install Ansible Galaxy roles:
make install-galaxy-roles
-
Run the Ansible production playbook:
make provision-prod
-
Activate the virtualenv:
# after this you can use "activate" and "deactivate" shortcuts source ~/.bashrc
-
Apply Django migrations:
make migrate
-
Load Django fixtures:
make load
-
Collect Django static files:
make collect
-
Profit ✔️
-
To use HTTPS you will need an SSL certificate. Get one from Certbot here: https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx
-
Copy the certificate and key to your remote user's home folder. The Nginx configuration expects the files to be named after the
host_name
, like this:~/dvang.io.crt ~/dvang.io.key
-
Update the production playboook to use SSL, in
ansible/prod.yml
:nginx_use_ssl: true
-
Enable SSL features for Django, in
src/settings_local.py
:# this enables secure cookies, HTTP redirect, etc. USE_SSL = True
-
Re-run the Ansible production playbook:
make provision-prod
-
Profit ✔️