cropse

Deploy設定簡單紀錄 2017- 2-07


這裡大概整理一下整個網站deploy的過程,等下次在詳細整理s3跟ec2設定的部分

django專案名稱是src, 路徑放置在/home/ubuntu/blog
先從配置文件開始
首先新增一個production.py在/home/ubuntu/blog/setting/
以下內容:


from .base import *
import os

# SECURITY WARNING: keep the secret key used in production secret!
# 請自己新增一個 不要直接用 http://www.miniwebtool.com/django-secret-key-generator/
SECRET_KEY = '-we5#2la2p=%tj1#w6t(3dgh#1=f8!tx#tmy7khui2yw1$%g32'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

# 這邊填上網址允許
ALLOWED_HOSTS = ['*']

# 選擇你的db, 這裡用postgresql
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '5432',
    }
}

# S3 設定用
INSTALLED_APPS+=[
    'storages',
]


STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

AWS_REGION = "ap-northeast-1"
AWS_QUERYSTRING_AUTH = False
AWS_ACCESS_KEY_ID = '<b>tag</b>'
AWS_SECRET_ACCESS_KEY = 'your access Key'
AWS_STORAGE_BUCKET_NAME = 'your s3 bucket name'
MEDIA_URL = 'http://%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

設定ec2伺服器

在ec2上面開一個ubuntu
連線後開始安裝套件, 這裡路徑是/home/ubuntu/blog

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/bash

# source /home/debian/VENV/bin/activate
# 先安裝套件
apt-get update -y
apt-get upgrade -y
apt-get install  nginx uwsgi python3 build-essential python3-dev python3-setuptools python3-pip -y
apt-get install uwsgi-plugin-python3 -y
apt-get install libpq-dev -y
apt-get install postgresql postgresql-contrib -y

apt install awscli -y
pip3 install uwsgi

# application import, 如果專案放github請用git clone
aws s3 cp s3://cropse/Blog/ /home/ubuntu/blog --recursive

PostgreSQL指令集
PostgreSQL 指令
【系統】PostgreSQL : 記錄一些設定和指令

設定nginx

接下來設定nginx,剛裝完之後可以直接開80 port檢查demo網頁確認運作正常
這邊用sock的方式對伺服器做存取而不是設定port
檔案名稱default

upstream django {
    server unix:///var/www/demo.sock;
}

server {
    listen 80;
    server_name 52.68.0.97;
    charset utf-8;
    client_max_body_size 20M;


    location /static {
          alias /home/ubuntu/blog/static_cdn;
    }

#   setting in media and static in local
#    location /static {
#          alias /home/ubuntu/blog/static_cdn;
#    }

#    location /media {
#          alias /home/ubuntu/blog/media_cdn;
#    }


    location / {
        uwsgi_pass django;
        # uwsgi_pass 127.0.0.1:8080;
        include /etc/nginx/uwsgi_params;
    }

}

接下來設定uwsgi,注意專案名稱跟路徑

uwsgi.ini
可以用'uwsgi -i uwsgi.ini'先開啟測試

[uwsgi]
chdir        = /home/ubuntu/blog/src
# Django's wsgi file
module       = src.wsgi:application
env          = DJANGO_SETTINGS_MODULE=src.settings.production
#.production
# the virtualenv (full path)
# home         = /home/debian/VENV
# home         = /usr/bin

# process-related settings
# master
master       = true
# maximum number of worker processes
processes    = 4
# the socket (use the full path to be safe
socket       = /var/www/demo.sock
# ... with appropriate permissions - may be needed
chmod-socket = 777
uid          = root
# gid          = 0
# clear environment on exit
vacuum       = true

寫好uwsgi的service

Systemd 入门教程:命令篇
init演化歷程 – [轉貼] 淺析 Linux 初始化 init 系統,第 3 部分: Systemd

[Unit]
Description=uWSGI Emperor
After=syslog.target

[Service]
ExecStart=/usr/local/bin/uwsgi --ini /home/ubuntu/blog/deploy_setting/uwsgi.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

搬設定檔跟安裝套件

# install package
pip3 install -r /home/ubuntu/blog/requirement.txt

#envirment of django settings
export DJANGO_SETTINGS_MODULE=src.settings.production


# nginx setting file
cp /home/ubuntu/blog/deploy_setting/default /etc/nginx/sites-enabled/default

# create user and collect static file
python3 manage.py createsuperuser
python3 manage.py collectstatic --noinput

# uwsgi setting
cp /home/ubuntu/blog/deploy_setting/emperor.uwsgi.service /etc/systemd/system/emperor.uwsgi.service
systemctl enable emperor.uwsgi.service
systemctl start emperor.uwsgi.service

關於deploy還有很多可以優化的部分(CI/CD), 先手動過一次之後大概了解整個部屬的流程,整個流程再慢慢改善紀錄

Tag: AWS deploy uwsgi nginx postgresql