使用Djangoのサービス展開nginxの+ Gunicorn +ダフネ+スーパーバイザ

1.準備

1.1ユーザー・グループの作成zanhu
groupadd zanhu
1.2 zanhuユーザーが作成しzanhu内のユーザーグループに参加します
useradd -m zanhu -g zanhu
1.3パスワード
passwd zanhu
1.4助成金は、ユーザーに実行権限をzanhu
chmod +x /home/zanhu
1.5所望の取り付けシステムに依存
yum install python-devel zlib-devel mysql-devel libffi-devel bzip2-devel openssl-devel java gcc wget

rpmのインストールmysql8.0として、以前使用してrpm -qa|grep mysql、すでに表示するコマンドは、MySQL-develのをインストールする必要はありませんMySQLのコミュニティ-develのが含まれています

1.6展開サービスのインストールが必要
yum install -y nginx redis supervisor
1.7ブートの設定
systemctl enable redis nginx supervisord
1.8 zanhuに切り替えるユーザー
su - zanhu
/ホーム/ zanhuディレクトリをコピーするための1.9によるXftp Djangoプロジェクト、注目のプロジェクトの所有者を変更するには、zanhuのグループです
chown zanhu:zanhu -R zanhu/
1.10は、店舗gunicorn、ジンチョウゲ、elasticsearch、セロリのログに使用され、プロジェクトディレクトリのログディレクトリに作成されます
mkdir logs
プロジェクトの下で生成された1.11元の仮想環境に依存しています
cd /root/zanhu
pipenv shell
pip free > /home/zanhu/zanhu/requirements.txt
プロジェクトディレクトリに1.12、依存関係をインストール

使用中のこの方法ケイトン、元の仮想環境下で遅延の原因ダウンロードの依存関係にテスト環境では比較的少数派を含むので、これらを削除するには、依存、インストール後に続行します

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r requirements.txt

2.ダウンロードしてインストールしelasticsearch

問題のみelasticsearch2.4.6バージョンのDjangoの干し草のバージョンは、公式ウェブサイトにはリソースがないので、githubのからダウンロードして、xftpを介してサーバにアップロードすることができます
ダウンロード

tar -xf elasticsearch-2.4.6.tar.gz

プロジェクトディレクトリの下にgunicorn 3.ファイル名を指定して実行

/usr/local/python37/bin/gunicorn --env DJANGO_SETTINGS_MODULE=config.settings.production -b 127.0.0.1:8888 --chdir /home/zanhu/zanhu config.wsgi

zanhuユーザーelasticsearchで4.スタート

./elasticsearch-2.4.6/bin/elasticsearch

5. [スタート]セロリ

/usr/local/python37/bin/celery --work=/home/zanhu/zanhu -A config.celery_app worker -l info
5.1は、以下の成功の開始を示しています
[tasks]
  . config.celery_app.debug_task
  . djcelery_email_send_multiple
  . zanhu.users.tasks.get_users_count

6.スタートダフネ

/usr/local/python37/bin/daphne -p 8000 config.asgi:application

7. [スタート] nginxの

7.1設定nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    upstream channels-backend {  # websocket请求转发配置
        server localhost:8000;
    }

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass http://127.0.0.1:8888;  # 动态请求转发给gunicorn服务

            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /staticfiles/ {
            root  /home/zanhu/zanhu/zanhu;  # staticfiles文件所在的目录路径
        }

        location /media/ {
            root   /home/zanhu/zanhu/zanhu;  # media文件所在的目录路径
        }

        location /ws/ {  # /ws/用于区分http请求和websocket
            proxy_pass http://channels-backend;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $server_name;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
7.2以降nginxの
systemctl start nginx
7.3ビューnginxのエラーログ
tail -f /var/log/nginx/error.log

手動展開Djangoプロジェクトの流れは、成功した操作かどうかを確認するには、ipブラウザを入力してください

問題の8.まとめ

あなたはelasticsearch検索機能、通常の要求を使用してデータを検索することはできません。その理由は、まだそれが、新しいプロジェクトには、インデックスの生成を忘れて、元の仮想環境で実行するプロジェクトだったと考えられているpython3 manage.py rebuild_index
静的ファイルのパーミッションの問題、あるいはDjangoプロジェクトセットアップの問題をルーティングすることができ、展開がロードできない静的なファイルやメディアファイル、完了すると、以下のルーティング構成の問題の解決

8.1 cookiecutter生成された経路。
from django.conf import settings
from django.urls import include, path
from django.conf.urls.static import static
from django.views.generic import TemplateView
from django.views import defaults as default_views


urlpatterns = [
    # 用户
    path("users/", include("zanhu.users.urls", namespace="users")),
    # 第三方应用
    path("search/", include("haystack.urls")),
    # 项目
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

ビューのソースコードは、static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)デバッグモードでのみ使用可能ですので、以下の変更を行い、本番環境で静的ファイルをロードすることはできません

#####修改后的urls.py
from django.conf import settings
from django.urls import include, path
from django.views.static import serve  # 注意区别,导入django.views.static下的serve方法
from django.views.generic import TemplateView
from django.views import defaults as default_views


urlpatterns = [
    # 用户
    path("users/", include("zanhu.users.urls", namespace="users")),
    # 第三方应用
    path("search/", include("haystack.urls")),
    # 项目
    re_path("^static/(?P<path>.*)$", serve, document_root=settings.STATIC_ROOT)  # 添加静态文件的路由
    re_path("^media/(?P<path>.*)$", serve, document_root=settings.MEDIA_ROOT)  # 添加media文件的路由
]

9.展開スーパバイザ制御

/etc/supervisord.dに9.1
cd /etc/supervisord.d
9.2編集zanhu_celery.ini
[program:celery]
# 执行用户
user = zanhu
# 执行的命令
command = /usr/local/python37/bin/celery --work=/home/zanhu/zanhu -A config.celery_app worker -l info
# 日志文件配置
loglevel = info
stdout_logfile = /home/zanhu/zanhu/logs/celery.log
stderr_logfile = /home/zanhu/zanhu/logs/celery_error.log
# 日志文件最大字节数
stdout_logfile_maxbytes = 100MB
# 备份数量
stdout_logfile_backups = 3
# 命名每个进程,便于管理
process_name = celery_worker%(process_num)s
# 启动的进程数,设置成服务器的CPU数
numprocs_start = 1
numprocs = 1
# 设置自启和重启
autostart = true
autorestart = true
redirect_stderr = true
9.3編集zanhu_daphne.ini
[program:daphne]
# 执行用户
user = zanhu
# 在该目录下执行下面command命令
directory = /home/zanhu/zanhu
# 执行的命令
command = /usr/local/python37/bin/daphne -p 8000 config.asgi:application
# 日志文件配置
loglevel = info
stdout_logfile = /home/zanhu/zanhu/logs/daphne.log
stderr_logfile = /home/zanhu/zanhu/logs/daphne_error.log
stdout_logfile_maxbytes = 100MB
stdout_logfile_backups = 3
# 给每个进程命名,便于管理
process_name = daphne_worker%(process_num)s
# 启动的进程数,设置成云服务器的vCPU数
numprocs_start = 1
numprocs = 1
max-requests = 5000
# 设置自启和重启
autostart = true
autorestart = true
redirect_stderr = True
9.4編集zanhu_elasticsearch.ini
[program:elasticsearch]
# 执行用户
user = zanhu
# 在该目录下执行下面command命令
directory = /home/zanhu/elasticsearch-2.4.6/bin/
# 执行的命令
command = /home/zanhu/elasticsearch-2.4.6/bin/elasticsearch -d
# 日志文件配置
loglevel = info
stdout_logfile = /home/zanhu/zanhu/logs/elasticsearch.log
stderr_logfile = /home/zanhu/zanhu/logs/elasticsearch_error.log
stdout_logfile_maxbytes = 100MB
stdout_logfile_backups = 3
# 给每个进程命名,便于管理
process_name = elasticsearch_worker%(process_num)s
# 启动的进程数,设置成云服务器的vCPU数
numprocs_start = 1
numprocs = 1
# 设置自启和重启
autostart = true
autorestart = true
redirect_stderr = True
9.5編集zanhu_gunicorn.ini
[program:gunicorn]
# 执行用户
user = zanhu
# 执行的命令
command = /usr/local/python37/bin/gunicorn --env DJANGO_SETTINGS_MODULE=config.settings.production -b 127.0.0.1:8888 --chdir /home/zanhu/zanhu config.wsgi
# 日志文件配置
loglevel = info
stdout_logfile = /home/zanhu/zanhu/logs/gunicorn.log
stderr_logfile = /home/zanhu/zanhu/logs/gunicorn_error.log
stdout_logfile_maxbytes = 100MB
stdout_logfile_backups = 3
# 给每个进程命名,便于管理
process_name = gunicorn_worker%(process_num)s
# 启动的进程数,设置成云服务器的vCPU数
numprocs_start = 1
numprocs = 1
# 最大访问次数,超过后重新启动
max-requests = 5000
# 设置自启和重启
autostart = true
autorestart = true
redirect_stderr = True
aommand = /usr/local/python3/bin/daphne -p 8000 config.asgi:application
9.6サービスの開始
sudo systemctl start supervisord

おすすめ

転載: www.cnblogs.com/listash/p/12466904.html