ubuntu NGINX uwsgi https 部署Django 遇到的問題

搞了3天終於把Django成功部署到Ubuntu,記錄一下;html

引用來自泡泡茶壺python

Ubuntu下的Nginx + Uwsgi + Django項目部署詳細流程nginx

前提說明:

Django做爲小程序的後端,因小程序的請求到後端的都是https請求,因此Django必須支持https請求
django

寫在前面:

部署Django項目前,先用Django自帶服務器運行一下看有沒有問題,再部署;不然可能項目自己有問題,卻覺得是部署不正確致使的小程序

運行命令:python manage.py runserver .....後端

各類配置:

一、nginx支持https請求bash

1)項目對象的nginx.conf配置服務器

# the upstream component nginx needs to connect to
upstream django {
    # unix:///home/breavo/PyWorkSpace/mysite_code_shuffle/config/eshop.sock
    server xx.xx.xx.xx:8002; # xx.xx.xx.xx是服務器的ip,如果騰訊雲服務器,則是服務器外部ip,這裏的ip和端口是和uwsgi通訊的方式,因此和uwsgi.ini配置文件中一致
}


# configuration of the server
server {
    # the port your site will be served on
    listen 90 ssl; # 監聽端口能夠是任何端口,但不要和已使用端口衝突
    # listen 8003;
    # the domain name it will serve for
    server_name  indoor.crazymonkey.ml; # 這裏能夠是域名也能夠是本機ip,同上面的xx.xx.xx.xx
    charset     utf-8;

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

    # ssl configuration
    ssl on; # 打開了ssl驗證,支持https請求
    ssl_certificate /root/hewenjuan/Indoor_Localization/2446810_www.preciselocation.top.pem; # ssl證書放的位置
    ssl_certificate_key /root/hewenjuan/Indoor_Localization/2446810_www.preciselocation.top.key; # 使用絕對路徑確定沒錯啦

    # Django media
    location /media  {
        alias /root/hewenjuan/Indoor_Localization/media; # your Django project's media files - amend as required,有圖片等,
    }

    location /static {
        # alias /path/to/your/mysite/static; # your Django project's static files - amend as required
        alias /root/hewenjuan/Indoor_Localization/static; # 靜態文件位置,Django項目admin靜態文件
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django; # 這個就是引用了上面的upstream django配置了
        # include     /home/breavo/PyWorkSpace/mysite_code_shuffle/config/uwsgi_params; # the uwsgi_params file you installed
        include /etc/nginx/uwsgi_params;
    }
}

  

  說明:用nginx -t命令查看上面nginx文件的正確性app

2)項目對應的uwsgi.ini配置dom

# mysite_uwsgi.ini file
[uwsgi]
socket =  xx.xx.xx.xx:8002 # 騰訊雲內部ip,端口和nginx.conf配置的Django部分相同
# Django-related settings
# the base directory (full path)
chdir           = /root/hewenjuan/Indoor_Localization  # Django項目所在目錄
# Django's wsgi file
module          = Indoor_Localization.wsgi # 
# module =  config/hello.py:application
wsgi_file = Indoor_Localization/wsgi.py # Django項目wsgi.py所在位置,相對於上面的chdir目錄
# wsgi_file =  /root/hewenjuan/Indoor_Localization/config/hello.py:application
# the virtualenv (full path)
virtualenv = /root/.pyenv/versions/indoorLocation/bin/python3.6  # python虛擬環境絕對位置
# home            = /root/.pyenv
home            = /root/.pyenv/versions/indoorLocation

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
# socket          = /root/hewenjuan/Indoor_Localization/Indoor_Localization.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

stats           = %(chdir)/config/uwsgi.status           

pidfile         = %(chdir)/config/uwsgi.pid
# daemonize = %(chdir)/config/uwsgi.log # 開啓該字段,uwsgi會在後臺運行,日誌保存在該文件中,若沒有開啓該字段,則在前臺運行,日誌顯示在界面上(調試用方便)
# pidfile = %(chdir)/config/uwsgi.pid
#plugins =  /root/.pyenv/versions/indoorLocation/bin/python3.6

  3)每次修改了Django項目文件代碼,都須要重啓uwsgi和nginx,因此本身寫了個啓動文件start.sh

killall -9 uwsgi  # 關閉全部uwsgi進程,有時候沒有關掉上次的uwsgi進程,啓動新的會報錯等問題
killall -9 uwsgi  # 額,有時候關閉一次會出現沒關掉的狀況
uwsgi --ini mysite_uwsgi.ini # 啓動mysite_uwsgi.ini文件,即啓動uwsgi進程
/etc/init.d/nginx restart # 重啓nginx進程

  說明:啓動start.sh命令:sh start.sh

 

二、uwsgi支持https請求

 1)uwsgi.ini配置

# mysite_uwsgi.ini file
[uwsgi]
# 2446810_www.preciselocation.top.pem,2446810_www.preciselocation.top.key是ssl證書 https = xx.xx.xx.xx:443,2446810_www.preciselocation.top.pem,2446810_www.preciselocation.top.key # xx.xx.xx.xx是騰訊雲內部ip # Django-related settings # the base directory (full path) chdir = /root/hewenjuan/Indoor_Localization # Django's wsgi file module = Indoor_Localization.wsgi # module = config/hello.py:application wsgi_file = Indoor_Localization/wsgi.py # wsgi_file = /root/hewenjuan/Indoor_Localization/config/hello.py:application # the virtualenv (full path) virtualenv = /root/.pyenv/versions/indoorLocation/bin/python3.6 # home = /root/.pyenv home = /root/.pyenv/versions/indoorLocation # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe # socket = /root/hewenjuan/Indoor_Localization/Indoor_Localization.sock # ... with appropriate permissions - may be needed chmod-socket = 666 # clear environment on exit vacuum = true stats = %(chdir)/config/uwsgi.status pidfile = %(chdir)/config/uwsgi.pid # daemonize = %(chdir)/config/uwsgi.log # pidfile = %(chdir)/config/uwsgi.pid #plugins = /root/.pyenv/versions/indoorLocation/bin/python3.6

  

遇到的問題:

一、每次退出帳號從新登陸都會提示,pyenv 不存在了

 

緣由:

方法:命令行輸入:source ~/.bashrc;即執行.bashrc文件中的語句(包含 了pyenv初始化語句)

二、nginx權限問題:django admin後臺樣式不顯示,查看nginx錯誤日誌(位置:/var/log/nginx/error.log),顯示權限拒絕Permission denied

緣由:Django項目下的static文件執行權限不夠

方法:打開文件/etc/nginx/nginx.conf,修改 user www-data爲user root(root:你目前登陸的帳號名稱,也多是Ubuntu等其餘)

參考qingspacehttp://www.javashuo.com/article/p-gblkbtsl-em.html

三、unavailable modifier requested

緣由:沒有激活python虛擬環境

方法:輸入命令:pyenv activate xx,激活xx環境,xx是你項目所在的python虛擬環境

 四、安裝項目依賴包:pip install -r requirements.txt,遇到一個問題:安裝不成功,界面沒有顯示不成功

出現下面顯示的Successfully ...才表示安裝成功

 緣由:requirements.txt文件中有win_xx的安裝包,即有python接入window系統所使用的安裝包,相似了win32等,啥除掉便可;

由於有可能寫代碼使用的是window系統,部署的時候放在了Linux系統

解決方法:刪除win32等針對window系統的安裝包

相關文章
相關標籤/搜索