nginx + PHP + MariaDB Web Server 安裝、設定

提供 Raspberry Pi 4 安裝、設定
inho
文章: 60
註冊時間: 2017-12-08, 23:00

nginx + PHP + MariaDB Web Server 安裝、設定

文章inho » 2019-10-07, 11:26

nginx + PHP + MariaDB Web Server 安裝、設定:

https://codertw.com/%E7%A8%8B%E5%BC%8F% ... 80/212415/
https://learn.getgrav.org/16/basics/what-is-grav
https://www.instructables.com/id/Raspbe ... PHP-MySql/

Raspberry PI (4B) Rasbian Buster Lite 從 0 開始、安裝UI 與設定
Raspberry PI Rasbian Stretch Lite 是陽春版的Rasbian OS,沒有GUI 的視窗,安裝以後許多套件要透過 Command Line 要靠自己手動安裝,因此可以只安裝自己所需要的軟體套件,減輕系統負擔,提升系統執行效能,速度快,感覺比較順暢,但是,要花時間及精神安裝必要套件。

如果要將 Raspberry PI 當作 IoT遠端控制的節點,這種做法似乎是不錯的選擇。
參考網站: https://diyprojects.io/install-pixel-de ... 6s6OGgzaUk

一、 規劃 MicroSD卡、下載及安裝 Rasbian Stretch Lite 作業系統,完成之後
二、 下載 Rasbian Stretch Lite 安裝後,沒有GUI Window 介面,command Line

$ Login : pi
$ Passwd:raspberry
===== Change Passwd ==========
$ passwd ==>
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install raspberrypi-ui-mods # 安裝 GUI Window 介面

=== 然後開啟應用程式,放到桌面上: /usr/share/applications 底下 有一些基本安裝的應用程式Icon,複製到 桌面上,如 Termit終端機,檔案管理程式等。
$ sudo raspi-config # pi 設定 包括 ssh VNC 中文化,audio、video、KB、Resolution ...
設定網路連線 --> /etc/dhcpcd.conf

$ sudo reboot
三、 安裝 Web Framework
(一)、 安裝 Python3
1、 Check python 版本
$ python –V
Python 2.7.16 (python2 default version)
$ python3 –V
Python 3.7.3 (python3 default version)
2、 Upgrade pip3
$ pip3 install --upgrade pip
3、
(二)、 Install Pip for Python
pip是一個以Python語言寫的軟體包管理系統,可以安裝和管理軟體包。
1、 Install pip for Python 2 and all of its dependencies:
$ sudo apt install python-pip
$ pip –version
pip 18.1 from /usr/lib/python2.7/dist-packages/pip (python 2.7)
2、 Install pip for Python 2 and all of its dependencies:
$ sudo apt install python3-pip
$ pip3 –version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
(三)、 安裝 Web Framework
https://gist.github.com/isc30/aa80d81df ... 2d55806381
安裝Nginx + php7.3 + MariaDb + phpMyAdmin Web Server
1、 安裝相關套件 (php7.3, mysql or mariaDB)
$ sudo apt-get install -y php7.3 php7.3-fpm php7.3-cli php7.3-opcache php7.3-mbstring php7.3-curl php7.3-xml php7.3-gd php7.3-mysql
2、 安裝 nginx
$ sudo apt-get install -y nginx
# 使用預設值開機 nginx 及 php7.3-fpm
$ sudo update-rc.d nginx defaults
$ sudo update-rc.d php7.3-fpm defaults
從client 端: http://<Server IP> 就可以看到歡迎網頁
Welcome to nginx!
此檔案 存放在 /var/www/html/index.nginx-debian.html
設定檔放在:/etc/nginx/sites-available/default
連結檔:/etc/nginx/sites-available/default -> /etc/nginx/sites-available/default
# 將 /etc/php/7.3/fpm/php.ini 內容中
;cgi.fix_pathinfo=1 改成
cgi.fix_pathinfo=0
$ sed -i 's/^;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/7.3/fpm/php.ini
3、 Nginx Setting (/etc/nginx/sites-available/default)
修改default 檔案內容
$ sudo vi /etc/nginx/sites-available/default
# Default server
server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html/public;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
# deny access to .htaccess files, should an Apache document root conflict with nginx
location ~ /\.ht {
deny all;
}
}
# wwg.society.org.tw server configuration
server {
listen 80;
listen [::]:80;

server_name wwg.society.org.tw www.wwg.society.org.tw
root /var/www/wwg/public;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
# deny access to .htaccess files, should an Apache document root conflict with nginx
location ~ /\.ht {
deny all;
}
}

# tku.society.org.tw server configuration
server {
listen 80;
listen [::]:80;

server_name tku.society.org.tw www.wwg.society.org.tw
root /var/www/tku/public;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
# deny access to .htaccess files, should an Apache document root conflict with nginx
location ~ /\.ht {
deny all;
}
}
$ mkdir -p /var/www/html/public
$ cat > /var/www/html/public/index.php << "EOF"
<?php
class Application
{
public function __construct()
{
phpinfo();
}
}
$application = new Application( ) ;
?>
EOF
$ sudo mkdir -p /var/www/wwg
$ sudo mkdir -p /var/www/wwg/public
cat > /var/www/wwg/public/index.php << "EOF"
<?php
class Application
{
public function __construct()
{
echo 'Hi ZonaIsc.com';
}
}
$application = new Application( ) ;
?>
EOF

$ sudo mkdir -p /var/www/tku
$ sudo mkdir -p /var/www/tku/public
cat > /var/www/tku/public/index.php << "EOF"
<?php
class Application
{
public function __construct()
{
echo 'TKU Student Project at society.org.tw';
}
}
$application = new Application( ) ;
?>
EOF

$ sudo chown -R www-data:pi /var/www
$ sudo chmod -R 775 /var/www
$ sudo usermod -aG www-data pi

$ sudo service nginx restart
$ sudo service php7.3-fpm restart

4、 安裝 MariaDB
$ sudo apt-get install -y mariadb-server mariadb-client
$ 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 [(none)]> exit


重新啟動 MariaDB,並設定開機自動啟動:
$ sudo systemctl restart mysqld.service
$ sudo systemctl enable mysqld.service
5、 Installing phpMyAdmin
$ sudo apt-get install -y phpmyadmin
$ sudo ln -s /usr/share/phpmyadmin /var/www/html/public
$ sudo ln -s /usr/share/phpmyadmin /var/www/wwg/public
$ sudo ln -s /usr/share/phpmyadmin /var/www/tku/public
Run phpmyadmin:
http://<my server IP>/phpmyadmin or
http://tku.society.org.tw/phpmyadmin

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客