1. 程式人生 > >Ubuntu下flask Django專案部署

Ubuntu下flask Django專案部署

Ubuntu下python框架 Django Flask 專案部署 使用nginx

一。下載安裝 nginx
方法1:https://blog.csdn.net/b_evan/article/details/72858149

方法2:
1.去nginx管網
2.選擇download
3.文件中找官網安裝方式
4.下載認證金鑰
wget http://nginx.org/keys/nginx_signing.key
會下載到當前目錄下的nginx_signing.key檔案中
5.安裝
sudo apt-key add nginx_signing.key
6.配置源
切換到對應的檔案,並編輯
vim /etc/apt/sources.list
加上源:
deb

http://nginx.org/packages/ubuntu/ codename nginx
deb-src http://nginx.org/packages/ubuntu/ codename nginx
注意將codename替換成當前codename 16.04 對應為: xenial
儲存退出
7.更新跟安裝
apt-get update
apt-get install nginx
8.檢視nginx服務是否開啟
ps -ef | grep nginx
9.啟動 sudo nginx
10.直接輸入地址可以訪問 即可…

**nginx使用 **

  1. sudo service nginx start 啟動
  2. sudo service nginx stop 停止
  3. sudo service nginx restart 重啟
  4. sudo nginx -c ‘配置檔案’ #以配置檔案中設定啟動伺服器

安裝uwsgi pip install uwsgi

二。部署到伺服器
Django配置與Flask配置相差不大

flask
一,在專案下新建uwsgi配置檔案。格式可以是.ini。將配置寫入儲存

[uwsgi]
#外部訪問地址,可以指定多種協議,現在用http便於除錯,之後用socket
socket = 0.0.0.0:8000 # uwsgi的監聽埠
#指向專案目錄
chdir = /home/xlg/blog/

flask啟動程式檔案

wsgi-file = manage.py #flask在manage.py檔案中的app名

callable = app

plugins = python
#這行一定要加上,不然請求時會出現-- unavailable modifier requested: 0 --錯誤提示

#處理器數
processes = 1

#執行緒數
threads = 2

二,新建nginx啟動設定檔案。字尾.conf。可命名為nginxflask.conf將配置寫入儲存

user root;

worker_processes 1;

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {

include       /etc/nginx/mime.types;

default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
                  
access_log  /var/log/nginx/access.log  main;

sendfile        on;

#tcp_nopush     on;

keepalive_timeout  65;

#gzip  on;

#include /etc/nginx/conf.d/*.conf;

server{

listen 80; # 伺服器監聽埠
server_name 10.31.160.242; # 這裡寫你的域名或者公網IP
location / {
uwsgi_pass 127.0.0.1:5000;
# 轉發埠,需要和uwsgi配置當中的監聽埠一致

           	include /etc/nginx/uwsgi_params; # 匯入uwsgi配置

            #uwsgi_param UWSGI_PYTHON /home/自己建立的目錄/venv; 
            # Python直譯器所在的路徑(這裡為虛擬環境)
            uwsgi_param UWSGI_PYTHON /home/fengyun/.local/virtualenvs/python3.5/bin;  

			uwsgi_param UWSGI_CHDIR  /home/fengyun/Desktop/nginxProject/blog;
			# 自己建立的目錄 專案根目錄

            uwsgi_param UWSGI_SCRIPT manage:app; # 指定啟動程式

        	#比如你測試用test.py檔案,檔案中app = Flask(name),那麼這裡就填 test:app
    }

}

}

三,停止當前執行nginx sudo service nginx stop
如果不能停止。可用 ps -ef | grep nginx檢視程序號 然後使用 kill 程序號殺死程序

四,使用個人配置的啟動nginx 伺服器
sudo nginx -c ~/Desktop/nginxconf/nginxFlask.conf

五,在專案目錄下使用虛擬環境啟動uwsgi

一般都會成功
Django與flask一樣
nginxdjango.conf檔案

user root;

worker_processes 1;

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {

include       /etc/nginx/mime.types;

default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
                  
access_log  /var/log/nginx/access.log  main;

sendfile        on;

#tcp_nopush     on;

keepalive_timeout  65;

#gzip  on;

#include /etc/nginx/conf.d/*.conf;

server {

 listen       80;

 server_name  10.31.160.242 ;

 charset utf-8;

 root   /var/project;

 index  hello.html;

 location / {

	 include /etc/nginx/uwsgi_params;

	 uwsgi_pass 127.0.0.1:8010;

 }

 location /static{

	alias  /var/project/axf1805/static;

 }

}

}

uwsgi.ini檔案
[uwsgi]
#使用nginx連線時 使用
socket=127.0.0.1:8010
#直接作為web伺服器使用
;http=127.0.0.1:8010
#配置工程目錄
chdir=/var/project/axffengyun
#配置專案的wsgi目錄。相對於工程目錄
wsgi-file=axffengyun/wsgi.py

#配置程序,執行緒資訊
processes=4
threads=2
enable-threads=True
master=True
pidfile=uwsgi.pid
daemonize=uwsgi.log