2026年10月29日 星期四

2026ubuntu+nginx+mariadb+php+phpmyadmin 實作紀錄

虛擬機裡是ubuntu24.04

安裝 nginx

sudo apt install nginx 

安裝 mariadb

sudo apt install mariadb-server mariadb-client
安全性設定,包括root密碼 
sudo mysql_secure_installation 

除了輸入root密碼其他我都是一路Y

建立mysql使用者和密碼
sudo mysql 就直接進入 mariadb 不需要輸入密碼
建立user1使用者
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'password1';
讓使用者擁有全部權限
GRANT ALL PRIVILEGES ON *.* TO 'user1'@'localhost';

更新權限
FLUSH PRIVILEGES;

如果只讓使用者有 db1的權限用
GRANT ALL PRIVILEGES ON 'db1'.* TO 'user1'@'localhost';

quit 離開mariadb

安裝php8.4


新增 PPA 儲存庫並安裝 PHP 8.4
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update

先裝基礎
sudo apt install php8.4 php8.4-fpm

後裝模組
sudo apt install php8.4-common php8.4-mysql php8.4-xml php8.4-curl php8.4-gd php8.4-cli php8.4-mbstring php8.4-opcache php8.4-zip php8.4-tokenizer

注意
php8.4-mysql會同時安裝 mysqli 和 pdo

調整 php.ini


sudo vi /etc/php/8.4/fpm/php.ini
修改項目參考如下:
upload_max_filesize = 32M
post_max_size = 48M
memory_limit = 256M 

調整 nginx default 檔


server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html;
index index.php index.html;

server_name _;
# 隱藏 nginx 版本
server_tokens off;
client_max_body_size 100m;
fastcgi_buffers 16 32k;
fastcgi_buffer_size 64k;


# 全站基本安全標頭
add_header X-Content-Type-Options nosniff always;

        location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

安裝 phpmyadmin


sudo apt install phpmyadmin 就會安裝適合的版本到 /usr/share/phpmyadmin

sudo ln -s /usr/share/phpmyadmin /var/www/html/newlinkname

這樣 http://網站ip/newlinkname就可以到 phpmyadmin

保護措施可以參考 /2019/06/108-ubuntunginxphpmariadb.html

也可用修改檔案的方式
cd
/usr/share/phpmyadmin

sudo vi index.php
在版本檢查後加上
$allowed = ["163.30.142.1", "163.30.200.2"];
if (!in_array($_SERVER["REMOTE_ADDR"], $allowed, true)) {
die("DENY");
}

把IP白名單放在 $allowed變數裡

sudo cp config.sample.inc.php config.inc.php
sudo vi config.inc.php
修改$cfg['blowfish_secret']為隨機字串
$cfg['blowfish_secret'] = 'HJK7r7h3p0fa6BLAg4eOLASDF0kmnlofKL72ESWER';


nginx 的其他安全設定


改成 https

首先將 /etc/nginx/sites-available/default 中的
server_name寫好
原來是 server_name _

改成像
server_name general.tyc.edu.tw;
當然DNS要對應好,有幾個 server段就設定幾個

(補充:server_name example.com www.example.com; # 這裡設定了兩個域名)

安裝 snap
sudo apt update
sudo apt install snapd

安裝  certbot
sudo snap install --classic certbot

將 certbot 指令連結到 /usr/local/bin ,通常這樣就可以直接用certbot,免輸入完整路徑
sudo ln -s /snap/bin/certbot /usr/local/bin/certbot
最後 安裝憑證
sudo certbot --nginx

依據文字說明輸入
首先問Email
Enter email address or hit Enter to skip.
(Enter 'c' to cancel):
輸入正確的email

然後接下來兩個問題都用 Yes 回答

然後選要取得憑證的domain

Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: general.tyc.edu.tw

Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1

直接按下enter就是全部

接下來他會修改 default






如果要做到 HSTS加上
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload" always;
到最後面
另一段也是certbot自動加的 他會把 http 自動變成 https
server { 
 if ($host = general.tyc.edu.tw) { 
        return 301 https://$host$request_uri; 
  } # managed by Certbot 
 listen 80 default_server; 
 listen [::]:80 default_server; 
 server_name general.tyc.edu.tw; 
return 404; 
# managed by Certbot 
}

sudo systemctl restart nginx 才會生效

補充 
如果是 apache2 module模式
那要加裝 libapache2-mod-php8.4
sudo apt install libapache2-mod-php8.4

#移除舊模組
sudo a2dismod php7.2 
#使用新模組
sudo a2enmod php8.4
#重啟
sudo systemctl restart apache2 

沒有留言:

張貼留言