Raspberry PI (3B、4B) Rasbian Buster
Installation、Configuration and Application of:
Python-based framework:nginx、uWSGI、mariaDB、php、phpMyAdmin and django
一、 前言
人工智慧、物聯網、AIot及Empower edge 是未來新興科技發展的趨勢,如何建置、開發一個功能強大又具備開發、延展性的IoT平台是許多應用廠商必備的基礎環境。本文說明如何利用Raspberry PI建置一個能提供 AIoT開發應用環境平台所需的系統條件、套件及安裝流程,包括建置一個python based Web framework 及openCV、darknet 的AI開發環境。
Raspberry PI Rasbian Buster Lite 是陽春版的Rasbian OS,沒有GUI 的視窗,安裝以後許多套件要透過 Command Line 要靠自己手動安裝,因此可以只安裝自己所需要的軟體套件,減輕系統負擔,提升系統執行效能,速度快,感覺比較順暢,但是,要花時間及精神安裝必要套件。如果要將 Raspberry PI 當作 IoT遠端控制的節點,這種做法似乎是不錯的選擇。
二、 安裝前準備
(一)、 Raspberry PI 3B、4B
(二)、 MicroSD with 64 GB or 128GB
(三)、 下載 Rasbian Buster with Desktop,解壓縮,找到 .img 檔案
(四)、 SDFormatter:下載 SDFormatter 軟體,formate MicroSD卡
(五)、 Win32DiskImager:下載Win32DiskImager軟體,將Rasbian Buster .img檔案寫入 MicroSD卡片
(六)、 Rasbian Buster安裝後,沒有GUI介面,command Line
$ Login : pi
$ Passwd:raspberry
===== Change Passwd ==========
$ passwd ==>
$ sudo apt update
$ sudo apt upgrade
$ sudo apt rpi-upgrade
=== 然後開啟應用程式,放到桌面上: /usr/share/applications 底下 有一些基本安裝的應用程式Icon,複製到 桌面上,如 Termit終端機,檔案管理程式等。
$ sudo raspi-config # pi 設定 包括 enable ssh、VNC,Localization中文化,WiFi 設定,可開啟手機當基地台。
如果有需要:
設定網路連線 --> /etc/dhcpcd.conf
設定固定網路IP連線:
$ sudo vi /etc/dhcpcd.conf
在檔案最後面加上這幾行:
……
interface eth0
#fallback static_eth0
static ip_address=163.13.XX.XX/24 # TKU Static IP
static routers=163.13.22.254
static domain_name_servers=163.13.1.160
static domain_name_servers=8.8.8.8
$ sudo reboot
(七)、 Check Python、Python3
Raspberry pi Buster 安裝完成後,Python預設已經安裝
1、 Check python 版本
$ python –V
Python 2.7.16 (python2 default version)
$ python3 –V
Python 3.7.3 (python3 default version)
$ pip --version
pip 18.1 from /usr/lib/python2.7/dist-packages/pip (python 2.7)
$ pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
三、 安裝 Web Framework:nginx、Django、MySQL(MariaDB)
(一)、 安裝 Web Framework相關套件
Python based web framework,包括Web Server、WSGI、Python Web Development、database、DBMS
1、 Web Server: Nginx、PHP
2、 Python Web Platform: Django
3、 Database : MySQL、MariaDB
4、 DBMS: phpMyAdmin
5、 WSGI : uWSGI
6、 Related Packages
(二)、 安裝Web Server (nginx)
https://gist.github.com/isc30/aa80d81df ... 2d55806381
Let’s create a new source list:
$ sudo vi /etc/apt/sources.list.d/10-buster.list
In that file add.
deb http://mirrordirector.raspbian.org/raspbian/ buster main contrib non-free rpi
After saving create the next file
$ sudo vi /etc/apt/preferences.d/10-buster
In that file add.
Package: *
Pin: release n=stretch
Pin-Priority: 900
Package: *
Pin: release n=buster
Pin-Priority: 750
Again save. Make the system aware of this source list with
$ sudo apt update
1、 安裝相關套件 (php7.3)
$ sudo apt-get install -y -t buster php7.3 php7.3-cli php7.3-fpm php7.3-curl php7.3-gd php7.3-intl php7.3-mbstring php7.3-mysql php7.3-imap php7.3-opcache php7.3-sqlite3 php7.3-xml php7.3-xmlrpc php7.3-zip php7.3-bcmath php-apcu
$ php -v
PHP 7.3.11-1~deb10u1 (cli) (built: Oct 26 2019 14:14:18) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.11-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies
Now we need to add a few fpm things for nginx to work properly. Create a extra config file
$ sudo vi /etc/php/7.3/fpm/conf.d/90-pi-custom.ini
And add:
cgi.fix_pathinfo=0
upload_max_filesize=64m
post_max_size=64m
max_execution_time=600
Finally reload php
$ sudo service php7.3-fpm reload
Now PHP is ready to us
2、 安裝 nginx
$ sudo apt-get install -y -t buster nginx
$ nginx –v
nginx version: nginx/1.14.2
安裝完成後,系統會增加一個存放nginx系統檔案的目錄
/etc/nginx 及 存放Web文件的 /var/www 及 /var/www/html 目錄,該目錄內有一個新建檔案:index.nginx-debian.html,是預設Web site 的首頁文件。
/etc/nginx/site-available/ 目錄則有一份 設定(Configure)檔案:default,內含 nginx web server 相關 的 參數與設定資料
/etc/nginx/site-enabled/ 目錄內則提供 在site-available 內設定檔的 soft link檔。
# 使用預設值開機 nginx 及 php7.3-fpm
$ sudo update-rc.d nginx defaults
$ sudo update-rc.d php7.3-fpm defaults
啟動 nginx server:
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
$ nginx –v
nginx version: nginx/1.14.2
Restart nginx
$ sudo nginx -t && sudo service nginx reload
do some global settings open:
$ sudo vi /etc/nginx/conf.d/99-custom.conf
Add the following line, we increase the allowed upload sizes.
client_max_body_size 64M;
從client 端: http://<Server IP> 就可以看到歡迎網頁
Welcome to nginx!
此檔案 存放在 /var/www/html/index.nginx-debian.html
設定檔放在:/etc/nginx/sites-available/default
連結檔:/etc/nginx/sites-available/default -> /etc/nginx/sites-available/default
在 /etc/nginx/sites-enable/default 可以看到 連結
# 將 /etc/php/7.3/fpm/php.ini 內容中
;cgi.fix_pathinfo=1 改成
cgi.fix_pathinfo=0
$ sed -i 's/^;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/7.3/fpm/php.ini
3、 Nginx Setting (/etc/nginx/sites-available/default)
修改default 檔案內容
$ sudo vi /etc/nginx/sites-available/default
# Default server
server {
listen 80 default_server;
listen [::]:80 default_server; # support IPv6
root /var/www/html;
# Make site accessible from http://localhost/
server_name localhost;
index index.php index.html;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/index.html /cgi-bin/pyindex.py?q=$uri;
}
location /cgi-bin {
include uwsgi_params;
uwsgi_modifier1 9;
uwsgi_pass 127.0.0.1:9000;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_intercept_errors on;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
# deny access to .htaccess files, should an Apache document root conflict with nginx
location ~ /\.ht {
deny all;
}
}
$ sudo mkdir -p /var/www/html/public
$ sudo cat > /var/www/html/public/index.php << "EOF"
<?php
class Application
{
public function __construct()
{
phpinfo();
}
}
$application = new Application( ) ;
?>
EOF
$ sudo chown -R www-data:pi /var/www
$ sudo chmod -R 775 /var/www
$ sudo usermod -aG www-data pi
$ sudo service nginx restart
$ sudo service php7.3-fpm restart
(三)、 安裝 Database:MySQL、MariaDB
1、 安裝
$ sudo apt-get install mariadb-server mariadb-client libmariadbclient-dev libssl-dev
$ mysql --version
mysql Ver 15.1 Distrib 10.3.17-MariaDB, for debian-linux-gnueabihf (armv7l) using readline 5.2
$ sudo mysql -u root -p
Enter password: (none by default, type enter)
安裝完成的預設passwd是空白的,按Enter即可
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 47
Server version: 10.3.17-MariaDB-0+deb10u1 Raspbian 10.0
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Change Password:
running MySQL 5.7.6 and later or MariaDB 10.1.20 and later to reset or change a user password:
MariaDB[(none)]>ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEWPASSWORD';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)] >FLUSH PRIVILEGES;
MariaDB [(none)]>show databases;
+----------------------+
| Database |
+----------------------+
| information_schema |
| mysql |
| performance_schema |
+-----------------------+
3 rows in set (0.001 sec)
MariaDB [(none)]>exit
用新的密碼再測試一次:
$ sudo mysql -u root –p
For MySQL 5.7.5 and earlier or MariaDB 10.1.20 and earlier, use the commands below to change the user password
MariaDB [(none)]>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
MariaDB [(none)] >FLUSH PRIVILEGES;
MariaDB [(none)]>exit
設定系統開機時自動執行 MariaDB的服務:
$ sudo systemctl start mariadb.service
$ sudo systemctl enable mariadb.service
$ sudo systemctl status mariadb.service
可以看到 active (running) 的 status
2、 安全性設定
$ sudo mysql_secure_installation
Enter current password for root (enter for none): newpasswd
之後,除了Change the root password?[Y/n] : n
其他都按 y
(四)、 Installing phpMyAdmin
$ sudo apt-get install -y phpmyadmin
$ sudo ln -s /usr/share/phpmyadmin /var/www/html/
Run phpmyadmin:
http://<my server IP>/phpmyadmin
???? 有 permission deny的問題 (403 Forbidden)
1、 Installing phpMyAdmin
$ sudo apt-get install phpmyadmin
Installation directory : /usr/share/phpmyadmin
安裝過程會有一些問題,原則上:
User: root (‘root’@’localhost’
Password : your passwd
2、
(五)、 安裝 Django with Nginx uWSGI + MariaDB
1、 安裝 Django
$ sudo pip3 install django mysqlclient
完成安裝,會在 /usr/local/bin/目錄底下 產生django-admin 及 django-admin.py 二個檔案,提供 Django web 的管理功能。
$ sudo django-admin --version
2.2.7
安裝mysqlclient 可提供 Django project 的application 可 connect mysql database.
2、 建立Django Project
(六)、 參考資料:
1、 Django on Raspberry Pi 3 with phpMyAdmin (Raspbian Stretch Lite, Apache, MariaDB and Python 3.x as the LAMP stack) https://knowledgeofthings.com/django-on-raspberry-pi-3/
2、 Django教學:https://developer.mozilla.org/zh-TW/doc ... ry_website
3、 Django 管理工具:https://www.runoob.com/django/django-ad ... -tool.html
4、 Django Introduction:https://developer.mozilla.org/en-US/doc ... troduction
5、 Setting up a Django development environment:https://developer.mozilla.org/en-US/doc ... nvironment
6、 Django Tutorial Part 2: Creating a skeleton website:https://developer.mozilla.org/en-US/doc ... on_website
7、 Adding Users to Your Django Project With A Custom User Model https://simpleit.rocks/python/django/ad ... ser-model/
8、 Nginx+uWsgi+Django+Python+MongoDB+mySQL服务器搭建https://www.cnblogs.com/geekma/archive/2013/02/22/2921952.html
9、 Learn about Python, Django, Marketing and Science:http://www.marinamele.com/django
10、 Best Practice to Structure Django Project Directories and Files:https://studygyaan.com/django/best-prac ... -and-files
11、 Front-end structure of large scale Django project:https://softwareengineering.stackexchan ... go-project
12、 Working Structure of Django MTV Architecture:https://towardsdatascience.com/working- ... 41c8c64082
13、 Django 基本教學 - 從無到有 Django-Beginners-Guide:https://github.com/twtrubiks/django-tutorial
14、 Django Social Login Tutorial:https://github.com/twtrubiks/django_soc ... n_tutorial
15、 Get Started With Django Part 1: Build a Portfolio App: https://realpython.com/get-started-with-django-1/
16、 The world's simplest facial recognition api for Python and the command line:https://github.com/ageitgey/face_recognition
17、 基於Linux環境搭建Nginx uWSGI Python框架方法介紹:https://codertw.com/%E7%A8%8B%E5%BC%8F% ... 80/115783/
18、 基于Linux环境搭建Nginx+uWSGI+Python框架方法介绍:https://segmentfault.com/a/1190000012918535
19、 用Nginx搭建Django生產環境的方法:https://kknews.cc/code/ynnljzj.html
Python-based framework:nginx、uWSGI、mariaDB、php、phpMyAdmin and django
誰在線上
正在瀏覽這個版面的使用者:沒有註冊會員 和 7 位訪客