树莓派: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:树莓派状态读取