树莓派WEB开发--智能家居方案

参考文章1--

树莓派搭建web服务器_mahaijun1996的博客-CSDN博客_树莓派搭建web服务器

参考文章2--

树莓派安装 Nginx + PHP7.0 + Pi Dashboard | 树莓派实验室   内部再找

https://github.com/nxez/pi-dashboard  内部再找

Pi Dashboard (Pi 仪表盘) - MAKE 趣无尽

开始

参考文章1很简单 直接上手 但是安装PHP5总是不行的  因为文章2说了 现在的树莓派已经升级为PHP7了 

那就根据文章2走路

sudo apt-get update
sudo apt-get install nginx php7.3-fpm php7.3-cli php7.3-curl php7.3-gd php7.3-cgi
sudo service nginx start
sudo service php7.3-fpm restart

注意中间那个 还是会失败 我是一个一个安装的 最后还有一个 文章1的

sudo apt-get install sqlite

sudo nano /etc/nginx/sites-available/default

修改为下面的 MV吧

default

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

    location / {
    index  index.html index.htm index.php default.html default.htm default.php;
    }
     
    location ~\.php$ {
    fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    #fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/run/php/php7.3-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

结合文章1增加数据库的

default

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

    location / {
    index  index.html index.htm index.php default.html default.htm default.php;
    }
     
    location ~\.php$ {
    fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    #fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/run/php/php7.3-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
      location ~ \.sqlite$ {

        deny all;
	}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

上面2个都OK的 随便使用一个

sudo service nginx restart

因为SSH连接的 PC是10.60.20.31  PI是10.60.20.125  现在PC打开浏览器 http://10.60.20.125/

这个网页就表示成功了 它是怎么来的 


pi@raspberrypi:/var/www/html $ cp index.nginx-debian.html /home/pi/html

就是这个描述的!!!其实在上面default文件 有写     root /var/www/html;

就是这个里面的HTML文件

修改下 这个文件

看到OKl

+++++++++++++++++++++++++++++++++++

现在 做一个表格 填写东西 放在本地

HTML 标签https://www.w3school.com.cn/tags/tag_input.asp

<!DOCTYPE html>
<html>
<body>

<form action="/demo/demo_form.asp">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form> 

<p>如果您点击提交,表单数据会被发送到名为 demo_form.asp 的页面。</p>

</body>
</html>

直接抄袭代码

修改一下路径

点击提交 我希望是保存本地 但是HTML是不行的 需要其他的

增加一个文件 内容是下面的 点击可以跳转一下

求HTML5 form中用于返回的asp文件(demo_form.asp)_360问答

 

++++++++++++++解答上面的问题 看raspap它里面用了很多SH比如点击按键可以reboot树莓派那就是PHP调用的SH+++++++++++++++++

https://www.w3school.com.cn/tags/tag_input.asp
直接抄袭代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>xxx.com</title>
</head>
<body>
<form action="welcome.php" method="get">
   NAME: 
<input type="text" name="name">
   AGE: 
<input type="text" name="age">
<input type="submit" value="submit">
</form>
</body>
</html>






<?php
$name = $_GET['name']; 
$age  = $_GET["age"]; 
echo $name;
echo $age;
?>


如果一旦点击 WIN10 就下载!!那是环境不对 default修改好 就不会了

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>xxx.com</title>
</head>
<body>
<form action="welcome.php" method="post">
   NAME: 
<input type="text" name="name">
   AGE: 
<input type="text" name="age">
<input type="submit" value="submit">
</form>
</body>
</html>

<?php
$name = $_POST['name']; 
$age  = $_POST["age"]; 
echo $name;
echo $age;
?>

有时候一旦点击就是 405 Not Allowed
https://www.zuopeng.gd.cn/article/details/id/116.html
但是我现在是好的

测试PHP

<?php
echo "Hello, world!\n";
?>

执行

root@raspberrypi:/home/pi# php /var/www/html/welcome.php
Hello, world!


说明 --我们PHP环境没有问题 

会下载 是环境不对
https://blog.csdn.net/qq_38688701/article/details/120169653?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EsearchFromBaidu%7Edefault-6.searchformbaiduhighlight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EsearchFromBaidu%7Edefault-6.searchformbaiduhighlight
修改 就不会下载了

https://www.cnblogs.com/hongfeizhou/p/5307013.html

此时怎么处理文件呢?

简化版本
<?php

echo "Hello, world!\n";

shell_exec("sudo /home/pi/wifible.sh 3 8 9");
?>


#!/bin/bash
ws_addr=$1
token=$2
ble_name=$3

echo " {                            " >  /tmp/wifibleconf
echo " \"ws_addr\" : \"$ws_addr\"   " >> /tmp/wifibleconf
echo " \"token\"   : \"$token\"     " >> /tmp/wifibleconf
echo " \"ble_name\": \"$ble_name\"  " >> /tmp/wifibleconf
echo " }                            " >> /tmp/wifibleconf


PI测试
pi@raspberrypi:~ $ sudo cp /home/pi/welcome.php /var/www/html/welcome.php
pi@raspberrypi:~ $ php /var/www/html/welcome.php
Hello, world!

pi@raspberrypi:~ $ cat /tmp/wifibleconf
 {
 "ws_addr" : "3"
 "token"   : "8"
 "ble_name": "9"
 }
pi@raspberrypi:~ $

说明本地执行是OK的!
但是再网页提交就是不可以!
https://www.jianshu.com/p/67cc4383da27

第一步

root@raspberrypi:/# cd /
root@raspberrypi:/# find -name php.ini
./etc/php/7.3/fpm/php.ini
把这个文件修改
disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
好像 我不要修改!!

第二部
root@raspberrypi:/# find -name php-fpm.conf
./etc/php/7.3/fpm/php-fpm.conf
root@raspberrypi:/# cp /etc/php/7.3/fpm/php-fpm.conf /home/pi/ini

也没有啊

https://www.cnblogs.com/-beyond/p/8570654.html
https://wenda.so.com/q/1526465057215477
linux下输入visudo
  然后我们在最后追加
  apache ALL=NOPASSWD:/bin/sh

我 输入 visudo
www-data ALL=(ALL) NOPASSWD: ALL
最后追加

在测试
问题解决了!!!也即是我这个PHP可以调用SH了 


现在全部完成
也就是NGX起一个WEB
通过index.html直观的前端显示UI
输入以后提交到welcome。php
它拿到数据以后就调用SH保存起来

全部文件

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>movella</title>
</head>
<body>
<form action="backgroundindex.php" method="post">
   ws_addr: 
<input type="text" name="ws_addr"  size="88"><br /> 
   token: 
<input type="text" name="token"  size="88"><br /> 
   ble_name:
<input type="text" name="ble_name"  size="88"><br /> 
<input type="submit" value="submit"><br /> 
</form>
</body>
</html>

backgroundindex.php

<?php
echo "Success!\n";
echo $_POST['ws_addr'] ;
echo $_POST['token'] ;
echo $_POST['ble_name'] ;

$ws_addr = escapeshellarg($_POST['ws_addr']);
$token = escapeshellarg($_POST['token']);
$ble_name = escapeshellarg($_POST['ble_name']);

echo $ws_addr;
echo $token;
echo $ble_name;
shell_exec("sudo /var/wifible.sh $ws_addr $token $ble_name");
?>

wifible.sh

#!/bin/bash
ws_addr=$1
token=$2
ble_name=$3

echo " {                            " >  /tmp/config.json
echo " \"ws_addr\" : \"$ws_addr\"   " >> /tmp/config.json
echo " \"token\"   : \"$token\"     " >> /tmp/config.json
echo " \"ble_name\": \"$ble_name\"  " >> /tmp/config.json
echo " }                            " >> /tmp/config.json

default

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;
	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;
 
	root /var/www/html;
	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;
 
	server_name _;
 
    location / {
    index  index.html index.htm index.php default.html default.htm default.php;
    }
     
    location ~\.php$ {
    fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    #fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/run/php/php7.3-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}
	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
      location ~ \.sqlite$ {
 
        deny all;
	}
}
 
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

最后把全文总结 流程写在下面脚本 直接bash web.bash 就好了 然后我会去码云上传

#!/bin/bash
echo -e "\033[32m --start-- \033[0m"
echo "1"
sudo apt-get -y install nginx
echo "2"
sudo apt-get -y install php7.3-fpm
echo "3"
sudo service nginx start
echo "4"
sudo service php7.3-fpm start
echo "5"
cp default /etc/nginx/sites-available/default
echo "6"
cp index.html /var/www/html/index.html
echo "7"
cp backgroundindex.php /var/www/html/backgroundindex.php
echo "8"
chmod 777 wifible.sh
cp wifible.sh /var/wifible.sh
echo "9"
sudo service nginx restart
echo "10"
sudo service php7.3-fpm restart
echo -e "\033[32m --end-- \033[0m"


echo -e "\033[31m visudo  www-data ALL=(ALL) NOPASSWD: ALL \033[0m"

+++++补充问题+++++++++

启动sh脚本时报错No such file or directory的解决办法_卡尔-Y的博客-CSDN博客

后面执行的还是 还是找不到脚本 本来是有的!!

这样就OK

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值