/django-production-ready-boilerplate

Production Ready Boilerplate for Django with NGINX, Sentry, Configuration

Primary LanguagePythonMIT LicenseMIT

pg_management

This repo contains all the code of the website from https://pg-management

Development

Uses the default Django development server.

  1. Check that you have your .env.dev and .env.dev.db. If for some reason these files are not generated, remember that you can find samples in the repository.

    $ cat .env.dev
    
    DEBUG=1
    SECRET_KEY=834fnebAB@$##@b$ren(@dbdnNDQE1@!@e@r!ed#d
    DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
    SQL_ENGINE=django.db.backends.postgresql
    SQL_DATABASE=test_dev
    SQL_USER=root
    SQL_PASSWORD=password
    SQL_HOST=db
    SQL_PORT=5432
    DATABASE=postgres
    
    $ cat .env.dev.db
    
    POSTGRES_USER=root
    POSTGRES_PASSWORD=password
    POSTGRES_DB=test_dev
  2. Create docker volumes to save the data:

    $ docker volume create --name=pg_management_postgres_data_dev
    $ docker volume create --name=pg_management_grafana
  3. Build the images and run the containers:

    $ docker-compose up -d --build

    Test it out at http://localhost:8000. The "app" folder is mounted into the container and your code changes apply automatically. Grafana dashboard will be accesible in http://localhost:3000.

Deploying to production

You will have to configure the the Dockerhub account, the Github actions for the CI/CD, DNS and the production server:

  • Dockerhub: You must create an access token to download the Docker images from the production server. Then you must create the following repositories in your Docker Hub account:

    • pg_management

    • pg_management-nginx

    • pg_management-prometheus

    • pg_management-grafana

  • Production server: For this example we will be using Digital Ocean. Digital Ocean provides with droplets with Docker pre-installed. For an easy installation, I really recommend you to use those droplets. image To deploy, you must create a deploy SSH key in your local machine that is going to be used to stablish connection between the CI/CD service and your production server. The following command will create a private ssh key at ~/.ssh/id_deploy_rsa and a public ssh key at ~/.ssh/id_deploy_rsa.pub

    $ ssh-keygen -t rsa -C "your_email@example.com" -f ~/.ssh/id_deploy_rsa -N ''

    To let Github actions deploy to your production server, you must add the id_deploy_rsa.pub in the ~/.ssh/authorized_keys file inside the production server.

    $ echo "ssh-rsa AAAACEzaC1yc2E...GvaQ your_email@example.com" >> ~/.ssh/authorized_keys
  • Environment files: Once the server is created, you must copy the .env files in the production server inside /opt/pg_management path.

    $ cat .env.prod
    $ cat .env.prod.db
    $ cat .env.prod.proxy-companion
    $ cat .env.prod.grafana
  • Github actions: You must enable Github actions in your Github repository and add the required secrets inside Github

    • Push this repository to Github.
    • Enable actions inside Repository > Settings > Actions > Actions permissions > Allow all actions.
    • Create the following secrets inside Repository > Settings > Secrets.
      • DOCKERHUB_USERNAME: Username of your Docker Hub.
      • DOCKERHUB_TOKEN: Access token generated previously in Docker Hub.
      • PRODUCTION_SERVER_USER: User used to connect to the production server via SSH.
      • PRODUCTION_SERVER_IP: Public IP of the production server.
      • SSH_PRIVATE_KEY: Generated deploy RSA private key (id_deploy_rsa).
      • SSH_PUBLIC_KEY: Generated deploy RSA public key (id_deploy_rsa.pub).
  • DNS: It's important that the DNS records are setup properly before deploying the app to the production server. You will need to make sure that you have the following records:

    • A: record to set the IPv4 of your production server
    • AAAA: record to set the IPv6 of your production server
    • CNAME: record to configure the "wwww" subdomain. Screenshot from 2021-07-11 19-28-45

Configuring Grafana in production

It will automatically load the provisioning configurations and serve the dashboard in http://pg-management:3000.

Hints to work with this project

  1. To execute a command inside the Django container in DEVELOPMENT environment.

    $ docker-compose exec web python manage.py shell
  2. To execute a command inside the Django container in PRODUCTION environment.

    $ docker-compose -f docker-compose.prod.yml exec web python manage.py shell
  3. To create Django migrations. In case you want to execute this in production environment, remember to use the -f parameter.

    $ docker-compose exec web python manage.py makemigrations
  4. To execute Django migrations. In case you want to execute this in production environment, remember to use the -f parameter.

    $ docker-compose exec web python manage.py migrate
  5. To create Django admin superuser. In case you want to execute this in production environment, remember to use the -f parameter.

    $ docker-compose exec web python manage.py createsuperuser
  6. To configure the debugger in Pycharm you can use guide in the following link. Remember that you must have a professional license to use this feature.

django-production-ready-boilerplate