From 074dd7b28cc2420ca3712f45e273f5bdf7d58b8f Mon Sep 17 00:00:00 2001 From: Sultan Date: Mon, 17 Aug 2020 04:37:51 +0600 Subject: [PATCH] messages --- .gitignore | 17 ++++++ README.md | 24 +++++++++ gunicorn/empty | 0 install.sh | 22 ++++++++ nginx/site.conf | 13 +++++ requirements.txt | 24 +++++++++ src/config/__init__.py | 0 src/config/settings.py | 112 +++++++++++++++++++++++++++++++++++++++ src/config/urls.py | 6 +++ src/config/wsgi.py | 7 +++ src/manage.py | 21 ++++++++ systemd/gunicorn.service | 13 +++++ 12 files changed, 259 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 gunicorn/empty create mode 100755 install.sh create mode 100644 nginx/site.conf create mode 100644 requirements.txt create mode 100644 src/config/__init__.py create mode 100644 src/config/settings.py create mode 100644 src/config/urls.py create mode 100644 src/config/wsgi.py create mode 100755 src/manage.py create mode 100644 systemd/gunicorn.service diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..370cada --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +logs/ +**/node_modules/ +**/.DS_Store +.idea/ +**/__pycache__ +*.aux +*.log +*.pickle +*.db +*.rdb +*.pid +*.toc +*.out +*.pickle +celerybeat-schedule +.ipynb_checkpoints +env/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..58dcc47 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +Это чистый шаблон Django проекта, с которым можно быстро начать разработку. В шаблон входит конфиг Systemd, nginx, gunicorn. + +Установка представляет собой просто указание Python интерпретатора и названия домена, запустите: + +```bash +./install.sh +``` + +В конфиге Django заполните настройки базы данных (`src/config/settings.py`). + +Посмотреть статус gunicorn демона: + +```bash +sudo systemctl status gunicorn +``` + +Логи gunicorn'а лежат в `gunicorn/access.log` и `gunicorn/error.log`. + +После изменения systemd конфига надо перечитать его и затем перезапустить юнит: + +```bash +sudo systemctl daemon-reload +sudo systemctl restart gunicorn +``` diff --git a/gunicorn/empty b/gunicorn/empty new file mode 100644 index 0000000..e69de29 diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..3818333 --- /dev/null +++ b/install.sh @@ -0,0 +1,22 @@ +#!/bin/bash +base_python_interpreter="" +project_domain="" +project_path=`pwd` + +read -p "Python interpreter: " base_python_interpreter +read -p "Your domain without protocol (for example, google.com): " project_domain +`$base_python_interpreter -m venv env` +source env/bin/activate +pip install -U pip +pip install -r requirements.txt + +sed -i "s~dbms_template_path~$project_path~g" nginx/site.conf systemd/gunicorn.service +sed -i "s~dbms_template_domain~$project_domain~g" nginx/site.conf src/config/settings.py + +sudo ln -s $project_path/nginx/site.conf /etc/nginx/sites-enabled/ +sudo ln -s $project_path/systemd/gunicorn.service /etc/systemd/system/ + +sudo systemctl daemon-reload +sudo systemctl start gunicorn +sudo systemctl enable gunicorn +sudo service nginx restart diff --git a/nginx/site.conf b/nginx/site.conf new file mode 100644 index 0000000..bca9f91 --- /dev/null +++ b/nginx/site.conf @@ -0,0 +1,13 @@ +server { + listen 80; + server_name dbms_template_domain; + + location = /favicon.ico { access_log off; log_not_found off; } + location /static/ { + root dbms_template_path/static; + } + location / { + include proxy_params; + proxy_pass http://unix:dbms_template_path/gunicorn/gunicorn.sock; + } +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..eca2ee8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,24 @@ +backcall==0.1.0 +certifi==2019.9.11 +chardet==3.0.4 +decorator==4.4.0 +Django==2.2.5 +gunicorn==19.9.0 +idna==2.8 +ipython==7.8.0 +ipython-genutils==0.2.0 +jedi==0.15.1 +parso==0.5.1 +pexpect==4.7.0 +pickleshare==0.7.5 +prompt-toolkit==2.0.9 +psycopg2==2.8.3 +ptyprocess==0.6.0 +Pygments==2.4.2 +pytz==2019.2 +requests==2.22.0 +six==1.12.0 +sqlparse==0.3.0 +traitlets==4.3.2 +urllib3==1.25.6 +wcwidth==0.1.7 diff --git a/src/config/__init__.py b/src/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/config/settings.py b/src/config/settings.py new file mode 100644 index 0000000..2a66bc4 --- /dev/null +++ b/src/config/settings.py @@ -0,0 +1,112 @@ +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'pw+*a^45y7s_f(im29%4ot2222m1h7zed4w+$03_trf4)0)!l!' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['dbms_template_domain'] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'config.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'config.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'db_name', + 'USER': 'dbms', + 'PASSWORD': 'db_password', + 'HOST': 'localhost', + 'PORT': '', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/2.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.2/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/src/config/urls.py b/src/config/urls.py new file mode 100644 index 0000000..dfc7362 --- /dev/null +++ b/src/config/urls.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/src/config/wsgi.py b/src/config/wsgi.py new file mode 100644 index 0000000..a9f185c --- /dev/null +++ b/src/config/wsgi.py @@ -0,0 +1,7 @@ +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') + +application = get_wsgi_application() diff --git a/src/manage.py b/src/manage.py new file mode 100755 index 0000000..58c5f10 --- /dev/null +++ b/src/manage.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/systemd/gunicorn.service b/systemd/gunicorn.service new file mode 100644 index 0000000..dba5552 --- /dev/null +++ b/systemd/gunicorn.service @@ -0,0 +1,13 @@ +[Unit] +Description=gunicorn daemon +After=network.target + +[Service] +User=www +Group=www-data +WorkingDirectory=dbms_template_path/src +ExecStart=dbms_template_path/env/bin/gunicorn --workers 3 --bind unix:dbms_template_path/gunicorn/gunicorn.sock config.wsgi:application --access-logfile dbms_template_path/gunicorn/access.log --error-logfile dbms_template_path/gunicorn/error.log +Restart=on-failure + +[Install] +WantedBy=multi-user.target