Jelajahi Sumber

add gunicorn and redy for server

Evgeny Polivanov 1 tahun lalu
induk
melakukan
6fb938fdea
8 mengubah file dengan 127 tambahan dan 32 penghapusan
  1. 4 1
      .gitignore
  2. 53 1
      README.md
  3. 2 3
      bin/webuser.sh
  4. 9 0
      core/conf_gunicorn.py
  5. 26 0
      core/config_template.py
  6. 20 17
      core/db_settings.py
  7. 10 9
      core/settings.py
  8. 3 1
      install_linux.sh

+ 4 - 1
.gitignore

@@ -4,6 +4,8 @@ venv/
 #module
 tickets/
 metaservicesynced/
+staticfiles/
+core/config.py
 # VSCode
 .vscode
 
@@ -13,4 +15,5 @@ __pycache__/
 *$py.class
 
 # Other
-*.sqlite3
+*.sqlite3
+.env

+ 53 - 1
README.md

@@ -2,7 +2,7 @@
 
 Admin system implemented as a Django application.
 
-## Installation
+## How install?
 
 1) Download or clone repository
 
@@ -21,6 +21,58 @@ If the installation is successful, the system will prompt you to create a superu
 6. y - to agree if the password is too simple
 
 3) To manually install the project, open the .bat file with a text editor and try these commands in the terminal
+
+## Server instalation
+1) Download or clone repository
+```bash
+git clone http://git.sharix-app.org/ShariX_Open/sharix-open-webapp-base.git name_project
+```
+2) Set up a configuration file ```nano core/config_template.py```
+```python
+#Create file config.py with this setting or rename this file to config.py
+
+#BASE
+DEBUG=True
+SECRET_KEY='secret-key(absolutely any character)'
+ALLOWED_HOSTS = ['127.0.0.1']
+CSRF_TRUSTED_ORIGINS = []
+
+#DATABSE
+DB_NAME=None
+DB_USER=None
+DB_PASSWORD=None
+DB_HOST=None
+
+#GUNICORN
+BIND = "127.0.0.1:8000"
+WORKERS = 2
+THREADS = 4
+
+#STATIC
+from pathlib import Path
+import os
+BASE_DIR = Path(__file__).resolve().parent.parent
+STATIC_URL = '/static/'
+STATICFILES_DIRS = [BASE_DIR / "SharixAdmin/static/", BASE_DIR / "tickets/static/"]
+STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
+```
+3) Run a **install_linux.sh**
+4) The system will prompt you to create a superuser
+```
+#Example
+7987654321
+admin
+pa$$w0rd
+pa$$w0rd
+```
+5) Set up the **bin/webuser.sh** file with valid paths
+```bash
+#!/bin/bash
+cd /path/to/project
+exec /path/to/project/env/bin/gunicorn core.wsgi:application -c core/conf_gunicorn.py
+```
+6) It remains to configure Nginx conf and start the daemon
+
 ## Settings
 
 Optional configuration params, which can be added to your project settings:

+ 2 - 3
bin/webuser.sh

@@ -1,4 +1,3 @@
 #!/bin/bash
-cd /root/sharix-webuser
-source /root/sharix-webuser/webuser/bin/activate
-exec python3 manage.py runserver 10.0.2.120:8001
+cd /path/to/project
+exec /path/to/project/env/bin/gunicorn core.wsgi:application -c core/conf_gunicorn.py

+ 9 - 0
core/conf_gunicorn.py

@@ -0,0 +1,9 @@
+#bind = "0.0.0.0:8001"
+import core.config as conf
+bind = conf.BIND
+workers = conf.WORKERS
+worker_class = "sync"
+threads = conf.THREADS
+timeout = 30
+max_requests = 1000
+capture_output = True

+ 26 - 0
core/config_template.py

@@ -0,0 +1,26 @@
+#Create file config.py with this setting or rename this file to config.py
+
+#BASE
+DEBUG=True
+SECRET_KEY='secret-key(absolutely any character)'
+ALLOWED_HOSTS = ['127.0.0.1']
+CSRF_TRUSTED_ORIGINS = []
+
+#DATABSE
+DB_NAME=None
+DB_USER=None
+DB_PASSWORD=None
+DB_HOST=None
+
+#GUNICORN
+BIND = "127.0.0.1:8000"
+WORKERS = 2
+THREADS = 4
+
+#STATIC
+from pathlib import Path
+import os
+BASE_DIR = Path(__file__).resolve().parent.parent
+STATIC_URL = '/static/'
+STATICFILES_DIRS = [BASE_DIR / "SharixAdmin/static/", BASE_DIR / "tickets/static/"]
+STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

+ 20 - 17
core/db_settings.py

@@ -1,21 +1,24 @@
 from pathlib import Path
 import os
-BASE_DIR = Path(__file__).resolve().parent.parent
-
-# DATABASES = {
-#     'default': {
-#         'ENGINE': 'django.db.backends.postgresql',
-#         'NAME': '*******',
-#         'USER': '******',
-#         'PASSWORD': '*******',
-#         'HOST': '127.0.0.1',
-#         'PORT': '5432',
-#     }
-# }
+import core.config as conf
 
-DATABASES = {
-    'default': {
-        'ENGINE': 'django.db.backends.sqlite3',
-        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+BASE_DIR = Path(__file__).resolve().parent.parent
+if conf.DB_NAME is None:
+    DATABASES = {
+        'default': {
+            'ENGINE': 'django.db.backends.sqlite3',
+            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+        }
     }
-}
+else:
+    DATABASES = {
+        'default': {
+            'ENGINE': 'django.db.backends.postgresql',
+            'NAME': conf.DB_NAME,
+            'USER': conf.DB_USER,
+            'PASSWORD': conf.DB_PASSWORD,
+            'HOST': conf.DB_HOST,
+            'PORT': '5432',
+        }
+    }
+

+ 10 - 9
core/settings.py

@@ -4,16 +4,16 @@ from core.db_settings import *
 from core.api_settings import *
 from core.tickets_mail_settings import *
 from core.jazzmin_settings import *
+import core.config as conf
 # Build paths inside the project like this: BASE_DIR / 'subdir'.
 BASE_DIR = Path(__file__).resolve().parent.parent
-
 # SECURITY WARNING: keep the secret key used in production secret!
-SECRET_KEY = 'django-insecure-t064c*-&=sq48kon3()$mge6cos*#x%*c$dr#1*n*+b$s6fw=*'
+SECRET_KEY = conf.SECRET_KEY
 
 # SECURITY WARNING: don't run with debug turned on in production!
-DEBUG = True
+DEBUG = conf.DEBUG
 
-ALLOWED_HOSTS = []
+ALLOWED_HOSTS = conf.ALLOWED_HOSTS
 CSRF_TRUSTED_ORIGINS = []
 
 # Application definition
@@ -39,7 +39,6 @@ INSTALLED_APPS = [
     'schema_graph',
     'drf_yasg',
     'django_spaghetti',
-    
 ]
 
 MIDDLEWARE = [
@@ -65,7 +64,9 @@ INTERNAL_IPS = [
 TEMPLATES = [
     {
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
-        'DIRS': [],
+        'DIRS': [
+            BASE_DIR / "templates"
+        ],
         'APP_DIRS': True,
         'OPTIONS': {
             'context_processors': [
@@ -123,9 +124,9 @@ SITE_ID = 1
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/4.1/howto/static-files/
 
-STATIC_URL = '/root/sharix-webuser/SharixAdmin/static/'
-STATICFILES_DIRS = [os.path.join(BASE_DIR, "core", "static", "/root/sharix-webuser/SharixAdmin/static/")]
-STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
+STATIC_URL = conf.STATIC_URL
+STATICFILES_DIRS = conf.STATICFILES_DIRS
+STATIC_ROOT = conf.STATIC_ROOT
 # Uploaded media
 MEDIA_ROOT = os.path.join(BASE_DIR, "media")
 MEDIA_URL = "/media/"

+ 3 - 1
install_linux.sh

@@ -14,6 +14,8 @@ python3 manage.py makemigrations
 
 python3 manage.py migrate
 
+python3 manage.py collectstatic
+
 python3 manage.py createsuperuser
 
-python3 manage.py runserver
+deactivate