Raspberry PI 安裝 Web Server及相關套件:nginx、mariaDB、php、phpMyAdmin

本版討論Raspberry PI相關技術開發、設計與應用
回覆文章
inho
文章: 42
註冊時間: 2017-12-08, 23:00

Raspberry PI 安裝 Web Server及相關套件:nginx、mariaDB、php、phpMyAdmin

文章 inho » 2018-01-29, 07:37

// 參考網站
https://howtoraspberrypi.com/install-ng ... eb-server/
https://blog.gtwang.org/iot/raspberry-p ... eb-server/

Raspberry PI Raspbian OS 安裝 Web Server 相關套件包括:Nginx, MariaDB、PHP-FPM
一、安裝 Nginx (不用 Apache,原因可查閱相關網站)
$ sudo apt-get install nginx
$ service nginx status
-----------------------------------
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
Active: active (running) since 五 2018-02-23 07:26:02 CST; 16min ago
Main PID: 757 (nginx)
CGroup: /system.slice/nginx.service
├─757 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─758 nginx: worker process
├─759 nginx: worker process
├─760 nginx: worker process
└─762 nginx: worker process
---------------------------------------------------------------------
$ sudo apt-get install php5-fpm
要確定 php是否安裝成功:
$ sudo ps aux | grep php
root 5977 0.0 1.9 91480 17396 ? Ss 07:37 0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
www-data 5978 0.0 1.1 91880 10140 ? S 07:37 0:00 php-fpm: pool www
www-data 5979 0.0 1.1 91612 10020 ? S 07:37 0:00 php-fpm: pool www
pi 13918 0.0 0.2 4548 2036 pts/0 S+ 07:50 0:00 grep --color=auto php
========================================================================

1、設定 Nginx
(1)、modify the folder containing your sites, add the index.php pages as default pages, and finally add PHP.
Nginx 設定檔在:/etc/nginx/sites-available/default
$ sudo vi /etc/nginx/sites-available/default
從檔案中找到這一行:
index index.html index.htm index.nginx-debian.html;
取代為:
index index.html index.htm index.php
(2)、設定在 Nginx 啟動時,也啟動 php-fpm
從檔案中找到這幾行:
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm: # fastcgi_pass unix:/var/run/php5-fpm.sock;
#}
修改後:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
==================================================
修改之後存檔
2、修改首頁目錄的屬性及測試首頁
$ sudo chown -R www-data:pi /var/www/html/
$ sudo chmod -R 770 /var/www/html/
測試 phpinfo.php 的測試首頁
$ echo "<?php phpinfo(); ?>" > /var/www/html/index.php
$ echo 'Nginx work’s !' > /var/www/html/index.html

// 啟動 Nginx,並設定開機自動啟動:
$ sudo systemctl start nginx.service
$ sudo systemctl enable nginx.service
$ sudo /etc/init.d/nginx restart
$ sudo /etc/init.d/nginx restart
並利用瀏覽器 查看測試首頁是否成功,網址: http://localhosthttp://192.168.1.XXX
如果出現 "502 Bad Gateway" Error Message,-->
https://www.datadoghq.com/blog/nginx-50 ... s-php-fpm/

// 這時,應該可以看到 PHP Version 7.0.27-0+deb9u1 的 資訊網頁

二、安裝 MariaDB **********************************************************
// 從系統預設的版本安裝,Installing MariaDB database server and command line client on Raspbian Stretch
$ sudo apt-get install mariadb-server mariadb-client -y
// 初始化 MariaDB:
$ sudo mariadb
> use mysql;
> update user set password=PASSWORD("new_password"), plugin = '' where User='root';
> flush privileges;
> exit

$ sudo mysql -u root -p
Enter password:new_password

重新啟動 MariaDB,並設定開機自動啟動:
$ sudo systemctl restart mysqld.service
$ sudo systemctl enable mysqld.service

//******************************************************************************************************
Installing PHP 7 on Raspbian Stretch
$ sudo apt-get install php7.0 php7.0-fpm php7.0-mysql -y
After the installation had completed, I entered the following command to check the status:
$ systemctl status php7.0-fpm.service
Seeing the following output assured me that my PHP Fast CGI Manager had started successfully:

php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2018-02-04 23:01:23 CST; 35s ago
Docs: man:php-fpm7.0(8)
Main PID: 10679 (php-fpm7.0)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
CGroup: /system.slice/php7.0-fpm.service
├─10679 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
├─10680 php-fpm: pool www
└─10681 php-fpm: pool www

2月 04 23:01:23 BullPI systemd[1]: Starting The PHP 7.0 FastCGI Process Manager...
2月 04 23:01:23 BullPI systemd[1]: Started The PHP 7.0 FastCGI Process Manager.
// 再利用瀏覽器 查看首頁,網址: http://localhosthttp://192.168.1.XXX,看到 phpinfor 的內容就會顯示 mbstring及 mysqli、mysqlnd 的內容
//********* 其他參考 安裝 Raspberry Pi Install PHP7 + Nginx + MySQL + PhpMyAdmin (last versions) 方式
https://gist.github.com/isc30/aa80d81df ... 2d55806381

三、 安裝 Raspberry Pi PhpMyAdmin (last versions)
$ sudo apt-get install phpmyadmin
During the installation, it will prompt you to select a web server (apache2 or lightttpd) to configure. Nginx isn’t in the list, so press the Tab key and hit OK to skip this step.
安裝過程,會顯示一個視窗讓你選擇:要自動重新設定的網頁伺服器: │
│ │
│ ] apache2 │
│ ] lighttpd
因為 Nginx 並沒有在選項哩,直接跳過,按確定就好了。

安裝過程還會再顯示視窗詢問
MySQL application password for phpmyadmin:
及確認 password:
========================================
設定 phpMyAdmin,修改 phpMyAdmin 認證方式:

$ sudo vi /usr/share/webapps/phpMyAdmin/config.inc.php
# 預設為 cookie
$cfg['Servers'][$i]['auth_type'] = 'http';

建立 Nginx 根目錄鏈結 phpMyAdmin 的根目錄路徑:
ln -s /usr/share/webapps/phpMyAdmin /home/pi

重啟 Nginx:
$ sudo systemctl restart nginx.service

測試
使用瀏覽器連結測試,在網址列輸入 http://192.168.1.200/phpMyAdmin,然後輸入帳密 > 點擊「登入」:


PhpMyAdmin 安裝好,登入phpmyadmin的時候,如果出現:1698無法登入Mysql伺服器,解決方法:(ERROR 1698 (28000): Access denied for user 'root'@'localhost')
***************************************
$ sudo mysql
> use mysql // 選擇資料庫這時候MariaDB [(none)]會變成MariaDB [mysql],如果不這麼做等下會發生ERROR 1046 (3D000): No database selected)
> UPDATE user SET Password = PASSWORD('your_passwd') where USER='root';
> GRANT all ON *.* TO root@'localhost' IDENTIFIED BY 'your_passwd'; //把所有資料庫和資料表授權給 root user
mysql > FLUSH PRIVILEGES; //更新權限,更動密碼後一定要執行此行指令
mysql > quit
如此之後我的phpmyadmin也成功運作啦!
*************************************
$ sudo mysql -u root -p
Enter password: your_passwd
-----------------------------------------------
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 55
Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0

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

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

MariaDB [(none)]>
-------------------------------------------------------------------
參考網站:
http://iam24j.blogspot.tw/2015/05/sqlmysqlmariadb.html

回覆文章