树莓派:django,uwsgi,nginx安装与设置
轉自:https://blog.csdn.net/weixiazailaide/article/details/52735076
PHP部分未驗證,故剪裁了原網頁中的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. 開始配置
測試uwsgi
編寫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"]測試
uwsgi –http :8000 –wsgi-file test.py
在樹莓派瀏覽器輸入 http://127.0.0.1:8000/
或者在電腦瀏覽器輸入 http://raspberrypi:8000
測試django
創建django
cd ~ django-admin.py startproject helloworld cd helloworld測試django
python manage.py runserver 0.0.0.0:8000在樹莓派瀏覽器輸入 http://127.0.0.1:8000/
或者在電腦瀏覽器輸入 http://raspberrypi:8000
用uwsgi測試
在樹莓派瀏覽器輸入 http://127.0.0.1:8000/
或者在電腦瀏覽器輸入 http://raspberrypi:8000
應與上一次測試結果相同
測試nginx
檢查uwsgi_params文件
github:https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
準備nginx.conf文件
cd ~/helloworld vi nginx.conf # django組件連接 upstream django{server unix:///tmp/uwsgi_1.sock; # sock,名字隨意,后邊要保持一致 } server {# 監視的網站端口listen 80;#UTF-8編碼charset utf-8;# 最大上傳大小128M,可自由定義client_max_body_size 128M; # 媒體文件 location /media {alias /home/pi/helloworld/media; }# 靜態文件location /static {alias /home/pi/helloworld/static; # 靜態網頁存放,位置可自定義,地址寫詳細}# 其他交由django處理location / {uwsgi_pass django;include uwsgi_params; # uwsgi} }軟連接nginx
先刪除default文件軟連接,再建立新的軟連接
測試nginx.conf 有無錯誤
sudo nginx -t
沒有錯誤
如果發現有錯,按照錯誤提示去更改
重新加載nginx服務
nginx.conf配置文件變化,需要重新加載nginx服務才起作用
測試nginx
在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi
如果打開報502 Bad Gateway
uwsgi --socket /tmp/uwsgi_1.sock --wsgi-file /home/pi/test.py --chmod-socket=666測試django、uwsgi、nginx一起運行
簡單測試
uwsgi --socket /tmp/uwsgi_1.sock --module helloworld.wsgi --chmod-socket=666在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/
使用ini文件配置服務器啟動
測試uwsgi_1.ini
uwsgi --ini uwsgi_1.ini在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/
建立文件夾,放置ini軟連接
emperor模式
wsgi --emperor /etc/uwsgi/vassals在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/
后臺運行與自啟動
編寫系統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/中并運行service
sudo cp emperor.uwsgi.service /etc/systemd/system/ sudo systemctl start emperor.uwsgi.service查看uwsgi和nginx服務狀態
sudo systemctl | grep uwsgi sudo systemctl | grep nginx
在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/
自啟動設置
3.python測試
建立第一個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與視圖函數,
cd /home/pi/helloworld/helloworld vi urls.py from django.conf.urls import url from helloworld.view import hellourlpatterns = [url(r'^hello/$', hello), ]在樹莓派瀏覽器輸入 http://127.0.0.1/hello
或者在電腦瀏覽器輸入 http://raspberrypi/hello
總結
以上是生活随笔為你收集整理的树莓派:django,uwsgi,nginx安装与设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生物刺激剂技术的作用
- 下一篇: 树莓派应用实例1:树莓派状态读取