DjangoサーバをNginxとuwsgiで配備

3480 ワード

サーバの配備
ubuntuシステム
データベース#データベース#
  • sudo apt update更新インストールパッケージ
  • apt install mysql-server mysql-client
  • リモートアクセスmysqlの設定
  • プロファイル
  • の変更
  • /etc/mysql/mysql.conf.d
  • mysqlを検索します.conf find/-name mysql.cnf
  • 注記mysql.cofファイルのbind_address
  • データベース操作
  • に進む.
  • mysql -u root -p
  • GRANT ALL PRIVILEGES ON . TO‘データベースアカウント’@’%’IDENTIFIED BY‘データベースパスワード’WITH GRANT OPTION;
  • 更新データ
  • flush privileges;
  • データベースを終了し、
  • データベースを再起動します.
  • service mysql restart
  • アリクラウドがセキュリティルールに3306を追加する場合、TCPのポート

  • djangoのプロファイルの変更
  • settings.py中
  • DEBUG=FALSE,ALLOWED_HOST=[‘*’]
  • STATIC_ROOT = os.path.join(BASE_DIR, ‘static’)

  • """
     urls.py
             from django.views.static import serve
             url       
             url(r'^static/(?P.*)$', serve, {"document_root": settings.STATIC_ROOT}),
             url(r'^media/(?P.*)$', serve, {"document_root": settings.MEDIA_ROOT}),
             url(r'^$', views.home)
    
    """

    サーバ
  • フォルダを作成するには、ルートディレクトリの下に必ず作成してください.ルートディレクトリであるかどうかはpwdで
  • を表示できます.
  • ファイル構造は以下の
  • である.
  • home
  • app
  • confこのフォルダの下にプロファイルを置く
  • logこのフォルダの下にログを置く
  • srcこのフォルダの下にプロジェクトを置く

  • インストールpip 3
  • apt install python3-pip
  • インストール必須ライブラリ
  • pip3 install django==1.11
  • pip3 install pymysql
  • pip3 install Pillow
  • pip3 install djangorestframework==3.4.6
  • pip3 install django-filter

  • プロセスの表示
  • netstat -lntp

  • プロジェクトの開始
  • python3 manage.py runserver 0.0.0.0:8000
  • 後ろのポートに注意して、サーバー上で対応するポート
  • を開く必要があります.
  • この方法でサーバにプロジェクトを導入できますが、実際にはより良い導入方法
  • があります.
    nginx+uwsgiを使用してdjangoプロジェクトを構成する
  • nginxのインストール
  • sudo apt-get install nginx

  • nginxのステータスを表示
  • systemctl status nginx nginx nginxステータス
  • を表示
  • systemctl start/stop/enable/disable nginx起動/停止/設定起動起動/起動禁止
  • service nginx status/stop/restart/start

  • uwsgiのインストール
  • pip3 install uwsgi

  • nginxのプロファイルにカスタムnginxのプロファイルをロード
  • /home/app/conf/の下にカスタムconfファイル
  • を作成
  • の内容は以下の
  • である.
    """
    server {
            listen 80; #   80  
            server_name 47.105.32.183 localhost; #     
    
            access_log /home/app/log/access.log; #     
            error_log /home/app/log/error.log; #       
    
            location / {
                include uwsgi_params; #         uwsgi  
                uwsgi_pass 127.0.0.1:8090; # uwsgi  ,        
            }
    
            location /static/ { #        nginx   
                alias /home/app/src/aixianfeng/static/; 
                expires 30d;
            }
    }
    """
  • は/etc/nginx/nginxにある.confでの構成の変更
  • includeがある場所を見つけて、次のコード
  • を加えます.
  • include/home/app/conf/*.conf;

  • /home/app/conf/の下にカスタムuwsgiを作成します.iniファイル
  • の内容は以下の
  • である.
    """
    #              #  
    [uwsgi]
    
    master = true
    
    processes = 4
    
    pythonpath = /home/app/src/aixianfeng
    
    module = aixianfeng.wsgi
    
    socket = 127.0.0.1:8090
    
    logto = /home/app/log/uwsgi.log
    """
  • 起動方法:現在のフォルダの下でuwsgi–ini uwsgi.ini