nginx+uwsgi+django1.11 環境部署

    前段時間配置 python3.6.2+nginx+uwsgi 用於 django1.11 做請求,整理記錄一下(自己配置測試沒有問題)。

一、安裝配置環境

python 3.6.2(virtualenv)
djano 1.11
nginx 1.12.1
uwsgi

1、安裝virtualenv python 3.6.2,請查看之前寫的環境初始化文檔

2、django安裝也查看環境初始化文檔
3、安裝nginx
在/etc/yum.repos.d/目錄下創建一個源配置文件nginx.repo:
cd /etc/yum.repos.d/
vim nginx.repo
#填寫如下內容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
保存,則會產生一個/etc/yum.repos.d/nginx.repo文件。
下面直接執行如下指令即可自動安裝好Nginx:
yum install nginx -y
安裝完成,下面直接就可以啓動Nginx了:
service nginx start
現在Nginx已經啓動了,直接訪問服務器就能看到Nginx歡迎頁面了的。
4、安裝uwsgi
pip install uwsgi

django
一個基於python的開源web框架,請確保自己熟悉它的框架目錄結構。
uWSGI
一個基於自有的uwsgi協議、wsgi協議和http服務協議的web網關
nginx
常用高性能代理服務器
wsgi.py

django項目攜帶的一個wsgi接口文件

例子項目名叫opsweb的話,此文件就位於 opsweb/opsweb/wsgi.py

二、配置

首先,確保你已經安裝好了nginx並可以正常使用。 
接着,別忘了確認自己項目所需的django已經完成安裝並正常工作。 

##配置前,項目目錄結構
cd /root/Documents/project
tree opsweb
opsweb/
	|-- accounts
	|-- dashboard
	|-- manage.py
	|-- opsweb
	|   |-- __init__.py
	|   |-- settings_dev.py
	|   |-- settings.py
	|   |-- urls.py
	|   `-- wsgi.py
	|-- resources
	|-- static

配置nginx
默認配置文件的路徑是:/etc/nginx/nginx.conf
然後,確保nginx.conf的同目錄下有uwsgi_params文件(/etc/nginx/uwsgi_params),沒有的話根據鏈接獲取, 後面要用到

vim /etc/nginx/nginx.conf
##修改第一行user配置root,如果你的項目用戶權限是nginx用戶也可配置nginx
user  root;
##最後http段落裏include配置成下面的內容
include /root/Documents/project/opsweb/opsweb.conf;

##這裏的文件是上面include指定的文件,也可以放置在nginx的默認/etc/nginx/conf.d/目錄下面
##這裏是爲了配置測試查看方便
vim /root/Documents/project/opsweb/opsweb.conf ##添加下面內容
server {
    listen 80;
    server_name localhost;
    charset     utf-8;
    access_log      /root/Documents/project/opsweb/access_log; ##nginx訪問日誌文件,最後放置項目文件下
    error_log       /root/Documents/project/opsweb/error_log;
    client_max_body_size 75M;


    location /static {
        alias /root/Documents/project/opsweb/static; ##指定項目的靜態文件目錄,注意權限
    }

    location / {
        include     /etc/nginx/uwsgi_params; ##queren這個文件是存在的
        uwsgi_pass  127.0.0.1:9090; 
    }
}

#配置nginx後,重啓nginx
service nginx restart

配置uwsgi
vim /root/Documents/project/opsweb/uwsgi_opsweb.ini
##這裏的uwsgi_opsweb.ini要命名成*.ini後綴文件
##uwsgi配置文件放置在項目文件裏,也可把這些文件整理放置在統一的目錄下
[uwsgi]
socket = 127.0.0.1:9090 #uwsgi監聽的地址及端口(與nginx配置是一致的)
chmod-socket=666
chdir = /root/Documents/project/opsweb ##項目的主目錄
module = opsweb.wsgi:application  ##是項目名稱.wsgi:application
wsgi-file = /root/Documents/project/opsweb/opsweb/wsgi.py  ##項目裏的wsgi.py文件路徑
master = true
pythonpath = /root/Desktop/reboot/env/lib/python3.6/site-packages  ##指定virtualenv環境裏python3.6.2的庫目錄,一定要指定,是使用virtualenv環境啓動python的主要配置行
processes=2
threads=2
max-requests=2000
enable-threads = true
vacuum=true
daemonize=/root/Documents/project/opsweb/opsweb.log  ##指定uwsig的日誌文件,否則會輸出到屏幕上,這個在調試時可以暫時不配置
pidfile = /root/Documents/project/opsweb/lin.pid
py-autoreload = 1

##確保進入virtualenv環境
source ~/Desktop/reboot/env/bin/activate
cd /root/Documents/project/opsweb ##項目目錄下
執行:uwsgi --ini /root/Documents/project/opsweb/uwsgi_opsweb.ini
##輸出下面內容,表示成功
'''
*** Starting uWSGI 2.0.15 (64bit) on [Tue Oct  3 09:06:36 2017] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-11) on 03 October 2017 01:29:18
os: Linux-2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014
nodename: web.com
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /root/Documents/project/opsweb
writing pidfile to /root/Documents/project/opsweb/lin.pid
detected binary path: /root/Desktop/reboot/env/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
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/Documents/project/opsweb
your processes number limit is 14717
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 127.0.0.1:9090 #uwsgi監聽的地址及端口(在nginx配置中會用到) fd 3
Python version: 3.6.2 (default, Aug  5 2017, 01:32:30)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]
Python main interpreter initialized at 0xeb43b0
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 249216 bytes (243 KB) for 4 cores
*** Operational MODE: preforking+threaded ***
added /root/Desktop/reboot/env/lib/python3.6/site-packages/ to pythonpath.
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0xeb43b0 pid: 6870 (default app)
mountpoint  already configured. skip.
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 6870)
spawned uWSGI worker 1 (pid: 6872, cores: 2)
spawned uWSGI worker 2 (pid: 6873, cores: 2)
Python auto-reloader enabled
'''

##配置完成後,項目目錄結構
cd /root/Documents/project
tree opsweb
opsweb/
	|-- access_log
	|-- accounts
	|-- dashboard
	|-- error_log
	|-- lin.pid
	|-- manage.py
	|-- opsweb
	|   |-- __init__.py
	|   |-- settings_dev.py
	|   |-- settings.py
	|   |-- urls.py
	|   `-- wsgi.py
	|-- opsweb.conf
	|-- opsweb.log
	|-- resources
	|-- static
	`-- uwsgi_opsweb.ini

使用瀏覽器訪問(是自己服務器的ip地址)。


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