django+uwsgi+nginx部署在ubuntu系統上

1. 安裝以前的準備工做
  • 修改項目下settings.py文件,將調試狀態改成False
DEBUG = False
ALLOWED_HOSTS = ['*']
  •  可否正常啓動項目:python manage.py runserver
2. 安裝nginx1.8.1
從http://nginx.org/en/download.html下載

安裝gcc g++的依賴庫

sudo apt-get install build-essential
sudo apt-get install libtool

安裝 pcre依賴庫html

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

安裝 zlib依賴庫(若是報錯,需再安裝ruby):python

$sudo apt-get install rub
sudo apt-get install zlib1g-dev
 
在nginx官網下載nginx1.8.1而後解壓
 
./configure --prefix=/usr/local/nginx 
#編輯nginx:
make
#安裝nginx:
sudo make install
#啓動nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路徑,不加的話,nginx會自動加載默認路徑的配置文件,能夠經過 -h查看幫助命令。
#查看nginx進程:
ps -ef|grep nginx
 
中止 Nginx服務器(
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s quit

能夠將nginx路徑加入系統的環境變量中,就無需寫路徑了:nginx -s stop

 
3.安裝uwsgi:

 python3 -m pip install uwsginginx

 

4.django+nginx+uwsgi
在項目目錄下建立myweb_uwsgi.ini配置文件(就是和manag.py同級)
# myweb_uwsgi.ini file[uwsgi]

# Django-related settings
socket = :8000(django項目啓動的端口)

# the base directory (full path)
chdir           = /home/fnngj/pydj/myweb(項目路徑)

# Django s wsgi file
module          = myweb.wsgi(注意)

# 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
 
接下來,切換到 myweb項目目錄下,經過uwsgi命令讀取myweb_uwsgi.ini文件啓動項目
uwsgi --ini django_hotel_uwsgi.ini 
(注意看有沒有報錯,根據錯誤信息進行修改)
 
再接下來要作的就是修改nginx.conf配置文件。打開/usr/local/nginx/conf/nginx.conf文件,添加以下內容
server {
    listen         10000; (外網訪問端口設置;要防火牆打開了這個端口,否則外網沒法訪問)
    server_name    127.0.0.1 
    charset UTF-8;
    access_log      /var/log/nginx/myweb_access.log;
    error_log       /var/log/nginx/myweb_error.log;

    client_max_body_size 75M;

    location / { 
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 2;
    }   
    location /static {
        expires 30d;
        autoindex on; 
        add_header Cache-Control private;
        alias /home/fnngj/pydj/myweb/static/;(靜態文件地址)
     }
 }
啓動nginx而後,訪問:http://127.0.0.1:10000
相關文章
相關標籤/搜索