Raspberry Pi 3 CentOS 7 ARM Apache + MariaDB + PHP + phpMyAdmin - MIS 腳印
MIS 腳印 logo

MIS 腳印

記錄 IT 學習的軌跡

Raspberry Pi 3 CentOS 7 ARM Apache + MariaDB + PHP + phpMyAdmin

在「樹莓派 3」使用 CentOS 7 ARM 實作 Apache、MariaDB 與 PHP(簡稱 LAMP)來架設 Web Server 的環境,並使用 phpMyAdmin 這套資料庫管理工具,搭配 MariaDB 來管理資料庫。

Raspberry Pi

Apache 網站伺服器

安裝

安裝 Apache(就是 httpd):

yum install httpd

啟動 Apache 服務,並設定開機時啟動:

systemctl start httpd
systemctl enable httpd

目錄權限設定

預設網站根目錄的擁有者與群組為 root;目錄權限為 755:

ls -ald /var/www/html/
drwxr-xr-x. 2 root root 4096  3月 30 19:49 /var/www/html/

設定擁有者與群組為 apache;目錄權限為 2755(2 表示新建子目錄的權限同父目錄):

chown apache:apache /var/www/html/
chmod 2755 /var/www/html/

確認設定是否正確:

ls -ald /var/www/html/
drwxr-sr-x. 2 apache apache 4096  3月 30 19:49 /var/www/html/

設定檔

Apache 的設定檔:

vi /etc/httpd/conf/httpd.conf
# 網路介面監聽的埠口預設都為 80
Listen 80
# 系統管理員的 EMail,當網站出現問題時自動回報
ServerAdmin root@localhost
# 主機名稱,如無另外指定,預設以 hostname 輸出
#ServerName www.example.com:80
# 網站預設根目錄
DocumentRoot "/var/www/html"

重啟動 Apache 服務:

systemctl restart httpd

重導向所有請求到單一檔案

許多現代的 Web Server 都使用的 RESTful,就是把所有請求重導到 index.php,來統一解析使用者要什麼。 首先我們要啟用 Apache 的 .htaccess 設定檔:

vi /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
    ... 以上省略 ...
    # 由於安全性等的考慮預設是 None,改為 All
    AllowOverride All
    ... 以下省略 ...
</Directory>

重啟動 Apache 服務:

systemctl restart httpd

使用 .htaccess 重寫規則來重新導向到 index.php:

cd /var/www/html/
vi .htaccess
# 把所有的請求重導到 index.php,然後解析 URL 以找出使用者想要什麼
<IfModule mod_rewrite.c>
    # 將轉址功能打開
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

PHP 網頁程式

安裝

安裝 PHP、PDO:

yum install php php-pdo

設定檔

自 PHP 5 開始,時區(date.timezone)一開始是未設置所以會以標準時區也就是 GMT+0,所以在使用 date 相關的函式的時候都會少了八個小時,因此必須修改 php.ini 的設定:

vi /etc/php.ini
# 預設為註解
date.timezone = Asia/Taipei

重啟動 Apache 服務:

systemctl restart httpd

Xdebug

這套 Debug 工具可以用來追蹤和分析 PHP 程式的運作狀況,而且會將錯誤訊息與輸出格式如 var_dump() 格式化輸出至 Browser。

安裝

安裝相關套件:

yum install php-devel php-pear gcc gcc-c++ make

PHP version < 5.5.0 必須安裝舊版本,因此必須指定版本號(目前所用 PHP version 5.4.16):

pecl install xdebug-2.2.7

Apache 網站根目錄權限可能會被更動,如有的話須改回來:

ls -ald /var/www/html/
drwxr-xr-x. 3 root root 4096  6月 10  2014 /var/www/html/
 
chown apache:apache /var/www/html
chmod 2755 /var/www/html

設定檔

指定 zend_extension 路徑:

vi /etc/php.d/xdebug.ini
zend_extension=/usr/lib/php//modules/xdebug.so

重啟動 Apache 服務:

systemctl restart httpd

測試

撰寫一個程式來查看 Xdebug 的格式化輸出:

vi /var/www/html/xdebug.php
<?php
// PHP 程式有問題時,將錯誤、警告訊息顯示於網頁
ini_set('display_errors', 1);

$arr = [1, 2, 3];

// 格式化輸出
var_dump($arr);
// 故意少寫一個 v,查看格式化輸出的錯誤訊息
var_dump($arr);

開啟 Bowser 連結至 xdebug.php 檔,格式化輸出的訊息更方便 Debug。

MariaDB 資料庫

安裝

安裝相關軟體:

yum install mariadb mariadb-server

初始化設定

須先啟用服務,才可進行初始化:

systemctl start mariadb

進行初始化設定:

mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):  # 輸入目前 MariaDB 的 root 密碼(第一次設定應該是空的,所以直接「ENTER」即可)
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]    # 直接「ENTER」
New password:               # 設定新的密碼
Re-enter new password:      # 再次確認密碼
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]  # 是否要移除 anonymous user 的資料,直接「ENTER」
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]  # 是否只允許讓 root 從 localhost 登入,無法從其他的網路登入,直接「ENTER」
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]  # 是否移除 test 的資料庫,直接「ENTER」
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]  # 是否要重新載入權限的 table 資訊,直接「ENTER」
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

服務設置

開機自動啟用服務:

systemctl enable mariadb

phpMyAdmin 資料庫工具

安裝

安裝相關軟體:

yum install php-mysql wget

目前 yum 套件資料庫中沒有 phpMyAdmin 的安裝資訊,因此須手動安裝。先至 phpMyAdmin 官網 > Download。

查詢 PHP 安裝版本為 5.4.16,符合 phpMyAdmin 的版本為 4.0.10.20,複製連結:

php -v
PHP 5.4.16 (cli) (built: Nov  8 2016 12:00:41)
... 以下省略 ...
  1. 下載 phpMyAdmin 壓縮檔(剛才複製的網址)
  2. 解壓縮
  3. 刪除壓縮檔
  4. 搬移解壓縮檔案至 Web Server 根目錄並更名
wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz
tar -zxvf phpMyAdmin-4.0.10.20-all-languages.tar.gz
\rm phpMyAdmin-4.0.10.20-all-languages.tar.gz
mv phpMyAdmin-4.0.10.20-all-languages /var/www/html/phpMyAdmin

設定

  1. 切換至 Web Server 根目錄
  2. 複製範本設定檔並重新命名
  3. 編輯設定檔並修改登入方式
cd /var/www/html
cp phpMyAdmin/config.sample.inc.php phpMyAdmin/config.inc.php
vi phpMyAdmin/config.inc.php
$cfg['Servers'][$i]['auth_type'] = 'http';

重啟 Apache:

systemctl restart httpd

測試

打開瀏覽器輸入網址 http://192.168.1.1/phpMyAdmin,會彈出需驗證的視窗 > 輸入使用者名稱、密碼,登入即可。

參考

在〈Raspberry Pi 3 CentOS 7 ARM Apache + MariaDB + PHP + phpMyAdmin〉中有 1 則留言

發表迴響