发布于 

树莓派搭建 LEMP 小记

前言

最近有一个音乐学院的朋友想搞一个音频资源共享系统,需要用到 PHP 进行开发,本来是他来提供服务器的,结果……他的服务器到期了,他人又在国外,基本联系不到,于是乎我掏出了我的祖传树莓派,配置一下 PHP 环境,然后,这个故事(事故)就这么开始了……

目标环境

  • Raspbian GNU/Linux 10 (buster)
  • Nginx
  • PHP7.3
  • Mariadb

准备工作

首先检查了一下祖传的树莓派是否还在正常的运行,顺便看了一下安装了些什么乱七八糟的服务,发现 Nginx 已经搭建好了。

安装 Nginx

本来以为安装 Nginx 会是最省事的一步,结果没想到配置解析 PHP 时出现了事故……具体配置后面再述。

1
2
3
4
5
# 国际惯例,首先更新软件列表
sudo apt-get update

# 安装 Nginx
sudo apt-get install nginx

安装 PHP7.3

1
2
# 不管是啥一股脑全装上
sudo apt-get -y -t buster php7.3-fpm php7.3-cgi php7.3-cli 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

输入 php -v 检查是否安装成功。

开启 php7.3-fpm

1
sudo service php7.3-fpm start

安装 Mariadb

1
sudo apt-get install mariadb-server-10.0

安装完成后输入

1
sudo mysql

进入 mysql,进入后用户变为 MariaDB [(none)]

输入以下命令初始化数据库:

1
2
3
4
5
6
7
USE mysql;
# 启用 root 用户密码登录
UPDATE user SET plugin='mysql_native_password' WHERE user='root';
# 修改 root 用户密码
UPDATE user SET password=PASSWORD('你想设置的密码') WHERE user='root';
flush privileges;
exit;

配置完 mysql 数据库之后重启 mysql

1
sudo service mysql restart

配置 Nginx

Nginx 安装成功之后,不出意外的话会自动开启服务,根目录在 /var/www/html,接下来开始配置 Nginx 解析 PHP

关于如何配置我之前也查过不少博客文章,发现千篇一律都是更改 /etc/nginx/sites-enabled/default 或者是 /etc/nginx/sites-available/default (这两个是链接文件,后者是源文件,改谁都一样)中的 location ~ \.php$ 段,然后在 index 中添加 index.php ,经过多次尝试,发现在我的系统上不适用,访问 .php 便会下载文件,于是又查阅了一些资料,终于在我的设备上成功访问 .php ,配置如下:

  1. 编辑 /etc/nginx/sites-available/default 文件:

    1
    sudo vim /etc/nginx/sites-available/default

    具体怎么修改就不多赘述了,直接贴最终配置文件:

    Nginx 配置文件
    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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    ##
    # 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;

    # 注意这里添加 index.php
    # 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;
    # }

    # 添加这么一条 location 段
    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;
    # }
    #}
  2. 编辑 /etc/nginx/site-enabled/default.conf 文件,注意这里就是很多教程没有改的东西,至于如何修改,参照第一步来就行,一摸一样……

  3. 重启 Nginx 服务

    1
    2
    3
    4
    5
    # 可以先检查一下配置文件是否正确
    sudo nginx -t

    # 验证没问题之后,让 Nginx 重载配置文件
    sudo nginx -s reload
  4. 浏览器访问一下 .php 页面试试,看看是不是成功了。

结语

以前刚买来树莓派折腾个人服务器的时候就配置过 PHP,但是那个时候没有太多时间折腾,试了几种方法不行之后就没再管过他,没想到今天又重新搞了一遍……说到底还是太菜了,流下了没有技术的眼泪。


本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

本站由 @吴咕咕 创建,使用 Stellar 作为主题。