部署django

添加uwagi配置文件

在你項目的根目錄中建立mysite.xml(名字無所謂),或者建立mysite.ini,輸入如下內容:nginx

<uwsgi>    
   <socket>127.0.0.1:8000</socket> <!-- 內部端口,自定義 --> 
   <chdir>/data/project1</chdir> <!-- 項目路徑 -->            
   <module>project1.wsgi</module>  <!-- mysite爲wsgi.py所在目錄名--> 
<!-- 由於是module(模塊),因此用  .  表示下一級-->
   <processes>4</processes> <!-- 進程數 -->     
   <daemonize>uwsgi.log</daemonize> <!-- 日誌文件 -->
</uwsgi>
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir           = /opt/mysite
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
home            = /opt/venv
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
# the socket (use the full path to be safe
socket          = 0.0.0.0:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

保存退出vim

安裝nginx並配置nginx.conf文件

cd /home/downloads/
wget http://nginx.org/download/nginx-1.13.7.tar.gz
下載完解壓
tar -zxvf nginx-1.13.7.tar.gz
解壓完進入文件夾,執行編譯安裝
./configure
make
make install
nginx通常安裝在/usr/local/nginx

爲了防止意外,在***/conf/中備份一下nginx.conf文件 app

cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.bak

打開這個配置文件,刪除 全部內容,加入如下內容(括號太多,註釋太多,簡便操做,後期須要什麼,就按照配置文件中的格式再往裏面加)socket

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 80;
        server_name  www.i-sekai.site; #改成本身的域名,沒域名修改成127.0.0.1:80
        charset utf-8;
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8000;  #端口要和uwsgi裏配置的同樣
           uwsgi_param UWSGI_SCRIPT mysite.wsgi;  #wsgi.py所在的目錄名+.wsgi(解釋過了)
           uwsgi_param UWSGI_CHDIR /data/project1; #項目路徑
           
        }
        location /static/ {
        alias /data/project1/static/; #靜態資源路徑
        }
         #媒體文件也要放在這裏吧?
    }
}

 

在配置中(.conf)可能會出現錯誤,post

/usr/local/nginx/sbin/nginx -t 測試配置文件是否正確
vim /usr/local/nginx/conf/nginx.conf 改寫配置文件

 

最後,沒有錯誤

cd /usr/local/nginx/sbin/
./nginx
此時,nginx已經啓動了。

 

cd /data/project1/
uwsgi -x mysite.xml
配置生效
/usr/local/nginx/sbin/nginx -s reload

重啓nginx,網站打開你的域名測試

ps:

必定要注意Uwsgi和Nginx配置文件裏的項目路徑和靜態資源路徑,填寫正確了才能成功訪問。否則會出現502錯誤。還有就是,修改Django文件和其它配置文件以後,必定要重啓Uwsgi和Nginx,否則不生效。網站

中止uwsgispa

ps -ef | grep uwsgi
killall -9 uwsgi

啓動uwsgi日誌

uwsgi -x mysite.xml

 重啓nginxcode

/usr/lcoal/nginx/sbin/nginx -s reload

把nginx添加到環境變量中

cd /etc
vim profile
將PATH=$PATH:/usr/local/nginx/sbin添加到最後,wq。
source profile
nginx 能夠直接使用,無需加前面的/usr/local/nginx/sbin
相關文章
相關標籤/搜索