#7ninjas Django Boilerplate
This project serves as a baseline for real-world Django-based applications.
- Django/Django REST framework
- Redis
- PostgreSQL
- Gunicorn WSGI HTTP Server
- nginx
- Tornado for handling websockets and more
- Celery Distributed Task Queue
- Circus - ZeroMQ-powered Proccess manager
- Config samples for each tool from above
- and more...
- Install VirtualBox.
- Install Vagrant.
- Install Ansible (version 2.0.2).
- Run
sudo ansible-galaxy install -r ./ansible/requirements.yml
from project's root directory. This will install all required dependencies for Ansible to build the project. - Run
sudo -- sh -c "echo '192.168.38.66 boilerplate.local flower.boilerplate.local api.boilerplate.local docs.boilerplate.local ws.boilerplate.local' >> /etc/hosts"
for simplified access to Vagrant machine from your browser (optional). - Run
vagrant up
from project's root directory in order to start the Vagrant machine. If you run this command for the first time, the machine will be provisioned automatically. - Configure PyCharm (if you use PyCharm for development, optional):
- Configure Python interpreter: File > Settings > Project Interpreter > Add Remote > Vagrant > Python interpreter path:
/home/vagrant/boilerplate/venv/bin/python
> OK - Enable Django support: File > Settings > Languages & Frameworks > Django > Enable Django support; Django project root:
{project_dir}
; Settings:boilerplate/settings.py
- Set up run/debug configurations: Run > Edit configurations… > Add new configuration > Django server > Name: Development Server; Host: 0.0.0.0; Port: 8000; Run browser: http://boilerplate.local:8000/; Path mapping:
Local path - path to project on host system : Remote path - path to project on vagrant machine
- Connect the database: Database > Add > Data Source > PostgreSQL > Download Driver > Host:
localhost
; Port:5433
; Database:boilerplate
; User:boilerplate
; Password:{password}
> Configure SSH > Check use SSH tunnel; Proxy host:127.0.0.1
; Port:2222
; Proxy user:vagrant
; Auth type: Key pair; Private key file:./.vagrant/machines/default/virtualbox/private_key
> OK > Test Connection > OK
- Configure Python interpreter: File > Settings > Project Interpreter > Add Remote > Vagrant > Python interpreter path:
#####Notice! This boilerplate uses Ansible Valut in order to encrypt sensitive parts of the configuration. Always encrypt your sensitive variables when working on real projects!
Files host_vars/staging/vault.yml
and host_vars/staging/vault.yml
are encrypted by default (default password is 7ninjas
).
These files are used to store sensitive data, such as database credentials, deployment keys, certificates etc.
Before deploying to public servers, you must:
- Decrypt necessary files by executing
ansible-vault decrypt host_vars/staging/vault.yml host_vars/production/vault.yml
inansible
directory. Use the default password, mentioned above. - Edit configuration in those files as needed.
If it's the first time you touch those files, you MUST change the following:
- database name, user and password;
- Django secret key (http://www.miniwebtool.com/django-secret-key-generator/); Passwords should be strong enough, it's better to use generated ones: (http://passwordsgenerator.net/).
- Encrypt files again with your NEW AND SECURE password using command
ansible-vault encrypt host_vars/staging/vault.yml host_vars/production/vault.yml
. - Create file
vault_pass.txt
in ansible root directory and write your password to it. - Have fun!
- Edit respective files in a
host_vars
directory, as well as inventory files. This repo includes default configuration samples for production and staging environments. - Execute
./deploy <inventory name> <tags>
command in the project's root directory, where is the name of your inventory (e.g. "staging" or "production"), and are optional tags that will execute only the tasks that were marked by this tag (e.g. "provision" tag, which will skip installing most part of the setup and only update the code from a repo and restart services). - Enjoy deployment :)
bin/ansible-playbook -i ./ansible/{environment}.ini ./ansible/site.yml
- Deploy to {environment} servers.
vagrant up
- Start the virtual machine.vagrant halt
- Shutdown the virtual machine.vagrant destroy
- Destroy the virtual machine.vagrant provision
- Triggers provisioning on a running virtual machine.vagrant ssh
- Create an ssh connection with the virtual machine.vagrant reload
- Restarts vagrant machine, loads new Vagrantfile configuration.vagrant status
- Outputs status of the vagrant machine.vagrant suspend
- Suspends the machine.vagrant resume
- Resume a suspended vagrant machine.vagrant share
- Share your Vagrant environment with anyone in the world.
ssh-keygen -t rsa -f ~/.ssh/id_rsa -C "[email protected]"
- Generate a new SSH key (https://help.github.com/articles/generating-ssh-keys/).cat ~/.ssh/id_rsa.pub
- Show SSH public key.cat ~/.ssh/id_rsa
- Show SSH private key.