centos7下基于nginx+uwsgi部署Django項目的實現(xiàn)
一:基礎(chǔ)環(huán)境介紹:
- Centos:7.8(cat /etc/redhat-release查看版本號)
- Python:3.9.5(python -V查看版本號)
- Django:4.2(django-admin --version查看版本號)
- Uwsgi:2.0.21(uwsgi --version查看版本號)
- Nginx:1.20.1 (nginx -v查看版本號)
- mysql-community-common-8.0(可自行安裝)
二:部署環(huán)境安裝配置:
1.基礎(chǔ)依賴環(huán)境安裝
yum -y groupinstall "Development tools" yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
2.安裝wegt,vim,unzip等必須命令
yum -y install wget vim unzip
3.安裝python與pip(或者python多版本管理工具pyenv等)
## 1.下載自己需要版本的python版本包 wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tar.xz ## 2.新建python3目錄 mkdir /usr/local/python3 ## 3.安裝Python3壓縮包,進入解壓目錄,指定安裝目錄,安裝Python3 tar -xvf Python-3.9.5.tar.xz cd Python-3.9.5 ./configure --prefix=/usr/local/python3 make && make install ## 4.安裝Python3時,會自動安裝pip,如果沒有就手動安裝 yum -y install python-pip ## 5.pip配置(更換pip的源) (1)編輯pip.conf [root@localhost ~]# cd ~ [root@localhost ~]# mkdir .pip [root@localhost ~]# vim pip.conf [global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com (2) 安裝依賴安裝好pip [root@localhost ~]# yum -y install epel-release [root@localhost ~]# yum install python-pip [root@localhost ~]# pip install --upgrade pip ## 6.升級下pip pip install --upgrade pip ## 7.創(chuàng)建軟鏈接 ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 ## 輸入python3測試是否安裝成功 python -V
4.安裝nginx
## 1.yum方式安裝nginx yum -y install nginx ## 2.查看nginx版本 nginx -v ## 3.啟動nginx systemctl status nginx ## 查看nginx狀態(tài) systemctl start nginx ## 開啟nginx服務(wù) systemctl stop nginx ## 關(guān)閉nginx服務(wù)
5.安裝uwsgi
## 1.安裝uwsgi pip3 install uwsgi ## 2.建立軟鏈接 ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi ## 3.查看uwsgi版本 uwsgi --version
三:部署django項目:
1.上傳項目并安裝項目模塊并啟動測試
## 1.pip導(dǎo)出項目依賴包 pip freeze > requirements.txt ## 2.上傳文件到服務(wù)器 ## 3.解壓項目 unzaip tman.zip ## 4.pip安裝項目依賴模塊 pip3 install -r requirements.txt ## 5.通過python3 manage.py runserver運行一下項目,如果能正常啟動則進行下一步,不能正常運行往上檢查。 這里需要注意使用python3來執(zhí)行manage.py,否則會出現(xiàn)報錯情況!
2.在項目里配置uwsgi.ini配置文件
(1)在settings.py所在目錄vim新文件項目名稱.ini
(2)配置項目名稱.ini文件
[uwsgi] master=true chdir=/var/tman module=tman.wsgi py-autoreload=1 lazy-apps=true socket=127.0.0.1:8000 processes=4 # pid文件,用于腳本啟動,停止 pidfile=uwsgi.pid buffer-size=32768 daemonize=uwsgi.log log-maxsize = 5000000 vacuum = true disable-logging = true
(3)檢查settings.py配置并收集項目靜態(tài)文件
一般都是這個配置,我的是基于vue的前后端分離項目:
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'dist/static'), ] STATIC_URL = 'static/'
url.py配置:
from django.contrib import admin from django.urls import path, include from django.views.generic.base import TemplateView urlpatterns = [ path('admin/', admin.site.urls), path('api/v1/', include('tadmin.urls')), path('', TemplateView.as_view(template_name='index.html')) ]
非前后端分離項目可以使用如下命令收集靜態(tài)文件:
python manage.py collectstatic
基于vue使用如下命令進行前端打包:
npm run build
(4)啟動測試,并查看日志
## 在當(dāng)前目錄啟動 uwsgi -i tman.ini ## 啟動之后查看進程 ps -ef | grep uwsgi
查看進程
查看日志
3.配置nginx
vim /etc/nginx/nginx.conf
nginx.conf配置
server { listen 80; listen [::]:80; server_name 192.168.75.188; charset utf-8; location /static/ { alias /var/tman/dist/static/; index index.html index.htm; } location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; } # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
(4)啟動nginx
[root@localhost tman]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost tman]# nginx -s reload
(5)訪問項目http://192.168.75.188/#/
四:備注
之后項目重啟只需要重啟uwsgi之后再重啟nginx即可,切記正式環(huán)境要關(guān)掉django的debug模式,防止報錯源碼被看到。
## 項目重啟 killall -9 uwsgi uwsgi -i tman.ini nginx -s reload
uwsgi操作
uwsgi --ini uwsgi.ini # 重啟uswgi uwsgi --reload uwsgi.pid # 停止uwsgi uwsgi --stop uwsgi.pid
五、部署報錯:
(1)pip3安裝mysqlclient==2.1.1報錯
/bin/sh: mysql_config: command not found /bin/sh: mariadb_config: command not found /bin/sh: mysql_config: command not found
解決辦法:
yum -y install mysql-devel
Error: MariaDB-compat conflicts with 1:mariadb-libs-5.5.68-1.el7.x86_64
Error: Package: 1:mariadb-devel-5.5.68-1.el7.x86_64 (base)
Requires: mariadb-libs(x86-64) = 1:5.5.68-1.el7
Installed: MariaDB-compat-10.4.25-1.el7.centos.x86_64 (@mariadb)
mariadb-libs(x86-64) = 1:10.1.48-1.el7.centos
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
解決辦法:
yum remove MariaDB-common
(2)open() "/root/tman/dist/static/js/vendor.652814051b5133caa1e9.js" failed (13: Permission denied)
原因,nginx目錄權(quán)限問題,我將項目放在root下,導(dǎo)致nginx權(quán)限不足,解決辦法,將項目移到其他目錄,如/var等
到此這篇關(guān)于centos7下基于nginx+uwsgi部署Django項目的實現(xiàn)的文章就介紹到這了,更多相關(guān)nginx uwsgi部署Django內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx+SSL+Node.js運行環(huán)境配置教程
這篇文章主要介紹了Nginx+SSL+Node.js運行環(huán)境配置教程,本文用反向代理的方式代理基于Node.js的Web應(yīng)用,需要的朋友可以參考下2014-09-09Nginx出現(xiàn)403錯誤,應(yīng)該如何解決
這篇文章主要介紹了Nginx出現(xiàn)403錯誤,應(yīng)該如何解決?具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Nginx配置編寫時支持邏輯運算與大小寫字母轉(zhuǎn)換的方法
這篇文章主要介紹了Nginx配置編寫時支持邏輯運算與大小寫字母轉(zhuǎn)換的方法,其中大小寫字母轉(zhuǎn)換是以lower upper case模塊來實現(xiàn),需要的朋友可以參考下2016-01-01