(麦知小车项目)
做一个小项目,如果实现了,那么将会学习到很多知识,并了解知识的应用。
进入主题:
环境:
树莓派系统 : Debian GNU/Linux 2015-09-24-raspbian-jessie
nginx : 1.6.2 nginx
uwsgi : 2.0.11.2 uwsgi
django : 1.6.8
python :2.7.9
安装:
首先安装
1
2
3
|
$sudo apt
-
get install python
-
dev
$sudo apt
-
get install python
-
pip
$sudo apt
-
get install libpcre3 libpcre3
-
dev
|
然后安装
1
2
3
|
nginx
sudo
apt-get
install
nginx
uwsgi
sudo
pip
install
uwsgi
django
sudo
pip
install
django==1.6.8
|
然后建立一个django 项目,简单可打开就可以。我用的是
1
|
django.admin.py startproject rascar
|
rascar:
做简单的配置,可以让项目可以看到 it works.就可以了。
然后进入到 nginx 与 uwsgi 的配置:
1 需要的文件夹:
/etc/nginx/conf.d
/etc/uwsgi/vassals/ (需要我们自己建立)
2 需要的文件 三个
nginx.conf
uwsgi.ini
uwsgi_params
文件内容:
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# the upstream component nginx needs to connect to
upstream django {
server unix:
///tmp/uwsgid
.sock;
# for a file socket
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 192.168.1.101;
# substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 128M;
# adjust to taste
# Django media
location
/media
{
alias
/home/pi/rascar/media
;
# your Django project's media files - amend as required
}
location
/static
{
alias
/home/pi/rascar/static
;
# your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include
/home/pi/rascar/uwsgi_params
;
# the uwsgi_params file you installed
}
}
|
uwsgi.ini
1
2
3
4
5
6
7
8
|
[uwsgi]
chdir =
/home/pi/rascar
# socket = /tmp/car.sock
module = rascar.wsgi:application
processes = 4
master = 1
uid = pi
gid = pi
|
uwsgi_params
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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;
|
我们把这三个文件放在django项目里面:
3 做软链接:
1
2
|
sudo
ln
-s
/home/pi/rascar/nginx
.conf
/etc/nginx/conf
.d/
sudo
ln
-s
/home/pi/rascar/uwsgi
.ini
/etc/uwsgi/vassals/
|
注意,一定要将相对路径写上,要不然系统找不到。
4 自启动:
写到这里,nginx+uwsgi 基本上配置完成,但是,做项目总不能每天开一次机然后重新输入下
1
|
sudo
uwsgi --ini uwsgi.ini
|
所以需要我们写入新的文件,来帮助这个项目实现自启动
emperor.uwsgi.service
emperor.uwsgi.socket
文件内容:
emperor.uwsgi.service
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[Unit]
Description=uWSGI Emperor
After=syslog.target
[Service]
ExecStart=
/usr/local/bin/uwsgi
--ini
/etc/uwsgi/vassals/uwsgi
.ini --logto
/home/pi/uwsgi
.log
RuntimeDirectory=uwsgi
# Requires systemd version 211 or newer
Restart=always
KillSignal=SIGQUIT
Type=notify
#StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
|
emperor.uwsgi.socket
1
2
3
4
5
6
7
|
[Unit]
Description=Socket
for
uWSGI Emperor
[Socket]
# Change this to your uwsgi application port or unix socket location
ListenStream=
/tmp/uwsgid
.sock
[Install]
WantedBy=sockets.target
|
然后将这两个文件放入指定文件夹中:
/etc/systemd/system/
如下图:
然后用命令操作:
1
2
3
4
|
$
sudo
systemctl start emperor.uwsgi.socket
$
sudo
systemctl start emperor.uwsgi.service
$
sudo
systemctl
enable
emperor.uwsgi.socket
$
sudo
systemctl
enable
emperor.uwsgi.service
|
然后我们用命令查看一下服务是否启动:
1
2
|
sudo
systemctl |
grep
uwsgi
sudo
systemctl |
grep
nginx
|
我看看到项目服务开启,也成功启动:
成功实现自启动,HaHaPPyPPy!
另项目配置文件代码可去下面地址下载,也算是给作者一些小小的鼓励: