欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx+uwsgi+django環(huán)境搭建的方法步驟

 更新時(shí)間:2019年11月25日 08:30:45   作者:陳某君  
這篇文章主要介紹了nginx+uwsgi+django環(huán)境搭建的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

環(huán)境搭建

1.安裝uwsgi、nginx和django

apt install nginx
pip install uwsgi
pip install django

2.測試uwsgi和nginx的連接

PS:下面的例子采用的是 unix socket 的鏈接發(fā)送

創(chuàng)文件foobar.py

def application(env, start_response):
  start_response('200 OK', [('Content-Type','text/html')])
  return [b"Hello World"] # python3
  #return ["Hello World"] # python2

創(chuàng)文件foobar_uwsgi.ini

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir      = /home/dmd/project/ENV/mysite
# Django's wsgi file
module     = foobar

# process-related settings
# master
master     = true
# maximum number of worker processes
processes    = 10
# the socket (use the full path to be safe
socket     = /home/dmd/project/ENV/mysite/foobar.sock
# ... with appropriate permissions - may be needed
# chmod-socket  = 664
# clear environment on exit
# 這個(gè)配置本來是true,即退出就刪掉socket,但這會(huì)導(dǎo)致nginx的啟動(dòng)失敗,所以改為false
vacuum     = false

創(chuàng)文件foobar_nginx.conf

server {
  listen     8099;
  server_name  127.0.0.1
  charset UTF-8;
  access_log   /var/log/nginx/myweb_access.log;
  error_log    /var/log/nginx/myweb_error.log;

  client_max_body_size 75M;

  location / {
    include uwsgi_params;
    uwsgi_pass unix:///home/dmd/project/ENV/mysite/foobar.sock; # 用unix socket
    # uwsgi_pass 127.0.0.1:8000 # 用TCP socket
    uwsgi_read_timeout 2;
  }
 }

將這個(gè)文件鏈接到/etc/nginx/sites-enabled,這樣nginx就可以看到它了

sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

啟動(dòng)nginx

sudo service nginx start

啟動(dòng)uwsgi

uwsgi --ini foobar_uwsgi.ini

訪問127.0.0.1:8099,如果出現(xiàn)“Hello world”就說明下面連接棧是成功的。

the web client <-> the web server <-> the socket <-> uWSGI <-> Python

3.建立整個(gè)連接棧

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

建立django項(xiàng)目

django-admin startproject mysite

在項(xiàng)目的根目錄建立mysite_uwsgi.ini

# myweb_uwsgi.ini file
[uwsgi]

# Django-related settings

socket = mysite.sock

# the base directory (full path)
chdir      = /home/dmd/project/ENV/mysite

# Django s wsgi file
module     = mysite.wsgi

# process-related settings
# master
master     = true

# maximum number of worker processes
processes    = 4

# ... with appropriate permissions - may be needed
# chmod-socket  = 664
# clear environment on exit
vacuum     = false

在項(xiàng)目根目錄建立mysite_nginx.conf

server {
  listen     8099;
  server_name  127.0.0.1
  charset UTF-8;
  access_log   /var/log/nginx/myweb_access.log;
  error_log    /var/log/nginx/myweb_error.log;

  client_max_body_size 75M;

  location / {
    include uwsgi_params;
    uwsgi_pass unix:///home/dmd/project/ENV/mysite/mysite.sock; # 用unix socket
    # uwsgi_pass 127.0.0.1:8000 # 用TCP socket
    uwsgi_read_timeout 2;
  }
  location /static {
    expires 30d;
    autoindex on;
    add_header Cache-Control private;
    alias /home/dmd/project/ENV/mysite/static/;
   }
 }

鏈接sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

運(yùn)行

# 運(yùn)行uwsgi
uwsgi --ini mysite_uwsgi.ini
# 開啟niginx
sudo service nginx start

測試。訪問 127.0.0.1:8099 ,如果看到django的頁面,說明成功。

完整的目錄樹

mysite/
├── db.sqlite3
├── manage.py
├── mysite
│  ├── __init__.py
│  ├── __pycache__
│  │  ├── __init__.cpython-36.pyc
│  │  ├── settings.cpython-36.pyc
│  │  ├── urls.cpython-36.pyc
│  │  └── wsgi.cpython-36.pyc
│  ├── settings.py
│  ├── urls.py
│  └── wsgi.py
├── mysite.sock
├── mysite_nginx.conf
├── mysite_uwsgi.ini
└── uwsgi_params

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論