天天看點

Nginx+uWSGI+Django架構搭建NginxuWSGIDjango其他Nginx->uWSGIuWSGI->DjangoDjango->MySQL參考

Nginx+uWsgi+Django

Nginx : 1.8.0

uWSGI : 2.0.6

Django : 1.8.4

系統環境:Ubuntu 14.04LTS

Nginx

官網:http://nginx.org/

安裝

1. 系統安裝

$sudo apt-get install nginx
           

2. 源碼安裝

官網下載下傳源碼包:建議下載下傳穩定版

位址:http://nginx.org/en/download.html

安裝依賴庫,依賴庫gcc,pcre,zlib,openssl

$sudo apt-get install gcc
$sudo apt-get install libpcre3 libpcre3-dev
$sudo apt-get install zlib1g zlib1g-dev
$sudo apt-get install openssl libssl-dev
           

解壓,配置,編譯,安裝

$tar zxvf nginx-.tar.gz
$cd nginx-
$./configure --with-pcre --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_stub_status_module --with-debug
$make
$sudo make install
           

通過指令檢視安裝位址:

whereis nginx
           

預設安裝路徑為:/usr/local/nginx

在啟動時nignx時遇到指令無法識别,執行以下指令

$sudo cp /usr/local/nginx/sbin/nginx /usr/bin/
           

然後啟動nginx

sudo nginx
           

通過浏覽器通路http://localhost:80

nginx預設的設定界面即表示啟動成功

Welcome to nginx

3. nginx配置檔案夾

conf : /usr/local/nginx/conf

相關指令

啟動:

$sudo nginx 
           

關閉:

$sudo nginx -s stop
           

重新開機:

$sudo nginx -s reload
           

uWSGI

安裝教程:http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html

1. 安裝

$sudo apt-get install uwsgi-plugin-python
$sudo apt-get install uwsgi
           

源碼安裝

下載下傳:http://projects.unbit.it/uwsgi/wiki/WikiStart

安裝依賴項:

解壓,配置,編譯,安裝

$tar zxvf uwsgi-.tar.gz
$cd uwsgi-
$sudo python setup.py install
           

如果提示

下載下傳:https://pypi.python.org/pypi/setuptools/

解壓,配置,編譯,安裝

$ tar zxvf setuptools-.tar.gz
$ cd setuptools-/
$ sudo python setup.py install
           

重新編譯uWSGI

當安裝完成後,會提示end of uWSGI configration

測試uWSGI

列印版本資訊

$uwsgi --version
           

編寫測試腳本,test.py

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

啟動uWSGI,使用未占用端口

在浏覽器輸入位址通路

浏覽器輸出 Hello World,表示安裝成功

相關指令

啟動:

執行相應腳本

關閉:

$killall  - uwsgi
$killall -s HUP /var/www/uwsgi 
$killall -s HUP /usr/local/bin/uwsgi
           

Django

安裝依賴庫

$sudo apt-get install python2
           

安裝Django

下載下傳源碼:https://www.djangoproject.com/download/

解壓,配置,編譯,安裝

$tar zxvf Django-.tar.gz
$cd Django-
$sudo python setup.py install
           

測試Django

列印Django版本資訊

$python
>>import django
>>django.VERSION
(,,, 'final', )
           

當出現如上顯示時,說明Django安裝成功

其他

MySQL

Mysql資料庫服務

Django程式通路資料庫,python接口

圖形化管理工具workbench

Nginx->uWSGI

修改配置檔案:預設路徑 /usr/local/nginx/conf/nginx.conf

配置Nginx,将Nginx接收到的HTTP請求轉發給uWSGI

// 将域名下的所有請求全部轉發
location / {
// nginx配置檔案同級目錄下需包含uwsgi_params檔案,預設是包含的
// 如果沒有可以建立軟連接配接
include uwsgi_params;
// 配置nginx與uWSGI通信的socket通信接口
uwsgi_pass :;
}
           

uWSGI->Django

uWSGI自啟動腳本

編寫自啟動腳本,讓uWSGI随系統自啟動

在/etc/init/目錄下建立uwsgi.conf檔案

cd /etc/init/
sudo gedit uwsgi.conf
           

啟動腳本如下:

#simple uWSGI script
description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]
respawn
env LOGTO=/var/log/uwsgi/emperor.log
exec uwsgi --master --emperor /etc/uwsgi/apps-enabled --die-on-term --uid 1000 --gid 1000 --logto $LOGTO --vacuum
           

建立軟連接配接,将uWSGI配置檔案連接配接到apps-enabled目錄下

sudo ln -s ~/path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/apps-enabled/mysite_uwsgi.ini
           

mysite_uwsgi.ini(根據項目要求設定,或參考官方文檔)

[uwsgi]
socket = .:
chdir = /home/foobar/myproject/
pythonpath = ..
env = DJANGO_SETTINGS_MODULE=myproject.settings
module = django.core.handlers.wsgi:WSGIHandler()
processes = 
threads = 
stats = .:
           

Django->MySQL

在Django項目的settings.py檔案中配置MySQL

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'djangodb',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '3306',                      # Set to empty string for default.
    }
}
           

重新開機,檢視端口是否已經啟動

參考

http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html

http://segmentfault.com/q/1010000002523354

http://www.nowamagic.net/academy/detail/1330331

http://www.nowamagic.net/academy/detail/1330334

http://www.linuxidc.com/Linux/2014-09/106928.htm

檢視端口指令

檢視系統端口占用情況,netstat

sudo netstat -plnt
           

繼續閱讀