树莓派:django,uwsgi,nginx安装与设置
轉(zhuǎn)自:https://blog.csdn.net/weixiazailaide/article/details/52735076
PHP部分未驗(yàn)證,故剪裁了原網(wǎng)頁(yè)中的php部分。
1. 安裝需要的軟件
#安裝pip sudo apt-get install python-dev sudo apt-get install python-pip sudo apt-get install libpcre3 sudo apt-get install libpcre3-dev sudo pip install --upgrade pip #安裝django sudo pip install django #安裝uwsgi sudo pip install uwsgi #安裝nginx sudo apt-get install nginx2. 開(kāi)始配置
測(cè)試uwsgi
編寫(xiě)test.py
cd ~ vi test.py #!/usr/bin/python #coding=utf-8 def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello World"]測(cè)試
uwsgi –http :8000 –wsgi-file test.py
在樹(shù)莓派瀏覽器輸入 http://127.0.0.1:8000/
或者在電腦瀏覽器輸入 http://raspberrypi:8000
測(cè)試django
創(chuàng)建django
cd ~ django-admin.py startproject helloworld cd helloworld測(cè)試django
python manage.py runserver 0.0.0.0:8000在樹(shù)莓派瀏覽器輸入 http://127.0.0.1:8000/
或者在電腦瀏覽器輸入 http://raspberrypi:8000
用uwsgi測(cè)試
在樹(shù)莓派瀏覽器輸入 http://127.0.0.1:8000/
或者在電腦瀏覽器輸入 http://raspberrypi:8000
應(yīng)與上一次測(cè)試結(jié)果相同
測(cè)試nginx
檢查uwsgi_params文件
github:https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
準(zhǔn)備nginx.conf文件
cd ~/helloworld vi nginx.conf # django組件連接 upstream django{server unix:///tmp/uwsgi_1.sock; # sock,名字隨意,后邊要保持一致 } server {# 監(jiān)視的網(wǎng)站端口listen 80;#UTF-8編碼charset utf-8;# 最大上傳大小128M,可自由定義client_max_body_size 128M; # 媒體文件 location /media {alias /home/pi/helloworld/media; }# 靜態(tài)文件location /static {alias /home/pi/helloworld/static; # 靜態(tài)網(wǎng)頁(yè)存放,位置可自定義,地址寫(xiě)詳細(xì)}# 其他交由django處理location / {uwsgi_pass django;include uwsgi_params; # uwsgi} }軟連接nginx
先刪除default文件軟連接,再建立新的軟連接
測(cè)試nginx.conf 有無(wú)錯(cuò)誤
sudo nginx -t
沒(méi)有錯(cuò)誤
如果發(fā)現(xiàn)有錯(cuò),按照錯(cuò)誤提示去更改
重新加載nginx服務(wù)
nginx.conf配置文件變化,需要重新加載nginx服務(wù)才起作用
測(cè)試nginx
在樹(shù)莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi
如果打開(kāi)報(bào)502 Bad Gateway
uwsgi --socket /tmp/uwsgi_1.sock --wsgi-file /home/pi/test.py --chmod-socket=666測(cè)試django、uwsgi、nginx一起運(yùn)行
簡(jiǎn)單測(cè)試
uwsgi --socket /tmp/uwsgi_1.sock --module helloworld.wsgi --chmod-socket=666在樹(shù)莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/
使用ini文件配置服務(wù)器啟動(dòng)
測(cè)試uwsgi_1.ini
uwsgi --ini uwsgi_1.ini在樹(shù)莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/
建立文件夾,放置ini軟連接
emperor模式
wsgi --emperor /etc/uwsgi/vassals在樹(shù)莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/
后臺(tái)運(yùn)行與自啟動(dòng)
編寫(xiě)系統(tǒng)service文件
vi emperor.uwsgi.service [Unit] Description=uWSGI Emperor After=syslog.target [Service] ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals/ --daemonize /var/log/uwsgi_emperor.log RuntimeDirectory=uwsgi KillSignal=SIGQUIT Restart=on-failure Type=forking [Install] WantedBy=multi-user.target把service放到/etc/systemd/system/中并運(yùn)行service
sudo cp emperor.uwsgi.service /etc/systemd/system/ sudo systemctl start emperor.uwsgi.service查看uwsgi和nginx服務(wù)狀態(tài)
sudo systemctl | grep uwsgi sudo systemctl | grep nginx
在樹(shù)莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/
自啟動(dòng)設(shè)置
3.python測(cè)試
建立第一個(gè)python程序
修改view.py文件
cd /home/pi/helloworld/helloworld vi view.py from django.http import HttpResponsedef hello(request):return HttpResponse("Hello world ! ")修改urls.py文件,綁定URL與視圖函數(shù),
cd /home/pi/helloworld/helloworld vi urls.py from django.conf.urls import url from helloworld.view import hellourlpatterns = [url(r'^hello/$', hello), ]在樹(shù)莓派瀏覽器輸入 http://127.0.0.1/hello
或者在電腦瀏覽器輸入 http://raspberrypi/hello
總結(jié)
以上是生活随笔為你收集整理的树莓派:django,uwsgi,nginx安装与设置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 生物刺激剂技术的作用
- 下一篇: 树莓派应用实例1:树莓派状态读取