# Django 部屬 ## 安裝 Linux 系統依賴 ```shell= $ sudo apt-get update $ sudo apt-get install -y python3-pip python3-dev libmysqlclient-dev python3.8-venv git vim ``` ## 安裝 poetry(選用) ```shell= $ curl -sSL https://install.python-poetry.org | python3 - $ poetry export -f requirements.txt --output requirements.txt ``` requirements.txt ``` django requests gunicorn whitenoise ``` ## 建立虛擬環境 ```shell= $ python3 -m venv .venv $ source .venv/bin/activate ``` ## 修改 settings.py ```python= DEBUG = False ALLOWED_HOSTS = ['*'] MIDDLEWARE = [ # ... "django.middleware.security.SecurityMiddleware", "whitenoise.middleware.WhiteNoiseMiddleware", # ... ] STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" STATIC_URL = "/static/" STATICFILES_DIRS = [ BASE_DIR / "static", ] STATIC_ROOT = BASE_DIR / "assets" MEDIA_URL = "/media/" MEDIA_ROOT = BASE_DIR / "media" ``` ## 建立檔案資料夾 > 原本目錄下有這些資料夾就不需要建立 ```shell= $ mkdir static $ mkdir assets $ mkdir media ``` ## 部屬 Django ```shell= $ python manage.py collectstatic $ python manage.py migrate $ python manage.py createsuperuser ``` ```shell= $ gunicorn --bind=:8000 --workers=4 --forwarded-allow-ips="*" core.wsgi ```