树莓派: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.pyvim 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
创建djangodjango-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.confvim 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
相关文章推荐
- CentOS 6.5下安装Python+Django+Nginx+uWSGI
- Cenos(6.6/7.1)下从源码安装Python+Django+uwsgi+nginx到写nginx的环境部署(一)
- Django Nginx+uwsgi 安装配置
- 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置
- 在 Ubuntu 14.x 搭建 Nginx Uwsgi Django 环境之(二):Django 的安装
- 安装Django环境(nginx+uwsgi + mysql)
- Django+uwsgi+Nginx安装部署
- 转:windows下安装设置Nginx+python+flup+django,并设为服务运行!
- Nginx+Python+uwsgi+Django的web开发环境安装及配置
- centos6.5安装nginx+python+uwsgi+django
- nginx+uwsgi+django - 1 安装nginx
- Django Nginx+uwsgi 安装配置
- centos7 nginx配置httpsCenos(6.6/7.1)下从源码安装Python+Django+uwsgi+nginx环境部署(二)
- centos 7 + uwsgi +django+nginx 安装方法
- django环境安装+站点部署(uwsgi+nginx)
- 安装Django环境(nginx+uwsgi + mysql)
- uWSGI+Nginx+Django安装和配置
- windows下安装设置Nginx+python+flup+django,并设为服务运行!
- Nginx+uWSGI+Django安装过程中遇到的问题
- 在 Ubuntu 14.x 搭建 Nginx Uwsgi Django 环境之(一):Uwsgi的安装