LEMP 全寫是 Linux + Nginx, + MySQL (MariaDB) + PHP。Nginx (發音能同 engine x) 是一款以性能取向的網頁伺服器, 較 Apache 使用較少系統資源, 以下是在 CentOS 8 安裝 Nginx, MariaDB 及 PHP 的教學.
安裝 Nginx
CentOS 8 的 Repository 已經內建了 Nginx, 設定比以前簡單, 不用再安裝 Nginx 的 Repository 或 EPEL, 可以用 dnf 或 yum 安裝, 輸入以下指令安裝 Nginx:
安裝好 Nginx 網頁伺服器後, 便可以用 systemctl 指令啟動/停止/重新啟動 Nginx, 現在啟動 Nginx 及設定開機自動啟動:
# systemctl enable nginx
然後需要設定 Firewalld 防火牆, 把 port 80 及 port 443 對外開放, 執行以下指令:
# firewall-cmd –permanent –add-service=https
# firewall-cmd –reload
最後可以用瀏覽器檢查 Nginx 是否可以連接, 例如:
http://server-ip/
如果可以看到 Nginx 的 Webcome 畫面, Nginx 便安裝成功了。
安裝 MariaDB
執行以下指令安裝 MariaDB:
啟動及設定開機自動執行 MariaDB:
# systemctl enable mariadb
執行以下指令設定 MariaDB 的安全設定:
執行後跟著回答問題, 分別是:
— 設定 root 帳號密碼
— 是否移除匿名帳號
— 是否允許 root 帳號遠端登入
— 是否移除 test 資料庫
完成後可以測試一下 MariaDB 是否可以登入:
安裝 PHP
以下指令除了會安裝 PHP 外, 還會安裝一些常用的 PHP 套件:
從 RHEL 及 CentOS 安裝的 PHP-FPM, 預設會使用 apache 作為執行的 user 及 group, 需要修改成 nginx, 開啟檔案 /etc/php-fpm.d/www.conf:
找到以下兩行:
user = apache
group = apache
改成
user = nginx
group = nginx
在 vi 儲存檔案及離開:
現在啟動 PHP-FPM 及設定開機自動開啟 PHP-FPM:
# systemctl enable php-fpm
安裝及設定好 PHP 後, 需要重新啟動 Nginx 才會生效:
要測試 PHP, 可以放一個簡單的 PHP Script 到 Nginx 的 DocumentRoot 目錄, 預設是 /usr/share/nginx/html/, 執行以下指令:
然後輸入以下幾行程式碼:
phpinfo();
?>
然後在瀏覽器看看 http://localhost/info.php, 如果看到 PHP 的設定資料便表示安裝完成了。