Django簡單博客實戰(zhàn)(八)--- CentOS7+Gunicorn+Nginx部署

CentOS7+Gunicorn+Nginx部署Django

步驟

gunicron配置

保證防火墻關(guān)閉

保證阿里云服務(wù)器的安全組中配置了相應(yīng)端口號

在這里插入圖片描述
  1. 安裝gunicron
pip install gunicron
  1. 將gunicron加入INSTALLED_APPS中,并將服務(wù)器ip加入ALLOWED_HOSTS中
INSTALLED_APPS = [
    'simpleui',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'haystack',
    'blogger',
    'comment',
    'post',
    'ckeditor',
    'ckeditor_uploader',
    'gunicron', # 加入該行
]
ALLOWED_HOSTS = ['IP地址']
  1. 查看防火墻狀態(tài)公你,并關(guān)閉防火墻
systemctl status firewalld  # 查看防火墻狀態(tài)
systemctl stop firewalld   # 關(guān)閉防火墻
# systemctl start firewalld # 開啟防火墻
  1. gunicron運行指令
gunicorn mypoject.wsgi:application -b 0.0.0.0:8000
# mypoject為項目名稱忽洛,wsgi為項目目錄下的wsgi.py文件蔗坯,application固定寫法

<font face="楷體" color=red> 如果項目運行后丟失樣式贩汉,gunicron出現(xiàn) Not Found 靜態(tài)文件的情況摹量,在根urls加入如下代碼</font>

from django.contrib.staticfiles.urls import staticfiles_urlpatterns  # 加入該行

# ... the rest of your URLconf goes here ...

urlpatterns += staticfiles_urlpatterns()  # 加入該行
  1. 在外部瀏覽器中輸入 “http://ip:8000/自己定義的url路由”
http://ip地址:8000/index

nginx配置

  1. 安裝Nginx
yum install nginx

安裝完成后Nginx會自動運行屠升,可以在外部瀏覽器測試下Nginx是否正常開啟

# 輸入服務(wù)器IP
http://IP地址

看到"Welcome to nginx",說明正常啟動

  1. 刪除default
  • 刪除 /etc/nginx/sites-enabled/default
  • 創(chuàng)建 /etc/nginx/sites-enabled/項目名

編輯創(chuàng)建的文件

server {
    listen 80 default_server;
    # listen [::]:80 default_server;
    server_name _; # 如果你映射了域名恬惯,可以寫在這里,如:server_name example.com蜻牢;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        proxy_pass http://127.0.0.1:8000; #轉(zhuǎn)發(fā)的地址烤咧,即Gunicorn運行的地址
        proxy_redirect      off;

#       proxy_set_header    Host              $host;
#       proxy_set_header    X-Real-IP         $remote_addr;
#       proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
#       proxy_set_header    X-Forwarded-Proto $scheme;
    }
    location ~ .*{
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header    Host              $http_host;
        proxy_set_header    X-Real-IP         $remote_addr;
        proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
}
#   location /static {
#       alias /home/xzx/flask_pro/flask_blog/bluelog/bluelog/static/;
#       expires 30d; #設(shè)置緩存過期時間
 #   }
}
  1. 設(shè)置 /etc/nginx/nginx.conf
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;  # 添加該行,保證讀取 "/etc/nginx/sites-enabled/項目名" 文件
    server {
       # listen       80 default_server;
       # listen       [::]:80 default_server;
       # server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

       # location / {
       # }
    }
  1. Nginx檢查語法是否錯誤
[root]$ nginx -t
  1. 啟動Nginx
systemctl start nginx

啟動Django應(yīng)用

gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000

外部瀏覽器測試:http://IP地址/index

  1. 后臺脫機運行Django應(yīng)用
nohup gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000 &
  1. 附加知識抢呆; Nginx部署多個web應(yīng)用(Django+Flask)
  • 已按照上面方式部署成功Django煮嫌,再部署一個flask,同時運行兩個web

創(chuàng)建 /etc/nginx/sites-enabled/項目名

編輯創(chuàng)建的文件

server {
    listen 8001 default_server; # 修改外部訪問端口
    # listen [::]:80 default_server;
    server_name _; # 如果你映射了域名抱虐,可以寫在這里,如:server_name example.com昌阿;
    access_log /var/log/nginx/blueblog_access.log;
    error_log /var/log/nginx/blueblog_error.log;

    location / {
        proxy_pass http://127.0.0.1:8892; #轉(zhuǎn)發(fā)的地址,即Gunicorn運行的地址
        proxy_redirect      off;

#       proxy_set_header    Host              $host;
#       proxy_set_header    X-Real-IP         $remote_addr;
#       proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
#       proxy_set_header    X-Forwarded-Proto $scheme;
    }
    location ~ .*{
        proxy_pass http://127.0.0.1:8892;
        proxy_set_header    Host              $http_host;
        proxy_set_header    X-Real-IP         $remote_addr;
        proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
}
#   location /static {
#       alias /home/xzx/flask_pro/flask_blog/bluelog/bluelog/static/;
#       expires 30d; #設(shè)置緩存過期時間
 #   }
}
  • 記得為flask創(chuàng)建wsgi.py文件

運行flask項目

nohup gunicorn -w 4 -b 127.0.0.1:8892 wsgi:app &

外部瀏覽器輸入:http://IP地址:8001/即可訪問

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末恳邀,一起剝皮案震驚了整個濱河市懦冰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌谣沸,老刑警劉巖刷钢,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異乳附,居然都是意外死亡内地,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門赋除,熙熙樓的掌柜王于貴愁眉苦臉地迎上來阱缓,“玉大人,你說我怎么就攤上這事举农【U耄” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵颁糟,是天一觀的道長祭犯。 經(jīng)常有香客問我,道長滚停,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任粥惧,我火速辦了婚禮键畴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己起惕,他們只是感情好涡贱,可當(dāng)我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著惹想,像睡著了一般问词。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嘀粱,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天激挪,我揣著相機與錄音,去河邊找鬼锋叨。 笑死垄分,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的娃磺。 我是一名探鬼主播薄湿,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼偷卧!你這毒婦竟也來了豺瘤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤听诸,失蹤者是張志新(化名)和其女友劉穎坐求,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛇更,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡瞻赶,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了派任。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片砸逊。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖掌逛,靈堂內(nèi)的尸體忽然破棺而出师逸,到底是詐尸還是另有隱情,我是刑警寧澤豆混,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布篓像,位于F島的核電站,受9級特大地震影響皿伺,放射性物質(zhì)發(fā)生泄漏员辩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一鸵鸥、第九天 我趴在偏房一處隱蔽的房頂上張望奠滑。 院中可真熱鬧丹皱,春花似錦、人聲如沸宋税。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杰赛。三九已至呢簸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間乏屯,已是汗流浹背根时。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瓶珊,地道東北人啸箫。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像伞芹,于是被迫代替她去往敵國和親忘苛。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,077評論 2 355