1. 程式人生 > >Linux 下部署Django項目

Linux 下部署Django項目

重啟nginx system files 退出 box 協議類型 nec ret views

Linux 下部署Django項目

說明:本文所使用的環境為CentOS 6+Python2.7+Django1.11

安裝Django、Nginx和uWSGI

1.確定已經安裝了2.7版本的Python;
2.安裝python-devel
yum install python-devel
3.安裝uwsgi
pip install uwsgi

測試uwsgi是否能正常工作

1.新建一個index.py;

# index.py
def application(env, start_response):
    start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)])
    return "Hello World"
  • 1
  • 2
  • 3
  • 4

2.uwsgi –http :8000 –wsgi-file index.py
瀏覽器訪問8000端口看是否有hello world輸出
註意:確保8000端口能被外網訪問

測試Django能否正常工作

$ cd /var/www/
$ django-admin startproject mysite
$ cd mysite
$ python manage.py runserver 0.0.0.0:8000
瀏覽器訪問8000端口看是否有hello world輸出

測試uwsgi是否能和django集成

uwsgi --http :8000 --chdir=/var/www/mysite --module mysite.wsgi


or
uwsgi --http :8008 --chdir /var/www/mysite --wsgi-file weixin/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9192

在瀏覽器中訪問8000端口,看能否正常訪問django網站。

參數說明:

# http : 協議類型和端口號
# processes : 開啟的進程數量
# workers : 開啟的進程數量,等同於processes(官網的說法是spawn the specified number ofworkers / processes)
# chdir : 指定運行目錄(chdir to specified directory before apps loading)
# wsgi-file : 載入wsgi-file(load .wsgi file)
# stats : 在指定的地址上,開啟狀態服務(enable the stats server on the specified address)
# threads : 運行線程。由於GIL的存在,我覺得這個真心沒啥用。(run each worker in prethreaded mode with the specified number of threads)
# master : 允許主進程存在(enable master process)
# daemonize : 使進程在後臺運行,並將日誌打到指定的日誌文件或者udp服務器(daemonize uWSGI)。實際上最常
用的,還是把運行記錄輸出到一個本地文件上。
# daemonize : 使進程在後臺運行,並將日誌打到指定的日誌文件或者udp服務器(daemonize uWSGI)。實際上最常
用的,還是把運行記錄輸出到一個本地文件上。
# vacuum : 當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

配置Nginx,使Nginx能為Django提供服務

在/etc/nginx/conf.d/下創建一個針對mysite項目的配置文件,詳細如下:

# /etc/nginx/conf.d/mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
    server 127.0.0.1:8000; # for a web port socket
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # 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 的static和 media目錄
    # 如果沒有static或media目錄,你需要先創建
    location /media  {
        alias /var/www/mysite/media; 
    }  

    location /static {
        alias /var/www/mysite/static; 
    }

    # 將所有非靜態文件的請求轉給django server處理,這裏的django server用的是uwsgi。
    location / {
        uwsgi_pass  django;
        include  /var/www/mysite/uwsgi_params; 
    }
}
#你可以從/etc/nginx/uwsgi_params復制一個拷貝到/var/www/mysite/uwsgi_params。
$ cp /etc/nginx/uwsgi_params /var/www/mysite/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

需要補充說明的是,在/etc/nginx/nginx.conf文件中,在最後一行的配置是include /etc/nginx/conf.d/*.conf,也就是說,/etc/nginx/conf.d/mysite_nginx.conf是會被包含在/etc/nginx/nginx.conf中的。

重啟nginx服務器,驗證訪問結果

/etc/init.d/nginx restart
通過瀏覽器訪問80端口,你發現了什麽?502 Bad Gateway?是不是?想一想,這是為什麽呢?原因是你訪問80端口時,請求的資源不是static,也不是media,這個時候Nginx就把請求轉給upstream django,upstream的網關配置的127.0.0.1:8000,而127.0.0.1:8000是要靠uwsgi啟動的,所以報了一個502 Bad Gateway。你,明白了嗎?

註:CentOS 7啟動服務的命令是systemctl restart nginx.service

啟動uwsgi,再次驗證結果

執行下面一個命令,啟動uwsgi。
uwsgi --socket :8000 --chdir=/var/www/mysite --module mysite.wsgi
重啟Nginx服務/etc/init.d/nginx restart,再次通過瀏覽器訪問80端口試試看。是不是成功了?

註:CentOS 7啟動服務的命令是systemctl restart nginx.service

如何使uwsgi以配置文件運行?Configuring uWSGI to run with a .ini file

創建一個mysite_uwsgi.ini文件,內容如下:

[uwsgi]
socket=:8000
chdir = /var/www/mysite
#wsgi-file = mysite/wsgi.py
module=mysite.wsgi:application
processes = 10
threads = 2
#django<1.4,必須指定env和module
env = DJANGO_SETTINGS_MODULE=mysite.settings

# clear environment on exit
vacuum = true
safe-pidfile = /tmp/project-master.pid # create a pidfile
harakiri = 20 # respawn processes taking more than 20 seconds
limit-as = 128 # limit the project to 128 MB
max-requests = 5000 # respawn processes after serving 5000 requests
daemonize = /var/log/uwsgi/mysite.log # background the process & log
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

執行命令uwsgi --ini mysite_uwsgi.ini即可運行

如何以Emperor模式運行?

什麽是Emperor模式?,官網說的很清楚,如下:

uWSGI can run in ‘emperor’ mode. In this mode it keeps an eye on a directory of uWSGI config files, and will spawn instances (‘vassals’) for each one it finds.

Whenever a config file is amended, the emperor will automatically restart the vassal.

按下面的步驟操作,即可以Emperor模式運行uwsgi:
1. create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
2. symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
3. run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid nginx --gid nginx

如何創建uwsgi服務?

在Linux中,一個服務其實就是一個shell腳本。在CenOS6中,服務腳本一般都在/etc/init.d/目錄下。
首先我們在/etc/initd/目錄下創建一個uwsgi文件,文件內容如下:

#!/bin/sh
#
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $syslog $remote_fs
# Should-Start: $time ypbind smtp
# Required-Stop: $syslog $remote_fs
# Should-Stop: ypbind smtp
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
# Check for missing binaries (stale symlinks should not happen)
UWSGI_BIN="/usr/local/bin/uwsgi"
UWSGI_EMPEROR_MODE=true
UWSGI_VASSALS="/etc/uwsgi/vassals/"
UWSGI_OPTIONS="--uid nginx  --gid nginx  --logto /var/log/uwsgi/uwsgi.log"
lockfile=/var/lock/subsys/uwsgi
UWSGI_OPTIONS="$UWSGI_OPTIONS --autoload"
if [ "$UWSGI_EMPEROR_MODE" = "true" ] ; then
    UWSGI_OPTIONS="$UWSGI_OPTIONS --emperor $UWSGI_VASSALS"
fi
case "$1" in
    start)
    echo "Starting uWSGI ... "
    daemon $UWSGI_BIN $UWSGI_OPTIONS &
    ;;
    stop)
    echo "Shutting down uWSGI ... "
    killproc $UWSGI_BIN
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    status)
    echo -n "Checking for service uWSGI "
    status $UWSGI_BIN
    ;;
    *)
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
    ;;
esac
exit 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

然後,我們可以使用此腳本來管理uwsgi,如下:

/etc/init.d/uwsgi start
/etc/init.d/uwsgi stop
/etc/init.d/uwsgi restart
/etc/init.d/uwsgi status

需要註意的是,日誌文件夾的所屬權應該歸配置文件中指定的用戶nginx
$ chown nginx.nginx /var/log/uwsgi -R

如何設置開機起動uwsgi?

把啟動uwsgi的命令添加到“/etc/rc.local”文件中即可。

多站點部署問題

#Simple HTTP server
server {
    listen   80; 
    root /usr/share/nginx/www;
    server_name host1.example.com;
}

#Django server
server {
    listen   80; 
    server_name host2.example.com;

    #...upstream config...
}

Linux 下部署Django項目