- Set up a python virtual environment. E.g. with Anaconda:
conda create -n django-starter python=3.5
source activate django-starter
pip install -r requirements.txt
If psycopg2 installation fails to install, try conda install psycopg2==2.6.2
-
Install
invoke
and related libraries in your virtual environment (you can do this in a separate environment if you prefer):
pip install invoke python-dotenv fabric3 pyyaml semver
-
Add the project name and details to as well as the virtual environment path to
local.yaml.
For Anaconda, if you don't know the path to your virtual environment, you can runwhich python
from within your virtual environment to find your virtual environment path. -
Use
.local.env.example
as a template to create a.local.env
with the project environmental variables.
One-liner for django secret key generation:python -c "import string,random; uni=string.ascii_letters+string.digits+string.punctuation; print repr(''.join([random.SystemRandom().choice(uni) for i in range(random.randint(45,50))]))"
-
Start the postgres database:
inv local.compose -c 'up -d postgres'
-
Check if Docker is running:
docker ps
orinv local.compose -c ps
-
Migrate database:
inv local.manage migrate
-
Start the webserver on port 8000:
inv local.manage 'runserver --insecure'
-
cd etc/server
-
Create your own
.server.env
by copying.server.env.example
-
Set your 'HOST NAME' to describe your server. For Google Cloud use
USERNAME:ubuntu
for Digital Ocean useUSERNAME:root
. Furthermore, for Digital Ocean add your token, whereas for Google Cloud you have to install Google Cloud SDK. -
Make sure
fabric3
is installed in your Python virtual environment. Activate your Python virtual environment, e.g.source activate django-starter
. -
Edit the provision function in fabfile.py:
- For Google Cloud set your project name and machine settings.
- For Digital Ocean set your machine settings.
-
Run
fab provision:gcloud
-
Run
fab ssh_config
-
Run
fab install
-
Run
fab nginx_letsencrypt
-
Provision your cloud server. Follow the steps above or look at the code in fabfile.py if you want to do it yourself.
-
Create .production.yaml and .production.env using .production.yaml.example and production.env.example as reference. The HOST_NAME variable should be the same as that set up for your provisioned machine in ~/.ssh/config, as was done in the steps above.
-
Set the VIRTUAL_HOST and LETSENCRYPT_HOST parameters to your domain name and then go to your domain's DNS settings and create an A record pointing to the IP address of your virtual machine. The nginx-proxy and letsencrypt-plugin docker containers should then take care of the rest when you deploy and automatically generate and configure SSL certificates.
-
Set the IMAGE parameter in .production.yaml to your full docker image location on DockerHub or other container registry.
-
Build your docker image on the server (the last parameter is the version numnber):
inv server.build production 0.0.1
inv server.push production 0.0.1
-
Run your server:
inv compose up production 0.0.1