nginx + uwsgi + django

注:

    python3.5

    uwsgi 2.0.15

    nginx 1.11.12

    django 1.10

一、安裝python3.5

    yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel  安裝一大堆的插件,我怕記不住,在這寫一下

     剩下的就是./configure --prefix=/usr/local/python3 && make && make install

     在此就不過多說明了


二、安裝uwsgi

  •        我的python裝在/usr/local/python3下(不在重複說明)

  •        pip install uwsgi (方式不重要,安裝上就行,安裝在python3目錄下)

  •        ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi(做一個軟鏈接)

  •        測試uwsgi能不能用

  •        vim test.py

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]  #注意以字節形式,否則前端看不到Helloween World

         運行uwsgi --http :8001 --wsgi-file test.py

         在瀏覽器訪問 127.0.0.1:8001 如果看到Hello World就成功了

三、安裝django

  •        python3 install django(方式不重要,安裝上就行)

  •        在/usr/local/python3/bin/下執行創建命令

    python3 django-admin.py startproject dida7 #創建一個項目
    cd dida7                                   #cd到項目裏
    python3 manage.py runserver 0.0.0.0:8000   #啓動項目

        在瀏覽器內輸入:http://127.0.0.1:8002,檢查django是否運行正常。

四、安裝nginx

       我的nginx直接從官網上下載的 http://nginx.org/en/download.html

       nginx 1.11.12

   mkdir /usr/usr/nginx
   tar xf nginx-1.11.12.tar.gz -C /usr/src/nginx 
   #解壓文件放到/usr/src/nginx目錄下
   
   ./configure --prefix=/usr/local/nginx && make && make install 
   #安裝目錄/usr/local/nginx

五、創建uwsgi.ini文件(可以用xml)

我的uwsgi.ini文件放在/root/dida7目錄下,和它平級的有manage.py文件
# myweb_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8888 #指定項目執行的端口號
# the base directory (full path)
chdir           = /root/dida7  #指定項目的目錄

# Django s wsgi file
wsgi-file = /root/dida7/dida7/wsgi.py 
#這個指定django項目的wsgi.py文件,wsgi.py文件創建項目就有,我項目放在/root/dida7

# 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          = true

運行uwsgi.ini文件

先cd到/root/dida7目錄
[root@CentOS-mode dida7]# uwsgi --ini uwsgi.ini 
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.15 (64bit) on [Sat Apr  1 13:50:08 2017] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-17) on 31 March 20
17 11:45:24os: Linux-2.6.32-642.13.1.el6.x86_64 #1 SMP Wed Jan 11 20:56:24 UTC 201
7nodename: CentOS-mode
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /root/dida7
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
 chdir() to /root/dida7
your processes number limit is 7347
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8888 fd 3
Python version: 3.5.1 (default, Mar 31 2017, 10:53:29)  [GCC 4.4.7 2012
0313 (Red Hat 4.4.7-17)]*** Python threads support is disabled. You can enable it with --enable
-threads ***Python main interpreter initialized at 0x206b9a0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 363840 bytes (355 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x206b9a0 
pid: 60031 (default app)*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 60031)
spawned uWSGI worker 1 (pid: 60033, cores: 1)
spawned uWSGI worker 2 (pid: 60034, cores: 1)
spawned uWSGI worker 3 (pid: 60035, cores: 1)
spawned uWSGI worker 4 (pid: 60036, cores: 1)

運行起來,看看有沒有報錯

六、修改nginx配置

vim /usr/local/nginx/conf/nginx.conf

        location / {
            include uwsgi_params;
            uwsgi_pass 192.168.1.90:8888; #這個地址要和uwsgi.ini中的一致
            uwsgi_read_timeout 2;
            index  index.html index.htm;
        }

接下來運行uwsgi,運行nginx

訪問192.168.1.90  成功

七、結合nginx,django-admin的樣式丟失

wKiom1jfTNmBEjlzAAB03bXIIoM592.png-wh_50訪問django-admin沒有樣式

解決辦法:

       修改settings.py文件

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,"static") 
#這個static是手工建立的文件夾,STAIC_ROOT的路徑是dida7/static

       在項目目錄下執行python3 manage.py collectstatic

       會django安裝目錄下的static文件拷貝到我們創建的static目錄下

       現在已經在/root/dida7/static/下生成admin文件夾,裏邊有css、img、js

       修改nginx.conf文件

        location /static/ {
        alias /root/dida7/static/;
     }   #又寫了一個location定義靜態文件路徑

重啓uwsgi和nginx

wKioL1jfUMiid2QqAACqx6aXwKM088.png-wh_50

成功!!!  我也是坑了兩天希望能幫到各位j_0057.gif

八、多站點配置(沒有試過)

我採用運行多個uwsgi服務的方法來實現多個站點。

重複第五步,創建uwsgi9091.ini,並相應修改文件中的

socket = 127.0.0.1:9091

然後修改nginx的配置文件爲:

server {
        listen       80;        
        server_name  localhost;        
        location / {
                    include uwsgi_params;
                    uwsgi_pass 192.168.1.90:8888; #這個地址要和uwsgi.ini中的一致
                    uwsgi_read_timeout 2;
                    index  index.html index.htm;
                }
    }

server {
    listen       1300; 
    server_name      localhost;       
    location / {            
        include  uwsgi_params;                        
        uwsgi_pass 127.0.0.1:9091; #這個地址要和uwsgi.ini中的一致
               uwsgi_read_timeout 2;           
        index  index.html index.htm;        
        }
     }

然後我們就可以通過http://127.0.0.1:1300來訪問新的網站了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章