2019年12月18日 星期三

樹莓派安裝 Apache2+MySQL+PHP7

上個月曾在樹莓派再次安裝 Apache+MySQL+PHP 卻失敗, 那時主要在忙 Python 的 Django 框架測試, 沒多餘時間去查原因, 只留下測試紀錄存查後便暫停下來, 參考 :

樹莓派安裝 Apache+MySQL+PHP 出現相依錯誤

今天在網路上看到一篇新做法, 就動手試試, 結果順利安裝了 PHP7 :

神佑のBlog : 樹梅派Raspberry Pi 架設網頁伺服器 Apache2 & PHP7 & MySQL & phpMyAdmin & vsftpd

有可能上次安裝所參考的文章使用之安裝指令已經太舊的關係.

這次我使用 PuTTY 遠端連線下指令以及 VNC Viewer 連線樹莓派桌面操作瀏覽器, 依照 "神佑のBlog" 的程序安裝之過程簡單記錄如下 :


1. 更新系統 : 

pi@raspberrypi:~ $ sudo apt-get update 


2. 安裝 Apache2 伺服器 :

pi@raspberrypi:~ $ sudo apt-get install apache2 

安裝過程會出現下列 action "start" failed 訊息沒有關係 :

invoke-rc.d: initscript apache2, action "start" failed.
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2019-12-18 19:31:24 CST; 35ms ago
  Process: 21023 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE)


3. 設定 Apache2 文件的權限

網頁放置路徑在 /var/www/html/ 底下, 以下列指令更改權限 :

pi@raspberrypi:~ $ sudo chown -R pi:www-data /var/www/html/ 
pi@raspberrypi:~ $ sudo chmod -R 770 /var/www/html/ 

770 意思是只有 owner 與 group 內成員擁有全部權限, 其餘用戶無任何權限. 改這個要重開機後才會生效.


4. 測試 Apache2 伺服器 : 

開啟樹莓派瀏覽器, 在網址列輸入 127.0.0.1 若顯示下列網頁表示 Apache2 網頁伺服器安裝 OK :




如果樹莓派以有其它網頁伺服器先佔用了 80 埠, 則要將 Apache2 的監聽埠從預設的 80 改為其他埠, 例如 81 或 8080 等, 這在 Rasbian 作業系統要編輯 /etc/apache2/ports.conf 這個檔 :

pi@raspberrypi:~ $ nano /etc/apache2/ports.conf

將其中的 Listen 80 改為 Listen 8080 即可 :

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8080

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet




然後重啟 Apache2 伺服器即可 :

pi@raspberrypi:~ $ sudo /etc/init.d/apache2 restart

這樣就不會跟別的網頁伺服器沖到了. Apache2 伺服器常用操作指令如下 :


 Apache2 操作 指令
 關閉伺服器 sudo /etc/init.d/apache2 stop
 啟動伺服器 sudo /etc/init.d/apache2 start
 重啟伺服器 sudo /etc/init.d/apache2 restart


參考 :

Ubuntu下 啟動/重啟/停止 apache
How to check whether a Webserver is Installed in Pi or not?
https://www.tecmint.com/change-apache-port-in-linux/
Apache2的port怎麼改呢?


5. 安裝 PHP7 : 

pi@raspberrypi:~ $ sudo apt install php php-mbstring

安裝程序應該不會出現任何錯誤訊息. 完畢後刪除網頁路徑 /var/www/html/ 下的預設首頁 index.html, 改用呼叫 phpinfo() 顯示 PHP 環境訊息的首頁 index.php 來取代 :

pi@raspberrypi:~ $ sudo rm /var/www/html/index.html
pi@raspberrypi:~ $ echo "<?php phpinfo ();?>" > /var/www/html/index.php

這時在樹莓派瀏覽器重新整理 127.0.0.1 網頁, 若顯示下列網頁表示 PHP7 引擎安裝 OK :




6. 安裝 MySQL (MariaDB) :

安裝 MySQL 會占用 175MB 空間, 過程會出現幾個選項或確認視窗, 還會要求設定密碼, 這些密碼必須記錄下來, 管理資料庫時會用到 :

pi@raspberrypi:~ $ sudo apt-get install mysql-server php-mysql 

安裝完成後須用下列指令進行安全設定, 包括設定 root 密碼, 刪除匿名使用者帳號, 禁止遠端 root 登入, 刪除測試資料庫等 :

pi@raspberrypi:~ $ sudo 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):    (直接按 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] Y
New password:   (輸入密碼例如 mysql)
Re-enter new password:   (確認密碼例如 mysql)
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] Y
 ... 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] Y
 ... 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] Y
 - 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] Y
 ... 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!

接著用下列指令進入 MySQL 的 Shell 確認 :

pi@raspberrypi:~ $ sudo mysql -u root -p
Enter password:  (輸入 root 密碼例如 mysql)
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.1.38-MariaDB-0+deb9u1 Raspbian 9.0

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> Ctrl-C -- exit!   (按 Ctrl+C 跳出 Shell)

這樣 MySQL 就安裝完成了.

MySQL Shell 指令參考 :

MySQL 語法匯整


7. 安裝 phpMyAdmin (MySQL 管理網站) :

先進入 MySQL Shell 新增一個 phpMyAdmin 網站之使用者帳號密碼 (這是網站的管理密碼, 與上面 MySQL 的 Shell 登入密碼不同, 但測試中為了避免帳密太多搞混, 此處設為相同的 mysql) :

pi@raspberrypi:~ $ sudo mysql -u root -p
Enter password:    (輸入 root 密碼例如 mysql)
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.1.38-MariaDB-0+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> GRANT ALL PRIVILEGES ON mydb.* TO 'pi'@'localhost' IDENTIFIED BY 'mysql'
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> Ctrl-C -- exit!   (按 Ctrl+C 跳出 Shell)

然後開始安裝 phpMyAdmin (會占用 50MB 空間) :

pi@raspberrypi:~ $ sudo apt-get install phpmyadmin   

首先會跳出一個視窗, 要求選擇以後執行 phpMyAdmin 時要使用的網頁伺服器, 請在 Apache2 那一項按 SPACE 鍵選定 (前面會出現 *), 然後按 ENTER 鍵確定後繼續執行安裝 :




按 "是" :



輸入 phpMyAdmin 網站管理密碼 (例如 mysql) :



輸入相同密碼 (例如 mysql) 按確認 :




等它跑完即完成 phpMyAdmin 安裝了.


8. 更改 Apache2 的 phpMyAdmin 路徑 :

開啟 Apache2 設定檔 apache2.conf :

pi@raspberrypi:~ $ sudo nano /etc/apache2/apache2.conf 

然後在最後一行貼上 Include /etc/phpmyadmin/apache.conf :




按 Ctrl+O 存檔跳出, 輸入下列指令重啟 Apache2 伺服器 :

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

這樣就完成 phpMyAdmin 安裝與設定了.

在瀏覽器網址列輸入 http://127.0.0.1/phpmyadmin, 輸入上面建立的帳號 pi 與密碼 mysql 即可登入管理網站 :





Bingo! 大功告成! 感謝 "神佑のBlog" 的指引.

參考 :

樹莓派常用的 Linux 指令

2019-12-19 補充 :

昨天先後在高雄與鄉下的 Pi 3 進行安裝時考慮不周, phpMyAdmin 管理密碼設定不一致, 想與 root 密碼一致設為 "mysql" 避免混淆, 方法是在 Terminal 登入 MySQL Shell, 然後輸入如下設定指令 :

SET PASSWORD FOR 'pi'@'localhost' = PASSWORD('mysql');

pi@raspberrypi:~ $ sudo mysql -u root -p   
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 43
Server version: 10.1.38-MariaDB-0+deb9u1 Raspbian 9.0

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SET PASSWORD FOR 'pi'@'localhost' = PASSWORD('mysql');
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>

登出 phpMyAdmin 後用新密碼 mysql 登入 pi 帳號 OK. 參考 :

MySQL修改密碼與忘記密碼重設

PS : 另外一個不同是, 鄉下的 Pi 3 使用 8080 埠 (因為 80 給 Nginx 用了), 而高雄的 Pi 3 使用 80 埠.

沒有留言 :