您的位置:首页 > 运维架构 > Nginx

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

2016-10-04 19:42 260 查看

树莓派组建web服务器django,uwsgi,nginx,php,python,sqlite

环境:树莓派系统,互联网,电脑

1. 安装

安装vim

下边用得着,也可以用自带的vi或nano

sudo apt-get install vim


安装pip

sudo apt-get install python-dev
sudo apt-get install python-pip
sudo apt-get install libpcre3 libpcre3-dev
sudo pip install --upgrade pip


安装django,uwsgi

sudo pip install django  uwsgi -timeout 6000


安装nginx

sudo apt-get install nginx


如需PHP支持

sudo apt-get install php5-fpm php5-sqlite


2. 开始配置

测试uwsgi

写test.py

vim test.py


# test.py
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

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

sudo cat /etc/nginx/uwsgi_params


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  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文件

vim nginx.conf


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
4000
/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文件配置服务器启动

vim uwsgi_1.ini


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模式

uwsgi --emperor /etc/uwsgi/vassals


在树莓派浏览器输入

http://127.0.0.1

或者在电脑浏览器输入 http://raspberrypi/



测试成功

后台运行与自启动

编写系统service文件

vim emperor.uwsgi.service


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/中

sudo cp emperor.uwsgi.service /etc/systemd/system/


查看是否复制进去

ls /etc/systemd/system/


运行service

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


自启动加入成功



php支持

修改nginx.conf

vim  nginx.conf


nginx.conf

#django组件
upstream django{
server unix:///tmp/uwsgi_1.sock;
}
server {
# 监视的网站端口
listen    80;
# UTF-8编码
charset    utf-8;
# 最大上传大小128M,可自由定义
client_max_body_size 128M;

#以下为更改内容
###################################
#nginx默认寻找路径
root /home/pi/helloworld;
#默认首页页类型
index index.html index.htm index.php;
# 默认首页路径
location =/ {
root /home/pi/helloworld;
}
#php文件
location ~\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
###################################
#以上为更改内容

# 媒体文件
location /media  {
alias /home/pi/helloworld/media;
}
# 静态文件
location /static {
alias /home/pi/helloworld/static; # 静态网页存放,位置可自定义,地址写详细
}
# 其他交由django处理
location / {
uwsgi_pass  django;
include    /home/pi/helloworld/uwsgi_params; # uwsgi
}
}


重载nginx配置

sudo /etc/init.d/nginx reload


写php文件

vim index.php


index.php

<?php
print <<< EOT
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<p>PHP Hello World!</p>
<p>PHP OK!</p>
</body>
</html>
EOT;
?>


在树莓派浏览器输入

http://127.0.0.1

或者在电脑浏览器输入 http://raspberrypi/



测试成功

python测试

建立第一个python程序

cd /home/pi/helloworld/helloworld
vim view.py


view.py

from django.http import HttpResponse

def hello(request):
return HttpResponse("Hello world ! ")


修改urls.py文件,绑定URL与视图函数,

vim urls.py

"""helloworld
"""
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



测试成功

3. 总结

简单配置就这些了,可以很愉快的编写python和PHP了☺

官方配置说明

http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django nginx python php uwsgi