转自: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 nginx
2. 开始配置
测试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测试
uwsgi --http :8000 --module helloworld.wsgi
在树莓派浏览器输入 http://127.0.0.1:8000/
或者在电脑浏览器输入 http://raspberrypi:8000
应与上一次测试结果相同
测试nginx
检查uwsgi_params文件
github:https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
pi@raspberrypi:~ $ sudo cat /etc/nginx/uwsgi_params
uwsgi_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;
准备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文件软连接,再建立新的软连接
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s -f /home/pi/helloworld/nginx.conf /etc/nginx/sites-enabled/
测试nginx.conf 有无错误
sudo nginx -t
没有错误
如果发现有错,按照错误提示去更改
重新加载nginx服务
nginx.conf配置文件变化,需要重新加载nginx服务才起作用
sudo /etc/init.d/nginx reload
测试nginx
uwsgi --socket /tmp/uwsgi_1.sock --wsgi-file /home/pi/test.py
在树莓派浏览器输入 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文件配置服务器启动
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
测试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/
后台运行与自启动
编写系统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/
自启动设置
sudo systemctl enable emperor.uwsgi.service
sudo reboot
3.python测试
建立第一个python程序
修改view.py文件
cd /home/pi/helloworld/helloworld
vi view.py
from django.http import HttpResponse
def 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 hello
urlpatterns = [
url(r'^hello/$', hello),
]
在树莓派浏览器输入 http://127.0.0.1/hello
或者在电脑浏览器输入 http://raspberrypi/hello