Install uWSGI, Nginx and Django to let them work together
Install Python: sudo apt-get install python2.7-dev
Install Virtual Environment: sudo apt-get install virtualenv virtualenv uwsgi-tutorialcd uwsgi-tutorialsource bin/activate pip install Djangodjango-admin.py startproject mysitecd mysite pip install uwsgi
Create a file: 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 wget http://localhost:8000 to test
Test Django project: python manage.py runserver 0.0.0.0:8000 (then kill) uwsgi --http :8000 --module mysite.wsgi
sudo apt-get install nginxsudo /etc/init.d/nginx start # start nginx You will need the uwsgi_params file, which is available in the nginx directory of the uWSGI distribution, or from (https://github.com/nginx/nginx/blob/master/conf/uwsgi_params)
Now create a file called mysite_nginx.conf, and put this in it:
mysite_nginx.conf
# the upstream component nginx needs to connect toupstream 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 serverserver { # 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 }}
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/ Before running nginx, you have to collect all Django static files in the static folder. First of all you have to edit mysite/settings.py adding: STATIC_ROOT = os.path.join(BASE_DIR, "static/") and then run python manage.py collectstatic Restart nginx: sudo /etc/init.d/nginx restart
Let’s get nginx to speak to the “hello world” test.py application. uwsgi --socket :8001 --wsgi-file test.py wget http://localhost:8000
Using Unix sockets instead of ports So far we have used a TCP port socket, because it’s simpler, but in fact it’s better to use Unix sockets than ports - there’s less overhead.
Edit mysite_nginx.conf, changing it to match: 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) and restart nginx.
Run uWSGI again: uwsgi --socket mysite.sock --wsgi-file test.py Try http://example.com:8000/ in the browser.
Running the Django application with uwsgi and nginx Let’s run our Django application: uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
Now uWSGI and nginx should be serving up not just a “Hello World” module, but your Django project.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.