DELOY SERVER LÊN HEROKU FREE
B1. Chỉnh sửa 1 số cài đặt
- Cài đặt thư viện:
pip install python-decouple
pip install dj-database-url
pip install python-dotenv
pip install whitenoise
- Cập nhật file settings tại djangoserver/settings
from decouple import config, Csv
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG') == 'TRUE'
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv())
INSTALLED_APPS = [
...
'whitenoise.runserver_nostatic', # <-- HERE
...
]
MIDDLEWARE = [
...
'whitenoise.middleware.WhiteNoiseMiddleware', # <-- HERE
...
]
import dj_database_url
import dotenv
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
dotenv.load_dotenv(dotenv_file)
DATABASES = {}
DATABASES['default'] = dj_database_url.config(conn_max_age=600)
B2. Tạo biến môi trường .env tại djangoserver/.env, thay secret_key phù hợp
SECRET_KEY=1+y#d^b8pmo7kaa2looz=dv%ce#imy#d194&flhd1+#z8u(l9k
DEBUG=TRUE
ALLOWED_HOSTS=*
DATABASE_URL=sqlite:///db.sqlite3
Chạy thử server:
python manage.py runserver
B3. Tạo file .gitignore tại thư mục djangoserver
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# .gitignore
*.sqlite3
.env
B4. Cài đặt Heroku
Link tải: https://devcenter.heroku.com/articles/heroku-cli
Đăng ký một tài khoản Heroku
Tải và cài đặt Git: https://git-scm.com/downloads
B5. Tạo 1 file Procfile tại thư mục django
web: gunicorn djangoserver.wsgi --log-file –
B6. Cài đặt 1 số packages và file requirements.txt
pip install gunicorn
pip install psycopg2
pip freeze > requirements.txt
B7. Tạo 1 app Heroku và deloy
heroku login
heroku create toolnaychillphet
git init
heroku git:remote -a toolnaychillphet
git add .
git commit -m "Initial commit"
heroku config:set DISABLE_COLLECTSTATIC=1
git push heroku master
B8. Thiết lập các tham số cho app Heroku theo file .env
ALLOWED_HOSTS = ...
DEBUG = ...
SECRET_KEY = ...
B9. Đồng bộ database
heroku run python manage.py migrate
B10. Tạo superuser trên Heroku
heroku run python manage.py createsuperuser --email admin@example.com --username admin
B11. Thiết lập để tăng tốc web
heroku ps:scale web=1
إرسال تعليق