Raspbian LAMP化 [1] 準備

Raspberry Pi で LAMP環境を構築する機会があったのでメモします。

環境

導入環境 詳細
SBC Raspberry Pi 3 Model B
microSD 16GB / Class 10
LAMP
Linux RASPBIAN STRETCH LITE
Minimal image based on Debian Stretch
Download Raspbian for Raspberry Pi
使用イメージ
2018-04-18-raspbian-stretch-lite.zip
Apache Apache/2.4.25 (Raspbian)
MariaDB mysql Ver 15.1 Distrib 10.1.23-MariaDB
PHP PHP 7.0.27-0+deb9u1 (cli) Raspbian LAMP化 [2] PHP

準備

今回はサーバ用途なので「RASPBIAN STRETCH LITE」をダウンロード、解凍後にSDカードに書き込みます。
※8GBのメディアでも導入可能です。

電源投入で起動し、自動でIPアドレスを取得します(DHCP環境下の場合)。
同一セグメントに DHCP サーバがない場合は、後述のIP固定化で任意設定してください。

どちらにしても、SSH接続まではディスプレイを繋いでおきます。
(IPアドレスを見つけてもデフォルトでSSHが無効化されており接続できないので)。

初期設定

ログイン

初期id・パスワードは以下のとおりです。

初期id 初期パスワード
pi raspberry

パスワード変更

まず、パスワードを変更しておきます。

$ passwd
passwd: パスワードは正しく更新されました

ファームウェアアップデート

$ sudo rpi-update
 *** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
 *** Performing self-update
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13545  100 13545    0     0  33072      0 --:--:-- --:--:-- --:--:-- 33036
 *** Relaunching after update
 *** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
 *** We're running for the first time
 *** Backing up files (this will take a few minutes)
 *** Backing up firmware
 *** Backing up modules 4.14.34-v7+
#############################################################
This update bumps to rpi-4.14.y linux tree
Be aware there could be compatibility issues with some drivers
Discussion here:
https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=197689
##############################################################
 *** Downloading specific firmware revision (this will take a few minutes)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   168    0   168    0     0    236      0 --:--:-- --:--:-- --:--:--   236
100 55.8M  100 55.8M    0     0   124k      0  0:07:39  0:07:39 --:--:--  117k
 *** Updating firmware
 *** Updating kernel modules
 *** depmod 4.14.52+
 *** depmod 4.14.52-v7+
 *** Updating VideoCore libraries
 *** Using HardFP libraries
 *** Updating SDK
 *** Running ldconfig
 *** Storing current firmware revision
 *** Deleting downloaded files
 *** Syncing changes to disk
 *** If no errors appeared, your firmware was successfully updated to 76f2126f958813868e6b07945c948761d8eec91d
 *** A reboot is needed to activate the new firmware

$ sudo reboot

アップデート

再起動後、アップデートしておきます。

$ sudo apt-get update
$ sudo apt-get upgrade

※「Raspberry Pi 初期設定まとめ」も是非ご覧ください。

SSH有効設定

初期状態だと、SSHが無効になっているので有効にします。

SSH有効化

ロケール(利用する言語や地域の指定)が未設定で警告が表示されます。

$ sudo systemctl enable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ssh
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_TIME = "ja_JP.UTF-8",
    LC_MONETARY = "ja_JP.UTF-8",
    LC_ADDRESS = "ja_JP.UTF-8",
    LC_TELEPHONE = "ja_JP.UTF-8",
    LC_NAME = "ja_JP.UTF-8",
    LC_MEASUREMENT = "ja_JP.UTF-8",
    LC_IDENTIFICATION = "ja_JP.UTF-8",
    LC_NUMERIC = "ja_JP.UTF-8",
    LC_PAPER = "ja_JP.UTF-8",
    LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_TIME = "ja_JP.UTF-8",
    LC_MONETARY = "ja_JP.UTF-8",
    LC_ADDRESS = "ja_JP.UTF-8",
    LC_TELEPHONE = "ja_JP.UTF-8",
    LC_NAME = "ja_JP.UTF-8",
    LC_MEASUREMENT = "ja_JP.UTF-8",
    LC_IDENTIFICATION = "ja_JP.UTF-8",
    LC_NUMERIC = "ja_JP.UTF-8",
    LC_PAPER = "ja_JP.UTF-8",
    LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.

ロケール設定

言語設定を ja_JP に変更(生成・設定)します。

$ sudo vi /etc/locale.gen 

$ cat /etc/locale.gen | grep ja_JP
# ja_JP.EUC-JP EUC-JP
ja_JP.UTF-8 UTF-8

$ sudo locale-gen 
Generating locales (this might take a while)...
  en_GB.UTF-8... done
  ja_JP.UTF-8... done
Generation complete.

$ sudo localectl set-locale LANG=ja_JP.UTF-8 LANGUAGE="ja_JP:ja"

$ source /etc/default/locale 

$ echo $LANG
ja_JP.UTF-8

SSH有効化 : 再度確認

一度 disable してから enable にします。
※ ロケール未設定だと、disable でも同じメッセージがでます。

$ sudo systemctl enable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ssh
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.

警告メッセージが消えました。

公開鍵エラーになる場合

known_hosts に登録されている公開鍵が違います、という警告メッセージが表示された場合は、Banana Pi で OpenMediaVault をご参照ください。

IP固定

下記ファイルのいずれかに設定を追記します。

  • /etc/network/interfaces
  • /etc/network/interfaces.d/default

今回は、default ファイルを新規作成しました。
設定後に再起動してネットワーク設定を反映させます。

$ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to bpi 用にパスワードを変更中
e used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

$ cat /etc/network/interfaces.d/default 
# Wired adapter #1
allow-hotplug eth0
no-auto-down eth0
iface eth0 inet static
address 192.168.2.211
netmask 255.255.255.0
gateway 192.168.2.1
dns-nameservers 8.8.8.8 8.8.4.4
$ sudo reboot