本篇試圖在 Raspberry Pi 上 Rasbian 版本執行 python web server
因為跟一般 server 建置稍為不同所以記錄
系統更新
sudo apt-get update
sudo apt-get dist-upgrade
安裝 python 相關套件
sudo apt-get install python-dev python-setuptools
sudo apt-get install python-pip
安裝 Django
sudo pip install django
用 Django 新建專案
django-admin.py startproject mysite
把專案跑起來
python manage.py runserver serverIp:serPort
把服務設成開機執行,參考 BOOT執行SHELL
編寫指令稿 /etc/init.d/startPythonWeb
#!/bin/bash
### BEGIN INIT INFO
# Provides: startPythonWeb
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Python Web Start
# Description: Python Web Start
### END INIT INFO
startService(){
ifconfig | grep "inet addr" | grep -v "127.0.0.1" | {
while read f1 f2 f3 f4
do
/usr/bin/python /apps/mysite/manage.py runserver ${f2:5}:80 &
done
}
}
stopService(){
ps aux|grep runserver|grep -v grep | {
while read f1 f2 f3 f4 f5 f6 f7
do
kill -9 $f2
done
}
}
case $1 in
start)
echo -n ¨Starting python runserver service: ¨
startService
;;
stop)
echo -n ¨Stopping python runserver service: ¨
stopService
;;
restart)
stopService
startService
;;
*)
echo ¨Usage: scripts {start|stop|restart}¨
exit 1
esac