Skip to content

PowerStudioTW/Nginx-PHP-MariaDB-phpMyAdmin-Installation-Tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

Nginx + PHP + MariaDB + phpMyAdmin Ubuntu安裝教學

安裝教學流程(全程都需要以管理員身分執行):

  • Nginx
  • PHP
  • MariaDB
  • phpMyAdmin
  • 優化

1. Nginx

1.1. 更新apt

使用管理員權限,更新apt:

sudo -s
apt update
apt upgrade -y

1.2. 安裝nginx套件

執行指令安裝nginx:

apt install nginx -y

查看nginx service是否執行

service nginx status

此時連線到這台伺服器,將會看到 Welcome to nginx! 字樣

2. PHP

2.1 安裝PHP套件

安裝PHP預設版本常用套件:

apt install php-fpm php-mysql php-curl -y

如需指定版本的PHP,也可指定版本安裝:

apt install software-properties-common
add-apt-repository ppa:ondrej/php
apt update

apt install php7.4-fpm php7.4-mysql php7.4-curl php7.4-dom php7.4-mbstring -y

更改config檔案,至 /etc/php/{版本代號,如7.4}/fpm 下修改php.ini

nano /etc/php/7.4/fpm/php.ini

使用F6搜尋,找到 cgi.fix_pathinfo,將comment移除並修改為cgi.fix_pathinfo=0

cgi.fix_pathinfo=0

2.2 測試PHP

修改nginx的預設router,至/etc/nginx/sites-available目錄,修改default檔案:

nano /etc/nginx/sites-available/default

server { } 中修改index

        #新增index.php
        index index.php index.html index.htm index.nginx-debian.html;

server { } 中新增一組location

        #新增php對應router,注意(/run/php/php7.4-fpm.sock)版本代號要與安裝的PHP版本相符
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }

檢查nginx語法是否有錯誤:

nginx -t

若語法正確將會回傳類似如下回應:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

/var/www/html中,新增一個index.php測試檔案:

nano /var/www/html/index.php
<?php
    echo phpinfo();
    exit();

此時重新整理網站,將會看到PHP版本資訊。

2.3. 安裝Composer套件

php -r "copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');"
sudo php /tmp/composer-setup.php --install-dir=/usr/bin --filename=composer
rm /tmp/composer-setup.php

檢查Composer安裝版本:

composer -V

3. MariaDB

3.1. 安裝MariaDB套件

直接安裝MariaDB:

apt install mariadb-server -y

或是指定版本安裝,可由MariaDB官方網站查看如何安裝: https://downloads.mariadb.org/mariadb/repositories/#mirror=nodesdirect

此教學以Ubuntu 18.04 LTS搭配MariaDB 10.4為例:

sudo apt-get install software-properties-common
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirror.nodesdirect.com/mariadb/repo/10.4/ubuntu bionic main'
apt update
apt install mariadb-server

檢查套件是否正常開啟:

service mysqld status

3.2. 執行MySQL安全安裝

在安裝mariaDB套件後必須執行mysql_secure_installation指令才能正常使用:

mysql_secure_installation

依照安裝步驟安裝即可。

3.3. 新增MariaDB使用者供外部使用

一般來說root使用者並不會開放非localhost連線使用,若您需要使用phpMyAdmin或是MySQL Workbench等圖形化介面登入,必須新增外部使用者。

執行mysql -u root -p,輸入剛剛所設定的密碼,進入MariaDB操作:

mysql -u root -p

新增一個帳號為tutorial,登入位置為%(皆可登入),密碼為tutorial2020的使用者(自行替換帳號密碼與登入位置):

CREATE USER 'tutorial'@'%' IDENTIFIED BY 'tutorial2020';

若需要給予tutorial帳號所有權限,可以執行:

GRANT ALL PRIVILEGES ON *.* TO 'tutorial'@'%' WITH GRANT OPTION;

4. phpMyAdmin

4.1. 下載phpMyAdmin

phpMyAdmin為簡易好上手的MySQL/MariaDB圖形化介面,在開始安裝之前需先安裝unzip套件:

apt install unzip -y

切換至/usr/share/目錄,並下載需要版本的phpMyAdmin( https://www.phpmyadmin.net/downloads/ ),解壓縮後更名為phpmyadmin,此以phpMyAdmin 5.0.2全語言版本為例:

cd /usr/share
wget https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin-5.0.2-all-languages.zip
unzip phpMyAdmin-5.0.2-all-languages.zip
mv phpMyAdmin-5.0.2-all-languages phpmyadmin
rm phpMyAdmin-5.0.2-all-languages.zip

4.2. 切換phpMyAdmin使用者

phpmyadmin資料夾的使用者群組、名稱切換為www-data:www-data

chown -R www-data:www-data phpmyadmin/

使用llls -lah指令查看使用者與群組、名稱:

ll

4.3. 設定Nginx Router

新增一個snippet名為phpmyadmin.conf位於/etc/nginx/snippets/

nano /etc/nginx/snippets/phpmyadmin.conf

內容為,記得將unix:/run/php/php7.4-fpm.sock;改為安裝的php版本:

location /phpmyadmin { 
	root /usr/share/; 
	index index.php index.html index.htm; 
	location ~ ^/phpmyadmin/(.+\.php)$ { 
		try_files $uri =404; 
		root /usr/share/; 
		fastcgi_pass unix:/run/php/php7.4-fpm.sock; 
		fastcgi_index index.php; 
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
		include /etc/nginx/fastcgi_params; 
	} 
	location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ { 
		root /usr/share/; 
	} 
} 

將default router引入snippets設定檔:

nano /etc/nginx/sites-available/default

server { } 中底部新增include snippet:

include snippets/phpmyadmin.conf;

驗證nginx語法是否有誤:

nginx -t

如果無誤,重新啟動nginx:

service nginx restart

重新連線至網站,並輸入網址/phpmyadmin,即可看到phpMyAdmin後台!

若希望只有特定IP位置可以存取此後台,可以在/etc/nginx/snippets/phpmyadmin.conflocation /phpmyadmin { }中加入:

        allow 0.0.0.0;
        deny all;

將會只允許特定IP位置的電腦可以連線至phpMyAdmin後台,增加安全性。

相關資訊 連結
Composer https://getcomposer.org/download/
MariaDB https://downloads.mariadb.org/mariadb/repositories
phpMyAdmin https://www.phpmyadmin.net/downloads/

About

Nginx + PHP + MariaDB + phpMyAdmin 安裝教學

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published