NGINX + uWSGI +Django +ubantu下發布
先安裝uWSGI :
pip install uwsgi測試測試是否安裝成功:$ uwsgi --version
2.0.15測試檔案:# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2執行uwsgi:uwsgi --http :8000 --wsgi-file test.py
如果想要執行專案來測試
# uwsgi --http :8000 --chdir 專案路徑 -w 專案.wsg --static-map=/static=static
uWSGI常用命令:
uwsgi --chdir=/path/to/your/project \ --module=mysite.wsgi:application \ --env DJANGO_SETTINGS_MODULE=mysite.settings \ --master --pidfile=/tmp/project-master.pid \ --socket=127.0.0.1:49152 \ # 可以ip地址,也可以是檔案 --processes=5 \ # 程序數量 --uid=1000 --gid=2000 \ # 如果是root使用者,uwsgi可以有刪除許可權 --harakiri=20 \ # 一個請求超時時間 --max-requests=5000 \ # 一個工作程序最大請求數 --vacuum \ # 退出時清楚環境 --home=/path/to/virtual/env \ # virtualenv的路徑 -- static # 做一個對映,指定靜態檔案 --http # 這個就和runserver一樣指定IP 埠 --daemonize=/var/log/uwsgi/yourproject.log # 日誌
配置專案啟動檔案uwsgi.ini
在專案根目錄建立uwsgi.ini檔案[uwsgi]# 指定IP埠
http=127.0.0.1:8000
#socket=/home/www/work/project/pro/nginx_uwsgi.socket#socket套接字,與uwsgi通訊
socket = 127.0.0.1:8001#專案目錄
chdir=/home/EdmureBlog/
chmod-socket=664# 啟用主程序
master=true程序數
processes=4執行緒數
threads=2# 指定專案的application
module=EdmureBlog.wsgi
#wsgi-file=uwsgi_test.py
#stats=127.0.0.1:9000# 自動移除unix Socket和pid檔案當服務停止的時候
#vacuum = true啟動配置$ uwsgi --ini uwsgi.ini # 啟動uwsgi配置
[uwsgi-static] added mapping for /static => /home/EdmureBlog//static # 啟動成功
$ uwsgi --stop uwsgi.pid # 關閉uwsgi
signal_pidfile()/kill(): Operation not permitted [core/uwsgi.c line 1659]
$ uwsgi --reload uwsgi.pid #重新載入配置
2.安裝NGINX (1)安裝NGINX $ sudo apt-get install nginx #安裝 是否安裝成功 $ ps -ef|grep -i nginx root 6961 1 0 03:56 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 6962 6961 0 03:56 ? 00:00:00 nginx: worker process pala 6985 2090 0 03:57 pts/0 00:00:00 grep --color=auto -i nginx NGINX常用命令: $ /etc/init.d/nginx start #啟動 $ /etc/init.d/nginx stop #關閉 $ /etc/init.d/nginx restart #重啟 $ killall nginx 殺死所有nginx # 如果是生產環境的話Nginx正在執行,就不要直接stop start 或者 restart 直接reload就行了 # 對線上影響最低 $ /etc/init.d/nginx reload 配置/etc/nginx/nginx.confserver { listen 80; server_name www.example.com; charset UTF-8; access_log /var/log/nginx/EdmureBlog_access.log;#nginx訪問日誌 error_log /var/log/nginx/EdmureBlog_error.log;#nginx錯誤日誌 proxy_request_buffering off; proxy_buffering off; client_max_body_size 75M; location / { include /etc/nginx/uwsgi_params; #uwsgi_pass unix:/home/EdmureBlog/myweb.sock; uwsgi_pass 127.0.0.1:8001;# uwsgi_read_timeout 150; #myweb.wsgi的路徑 uwsgi_param UWSGI_SCRIPT myweb.wsgi; uwsgi_param UWSGI_CHDIR /home/EdmureBlog; root /home/EdmureBlog/templates; #alias /home/wb/myweb/templates/; index index.html index.htm; } #靜態檔案日誌 location /static/ { #autoindex on; alias /home/EdmureBlog/static/; } #上傳檔案日誌 location /media/ { #autoindex on; alias /home/EdmureBlog/media/; } } 檢查nginx的語法$ nginx -t # 檢查nginx語法問題nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful重啟nginx$ /etc/init.d/nginx restart #重啟uwsgi預設不會載入靜態檔案,需要手動載入Django專案下的setting.py設定DEBUG = False #生產下的配置,除錯關了STATIC_ROOT = os.path.join(BASE_DIR, 'statics')#將靜態檔案都收集到此指定目錄執行collectstatic命令:
python manage.py collectstatic