久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

詳解Ubuntu環(huán)境下部署Django+uwsgi+nginx總結(jié)

瀏覽:123日期:2024-10-12 15:50:18

前言

這是我在搭建Django項目時候的過程,拿來總結(jié)記錄,以備不時之需。

項目采用nginx+uwsgi的搭配方式。

項目依賴包采用 requirements.txt 文件管理的方式。

本地準(zhǔn)備工作

確認(rèn)項目能夠運行起來,沒有 bug

將當(dāng)前環(huán)境的包導(dǎo)出 pip freeze > requirements.txt

將項目上傳到服務(wù)器上的 /srv 目錄下。這里以 git 的形式為例, 打開終端, 依次輸入如下命令:

$ git init$ git remote add origin xxx.git # 替換成你的項目git地址$ git add .$ git commit -m ’first commit’$ git pull origin master --allow-unrelated-histories$ git push origin master

部署項目到服務(wù)器

安裝python

安裝好項目用到的 python 。

$ sudo apt install python$ sudo apt install python-pip$ pip install --upgrade pip

安裝 virtualenv 以及 virutalenvwrapper ,并創(chuàng)建虛擬環(huán)境。

$ pip install virtualenv$ pip install virtualenvwrapper$ sudo apt install vim

編輯文件 ~/.bashrc

$ vim ~/.bashrc# 添加如下2行代碼export WORKON_HOME=$HOME/.virtualenvssource /usr/local/bin/virtualenvwrapper.sh# 保存文件,讓文件成效$ source ~/.bashrc

安裝git:

$ sudo apt install git# 為了方便XShell或者CRT連接服務(wù)器,建議安裝OpenSSH$ sudo apt install openssh-server openssh-client$ service ssh restart

安裝MySQL

$ sudo apt install mysql-server mysql-client$ sudo apt-get install libmysqld-dev

測試配置

安裝依賴包,進(jìn)入虛擬環(huán)境 workon *** ,進(jìn)入項目根目錄,執(zhí)行命令 pip install -r requirements.txt 創(chuàng)建數(shù)據(jù)庫,新打開一個終端,登錄數(shù)據(jù)庫, mysql -uroot -p , 創(chuàng)建相應(yīng)的數(shù)據(jù)庫 CREATE DATABASE IF NOT EXISTS my_db default charset utf8mb4; 遷移數(shù)據(jù), python manage.py migrate 收集靜態(tài)文件, python manage.py collectstatic 啟動服務(wù)器,執(zhí)行 python manage.py runserver 0.0.0.0:8000 ,然后在你自己電腦上,在瀏覽器中輸入 http://<your server ip>:8000 ,訪問下網(wǎng)站所有頁面,確保所有頁面都沒有錯誤。

注意:

設(shè)置 ALLOW_HOST 為你的域名或 ip 地址。 設(shè)置 DEBUG=False 。

安裝uwsgi

uwsgi 是一個應(yīng)用服務(wù)器,非靜態(tài)文件的網(wǎng)絡(luò)請求就必須通過他完成,他也可以充當(dāng)靜態(tài)文件服務(wù)器,但不是他的強項。

uwsgi 是使用 python 編寫的,因此通過 pip install uwsgi 就可以了。( uwsgi 必須安裝在系統(tǒng)級別的 Python 環(huán)境中,不要安裝到虛擬環(huán)境中)。

命令行啟動 uwsgi :

$ uwsgi --http :8000 --module test.wsgi --vritualenv=/root/.virtualenvs/django-env-py36

如果能夠在瀏覽器中訪問到測試的頁面,說明uwsgi可以加載項目了。

配置文件方式啟動 uwsgi :

在項目的根路徑下面,創(chuàng)建一個文件 djangotest.ini ,填寫以下代碼:

[uwsgi]# Django相關(guān)的配置# 必須全部為絕對路徑# 項目的路徑chdir=/srv/djangotest# Django的wsgi文件module=djangotest.wsgi# Python虛擬環(huán)境的路徑home=/root/.virtualenvs/django-env-py36# 進(jìn)程相關(guān)的設(shè)置# 主進(jìn)程master=true# 最大數(shù)量的工作進(jìn)程processes=10# socket文件路徑,絕對路徑socket=/srv/djangotest/djangotest.sock# 設(shè)置socket的權(quán)限chmod-socket=666# 退出的時候是否清理環(huán)境vacuum=true

然后使用命令uwsgi --ini djangotest.ini,看下是否還能啟動這個項目。

安裝nginx

nginx 是一個 web 服務(wù)器。用來加載靜態(tài)文件和接收 http 請求的。

通過命令 sudo apt install nginx 即可安裝。

nginx 常用命令:

啟動nginx:service nginx start 關(guān)閉nginx:service nginx stop 重啟nginx:service nginx restart

收集靜態(tài)文件:

靜態(tài)文件應(yīng)該讓 nginx 來處理,而不是讓 django 來做。

首先確保你的 settings.py 文件中有一個 STATIC_ROOT 配置,這個配置應(yīng)該指定你的靜態(tài)文件要放在哪個目錄下。

那么我們可以執(zhí)行以下命令: python manage.py collectstatic 來收集所有靜態(tài)文件(已經(jīng)執(zhí)行過請忽略)。

編寫nginx配置文件,在 /etc/nginx/conf.d 目錄下,新建一個文件 djangotest.conf ,然后將以下代碼貼進(jìn)去:

upstream djangotest { server unix:///srv/djangotest/djangotest.sock; }# 配置服務(wù)器server { # 監(jiān)聽的端口號 listen 80; # 域名 server_name 192.168.0.101; charset utf-8; # 最大的文件上傳尺寸 client_max_body_size 75M; # 靜態(tài)文件訪問的url location /static { # 靜態(tài)文件地址 alias /srv/djangotest/static_dist; } # 最后,發(fā)送所有非靜態(tài)文件請求到django服務(wù)器 location / { uwsgi_pass djangotest; # uwsgi_params文件地址 include /etc/nginx/uwsgi_params; }}

測試配置文件: service nginx configtest 。注意:每次修改完配置需要重啟 nginx : service nginx restart

使用supervisor

讓supervisor管理uwsgi,可以在uwsgi發(fā)生意外的情況下,自動重啟。

安裝 supervisor :在系統(tǒng)級別的python環(huán)境下 pip install supervisor 。

在項目根目錄下創(chuàng)建一個文件 my_supervisor.conf 。編寫內(nèi)容:

# supervisor的程序名字[program:mysite]# supervisor執(zhí)行的命令command=uwsgi --ini zlkt_uwsgi.ini# 項目的目錄directory = /srv/djangotest # 開始的時候等待多少秒startsecs=0# 停止的時候等待多少秒stopwaitsecs=0 # 自動開始autostart=true# 程序掛了后自動重啟autorestart=true# 輸出的log文件stdout_logfile=/srv/djangotest/log/supervisord.log# 輸出的錯誤文件stderr_logfile=/srv/djangotest/log/supervisord.err[supervisord]# log的級別loglevel=info# 使用supervisorctl的配置[supervisorctl]# 使用supervisorctl登錄的地址和端口號serverurl = http://127.0.0.1:9001# 登錄supervisorctl的用戶名和密碼username = adminpassword = 123[inet_http_server]# supervisor的服務(wù)器port = :9001# 用戶名和密碼username = adminpassword = 123[rpcinterface:supervisor]supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

運行 supervisor ,執(zhí)行 supervisord -c my_supervisor.conf 。

進(jìn)入 supervisor 管理控制臺, supervisorctl -c my_supervisor.conf

supervisor 管理控制臺常用命令

# 查看狀態(tài)status# 啟動程序start program_name# 重新啟動程序restart program_name# 關(guān)閉程序stop program_name# 重新加載配置文件reload# 退出控制臺quit

到此這篇關(guān)于Ubuntu環(huán)境下部署Django+uwsgi+nginx總結(jié)的文章就介紹到這了,更多相關(guān)Ubuntu環(huán)境下部署Django+uwsgi+nginx總結(jié)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Ubuntu
相關(guān)文章:
主站蜘蛛池模板: 虹口区| 上思县| 渭源县| 涞水县| 马公市| 禄劝| 馆陶县| 灵丘县| 湘西| 武邑县| 绥芬河市| 高邑县| 子长县| 海丰县| 丹东市| 沾益县| 淮滨县| 体育| 郑州市| 和政县| 南陵县| 碌曲县| 武川县| 青川县| 棋牌| 蚌埠市| 永春县| 遂溪县| 古交市| 鄂托克前旗| 前郭尔| 云霄县| 正镶白旗| 手游| 龙陵县| 德惠市| 尉犁县| 玛多县| 大田县| 太仆寺旗| 安龙县|