16. Django Python – Deployment

django python 1

Bid TIK Polda Kepri

Persiapan

Siklus deployment:

  1. Development
  2. Testing
  3. Production
1 pNTf9tIEwnU72 dda7 ajg

Kebutuhan:

  1. App
  2. Terminal / Putty
  3. Server / VPS
  4. Domain

Deploy ke Server

deploy ke server.001
  • Referensi:
gist.github.com/HilmiZul

django-deploy.md

Mulai Deploy ke Server

$ apt-get update
$ apt-get install python3-pip python3-dev nginx

$ sudo -H pip3 install --upgrade pip

$ sudo -H pip3 install virtualenv
$ clone https://github.com/writerlab/perpus

$ virtualenv Env
$ source Env/bin/activate 

$ pip install django==2.2.12 pillow django-import-export gunicorn
  • Konfigurasi host.
  • STATICFILES_DIRS tidak dipakai,
    diganti STATIC_ROOT.
ALLOWED_HOSTS = [‘192.168.1.1’]
...
...
STATIC_ROOT = os.path.join(BASE_DIR, ‘static’)
  • Kumpulkan semua konten static ke direktori yg telah ditentukan.
$ python3 manage.py collecstatic
  • Allow firewall.
$ sudo ufw allow 8000
  • Coba jalankan server.
$ python3 manage.py runserver 0.0.0.0:8000
  • Coba jalankan server menggunakan gunicorn.
$ gunicorn --bind 0.0.0.0:8000 perpus.wsgi
  • Keluar dari virtual environment.
$ deactivate
  • Buat service unicorn
$ nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=zulx
Group=www-data
WorkingDirectory=/home/zulx/perpus
ExecStart=/home/zulx/perpus/.env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/zulx/perpus/perpus.sock perpus.wsgi:application

[Install]
WantedBy=multi-user.target
  • Start gunicorn.
$ sudo systemctl start gunicorn
$ sudo systemctl enable gunicorn
$ sudo systemctl status gunicorn
  • Jika ada perubahan pada file gunicorn.service..
$ sudo systemctl daemon-reload
$ sudo systemctl restart-gunicorn
  • Konfigurasi Nginx.
$ nano /etc/nginx/sites-available/perpus
server {
    listen 80;
    server_name 192.168.1.1;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/zulx/perpus;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/zulx/perpus/perpus.sock;
    }
}
  • Enable Nginx
$ ln -s /etc/nginx/sites-available/perpus /etc/nginx/sites-enabled/
$ nginx -t
$ sudo systemctl restart nginx
  • Buat firewall
$ sudo ufw delete allow 8000
$ sudo ufw allow ‘Nginx Full’
  • Akses webapp.
192.168.1.1
  • Nonaktifkan mode debug pada saat mode production.
...
DEBUG = False
...
  • Pada mode production, jangan mengubah-ubah project.
  • Development hanya dapat dilakukan di local atau di github.
  • Lebih baik lagi menggunakan server khusus testing.

Lanjutkan Membaca Materi Lengkap