Ubuntu18.04 Djangoプロジェクトの導入(Nginx+uWSDI)
1.Nginxのインストール
sudo apt-get install nginx
sudo service nginx start
sudo service nginx stop
sudo service nginx restart
または
sudo /etc/init.d/ngnix start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart
grep /var/log/nginx/error.log
2.uWSDIのインストール
apt-get install python-dev
pip install uwsgi
def application(env, start_response):
start_response('200 OK', [('Contest-Type', 'text/html')] )
return [b'uWSGI is Good!'] #python3
#return ['uWSGI is Good!'] #python2
uWSDiをテストし、正しく表示されている場合は、web client uWSDi Pythonの3つのセクションがスムーズであることを示します.
uwsgi --http :8080 --wsgi-file test.py
パラメータの説明:1.http:8080:httpプロトコルを使用し、ポート番号は8080 2.wsgi-file test.py:指定したファイルをロード
uwsgi --http :8080 --module mysite.uwsg
3.Nginxの構成
3.1サーバ構成の追加
# the upstream component nginx needs to connect to
upstream django {
# server unix:///home/wfc/works/Django/LicenseSite/license.sock; # socket
server 127.0.0.1:8080; # TCP
}
# configuration of the server
server {
# the port your site will be served on
listen 8080;
# the domain name it will serve for
server_name 127.0.0.1; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
alias /home/wfc/works/Django/LicenseSite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/wfc/works/Django/LicenseSite/uwsgi_params; # the uwsgi_params file you installed
}
}
sudo ln -s /home/wfc/works/Django/LicenseSite/mysite_nginx.conf /etc/nginx/sites-enabled/
3.2 staticファイルの配備
djangoプロジェクトのsettingファイルに、次の行を追加します.
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
collectstaticを実行し、主にadminの静的ファイルをプロジェクトディレクトリにコピーします.そうしないと、開いているページにはフォーマット効果がありません.
python manage.py collectstatic
3.3試験Nginx
sudo service nginx restart
4.TCP portの代わりにUnix socketを使用
server unix:///home/wfc/works/Django/LicenseSite/license.sock; # socket
#server 127.0.0.1:8080; # TCP
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666 # (very permissive)
または
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664 # (more sensible)
5.使用iniファイル実行uWSDI
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/wfc/works/Django/LicenseSite/
# Django's wsgi file
module = LicenseSite.wsgi
# the virtualenv (full path)
home = /home/wfc/works/Django/LicenseSite/venv/
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /home/wfc/works/Django/LicenseSite/license.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
uwsgi --ini mysite_uwsgi.ini
6.uWSDIの自己起動を設定する
[Unit]
Description=uWSGI instance
After=network.target
[Service]
User=wfc
WorkingDirectory=/home/wfc/works/Django/LicenseSite
Enviroment=/home/wfc/works/Django/LicenseSite/venv/pyvenv.cfg
ExecStart=/home/wfc/.local/bin/uwsgi --ini /home/wfc/works/Django/LicenseSite/license_uwsgi.ini
[Install]
WantedBy=multi-user.target
sudo systemctl enable mysite
sudo systemctl start epolicense
sudo systemctl restart epolicense
sudo systemctl stop epolicense
sudo systemctl status epolicense