django+uwsgi+nginx部署
安裝nginx
sudo apt-get install nginx
即可巷怜。
安裝python解析器
python解析器可以選擇直接python官網(wǎng)下載鳍徽,或者使用anaconda唉匾。
直接wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh
下載到服務(wù)器后舵稠,使用bash Anaconda3-5.3.1-Linux-x86_64.sh
無腦安裝即可史飞,中間會(huì)有問你是否幫你配置環(huán)境變量選擇yes牵舱。直接跳過了也可以自己配置環(huán)境變量。最后在終端輸入python -V
顯示版本號(hào)即成功。
然后使用pip install django
安裝包枉圃,項(xiàng)目中其他包自行安裝
安裝uwsgi
使用的是阿里云學(xué)生機(jī)功茴,在安裝完python解析器后,使用命令安裝pip install uwsgi
失敗孽亲,發(fā)生錯(cuò)誤坎穿,錯(cuò)誤如下
Command "/root/anaconda3/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-wnkwliqy/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-ivs7ibgw/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-wnkwliqy/uwsgi/
在網(wǎng)上查找攻略,發(fā)現(xiàn)大多數(shù)的解決方法都是使用安裝返劲,apt-get install python3.6-dev
玲昧,但是在unbuntu20.04下安裝發(fā)現(xiàn)無法找到該包。我嘗試安裝篮绿,但是失敗孵延。
還有博客說是gcc版本過高,所以嘗試降低gcc版本搔耕,使用apt-get install gcc-4.8
隙袁,發(fā)現(xiàn)直接無法找到安裝包。我們用增加下載源弃榨,然后更新下載源,就可以繼續(xù)使用apt下載gcc-4.8了
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n16" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;"> sudo vim /etc/apt/sources.list
deb http://dk.archive.ubuntu.com/ubuntu/ xenial main
deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe
sudo apt update</pre>
使用命令安裝gcc-4.8
和g++-4.8
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n18" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;"> sudo apt-get install gcc-4.8
sudo apt-get install g++-4.8</pre>
安裝后使用ls /usr/bin/gcc*
就可以看見你剛剛裝的gcc和系統(tǒng)自帶的gcc梨睁,因?yàn)橄到y(tǒng)中默認(rèn)使用的是gcc-9.3
鲸睛,我們需要將gcc命令優(yōu)先設(shè)置為gcc-4.8
。
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n20" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;"> sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 100</pre>
然后使用gcc --version
查看當(dāng)前gcc版本坡贺,是4.9就成功了官辈。
最后嘗試pip install uwsgi
,成功安裝遍坟,所以問題關(guān)鍵在于gcc和g++的版本
安裝mysql
自行安裝
部署django項(xiàng)目
在將整個(gè)項(xiàng)目傳輸?shù)椒?wù)器后拳亿,我們?cè)陧?xiàng)目根目錄下創(chuàng)建一個(gè)uwsgi.ini
的配置文件,內(nèi)容如下
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n28" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;"> [uwsgi]
使用nginx連接時(shí)使用
socket=:8000
直接做web服務(wù)器使用 python manage.py runserver ip:port
http=0.0.0.0:8000
項(xiàng)目目錄
chdir=/root/project/logistic_simulation
項(xiàng)目中wsgi.py文件的目錄愿伴,相對(duì)于項(xiàng)目目錄
wsgi-file=logistic_simulation/wsgi.py
module=logistic_simulation.wsgi
指定啟動(dòng)的工作進(jìn)程數(shù)
processes=2
指定工作進(jìn)程中的線程數(shù)
threads=2
指定在這些進(jìn)程里有一個(gè)主進(jìn)程
master=True
保存啟動(dòng)之后主進(jìn)程的pid
pidfile=uwsgi.pid
設(shè)置uwsgi后臺(tái)運(yùn)行肺魁,uwsgi.log保存日志信息
daemonize=uwsgi.log
設(shè)置日志文件最大字節(jié)數(shù)
log-maxsize = 100000
設(shè)置每個(gè)進(jìn)程最大請(qǐng)求數(shù)
max-requests = 1000</pre>
然后使用啟動(dòng)uwsgi服務(wù),輸入命名uwsgi --ini uwsgi.ini
隔节,發(fā)現(xiàn)報(bào)錯(cuò)
uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解決方法是創(chuàng)建一個(gè)軟鏈接
find / -name "libpcre.so.*"
來尋找路徑鹅经,然后創(chuàng)建軟鏈接到庫中ln -s /root/anaconda3/lib/libpcre.so.1 /lib
。若還出現(xiàn)同樣的錯(cuò)誤怎诫,就一樣創(chuàng)建軟鏈接瘾晃,缺哪個(gè)創(chuàng)建哪個(gè)
使用uwsgi --ini uwsgi.ini
輸出信息[uWSGI] getting INI configuration from uwsgi.ini
即成功
配置nginx
修改nginx配置文件,向server里加入下面的復(fù)制內(nèi)容幻妓,注意不要重復(fù)復(fù)制蹦误,使用命令vim /etc/nginx/sites-enabled/default
加入下面內(nèi)容,注意修改成你的靜態(tài)目錄
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n36" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;"> server {
listen 80;
----復(fù)制內(nèi)容
location /static {
alias /root/project/logistic_simulation/static; #靜態(tài)文件目錄
}
location / {
uwsgi_pass 127.0.0.1:8000; #將請(qǐng)求轉(zhuǎn)發(fā)重定向到127.0.0.1的8000端口
include /etc/nginx/uwsgi_params; #將所有的參數(shù)轉(zhuǎn)到uwsgi下
}
----復(fù)制內(nèi)容
}</pre>
重啟nginx服務(wù)service nginx restart
。
同時(shí)我們需要將自己項(xiàng)目文件夾下settings.py中內(nèi)容修改强胰,一個(gè)是關(guān)閉debug舱沧,還有往host里加入*,設(shè)置你的靜態(tài)問價(jià)目錄哪廓。
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n39" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;"> DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_ROOT = os.path.join(BASE_DIR, "static") </pre>
這個(gè)時(shí)候我們?cè)L問服務(wù)器的公網(wǎng)ip狗唉,就會(huì)發(fā)現(xiàn)進(jìn)入到自己項(xiàng)目的首頁
如果出現(xiàn)樣式文件加載是403的狀態(tài),那么我們需要修改nginx配置文件涡真,輸入命令vim /etc/nginx/nginx.conf
將第一行的內(nèi)容改成
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;"> user root;</pre>
學(xué)會(huì)查看日志文件
在部署過程中如果出現(xiàn)失敗分俯,一定要學(xué)會(huì)找出錯(cuò)誤,只有找出錯(cuò)誤哆料,搜索解決缸剪,才能針對(duì)這個(gè)問題采用解決方案,nginx有他的錯(cuò)誤日志cat /var/log/nginx/error.log
东亦,uwsgi日志文件路徑在自己設(shè)置uwsgi.ini文件里杏节。學(xué)會(huì)看日志真的很重要。
總結(jié)
部署django項(xiàng)目已經(jīng)好多次了典阵,一次次都在進(jìn)步奋渔,依稀記得第一次部署的時(shí)候,搞不清楚nginx的配置文件壮啊,安裝不上uwsgi嫉鲸,服務(wù)器下包失敗,網(wǎng)速慢歹啼,配置結(jié)束出現(xiàn)各種各樣的錯(cuò)誤玄渗,一度讓人崩潰,一定要沉下心來狸眼,找出錯(cuò)誤藤树,網(wǎng)上博客千千萬,各有各自的配置拓萌,需要自己有一定的甄別能力岁钓。
在這篇博客里主要是自己逐個(gè)配置環(huán)境,現(xiàn)在docker大火司志,使用docker就能很輕松的配置項(xiàng)目環(huán)境甜紫。后面會(huì)使用docker進(jìn)行部署。