1. 程式人生 > >ubuntu+uwsgi+nginx+django 手記

ubuntu+uwsgi+nginx+django 手記

nginx

  • 安裝nginx
apt-get install nginx
  • 啟動與停止
#啟動nginx服務
service nginx start
#檢視nginx執行狀態
ps -ef | grep nginx
#停止nginx服務
service nginx stop

測試,瀏覽器輸入ip地址,顯示如下:
這裡寫圖片描述

  • 專案nginx配置

1.在工程下新建檔案nginx.conf

  • nginx.conf:
# the upstream component nginx needs to connect to
upstream django {
server unix:///your/project/path/mysite.sock; # for a file socket
#server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server

server {
# the port your site will be served on
listen      80;
# the domain name it will serve for
server_name  your_IP_address; # substitute your machine's IP address or FQDN
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
location /media  {
    alias /your/project/path/media;  # 指向django的media目錄
}

location /static {
    alias /your/project/path/static; # 指向django的static目錄
}

# Finally, send all non-media requests to the Django server.
location / {
    uwsgi_pass  django;
    include     uwsgi_params; # the uwsgi_params file you installed
}
}

2.將工程下nginx.conf檔案建立軟連線到/etc/nginx/conf.d/下

ln -s /your/project/path/nginx.conf /etc/nginx/conf.d/

3.拉取所有需要的static file 到同一個目錄

#在django的setting檔案中,新增下面一行內容:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
#執行命令
python manage.py collectstatic

nginx相關檔案目錄

#Nginx 目錄、檔案 
web目錄:/root /var/www/html 
安裝目錄:/etc/nginx 
服務主機配置檔案:/etc/nginx/sites-available/default
日誌:/var/log/nginx/error.log
#Nginx 指令碼相關操作 
開啟:sudo /etc/init.d/nginx start 
重啟:sudo /etc/init.d/nginx restart 
停止:sudo /etc/init.d/nginx stop 
狀態:sudo /etc/init.d/nginx status

/etc/nginx/nginx.conf 配置檔案詳解
https://www.cnblogs.com/phpdragon/p/3248373.html

uwsgi

  • 專案配置檔案uwsgi.ini
# uwsgi.ini file
    [uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /your/project/PATH
# Django's wsgi file
module          = yourprojectNAME.wsgi

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 3
#buffer-size = 65536
# the socket (use the full path to be safe
#socket          = /your/project/PATH/mysite.sock
#socket          = 127.0.0.1:8000
http          = your machine's IP:8000
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true
#    logto = /tmp/mylog.log
# 儲存pid程序
pidfile		=uwsgi0.pid
# 儲存uwsgi狀態
stats		=uwsgi.status
#後臺執行,儲存log
daemonize 		=uwsgi.log

  • 啟動
uwsgi -i uwsgi.ini
  • 停止
#檢視程序號
cat uwsgi.pid
#停止程序
uwsgi --stop uwsgi.pid
kill pid
  • 伺服器開機自啟動
#編輯檔案/etc/rc.local, 新增下面內容到這行程式碼之前exit 0:
/usr/local/bin/uwsgi --ini /your/project/path/uwsgi.ini