LEMP 意思是 Linux + Nginx, + MySQL (MariaDB) + PHP。Nginx (發音能同 engine x) 是一款以性能取向的網頁伺服器, 較 Apache 及 Lighttpd 使用較少記憶體, 而配置 Nginx 及 PHP 的方法與 Apache 有點不同, 因為 Nginx 是透過 php-fpm (FastCGI Process Manager) 運作的, 以下會示範在 CentOS 7 安裝 Nginx, MariaDB 及 PHP.
安裝 Nginx
在 RHEL, CentOS 或 Fedora 安裝 Nginx, 最簡單的方法是先加入 Nginx 的 CentOS 7 yum repository, 然後用 Yum 安裝, 輸入以下指令安裝:
# yum install nginx
安裝好 Nginx 網頁伺服器後, 便可以用 systemctl 啟動/停止/重新啟動 Nginx, 現在啟動 Nginx 及設定開機自動啟動:
# systemctl enable nginx.service
啟動後, 便可以用瀏覽器檢查 Nginx 是否可以連接, 例如:
http://server-ip/
安裝 MariaDB
執行以下指令安裝 MariaDB:
啟動及設定開機自動執行 MariaDB:
# systemctl enable mariadb.service
執行以下指令設定 MariaDB 的 root 密碼, 預設是空密碼, 所以建議盡快修改:
完成後可以用測試一下 MariaDB 是否已經啟動:
安裝 PHP
安裝好 PHP 後, 開啟 /etc/php.ini, 找到 cgi.fix_pathinfo=1, 將 1 改成 0, 即這樣:
cgi.fix_pathinfo=0
儲存檔案後, 再開啟 /etc/php-fpm.d/www.conf, 找到 listen 一行, 改成這這樣:
listen = /var/run/php-fpm/php-fpm.sock
儲存檔案後, 可以用以下指令啟動 php-fpm
# systemctl enable php-fpm.service
配置 Nginx 及 PHP
Nginx 的 server block 差不多等同 Apache 的 virtual hosts, 以下會透過 server block 設定, 開啟 /etc/nginx/conf.d/default.conf
檔案會是 Nginx 的預設內容, 即只可以處理靜態網頁, 要在要設定 Nginx 能編譯 php 檔案, 及將 index.php 設定成 index 檔, 改成這樣:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
server { listen 80; server_name server-ip; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } |
儲存檔案後, 重新啟動 Nginx 便完成了:
這是我看過部署LNMP文章中最棒的一篇了
淺顯易懂,非常感謝^^