How to do it...

To create a deployment script for production, perform these steps:

  1. Make sure to have the deployment/ansible_templates directory with the Jinja templates for service configuration that we created in the previous Deploying on Apache with mod_wsgi for the staging environment recipe.
  2. Create the deployment/production and deployment/production/ansible directories for the Ansible scripts.
  1. There, create a hosts directory with a remote file containing the following content:
# deployment/production/ansible/hosts/remote
[servers]
myproject-apache

[servers:vars]
ansible_python_interpreter=/usr/bin/python3
  1. Create a vars.yml file there with the variables that will be used in the installation scripts and Jinja templates for configurations:
# deployment/production/ansible/vars.yml
---
# a unix path-friendly name (IE, no spaces or special characters)
project_name: myproject

user_username: "{{ project_name }}"

# the base path to install to. You should not need to change this.
install_root: /home

project_root: "{{ install_root }}/{{ project_name }}"

# the python module path to your project's wsgi file
wsgi_module: myproject.wsgi

# any directories that need to be added to the PYTHONPATH.
python_path: "{{ project_root }}/src/{{ project_name }}"

# the git repository URL for the project
project_repo: [email protected]:archatas/django-myproject.git

# The value of your django project's STATIC_ROOT settings.
static_root: "{{ python_path }}/static"
media_root: "{{ python_path }}/media"

locale: en_US.UTF-8
timezone: Europe/Berlin

domain_name: myproject.142.93.167.30.xip.io
django_settings: myproject.settings.production

# letsencrypt settings
letsencrypt_email: [email protected]
wsgi_file_name: wsgi_production.py
  1. Also, we'll need a secrets.yml file with secret values including passwords and authentication keys. First, create a sample_secrets.yml file that will have no sensitive information, but only the variable names, and then copy it to secrets.yml and fill in the secrets. The former file will be under version control whereas the latter will be ignored:
# deployment/production/ansible/sample_secrets.yml
# Django Secret Key
django_secret_key: "change-this-to-50-characters-
long-random-string"

# PostgreSQL database settings
db_name: "myproject"
db_user: "myproject"
db_password: "change-this-to-a-secret-password"
db_host: "localhost"
db_port: "5432"

# Email SMTP settings
email_host: "localhost"
email_port: "25"
email_host_user: ""
email_host_password: ""

# a private key that has access to the repository URL
ssh_github_key: ~/.ssh/id_rsa_github
  1. Now create an Ansible script (a playbook) at deployment/production/ansible/setup.yml for installing all the dependencies and configuring services. Copy the content for this file from https://raw.githubusercontent.com/PacktPublishing/Django-3-Web-Development-Cookbook-Fourth-Edition/master/ch12/myproject_virtualenv/src/django-myproject/deployment-apache/production/ansible/setup.yml
  2. Then create another Ansible script, deployment/production/ansible/deploy.ymlfor dealing with the Django project. Copy the content for this file from https://raw.githubusercontent.com/PacktPublishing/Django-3-Web-Development-Cookbook-Fourth-Edition/master/ch12/myproject_virtualenv/src/django-myproject/deployment-apache/production/ansible/deploy.yml
  1. Create a bash script that you can execute to start the deployment:
# deployment/production/ansible/setup_remotely.sh
#!/usr/bin/env bash
echo "=== Setting up the production server ==="
date

cd "$(dirname "$0")"
ansible-playbook setup.yml -i hosts/remote
  1. Add execution permissions for the bash script and run it:
$ chmod +x setup_remotely.sh
$ ./setup_remotely.sh
  1. If the script fails with errors, it's likely that the dedicated server needs to be rebooted for the changes to take effect. You can do that by connecting to the server via ssh and rebooting as follows:
$ ssh myproject-apache
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-74-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

System information as of Wed Jan 15 11:39:51 CET 2020

System load: 0.08 Processes: 104
Usage of /: 8.7% of 24.06GB Users logged in: 0
Memory usage: 35% IP address for eth0: 142.93.167.30
Swap usage: 0%

* Canonical Livepatch is available for installation.
- Reduce system reboots and improve kernel security. Activate at:
https://ubuntu.com/livepatch

0 packages can be updated.
0 updates are security updates.


*** System restart required ***

Last login: Sun Jan 12 12:23:35 2020 from 178.12.115.146
root@myproject:~# reboot
Connection to 142.93.167.30 closed by remote host.
Connection to 142.93.167.30 closed.
  1. Create another bash script just for updating the Django project:
# deployment/production/ansible/deploy_remotely.sh
#!/usr/bin/env bash
echo "=== Deploying project to production server ==="
date

cd "$(dirname "$0")"
ansible-playbook deploy.yml -i hosts/remote
  1. Add execution permissions for this bash script:
$ chmod +x deploy_remotely.sh
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.254.231