IoT開発(7)
RaspberryPiをIoTゲートウェイにする(II)
VM上の開発環境にWEBサーバーをインストール

2025年2月11日

どもです。
このエントリは、前回の続きです。

今回は、前回のエントリで作成したVM上のRaspberryPi OSにnginx、php、MariaDBをインストール/設定して、WEBサーバー化します。

1.インストール

nginx、php、MariaDBを順番にインストールします。

1.1.nginx

コマンドプロンプトから、下記コマンドを実行します。

xx@raspberry:~$sudo apt-get install nginx

インストール完了後、ホストPCのブラウザで「http://(VMマシンのIPアドレス)」で次の画面が表示できれば、インストール完了です。

1.2.PHP

1.2.1.インストール

コマンドプロンプトから、下記コマンドを実行します。

xx@raspberry:~$sudo apt-get install php-fpm

見てわかる通り、コマンドの実行が完了したら、以下のコマンドを実行、バージョン情報が得られることを確認します。
なお私の環境では、

Ver.7.3.19

でした。

xx@raspberry:~$php-fpm -v
PHP 7.3.19-1~deb10u1 (fpm-fcgi) (built: Jul 5 2020 06:46:45)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.19, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.19-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies

1.2.2.設定

nginxでは、phpはインストールするだけでは使用できません。まず、nginxの設定を行います。
設定ファイルは、以下のコマンドを実行して開きます。

xx@raspberrypi:~$sudo vi /etc/nginx/sites-available/default

開いたファイルの44行目(私の環境)に、「index.php」を追加します。
また、その下にある「pass PHP scripts to FastCGI server」の部分をコメントアウトします。

43 # Add index.php to the list if you are using PHP
44 #index index.html index.htm index.nginx-debian.html;
                                                        追加
                                                         ↓
45 index index.html index.htm index.nginx-debian.html index.php;
46
47 server_name _;
48
49 location / {
50 # First attempt to serve request as file, then
51 # as directory, then fall back to displaying a 404.
52 try_files $uri $uri/ =404;
53 }
54
55 # pass PHP scripts to FastCGI server
56 #
   コメントアウト解除(2行)
       ↓
57 location ~ .php$ {
58 include snippets/fastcgi-php.conf;
59 #
60 # # With php-fpm (or other unix sockets):
   コメントアウト解除
       ↓
61 fastcgi_pass unix:/run/php/php7.3-fpm.sock;
62 # # With php-cgi (or other tcp sockets):
63 # fastcgi_pass 127.0.0.1:9000;
   コメントアウト解除
   ↓
64 }
65
66 # deny access to .htaccess files, if Apache's document root
67 # concurs with nginx's one
68 #
69 #location ~ /.ht {
70 # deny all;
71 #}

次に、php.iniを変更します。
以下のコマンドを実行して、phpの設定ファイルを開きます。

xx@raspberry:~ $ sudo vi /etc/php/7.3/fpm/php.ini

「7.3」の部分は、インストールしたphpのバージョンに従って変更してください。
開いたファイルの中で、790行目辺りに、

;cgi.fix_pathinfo=0

のコメントアウトを解除して、

cgi.fix_pathinfo=1

と変更します。
以上で、phpの設定は完了です。
設定が完了したら、phpとnginxを再起動します。
再起動は、それぞれ以下のコマンドを実行します。

xx@raspberry:~$sudo /etc/init.d/php7.3-fpm restart
xx@raspberry:~$sudo /etc/init.d/nginx restart

次に、ドキュメントのルートフォルダに、以下のphpファイルを作成します。
以下のコマンドを実行し、下記コードを入力します。

xx@raspberrypi:~$sudo vi /var/www/html/test.php

ホストPCのブラウザで「http://(VMマシンのIPアドレス)/test.php」で次の画面(phpのステータス画面)が表示できれば、インストール完了です。

1.3.MariaDB

最後に、MariaDBをインストールします。
MariaDBのインストールは、コマンドライン上で下記コマンドを実行します。

sudo apt-get install mariadb-server mariadb-client

インストールが完了したら、次のコマンドを実行し、バージョンが取得できればインストール完了です。

xx@raspberry:~ $ sudo mariadb -V
mariadb Ver 15.1 Distrib 10.3.22-MariaDB, for debian-linux-gnu (i686) using readline 5.2

2.まとめ

今回、RaspberryPi OSにengix、PHP、MariaDBをインストールしました。
これで、他のセンサー/デバイスが送信したデータを「受信する」準備ができました。
次は、この構築した環境に、センサーの値を書き込む準備を行います。

ではっ!