日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

树莓派:django,uwsgi,nginx安装与设置

發(fā)布時(shí)間:2025/5/22 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树莓派:django,uwsgi,nginx安装与设置 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(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 nginx

2. 開始配置

測(cè)試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"]

測(cè)試
uwsgi –http :8000 –wsgi-file test.py

在樹莓派瀏覽器輸入 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

在樹莓派瀏覽器輸入 http://127.0.0.1:8000/
或者在電腦瀏覽器輸入 http://raspberrypi:8000

用uwsgi測(cè)試

uwsgi --http :8000 --module helloworld.wsgi

在樹莓派瀏覽器輸入 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

pi@raspberrypi:~ $ sudo cat /etc/nginx/uwsgi_paramsuwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length;uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param REQUEST_SCHEME $scheme; uwsgi_param HTTPS $https if_not_empty;uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;

準(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è)存放,位置可自定義,地址寫詳細(xì)}# 其他交由django處理location / {uwsgi_pass django;include uwsgi_params; # uwsgi} }

軟連接nginx
先刪除default文件軟連接,再建立新的軟連接

sudo rm /etc/nginx/sites-enabled/default sudo ln -s -f /home/pi/helloworld/nginx.conf /etc/nginx/sites-enabled/

測(cè)試nginx.conf 有無(wú)錯(cuò)誤

sudo nginx -t


沒(méi)有錯(cuò)誤
如果發(fā)現(xiàn)有錯(cuò),按照錯(cuò)誤提示去更改

重新加載nginx服務(wù)
nginx.conf配置文件變化,需要重新加載nginx服務(wù)才起作用

sudo /etc/init.d/nginx reload


測(cè)試nginx

uwsgi --socket /tmp/uwsgi_1.sock --wsgi-file /home/pi/test.py

在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi

如果打開報(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

在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/

使用ini文件配置服務(wù)器啟動(dòng)

cd ~/helloworld vi uwsgi_1.ini [uwsgi] chdir = /home/pi/helloworld socket = /tmp/uwsgi_1.sock module = helloworld.wsgi chmod-socket = 666 processes = 4 master = true vacuum = true uid = pi gid = pi

測(cè)試uwsgi_1.ini

uwsgi --ini uwsgi_1.ini

在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/

建立文件夾,放置ini軟連接

sudo mkdir /etc/uwsgi sudo mkdir /etc/uwsgi/vassals sudo ln -s /home/pi/helloworld/uwsgi_1.ini /etc/uwsgi/vassals/

emperor模式

wsgi --emperor /etc/uwsgi/vassals

在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/

后臺(tái)運(yùn)行與自啟動(dòng)

編寫系統(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


在樹莓派瀏覽器輸入 http://127.0.0.1
或者在電腦瀏覽器輸入 http://raspberrypi/

自啟動(dòng)設(shè)置

sudo systemctl enable emperor.uwsgi.service sudo reboot

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), ]

在樹莓派瀏覽器輸入 http://127.0.0.1/hello
或者在電腦瀏覽器輸入 http://raspberrypi/hello

總結(jié)

以上是生活随笔為你收集整理的树莓派:django,uwsgi,nginx安装与设置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。