树莓派 Web服务器 LNSP

采用LNSP配置(Linux + Nginx + Sqlite + PHP)

1. Nginx

sudo apt-get install nginx,安装

sudo /etc/init.d/nginx start,启动服务

修改 sudo vim /etc/nginx/sites-available/default 其中的部分内容,其他保持不变。

  1. listen 改为8080端口(为了防止和80端口冲突)
  2. index 增加 index.php
  3. 去除php段落的部分注释
  4. html 所在默认路径, root /var/www/html;
server {
    listen 8080 default_server;
    #listen [::]:8080 default_server;    
 
    root /var/www/html; 

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

    server_name _; 

    location / { 
            # First attempt to serve request as file, then 
            # as directory, then fall back to displaying a 404. 
            try_files $uri $uri/ =404; 
    } 

    # 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; 
    #} 
}

sudo /etc/init.d/nginx reload,重载。

这时通过系统自带的浏览器访问127.0.0.1:8080,可以看见nginx初始页面。

2. php和sqlite

sudo apt-get install php-fpm php-sqlite3,安装php和sqlite3。

注意使用 php-fpm 和 php-sqlite3 作为默认参数,可以安装最新版本。

在html所在默认路径,新建文件index.php

<?php echo phpinfo(); ?>

期间遇到了一些问题,最初以为是ngnix和php配置的问题,最后发现是因为最初index.php文件里的语法问题,返回HTTP 500的错误。
通过修改sudo vim /etc/php/7.3/fpm/php.ini 文件中 display_errors = On,可以显示出错误所在,进行修正。

3. 反向代理

使用frp作为内网穿透,因为没有多余的域名,只能通过IP访问,所以在frpc.ini 中添加

[Web]
type = tcp
local_ip = 127.0.0.1
local_port = 8080
remote_port = [remote_port]

使用frp的 server_IP:[remote_port]即可以从外网访问处于内网上的树莓派。

4. nginx和php相关指令

/etc/init.d/php-fpm7.3 {start|stop|status|restart|reload|force-reload}
nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注