在 Raspberry Pi 4 上部署 Apache 2 + PHP 7 + MongoDB

在前面几天终于把 64-bit MongoDB Server 折腾编译好之后,现在就可以在 Raspberry Pi 上部署 Apache 2 + PHP 7 + MongoDB 的环境了~

编译和安装可以参考之前两篇post~在 Raspberry Pi 4 上编译 64-bit 的 MongoDB Server在 Raspberry Pi 上安装 64-bit MongoDB Server 服务

那么现在就是接着在上次的 chroot 环境里部署 Apache 2 + PHP 7,以及 PHP 的 MongoDB 接口~

  1. 安装 Apache 2 与设置 Systemd Service 启动 chroot 环境下的 Apache 2
  2. 安装 PHP 7 与编译设置 PHP MongoDB 扩展

1. 安装 Apache 2 与设置 Systemd Service 启动 chroot 环境下的 Apache 2

将 Apache 2 安装到 mongod 的 chroot 环境中~

sudo schroot -c mongod -- apt install -y apache2

接下来是创建让 mongod 环境中的 Apache 2 随树莓派一起启动的 systemd service 文件~

cat <<EOF | sudo tee /etc/systemd/system/apache2.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=https://httpd.apache.org/docs/2.4/

[Service]
Type=exec
Environment=APACHE_STARTED_BY_SYSTEMD=true
ExecStart=/usr/bin/schroot -c mongod -- /usr/sbin/apachectl -D FOREGROUND
Restart=always
User=root

[Install]
WantedBy=multi-user.target
EOF

然后就在启用这个服务后,试着启动一下~

sudo systemctl enable apache2.service
sudo service apache2 start
sudo service apache2 status

看到 sudo service apache2 status 的输出结果中,服务状态是 active (running) 就没问题啦~

2. 安装 PHP 7 与编译设置 PHP MongoDB 扩展

先来说说在 mongod 环境中安装 PHP 7~

sudo schroot -c mongod -- apt install php7.3-dev libapache2-mod-php7.3 php7.3-mbstring php7.3-json php-pear

理论上上面的命令是没问题的,但是我却遇到了 dpkg 的报错——

dpkg: unrecoverable fatal error, aborting:
unknown system group 'ssl-cert' in statoverride file; the system group got removed
before the override, which is most probably a packaging bug, to recover you
can remove the override manually with dpkg-statoverride
E: Sub-process /usr/bin/dpkg returned an error code (2)

解决办法写到了另一篇 post 里~Solution for "dpkg: unknown user / system group in statoverride file"

接下来则是安装 PHP MongoDB 的扩展~

sudo schroot -c mongod -- pecl install mongodb

在它编译好之后,会提示我们需要在对应的 php.ini 中加上 extension=mongodb.so 来启用这个扩展

那么 Apache 2 所对应的 php.ini 文件的路径则是 /etc/php/7.3/apache2/php.ini(当然,这里路径中的 7.3 是我目前安装的 PHP 的版本,在你的设备上可能是别的数字,请按实际情况更改~)

添加的位置的话,大约在 923 行左右,这个也不一定啦,请根据实际情况自行决定~

sudo schroot -c mongod -- vim /etc/php/7.3/apache2/php.ini

保存 php.ini 文件之后,就可以重启一下 Apache 2 服务~

sudo service apache2 restart

那么最后的话,就来测试一下 PHP 和 PHP MongoDB 扩展都正确安装了~我们在 mongod/var/www/html/ 中写一个测试用的 PHP 文件

sudo schroot -c mongod -- vim /var/www/html/info.php

它的内容其实非常简单~

<?php
    phpinfo();

保存好之后,就可以试着访问一下了!http://raspberrypi.local/info.php

Leave a Reply

Your email address will not be published. Required fields are marked *

nine + 6 =