The Issue of the Day Before

安裝 LEMP 開發環境

linux -

在 Ubuntu 上安裝 Nginx + PHP-fpm 開發環境

Install Nginx

參看 Nginx 安裝指南 說明安裝或依下列步驟安裝.

> sudo apt-get update
> lsb_release -a| grep Codename
// Codename:       bionic
> export os=$(lsb_release -a 2>/dev/null | grep Codename | awk -F":" '{print $2}')
> sudo wget https://nginx.org/keys/nginx_signing.key
> sudo apt-key add nginx_signing.key     // (1)
> sudo echo 'deb https://nginx.org/packages/mainline/ubuntu/ $os nginx' >> /etc/apt/sources.list
> sudo echo 'deb-src https://nginx.org/packages/mainline/ubuntu/ $os nginx' >> /etc/apt/sources.list                    // (2)
> sudo apt remove nginx-common       // (3)
> sudo apt update
> sudo apt install nginx             // (4)
  1. 加入 Nginx 軟體倉儲金鑰

  2. 將 Nginx 軟體倉儲位址加入 apt 來源列表

  3. 刪除舊版

  4. 更新後安裝 Nginx

在 Ubuntu 22.04 安裝時,不用上述<1>加入 Nginx 軟體倉儲金鑰,但可能會出現下列錯誤訊息, 需要為密碼庫加入金鑰

W: GPG error: https://nginx.org/packages/ubuntu focal InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY $key

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $key

Start Nginx

> systemctl start nginx    // (1)
> systemctl status nginx   // (2)
> curl -I 127.0.0.1        // (3)
  1. nginx 啟動

  2. nginx 狀態

  3. 是否正回應

Install PHP-fpm

> sudo apt-get install -y php-fpm
> php -v               // (1)
> ps -ef | grep [p]hp  // (2)
> systemctl status php<version>-fpm.service
  1. 檢查版本

  2. 檢查 fpm 是否啟動

Setup PHP-fpm + Nginx

> ps -ef | grep [p]hp
// root     10676     1  0 07:11 ?        00:00:00 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
// www-data 10691 10676  0 07:11 ?        00:00:00 php-fpm: pool www
// www-data 10692 10676  0 07:11 ?        00:00:00 php-fpm: pool www

> ps -ef | grep [n]ginx
// root      4449     1  0 07:00 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
// nginx     4453  4449  0 07:00 ?        00:00:00 nginx: worker process


> ls -la /var/run/php/*.sock
// srw-rw---- 1 www-data www-data 0 Dec 24 07:11 /var/run/php/php7.2-fpm.sock

因為讓 Nginx 透過 unix socket 聯繫上 PHP-fpm, 需要 Nginx 和 PHP-fpm 同一個 USER

修改設定檔 /etc/php/7.2/fpm/pool.d/www.conf, 將 PHP-fpm 的由改 nginx 啟動

> sed -i 's/listen.owner = www-data/listen.owner = nginx/g' /etc/php/7.2/fpm/pool.d/www.conf
> sed -i 's/listen.group = www-data/listen.group = nginx/g' /etc/php/7.2/fpm/pool.d/www.conf

修改設定檔 /etc/php/7.2/fpm/php.ini, 避免由 nginx 對 php 注入程式碼的風險

> sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.2/fpm/php.ini

重新以 owner=nginx 啟動 php-fpm

> systemctl stop php7.2-fpm.service
> rm -rf /var/run/php/php7.2-fpm.sock
> systemctl start php7.2-fpm.service

編輯設定檔在 server 區間 ./etc/nginx/conf.d/default.conf

    root   /usr/share/nginx/html;
-    index  index.html index.htm;
+    index  index.php index.html index.htm;

+   include        fastcgi_params;
+   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
+   fastcgi_index  index.php;

+   location ~* \.php$ {
+       fastcgi_pass   unix:/run/php/php7.2-fpm.sock;
+   }

放一個 PHP 測試檔案, 重新啟動 nginx

> echo '<?php phpinfo() ?>' > /usr/share/nginx/html/index.php
> systemctl restart nginx.service

Install PHP-mysql

> sudo apt-get install -y php-mysql
> phpenmod mysqli
> systemctl restart php7.2-fpm.service
閱讀在雲端