https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
pip install uwsgi
[x86_64-linux-gnu-gcc -pthread] plugins/python/python_plugin.o
In file included from plugins/python/python_plugin.c:1:0:
plugins/python/uwsgi_python.h:2:10: fatal error: Python.h: No such file or directory
#include <Python.h>
^~~~~~~~~~
위와 같은 오류가 발생 -> 구글링을 결과 python3-dev를 설치해야함.
sudo apt-get install python3-dev
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
uwsgi --http :8000 --wsgi-file test.py
2020] GET / => generated 11 bytes in 0 msecs (HTTP/1.1 200) 1 headers in 44 bytes (1 swi
tches on core 0)
sudo /etc/init.d/nginx start
https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
: uwsgi_params는 프로젝트 디렉토리에 복사하여 위치시킨다. (~/mysite/uwsgi_params)
# /etc/nginx/site-avaliable/mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/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 /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
: 위 설정은 media와 static은 filesystem이 처리하고, 나머지 요청은 Django App에서 처리되도록 nginx를 설정하는 것이다.
sudo ln -s /etc/nginx/site-avaliable/mysite_nginx.conf /etc/nginx/sites-enabled/mysite_nginx.conf
: 실제로 nginx에서 conf파일을 보도록 symbolik link를 걸어준다.
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
그리고 다음 명령어를 수행한다.
python manage.py collectstatic
sudo /etc/init.d/nginx restart
uwsgi --socket :8001 --wsgi-file test.py
server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
/etc/init.d/nginx restart
uwsgi --socket mysite.sock --wsgi-file test.py
uwsgi --socket mysite.sock --wsgi-file testpy --chmod-socket=666
uwsgi --socket mystie.sock --module mysite.wsgi --chomd-socket=666
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = project.wsgi
# the virtualenv (full path)
home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
uwsgi --ini mysite_uwsgi.ini
deactivate
sudo pip3 install uwsgi
# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
# /etc/rc.local
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log