Django項目部署(Nginx+uWSGI)

一、服務器連接

  • 重置密碼
  • 安全組設置
  • ssh root@112.74.55.3

二、服務器基本配置

  • 虛擬環境安裝
# 第一步: 安裝
$ pip install virtualenv
$ pip install virtualenvwrapper

# 第二步: 查看安裝目錄
$ type virtualenvwrapper.sh

# 第三步: 配置
$ vi ~/.bashrc
    export WORKON_HOME=~/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh

# 第四步: 創建目錄
$ mkdir ~/.virtualenvs

# 第五步: 刷新環境
$ source ~/.bashrc

# 第六步: 創建虛擬環境
$ mkvirtualenv python3 -p /usr/bin/python3.5

# 第七步: 檢查是否成功(是否python3.5版本)
$ python

# 備注: ubuntu中Python2的環境默認都是全的,但是Python3的集成不夠完整,有部分包是欠缺的
$ apt update
$ apt install python3-dev

  • 數據庫安裝
# 更新
$ apt update

# 安裝
$ apt install mysql-server

# 設置開機自啟動
$ systemctl enable mysql.service

# 查看狀態
$ systemctl status mysql.service

# 連接測試
$ mysql -uroot -p

三、Nginx簡介

Nginx是一個非常輕量級的HTTP服務器, 是一個高性能的HTTP和反向代理服務器,同時也是一個IMAP/POP3/SMTP 代理服務器。

作為web服務器: 相比于Apache,Nginx使用資源更少,支持更多并發連接,體現更高的效率,能夠支持高達5W個并發連接的響應;

作為負載均衡服務器: Nginx既可以在內部直接支持Redis和PHP,也可以支持作為HTTP代理服務器對外進行服務,Nginx使用C編寫的,不論是系統資源開銷還是CPU使用效率都處理的非常好;

中文文檔資料: http://www.nginx.cn/doc/index.html
官方文檔: http://nginx.org

  • 安裝nginx
# 安裝
## key驗證
$ wget http://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key

## 添加到 /etc/apt/sources.list 文件中
deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

## 更新源
$ apt update

## 安裝
$ apt install nginx

# 設置開機自啟動
$ systemctl enable nginx.service

# 查看狀態
$ systemctl status nginx.service

# 檢查是否安裝成功
瀏覽器中輸入服務器IP地址,可以看到`Welcome to nginx!`說明安裝成功!

卸載: apt remove nginx

殺死進程: pkill -9 nginx

  • nginx指定配置文件啟動
# 不運行,僅測試配置文件
$ nginx -t 

# 從指定路徑加載配置文件
$ nginx -c configPath

# 測試執行配置文件
$ nginx -t -c configPath

  • nginx配置文件結構
main    # 全局設置
events{     # 工作模式,連接配置
    ....
}
http{       # http的配置
    ...
    upstream xxx{...}   # 負載均衡配置
    server{             # 主機設置
        ...
        location xxx{   # URL匹配
            ...
        }
    }
}

ubuntu中默認配置文件: /etc/nginx/nginx.conf

  • 2048游戲 測試配置
# 說明: 2048目錄中2048.html、bind_polyfill.js、style.css

# 第一步
將2048目錄拷貝到 /var/www/game/ 中

# 第二步/etc/nginx/mynginx.conf配置
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    #設定虛擬主機配置
    server {
        #監聽80端口
        listen 80;
        listen [::]:80 ipv6only=on default_server;

        #服務器IP
        server_name 112.74.55.3;

        #默認請求
        location /2048 {
            #別名
            alias /var/www/game/2048/;
        }
    }

    #include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*;
}

# 第三步,指定配置文件啟動
nginx -t -c mynginx.conf

# 第四步,瀏覽器中訪問
http://112.74.55.3/2048/2048.html

注意: 此處配置確保nginx能正常運行并訪問!

四、Django項目基本配置

  • 項目基本配置
- 上傳項目
    $ scp -r axf root@112.74.55.3:/var/www

- 安裝依賴
    # 切換到項目目錄中
    $ pip install -r requirements.txt

- 關閉調試模式 settings.py
    DEBUG = False

- 開啟訪問權限 settings.py
    ALLOWED_HOSTS = ['*']

- 啟動項目
    $ python manage.py runserver 0.0.0.0:8000

- 瀏覽器(此時靜態文件是訪問不了的)
    112.74.55.3:8000/axf/

確保項目是能夠正常啟動的,后續再對接nginx!

  • 項目靜態文件配置(對接nginx)
- myniginx.conf配置(對應項目static的目錄位置)
    #靜態文件配置
    location /static {
        #別名
        alias /var/www/Python1807AXF/static/;
    }

- 關閉nginx
    pkill -9 nginx

- 對應配置文件啟動
    nginx -c mynginx.conf

- 瀏覽器訪問靜態文件(確保能夠訪問項目的靜態文件)
    http://112.74.55.3/static/base/css/reset.css

五、uwsgi

web服務器,支持多線程。在生產環境中使用WSGI作為python web的服務器。Python Web服務器網關接口,是Python應用程序或框架和Web服務器之間的一種接口,被廣泛接受。

  • uwsgi基本使用
- 安裝(安裝在虛擬環境中!!!)
    $ pip install uwsgi

- 項目目錄中 添加 uwsgi.ini文件
    # 即是在axf目錄中添加
    touch uwsgi.ini

- 配置uwsgi.ini文件(測試: 直接使用uwsgi,而不對接nginx)
    # uwsgi基本使用沒問題,再對接上nginx,即打開socket,關閉http
    [uwsgi]
    # 使用nginx連接時 使用
    #socket=0.0.0.0:8000
    # 直接作為web服務器使用
    http=0.0.0.0:8010
    # 配置工程目錄
    chdir=/var/www/axf/Python1807AXF
    # 配置項目的wsgi目錄。相對于工程目錄
    wsgi-file=Python1807AXF/wsgi.py

    #配置進程,線程信息
    processes=1
    threads=1
    enable-threads=True
    master=True
    pidfile=uwsgi.pid
    daemonize=uwsgi.log

- 使用
    # 啟動
    $ uwsgi --ini uwsgi.ini  
    # 停止
    $ uwsgi --stop uwsgi.ini

# 訪問測試(確保uswgi能夠啟動項目)
    http://112.74.55.3:8010/axf/

查看進程: ps -ef | grep uwsgi
關閉對應服務: pkill -9 uwsgi

六、uwsgi與nginx對接

nginx和uwsgiI都是Web服務器,nginx負責靜態內容,uwsgi負責Python這樣的動態內容,二者配合共同提供Web服務以實現提高效率和負載均衡等目的。

  • uwsgi對接nginx
- uwsig.ini文件
    [uwsgi]
    # 使用nginx連接時 使用
    socket=0.0.0.0:8000
    # 直接作為web服務器使用
    #http=127.0.0.1:8010
    # 配置工程目錄
    chdir=/var/www/axf/PythonAXF
    # 配置項目的wsgi目錄。相對于工程目錄
    wsgi-file=PythonAXF/wsgi.py

    #配置進程,線程信息
    processes=1
    threads=1
    enable-threads=True
    master=True
    pidfile=uwsgi.pid
    daemonize=uwsgi.log

- mynginx.conf配置文件
    #默認請求
    location / {
        #導入了uwsgi的配置
        include /etc/nginx/uwsgi_params;
        #設定了uwsig服務器位置
        uwsgi_pass 127.0.0.1:8000;
    }
    location /2048 {
        #別名
        alias /var/www/game/2048/;
    }
    #靜態文件
    location /static{
        alias /var/www/axf/Python1807AXF/static/;
    }

- 瀏覽器訪問
    http://112.74.55.3/axf/

七、其他

  • Linux中文件傳輸
# 上傳目錄
scp -r ./atom  用戶名@IP:/home/

# 上傳文件
scp ./atom  用戶名@IP:/home/

  • uwsgi錯誤1
啟動uwsgi時,錯誤: 提示沒有 'uwsgi_params'

#默認請求
    location / {
        #導入了uwsgi的配置
        include /etc/nginx/uwsgi_params;
        #設定了uwsig服務器位置
        uwsgi_pass 127.0.0.1:8000;
    }
    location /2048 {
        #別名
        alias /var/www/game/2048/;
    }
    location /static{
        alias /var/www/axf/Python1807AXF/static/;
    }

比較常見的是uwsgi_params缺少問,這可以直接指定包含路徑!

  • uwsgi錯誤2
問題描述:
    uwsgi --ini uwsgi.ini 啟動項目
    但瀏覽器訪問時,顯示服務器內部錯誤

問題分析:
    查看運行日志: uwsgi.log
    Traceback (most recent call last):
    File "Python1807AXF/wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)

    項目運行環境是在虛擬環境中,而uwsgi并沒安裝到虛擬環境導致的!
    通過whereis uwsgi可以查看到!

解決:
    方式一: 卸載掉uwsgi,在虛擬環境中重新安裝uwsgi即可
    方式二: 虛擬環境安裝uwsgi,并直接使用虛擬環境中的uwsgi啟動uwsgi.ini文件

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容