nginx + Gunicorn + SSLでDjangoサイトを実行する

序文

この記事を書くのに多くの時間と労力を要しました。私は英語とロシア語の両方でたくさんの指示に出くわしました、しかし私が理解したように、それらはすべてデジタルオーシャンに関する元の記事のクローンでした。あなたはなぜそう思うのかと尋ねますが、それはすべて、すべてのエラーと不正確さが変更なしに1つのリソースから別のリソースに転送されるためです。





トレーニング

Ubuntu OSで通常のVPSを使用しており、DjangoのPyCharmまたはメモ帳でサイトを作成しています。あとは公開、ドメインのバインド、証明書のインストールを行ってください。





最初のステップは、リポジトリのリストを更新し、nginxパッケージをすぐにインストールすることです。





apt-get update
apt-get install nginx
      
      



サイトファイルを/ var / www /ディレクトリに保存することにしました。この場合、cd / var / www / ディレクトリに移動し、 新しいmkdir geekheroディレクトリ を作成して 、次のパスを取得します:/ var / www / geekhero /





新しいgeekheroディレクトリに移動し ます:  cd geekheroそして仮想環境を作成します:  python3 -m venv geekhero_env





仮想環境をアクティブ化します:  source geekhero_env / bin /アクティブ化 してDjangoをインストールします:pip install Djangoそしてすぐにpipinstallgunicornをインストールします





django-admin startproject ghproj







; : python manage.py makemigrations



  ,  python manage.py migrate



  .





: python manage.py createsuperuser



.





applications, , .





Settings.py , :

import os



– , :





STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
      
      



 Gunicorn

/etc/systemd/system/ : gunicorn.service gunicon.socket:





gunicorn.service:





[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
WorkingDirectory=/var/www/geekhero #     manage.py
ExecStart=/var/www/geekhero/geekhero_env/bin/gunicorn --workers 5 --bind unix:/run/gunicorn.sock ghproj.wsgi:application
#   gunicorn   

[Install]
WantedBy=multi-user.target
      
      



gunicorn.socket:





[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target
      
      



gunicorn.service :





systemd-analyze verify gunicorn.service
      
      



NGINX

: /etc/nginx/sites-available/ geekhero ( ) :





server {
    listen 80;
    server_name example.com;
    
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/geekhero;           #  static 
    }
    
    location /media/ {
        root /var/www/geekhero;           #  media 
    }
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
      
      



,  /etc/nginx/site-enabled/  :





sudo ln -s /etc/nginx/sites-available/geekhero /etc/nginx/sites-enabled/
      
      



, sites-enabledsudo systemctl restart nginx







nginx :





sudo nginx -t
      
      



sudo nginx -t



gunicorn socket:





sudo systemctl enable gunicorn
sudo systemctl start gunicorn
      
      



:





sudo systemctl disable gunicorn

sudo systemctl stop gunicorn





, , - HTML python , , , , , python manage.py makemigrations <app> migrate <app> .





/ Gunicorn: 

service gunicorn start / service gunicorn stop







:





sudo systemctl status gunicorn

sudo journalctl -u gunicorn.socket
(     :  05 16:40:19 byfe systemd[1]: Listening on gunicorn socket. )
      
      



, :





file /run/gunicorn.sock
      
      



: /run/gunicorn.sock: socket





- gunicorn.service .socket, :





systemctl daemon-reload
      
      



, nginx:





sudo service nginx start
      
      



SSL

certbot Let's Encrypt: sudo apt-get install certbot python-certbot-nginx







certbot: sudo certbot certonly --nginx







- nginx: sudo certbot install --nginx







nginxサービスを再起動するだけです。 sudo systemctl restart nginx







結果

この記事の一部として、Django、Gunicorn、nginx、さらにはLet's Encryptのcertbotをインストールして、サイトを本番環境に移行する方法について説明しました。








All Articles