2017年1月24日 星期二

在樹莓派上架設 PHP+MySQL 網站伺服器

由於去年底向英國 Hostinger 租用的虛擬主機在實際使用之後發現有 "Resource Limit Exceeded" 問題, 雖然經過 PHP 程式優化以及刪除不需要的 Cron Jobs 後已經很少出現, 但是我覺得有必要另外準備一個替代方案.

最近想到可用樹莓派來試試看, 因為我的 PHP 專案主要是跑網路爬蟲程式進行資料探勘, 網站不一定要架在 Internet 上, 可以架設在區域網路中, 透過網路連線存取 Internet, 只要定時將資料分析結果丟上雲端即可. 如果測試 OK 的話, 將來 Hostinger 主機就轉變成資料彙總主機, 負載就不會這麼高了.

在樹莓派架設 LAMP (Linux + Apache + MySQL + PHP) 的程序, 我主要是按照柯博文的 "Raspberry Pi 最佳入門與實戰應用 (第二版)" 第 6 章進行測試的.

Source : 金石堂

參考 :

# 在樹莓派上架設Apache+MySQL+PHP

不過這本書中有一些地方需要修正, 例如書中的網頁根目錄預設是在 /var/www 下, 但我使用的 Apache 版本較新,  網頁根目錄預設是在 /var/www/html 下.

1. 更新樹莓派已安裝之套件 : (參考鳥哥的文章)

$ sudo apt-get update        (從 apt 伺服器更新用戶端套件表頭清單)
$ sudo apt-get upgrade      ( 完整升級已安裝的套件)

2. 安裝 Apache2 網頁伺服器 :

$ sudo apt-get install apache2  

3. 安裝 PHP5 語言解譯引擎 :

$ sudo apt-get install php5 libapache2-mod-php5  

4. 重新啟動 Apache 伺服器 :

$ sudo service apache2 restart

經過這四道程序後, Apache + PHP 網頁伺服器就架起來了. 用瀏覽器連線樹莓派 IP, 如果看到下面網頁就是成功了 :


這裡我是以 Headless (無頭) 方式從筆電遠端操控樹莓派, 筆電與樹莓派都同時透過手機分享的 wifi 上網, 所以上圖也是從筆電瀏覽器連線樹莓派的 IP (即與 Putty 遠端連線一樣的 IP).

這個預設網頁是存放於 /var/www/html 下的 index.html 檔, 我們可以撰寫自己的首頁檔覆蓋此檔. 此預設首頁檔有一些值得參考的資訊, 它提到 Apache2 的設定檔放在 /etc/apache2 目錄下, 完整的說明文件則放在下列壓縮檔中 :

/usr/share/doc/apache2/README.Debian.gz

Debian 的 Apache2 設定與一般不同, 分散在不同的設定檔裡面. 例如 , 其中 apache2.conf 是最主要的設定檔; 設定檔 ports.conf 用來確定連線要求之傾聽埠. 而 mods-enabled, conf-enabled 與 sites-enabled 目錄則是用來管理 modules, global configuration, 以及 virtual host 等相關設定.

Apache 伺服器的執行檔放在 /usr/bin/apache2, 但是因為使用環境變數的緣故, 直接呼叫 apache2 來啟動與停止 Apache 是沒有用的, 必須透過 /etc/init.d/apache2 或 /etc/init.d/apache2ctl 才行.

Apache2 的預設網頁根目錄為 /var/www/html (書上寫的是 /var/www, 我放在這底下無效, 可能那是舊版用法), 預設情況下 Debian 只允許透過瀏覽器存取 /var/www/html (網頁程式) 與 /usr/share (網頁應用) 這兩個目錄. 如果要將網頁根目錄放在別處, 必須在 /etc/apache2/apache2.conf 設定檔中將此目錄加進白名單裡.

我用 nano 寫了一個呼叫 phpino() 的程式 phpinfo.php, 放在 /var/www/html 下 :

pi@raspberrypi:~ $ sudo nano /var/www/html/phpinfo.php
pi@raspberrypi:~ $ cat /var/www/html/phpinfo.php
phpinfo();
?>
pi@raspberrypi:~ $

然後在筆電瀏覽器以 headless 方式連線 192.168.43.26/phpinfo.php 結果可順利執行 PHP 程式 :


這樣就完成 Apache + PHP 的安裝了.

值得一提的是, 第一步更新套件列表與升級已安裝的套件會花比較久時間 (大概 1 小時), 而且升級到 lightdm 時會詢問是否要更新 distribution 版本, 只要按 Enter (預設=N) 繼續即可, 但是我卻按了 D 去看看有啥不同, 結果進入顯示頁面就出不來了 :

........
Setting up libservlet2.5-java (6.0.45+dfsg-1~deb8u1) ...
Setting up libsrtp0 (1.4.5~20130609~dfsg-1.1+deb8u1) ...
Setting up libwayland-cursor0:armhf (1.11.0-2) ...
Setting up libxfont1:armhf (1:1.5.2-1) ...
Setting up lightdm (1.10.3-3+rpi) ...

Configuration file '/etc/lightdm/lightdm.conf'
 ==> Modified (by you or by a script) since installation.
 ==> Package distributor has shipped an updated version.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** lightdm.conf (Y/I/N/O/D/Z) [default=N] ? D
~
~
~
~
~
~
(END)    (在這邊停住無法繼續, 不知是按 Ctrl+D 還是 Ctrl+Z 才跳出來)
[1]+  Stopped                 sudo apt-get upgrade
pi@raspberrypi:~ $

跳出來後想說就安裝 Apache 吧! 結果卻說 dpkg 更新無法繼續, 好像被某程序咬住. 我想可能是 upgrade 被我用 Ctrl+D (?) 中斷之故, 所以在 lightdm 更新時若輸入 N 應該就不會這樣了. 無奈之下只好下 sudo poweroff 關機重開, 再次下 sudo apt-get upgrade, 但是回應說因為 upgrade 被中斷, 要改用 sudo dpkg --configure -a 指令去修正, 以下是 upgrade 的修正過程以及 Apache + PHP 的安裝紀錄 :

=========關機重新登入=========
login as: pi
pi@192.168.2.111's password:

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Jan 24 11:44:37 2017
pi@raspberrypi:~ $ sudo apt-get upgrade
E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to cor
pi@raspberrypi:~ $ sudo dpkg --configure -a
Setting up python3-gpiozero (1.3.1) ...
Setting up bluej (3.1.7b) ...
......
......
......
update-alternatives: using /usr/bin/lxterminal to provide /usr/bin/x-terminal-em                                                      ulator (x-terminal-emulator) in auto mode
Setting up lightdm (1.10.3-3+rpi) ...

Configuration file '/etc/lightdm/lightdm.conf'
 ==> Modified (by you or by a script) since installation.
 ==> Package distributor has shipped an updated version.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** lightdm.conf (Y/I/N/O/D/Z) [default=N] ? N    (要輸入 N)
Setting up python-gpiozero (1.3.1) ...
Setting up python-picamera (1.12) ...
Setting up raspi-gpio (0.20170105) ...
Processing triggers for dbus (1.8.22-0+deb8u1) ...
Setting up ruby2.1 (2.1.5-2+deb8u3) ...
Setting up pi-package-session (0.2) ...
Setting up x11-common (1:7.7+16) ...
Installing new version of config file /etc/init.d/x11-common ...
update-rc.d: warning: start and stop actions are no longer supported; falling ba                                                      ck to defaults
Setting up ruby (1:2.1.5+deb8u2) ...
Processing triggers for ca-certificates (20141019+deb8u2) ...
Updating certificates in /etc/ssl/certs... 10 added, 10 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
Setting up raspberrypi-net-mods (1.2.5) ...
Modified /etc/network/interfaces detected. Leaving unchanged and writing new fil                                                      e as interfaces.new.
Setting up lxkeymap (0.8.0~bzr25-1+rpi3) ...
Setting up python-pil:armhf (2.6.1-2+deb8u3) ...
Setting up python-rpi.gpio (0.6.3~jessie-1) ...
Setting up pi-package (0.2) ...
Setting up pipanel (20161206~130650) ...
Setting up xserver-common (2:1.18.4-2+rpi1) ...
Setting up xserver-xorg-core (2:1.18.4-2+rpi1) ...
Setting up rc-gui (1.5) ...
Setting up python-sense-hat (2.2.0-1) ...
Setting up xserver-xorg-video-fbturbo (1.20161111~122359) ...
Setting up xserver-xorg-input-synaptics (1.8.3-2) ...
Setting up xserver-xorg-input-evdev (1:2.10.3-1) ...
Setting up xserver-xorg-video-fbdev (1:0.4.4-1+rpi2) ...
Setting up xserver-xorg (1:7.7+16) ...
Processing triggers for systemd (215-17+deb8u6) ...
pi@raspberrypi:~ $ sudo apt-get install apache2     (安裝 Apache)
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libasn1-8-heimdal libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal
  libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal libxfce4ui-1-0 xfce-keyboard-shortcuts
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.1-0 ssl-cert
Suggested packages:
  apache2-doc apache2-suexec-pristine apache2-suexec-custom openssl-blacklist
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.1-0 ssl-cert
0 upgraded, 10 newly installed, 0 to remove and 17 not upgraded.
Need to get 1,750 kB of archives.
After this operation, 5,241 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libapr1 armhf 1.5.1-3 [77.1 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libaprutil1 armhf 1.5.4-1 [75.9 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main libaprutil1-dbd-sqlite3 armhf 1.5.4-1 [17.7 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main libaprutil1-ldap armhf 1.5.4-1 [16.7 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main liblua5.1-0 armhf 5.1.5-7.1 [83.7 kB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ jessie/main apache2-bin armhf 2.4.10-10+deb8u7 [893 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ jessie/main apache2-utils armhf 2.4.10-10+deb8u7 [194 kB]
Get:8 http://mirrordirector.raspbian.org/raspbian/ jessie/main apache2-data all 2.4.10-10+deb8u7 [163 kB]
Get:9 http://mirrordirector.raspbian.org/raspbian/ jessie/main apache2 armhf 2.4.10-10+deb8u7 [207 kB]
Get:10 http://mirrordirector.raspbian.org/raspbian/ jessie/main ssl-cert all 1.0.35 [20.9 kB]
Fetched 1,750 kB in 6s (269 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libapr1:armhf.
(Reading database ... 127681 files and directories currently installed.)
Preparing to unpack .../libapr1_1.5.1-3_armhf.deb ...
Unpacking libapr1:armhf (1.5.1-3) ...
Selecting previously unselected package libaprutil1:armhf.
Preparing to unpack .../libaprutil1_1.5.4-1_armhf.deb ...
Unpacking libaprutil1:armhf (1.5.4-1) ...
Selecting previously unselected package libaprutil1-dbd-sqlite3:armhf.
Preparing to unpack .../libaprutil1-dbd-sqlite3_1.5.4-1_armhf.deb ...
Unpacking libaprutil1-dbd-sqlite3:armhf (1.5.4-1) ...
Selecting previously unselected package libaprutil1-ldap:armhf.
Preparing to unpack .../libaprutil1-ldap_1.5.4-1_armhf.deb ...
Unpacking libaprutil1-ldap:armhf (1.5.4-1) ...
Selecting previously unselected package liblua5.1-0:armhf.
Preparing to unpack .../liblua5.1-0_5.1.5-7.1_armhf.deb ...
Unpacking liblua5.1-0:armhf (5.1.5-7.1) ...
Selecting previously unselected package apache2-bin.
Preparing to unpack .../apache2-bin_2.4.10-10+deb8u7_armhf.deb ...
Unpacking apache2-bin (2.4.10-10+deb8u7) ...
Selecting previously unselected package apache2-utils.
Preparing to unpack .../apache2-utils_2.4.10-10+deb8u7_armhf.deb ...
Unpacking apache2-utils (2.4.10-10+deb8u7) ...
Selecting previously unselected package apache2-data.
Preparing to unpack .../apache2-data_2.4.10-10+deb8u7_all.deb ...
Unpacking apache2-data (2.4.10-10+deb8u7) ...
Selecting previously unselected package apache2.
Preparing to unpack .../apache2_2.4.10-10+deb8u7_armhf.deb ...
Unpacking apache2 (2.4.10-10+deb8u7) ...
Selecting previously unselected package ssl-cert.
Preparing to unpack .../ssl-cert_1.0.35_all.deb ...
Unpacking ssl-cert (1.0.35) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for systemd (215-17+deb8u6) ...
Setting up libapr1:armhf (1.5.1-3) ...
Setting up libaprutil1:armhf (1.5.4-1) ...
Setting up libaprutil1-dbd-sqlite3:armhf (1.5.4-1) ...
Setting up libaprutil1-ldap:armhf (1.5.4-1) ...
Setting up liblua5.1-0:armhf (5.1.5-7.1) ...
Setting up apache2-bin (2.4.10-10+deb8u7) ...
Setting up apache2-utils (2.4.10-10+deb8u7) ...
Setting up apache2-data (2.4.10-10+deb8u7) ...
Setting up apache2 (2.4.10-10+deb8u7) ...
Enabling module mpm_event.
Enabling module authz_core.
Enabling module authz_host.
Enabling module authn_core.
Enabling module auth_basic.
Enabling module access_compat.
Enabling module authn_file.
Enabling module authz_user.
Enabling module alias.
Enabling module dir.
Enabling module autoindex.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module filter.
Enabling module deflate.
Enabling module status.
Enabling conf charset.
Enabling conf localized-error-pages.
Enabling conf other-vhosts-access-log.
Enabling conf security.
Enabling conf serve-cgi-bin.
Enabling site 000-default.
Setting up ssl-cert (1.0.35) ...
Processing triggers for libc-bin (2.19-18+deb8u7) ...
Processing triggers for systemd (215-17+deb8u6) ...
pi@raspberrypi:~ $ sudo apt-get install php5 libapache2-mod-php5  (安裝 PHP5)
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libasn1-8-heimdal libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal
  libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal libxfce4ui-1-0 xfce-keyboard-shortcuts
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  libonig2 libperl4-corelibs-perl libqdbm14 lsof php5-cli php5-common php5-json php5-readline
Suggested packages:
  php-pear php5-user-cache
The following NEW packages will be installed:
  libapache2-mod-php5 libonig2 libperl4-corelibs-perl libqdbm14 lsof php5 php5-cli php5-common php5-json php5-readline
0 upgraded, 10 newly installed, 0 to remove and 17 not upgraded.
Need to get 5,085 kB of archives.
After this operation, 18.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y    (繼續安裝)
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libonig2 armhf 5.9.5-3.2 [101 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libperl4-corelibs-perl all 0.003-1 [43.6 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main lsof armhf 4.86+dfsg-1 [321 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main libqdbm14 armhf 1.8.78-5+b1 [86.0 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main php5-common armhf 5.6.29+dfsg-0+deb8u1 [720 kB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ jessie/main php5-json armhf 1.3.6-1 [16.9 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ jessie/main php5-cli armhf 5.6.29+dfsg-0+deb8u1 [1,904 kB]
Get:8 http://mirrordirector.raspbian.org/raspbian/ jessie/main php5 all 5.6.29+dfsg-0+deb8u1 [1,312 B]
Get:9 http://mirrordirector.raspbian.org/raspbian/ jessie/main libapache2-mod-php5 armhf 5.6.29+dfsg-0+deb8u1 [1,879 kB]
Get:10 http://mirrordirector.raspbian.org/raspbian/ jessie/main php5-readline armhf 5.6.29+dfsg-0+deb8u1 [11.2 kB]
Fetched 5,085 kB in 10s (486 kB/s)
Selecting previously unselected package libonig2:armhf.
(Reading database ... 128369 files and directories currently installed.)
Preparing to unpack .../libonig2_5.9.5-3.2_armhf.deb ...
Unpacking libonig2:armhf (5.9.5-3.2) ...
Selecting previously unselected package libperl4-corelibs-perl.
Preparing to unpack .../libperl4-corelibs-perl_0.003-1_all.deb ...
Unpacking libperl4-corelibs-perl (0.003-1) ...
Selecting previously unselected package lsof.
Preparing to unpack .../lsof_4.86+dfsg-1_armhf.deb ...
Unpacking lsof (4.86+dfsg-1) ...
Selecting previously unselected package libqdbm14.
Preparing to unpack .../libqdbm14_1.8.78-5+b1_armhf.deb ...
Unpacking libqdbm14 (1.8.78-5+b1) ...
Selecting previously unselected package php5-common.
Preparing to unpack .../php5-common_5.6.29+dfsg-0+deb8u1_armhf.deb ...
Unpacking php5-common (5.6.29+dfsg-0+deb8u1) ...
Selecting previously unselected package php5-json.
Preparing to unpack .../php5-json_1.3.6-1_armhf.deb ...
Unpacking php5-json (1.3.6-1) ...
Selecting previously unselected package php5-cli.
Preparing to unpack .../php5-cli_5.6.29+dfsg-0+deb8u1_armhf.deb ...
Unpacking php5-cli (5.6.29+dfsg-0+deb8u1) ...
Selecting previously unselected package libapache2-mod-php5.
Preparing to unpack .../libapache2-mod-php5_5.6.29+dfsg-0+deb8u1_armhf.deb ...
Unpacking libapache2-mod-php5 (5.6.29+dfsg-0+deb8u1) ...
Selecting previously unselected package php5.
Preparing to unpack .../php5_5.6.29+dfsg-0+deb8u1_all.deb ...
Unpacking php5 (5.6.29+dfsg-0+deb8u1) ...
Selecting previously unselected package php5-readline.
Preparing to unpack .../php5-readline_5.6.29+dfsg-0+deb8u1_armhf.deb ...
Unpacking php5-readline (5.6.29+dfsg-0+deb8u1) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up libonig2:armhf (5.9.5-3.2) ...
Setting up libperl4-corelibs-perl (0.003-1) ...
Setting up lsof (4.86+dfsg-1) ...
Setting up libqdbm14 (1.8.78-5+b1) ...
Setting up php5-common (5.6.29+dfsg-0+deb8u1) ...

Creating config file /etc/php5/mods-available/pdo.ini with new version
php5_invoke: Enable module pdo for cli SAPI
php5_invoke: Enable module pdo for apache2 SAPI

Creating config file /etc/php5/mods-available/opcache.ini with new version
php5_invoke: Enable module opcache for cli SAPI
php5_invoke: Enable module opcache for apache2 SAPI
Setting up php5-json (1.3.6-1) ...
php5_invoke: Enable module json for cli SAPI
php5_invoke: Enable module json for apache2 SAPI
Setting up php5-cli (5.6.29+dfsg-0+deb8u1) ...
update-alternatives: using /usr/bin/php5 to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar5 to provide /usr/bin/phar (phar) in auto mode

Creating config file /etc/php5/cli/php.ini with new version
Setting up libapache2-mod-php5 (5.6.29+dfsg-0+deb8u1) ...

Creating config file /etc/php5/apache2/php.ini with new version
Module mpm_event disabled.
Enabling module mpm_prefork.
apache2_switch_mpm Switch to prefork
apache2_invoke: Enable module php5
Setting up php5 (5.6.29+dfsg-0+deb8u1) ...
Setting up php5-readline (5.6.29+dfsg-0+deb8u1) ...

Creating config file /etc/php5/mods-available/readline.ini with new version
php5_invoke: Enable module readline for cli SAPI
php5_invoke: Enable module readline for apache2 SAPI
Processing triggers for libc-bin (2.19-18+deb8u7) ...
Processing triggers for libapache2-mod-php5 (5.6.29+dfsg-0+deb8u1) ...
pi@raspberrypi:~ $ sudo service apache2 restart  (重新啟動 Apache)

這樣 PHP 網站伺服器就架起來了. 注意, PHP 的設定檔位置在 :

/etc/php5/apache2/php.ini

接下來是安裝 MySQL 資料庫 :

5. 安裝 MySQL 資料庫 :

首先是安裝與 MySQL 連線的相關模組, 其中 php5-mysql 提供 PHP 與 MySQL 連結之函數 :

$ sudo apt-get install mysql-server mysql-client php5-mysql  (安裝 MySQL)

在安裝途中 (出現 preconfiguring package 的地方) 會要求設定 MySQL 管理員 ( root 帳號) 的密碼, 我通常設為 mysql, 輸入完按 tab 鍵就會移到 OK 按鈕, 按 Enter 即可 :



然後是安裝  PHP5 安全性模組 :

$ sudo apt-get install php5-mcrypt  (安裝  PHP5 安全性模組)

最後是下載安裝 phpMyAdmin 程式來管理 MySQL 資料庫 :

6. 安裝 MySQL 管理程式 phpMyAdmin :

https://www.phpmyadmin.net/downloads/

目前最新版是 4.6.6 版, 滑鼠移到要下載的 phpMyAdmin-4.6.6-all-languages.tar.bz2 上按右鍵, 選擇 "複製連結網址" :


然後將此網址放在 wget 指令後面進行下載 :

$ wget https://files.phpmyadmin.net/phpMyAdmin/4.6.6/phpMyAdmin-4.6.6-all-languages.tar.bz2   (從 myPHPadmin 網站下載最新版)

完成後就用 tar 指令進行解壓縮, 這會在 /home/pi 下面產生 phpMyAdmin-4.6.6-all-languages 目錄來存放 phpMyAdmin 程式 :

$ tar -jxf phpMyAdmin-4.6.6-all-languages.tar.bz2  (解壓縮下載檔)

由於資料夾名稱太長了, 用 mv 指令改為較短的 mysql, 然後搬移到網頁根目錄 /var/www/html 下面 :

$ mv phpMyAdmin-4.6.6-all-languages mysql  (將解壓縮後目錄改成 mysql)
$ sudo mv mysql /var/www/html  (將 phpMyAdmin 目錄 mysql 搬移到網頁根目錄下)

這樣就完成了 phpMyAdmin 的安裝了, 但是如果這時馬上瀏覽 192.168.2.111/mysql 將顯示 "缺少 mysqli 擴充套件" 的錯誤 :


其實只要重新啟動 Apache 伺服器即可 :

sudo service apache2 restart

或者 :

pi@raspberrypi:~ $ sudo /etc/init.d/apache2 restart
[ ok ] Restarting apache2 (via systemctl): apache2.service.
pi@raspberrypi:~ $

然後重新整理 phpMyAdmin 網頁就會正常顯示 MySQL 登入頁面了 :

 手動新增資料庫時, 對於中文系統應該將伺服器連線與編碼 (collation) 設為 utf8-unicode_ci :


參考 :

# Extension mysqli is missing, phpmyadmin doesn't work
# Ubuntu系統-phpmyadmin 缺少 mysqli 擴充套件。請檢查 PHP 設定。 請參考 our 文件 以取得更多的資訊。
# 在 Ubuntu 上安裝 LAMP (Apache, PHP, MySQL)

2017-01-25 補充 :

昨天架好網站後就迫不及待將我的 PHP 專案壓縮成 zip 檔, 在 Win10 筆電中透過 pscp.exe 程式將 EasyUICMS 專案壓縮檔傳送到樹莓派的網頁根目錄 /var/www/html/ 下, 解壓縮並調整好 db.php 為 localhost.php 後, 果然能正常依照 install.php 檔建立起整個網站.



但是當我要上傳應用程式時卻發現傳檔失敗, 原因是檔案 "移動失敗" :


我檢視了 sys.php 中關於檔案上傳的原始碼以及 /etc/php5/apache2/php.ini 檔, 並未發現有需要調整的地方. 用 Google 找了好久還是無線索. 後來想到既然是移動失敗, 上傳應該沒問題, 只是要從系統預設暫存區移到 apps 目錄時失敗而已, 那是否跟目錄的寫入權限有關呢? 我檢查了專案目錄權限, 發現 apps 與 files 這兩個會儲存上傳檔案的目錄在 group 與 others 部分只有讀取權限 (r-x), 亦即只有 755 權限 :

pi@raspberrypi:~ $ ls -l /var/www/html/twstockbot
total 2332
drwxr-xr-x 2 pi pi    4096 Nov 28 21:45 apps
drwxr-xr-x 2 pi pi    4096 Nov 28 21:45 cron
drwxr-xr-x 2 pi pi    4096 Nov 28 21:45 data
drwxr-xr-x 2 pi pi    4096 Nov 28 21:45 db
-rw-r--r-- 1 pi pi     141 Jan 25 11:10 db.php
drwxr-xr-x 2 pi pi    4096 Jan 25 11:33 files

應該改為 rw- (766) :

pi@raspberrypi:~ $ sudo chmod 766 /var/www/html/twstockbot/apps
pi@raspberrypi:~ $ sudo chmod 766 /var/www/html/twstockbot/files
pi@raspberrypi:~ $ ls -l /var/www/html/tony1966
total 2332
drwxrw-rw- 2 pi pi    4096 Nov 28 21:45 apps
drwxr-xr-x 2 pi pi    4096 Nov 28 21:45 cron
drwxr-xr-x 2 pi pi    4096 Nov 28 21:45 data
drwxr-xr-x 2 pi pi    4096 Nov 28 21:45 db
-rw-r--r-- 1 pi pi     141 Jan 25 11:10 db.php
drwxrw-rw- 2 pi pi    4096 Jan 25 11:33 files

但是, 這樣還是檔案移動失敗, 難道權限還要放寬到 777 嗎? 只好試試看 :

pi@raspberrypi:~ $ sudo chmod 777 /var/www/html/twstockbot/apps
pi@raspberrypi:~ $ sudo chmod 777 /var/www/html/twstockbot/files
pi@raspberrypi:~ $ ls -l /var/www/html/tony1966
total 2332
drwxrwxrwx 2 root root    4096 Jan 25 23:37 apps
drwxr-xr-x 2 root root    4096 Nov 28 21:45 cron
drwxr-xr-x 2 root root    4096 Nov 28 21:45 data
drwxr-xr-x 2 root root    4096 Nov 28 21:45 db
-rw-r--r-- 1 root root     143 Jan 25 23:34 db.php
drwxrwxrwx 2 root root    4096 Jan 25 23:25 files

哈哈哈! 改成 777 後就可以順利上傳了. 我在下列文章讀到上傳目錄最適合的權限是 755, 但我試過改成 755 也是不行 :

# What are the proper permissions for an upload folder with PHP/Apache?

此問題暫且 hold 住, 以後再研究, 反正 777 能用就行了.

參考 :

PHP檔案上傳
PHP 檔案上傳、多檔案上傳
# 第五章、Linux 的檔案權限與目錄配置
# php 檔案上傳error code [handling file upload error code]
# Uploading big files > 512MB (as set by default)

5 則留言 :

qq 提到...

我照著書做真有有些地方因為版本不同要改,謝謝你

輕鬆一下, 你呢? 提到...

Hello, Tony

現在 PHP 已經前進至 7.0 版,在下裝 Apache, PHP, Mysql 在樹莓派 3 可以成功, 可唯一裝 phpmyadmin 出現
Error, Error 訊息: "The mysqli extension is missing" 始終存在. 就跟本文中一模一樣, 重啟也沒用. 在下參考 stackoverflow 方法(https://stackoverflow.com/questions/43731317/how-to-debug-error-the-mysqli-extension-is-missing-please-check-your-php-confi), 去修改 php.ini, 重啟;或是本文中方法,把 phpmyadmin 放在 /var/www/html 下重啟, 結果依舊,十分傷心. 請問, 有什麼 idea ?? Thk!

小狐狸事務所 提到...

陳兄您好, 我目前還是在用 php5 呢! 還沒進化到 php7. 關於 mysqli 擴充套件找不到問題或許可參考下列 :

https://stackoverflow.com/questions/44611252/phpmyadmin-error-the-mysqli-extension-is-missing

xun 提到...

參考看看
https://www.mynote.idv.tw/my_note/22-raspberry-2-host-php5-update-to-php-7.html

小狐狸事務所 提到...

感謝您! 我試試看.