Skip to content

Instantly share code, notes, and snippets.

@openhubdev
Last active December 15, 2020 23:35
Show Gist options
  • Save openhubdev/5171adc75470394bbd077d3179d17ca3 to your computer and use it in GitHub Desktop.
Save openhubdev/5171adc75470394bbd077d3179d17ca3 to your computer and use it in GitHub Desktop.

Django Deployment to Digital Ocean w Ubuntu 18.04 | Ubuntu 18.04를 사용하여 Digital Ocean에 Django 배포

Reference: digital ocean doc | Credit to bradtraversy

  • "$" 있는 명령 : 로컬
  • "#" 있는 명령 : 원격 서버 (이 경우 Digital Ocean)

Create A Digital Ocean Droplet | 첫째, 디지털 바다 방울 만들기

그 다음,

Security & Access

Creating SSH keys (Optional) SSH 키 만들기 (없으면 | 선택 사항)

You can choose to create SSH keys to login if you want. If not, you will get the password sent to your email to login via SSH 원하는 경우 로그인 할 SSH 키를 만들도록 선택할 수 있습니다. 그렇지 않은 경우 SSH를 통해 로그인 할 수 있도록 이메일로 비밀번호가 전송됩니다.

To generate a key on your local machine 로컬 컴퓨터에서 키를 생성

$ ssh-keygen

Hit enter all the way through and it will create a public and private key at the end 엔터를 끝까지 누르면 공개 및 개인 키가 생성됩니다.

~/.ssh/id_rsa
~/.ssh/id_rsa.pub

You want to copy the public key (.pub file) 공개 키 (.pub 파일)를 복사

$ cat ~/.ssh/id_rsa.pub  
or
$ pbcopy < ~/.ssh/id_rsa.pub

Copy the entire output and add as an SSH key for Digital Ocean 전체 출력을 복사하고 Digital Ocean 용 SSH 키로 추가 서버에 로그인

Login To Your Server

If you setup SSH keys correctly the command below will let you right in. If you did not use SSH keys, it will ask for a password. This is the one that was mailed to you

$ ssh root@YOUR_SERVER_IP

Create a new user 새 사용자 만들기

It will ask for a password, use something secure. You can just hit enter through all the fields. I used the user "djangoadmin" but you can use anything

# adduser djangoadmin

Give root privileges 루트 권한 부여

# usermod -aG sudo djangoadmin

SSH keys for the new user 새 사용자를위한 SSH 키

Now we need to setup SSH keys for the new user. You will need to get them from your local machine

Exit the server

You need to copy the key from your local machine so either exit or open a new terminal

# exit

You can generate a different key if you want but we will use the same one so lets output it, select it and copy it

$ cat ~/.ssh/id_rsa.pub
or
$ pbcopy < ~/.ssh/id_rsa.pub

Log back into the server 서버에 다시 로그인

$ ssh root@YOUR_SERVER_IP

Add SSH key for new user 새 사용자를위한 SSH 키 추가

Navigate to the new users home folder and create a file at '.ssh/authorized_keys' and paste in the key 새 사용자 홈 폴더로 이동하여 '.ssh / authorized_keys'에 파일을 만들고 키에 붙여 넣습니다.

# cd /home/djangoadmin
# mkdir .ssh
# cd .ssh
# nano authorized_keys

>>> Paste the key and hit "ctrl-x", hit "y" to save and "enter" to exit

Login as new user 새 사용자로 로그인

$ ssh djangoadmin@YOUR_SERVER_IP

Disable root login 루트 로그인 비활성화

# sudo nano /etc/ssh/sshd_config

Change the following

PermitRootLogin no
PasswordAuthentication no

Reload sshd service

# sudo systemctl reload sshd

Simple Firewall Setup

See which apps are registered with the firewall

# sudo ufw app list

Allow OpenSSH

# sudo ufw allow OpenSSH

Enable firewall 방화벽 활성화

# sudo ufw enable

To check status

# sudo ufw status

We are now done with access and security and will move on to installing software 이제 액세스 및 보안이 완료되었으며 소프트웨어 설치로 넘어갈 것입니다.

Software

Update packages

# sudo apt update
# sudo apt upgrade

Install Python 3, Postgres & NGINX

# sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl

Postgres Database & User Setup

# sudo -u postgres psql

You should now be logged into the pg shell

Create a database

CREATE DATABASE btre_prod;

Create user

CREATE USER dbadmin WITH PASSWORD 'abc123!';

Set default encoding, tansaction isolation scheme (Recommended from Django)

ALTER ROLE dbadmin SET client_encoding TO 'utf8';
ALTER ROLE dbadmin SET default_transaction_isolation TO 'read committed';
ALTER ROLE dbadmin SET timezone TO 'UTC';
//ALTER ROLE dbadmin WITH SUPERUSER; -> makes dbadmin SUPERUSER

Give User access to database

GRANT ALL PRIVILEGES ON DATABASE btre_prod TO dbadmin;

Quit out of Postgres

\q 

Vitrual Environment

You need to install the python3-venv package

# sudo apt install python3-venv

Create project directory

# mkdir pyapps
# cd pyapps

Create venv

# python3 -m venv venv

Activate the environment

# source venv/bin/activate

Git & Upload

Pip dependencies

From your local machine, create a requirements.txt with your app dependencies. Make sure you push this to your repo 로컬 머신에서 앱 종속성이있는 requirements.txt를 만듭니다. 이것을 저장소에 푸시하십시오

$ pip freeze > requirements.txt

Create a new repo and push to it (you guys know how to do that) 새 저장소를 만들고 git push

Clone the project into the app folder on your server (Either HTTPS or setup SSH keys)

# git clone https://github.com/yourgithubname/btre_project.git 

Install pip modules from requirements

# pip install -r requirements.txt

Local Settings Setup

Add code to your settings.py file and push to server settings.py 파일에 코드를 추가하고 서버에 푸시

try:
    from .local_settings import *
except ImportError:
    pass

// MAKE SURE you cd into your Djnago "project folder" (= where wsgi.py & settings.py exist; in this case it's "btre_project") // Djnago "프로젝트 폴더"(= wsgi.py 및 settings.py가 존재하는 곳,이 경우 "btre_project")로 cd했는지 확인합니다.

Create a file called local_settings.py on your server along side of settings.py and add the following settings.py 외에, 서버에 local_settings.py라는 파일을 만들고 다음을 추가하십시오.

  • SECRET_KEY
  • ALLOWED_HOSTS
  • DATABASES
  • DEBUG
  • EMAIL_*

Run Migrations

# python manage.py makemigrations
# python manage.py migrate

Create super user

# python manage.py createsuperuser

Create static files

python manage.py collectstatic

Create exception for port 8000

# sudo ufw allow 8000

Run Server

# python manage.py runserver 0.0.0.0:8000

Test the site at YOUR_SERVER_IP:8000

Add some data in the admin area

Gunicorn Setup

Install gunicorn

# pip install gunicorn

Add to requirements.txt

# pip freeze > requirements.txt

Test Gunicorn serve

# gunicorn --bind 0.0.0.0:8000 btre.wsgi

Your images, etc will be gone

Stop server & deactivate virtual env

ctrl-c
# deactivate

Open gunicorn.socket file

# sudo nano /etc/systemd/system/gunicorn.socket

Copy this code, paste it in and save

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

Open gunicorn.service file

# sudo nano /etc/systemd/system/gunicorn.service

Copy this code, paste it in and save

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=djangoadmin
Group=www-data
WorkingDirectory=/home/djangoadmin/pyapps/btre_project
ExecStart=/home/djangoadmin/pyapps/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          btre.wsgi:application

[Install]
WantedBy=multi-user.target

Start and enable Gunicorn socket

# sudo systemctl start gunicorn.socket
# sudo systemctl enable gunicorn.socket

Check status of guinicorn

# sudo systemctl status gunicorn.socket

Check the existence of gunicorn.sock

# file /run/gunicorn.sock

NGINX Setup

Create project folder

# sudo nano /etc/nginx/sites-available/btre_project

Copy this code and paste into the file

server {
    listen 80;
    server_name YOUR_IP_ADDRESS;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/djangoadmin/pyapps/btre_project;
    }
    
    location /media/ {
        root /home/djangoadmin/pyapps/btre_project;    
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Enable the file by linking to the sites-enabled dir

# sudo ln -s /etc/nginx/sites-available/btre_project /etc/nginx/sites-enabled

Test NGINX config

# sudo nginx -t

Restart NGINX

# sudo systemctl restart nginx

Remove port 8000 from firewall and open up our firewall to allow normal traffic on port 80

# sudo ufw delete allow 8000
# sudo ufw allow 'Nginx Full'

You will probably need to up the max upload size to be able to create listings with images

Open up the nginx conf file

# sudo nano /etc/nginx/nginx.conf

Add this to the http{} area

client_max_body_size 20M;

Reload NGINX

# sudo systemctl restart nginx

Media File Issue

You may have some issues with images not showing up. I would suggest, deleting all data and starting fresh as well as removeing the "photos" folder in the "media folder"

# sudo rm -rf media/photos

Domain Setup

Go to your domain registrar and create the following a record

@  A Record  YOUR_IP_ADDRESS
www  CNAME  example.com

Go to local_settings.py on the server and change "ALLOWED_HOSTS" to include the domain

ALLOWED_HOSTS = ['IP_ADDRESS', 'example.com', 'www.example.com']

Edit /etc/nginx/sites-available/btre_project

server {
    listen: 80;
    server_name xxx.xxx.xxx.xxx example.com www.example.com;
}

Reload NGINX & Gunicorn

# sudo systemctl restart nginx
# sudo systemctl restart gunicorn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment