1 頁 (共 1 頁)

Linux 用 Nginx架站,MariaDB, PHP

發表於 : 2018-02-14, 08:21
inho
Linux (包括Raspberry PI) 用 Nginx 架設網站
參考網站: http://www.smalljacky.com/embedded-syst ... hpmyadmin/
------------------------------------------------------------------------------------
一、安裝 Nginx Web Server (不用 Apache,原因可查閱相關網站)及相關套件,包括: MariaDB、PHP-FPM、PHP-FPM
使用 “aptitude” package manager to install Nginx and php-fpm on the Raspberry from the Raspbian repositories. (https://howtoraspberrypi.com/install-ng ... web-server)
$ sudo aptitude install nginx php-fpm
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

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

關於 Nginx 的設定檔
Nginx 所有設定檔:放置於 /etc/nginx 目錄下
Nginx 主要設定檔:/etc/nginx/nginx.conf
Nginx 程序設定檔:/usr/sbin/nginx
Nginx Log 檔:/var/log/nginx
Nginx 首頁資料目錄: /var/www/html

$ sudo vi /etc/nginx/nginx.conf
# 啟動 Nginx 伺服器的使用者(預設為 www-data),所以設定 網頁根目錄的 Owner group 為 www-data:$ sudo chown -R www-data:pi /var/www/html/ ,在此,可自訂使用者,但是,修改以後,需要重新設定 $ sudo chown -R group:pi /var/www/html/
*************************************************************************************************************************
二、安裝 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)

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

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

若要移除 phpmyadmin 的安裝
$ sudo apt-get purge phpmyadmin