iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 24
1
Software Development

30天開發與部署 Laravel 專案系列 第 24

GCP IaaS : Compute Engine with LNMP Environment

GCP Compute Engine

  • Infrastructure as a Service (IaaS) 提供基礎資源,使用者需自行控制底層,包含使用邏輯後,可自行部署與進行雲端運算。
  • GCP 的Iaas為 Compute Engine,一樣得先建立project後才能建立instance。
  • 進入 Compute Engine 頁面後點選新增才開始建立一台vm的設置。
    • 建議至少選擇2G記憶體的機器,不然光是安裝LNMP環境就有可能出錯。
      https://ithelp.ithome.com.tw/upload/images/20201009/20125263f0RUd8JYFq.png
    • 作業系統我使用ubuntu,但 GCP Compute Engine 支援以下作業系統
      https://ithelp.ithome.com.tw/upload/images/20201009/201252639Jq7Tb2lfd.png
    • 安裝成功後回到頁面可以找到外部ip與透過ssh連線到instance的方式。
      https://ithelp.ithome.com.tw/upload/images/20201009/20125263SUfaGsjUt6.png

設定 LNMP

第3天的文章有提過,LNMP 為 Linux、Nginx、MySQL、PHP的架構。

個人習慣先確認開發環境中 Nginx可否正常啟動 PHP、 MySQL來確認開發環境是否正常。

安裝 Nginx

su - 建議可先使用root帳號

sudo apt update 更新安裝套件

sudo apt install nginx

sudo ufw app list 開防火牆

打開GCP Computer Engine 所提供的外部網址,會顯示預設Nginx預設連線畫面
https://ithelp.ithome.com.tw/upload/images/20201009/20125263x7M79h7VDm.png

透過Nginx 啟動 php

安裝 php 7.4

sudo apt-get update 更新安裝套件

sudo apt-get install php7.4-fpm php7.4-cli php7.4-mysql php7.4-gd php7.4-tidy php7.4-xmlrpc 安裝php相關套件

php -v 檢查版本

設置Nginx去讀PHP檔案

先處理php-fpm

/etc/php/7.4/fpm/php.ini 裡面更改 cgi.fix_pathinfo 為 0

結束後重啟php-fpm

處理 Nginx設定檔開啟php啟動限制

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm;
        server_name example.com;

        location / {
            try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        }
}

基本上用這一段,不過範例網址是另外寫一個domain的檔案,但我直接用default處理

檢查 Nginx 服務狀態

  • nginx -t建議重啟服務前,先跑一次
  • 成功範例
    https://ithelp.ithome.com.tw/upload/images/20201009/20125263krU4FMYp6L.png
  • 失敗範例
    https://ithelp.ithome.com.tw/upload/images/20201009/20125263PnPzQt1NDR.png

檢查Log

  • 在 ubuntu的路徑為 /var/log/nginx
  • 因為沒有GUI介面顯示錯誤訊息,所以得在Log檔中確認哪些出錯。

參考資料
https://zh.wikipedia.org/wiki/%E5%9F%BA%E7%A4%8E%E8%A8%AD%E6%96%BD%E5%8D%B3%E6%9C%8D%E5%8B%99
http://nginx.org/en/docs/
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04#step-4-%E2%80%94-configuring-nginx-to-use-the-php-processor
https://ithelp.ithome.com.tw/articles/10217403
https://kknews.cc/zh-tw/code/v3ya8b2.html
https://yii.im/posts/the-right-way-to-set-nginx-for-laravel/
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04#step-4-—-configuring-nginx-to-use-the-php-processor


上一篇
GCP PaaS : App Engine
下一篇
GCE /Ubuntu/Nginx環境下建立 Laravel專案
系列文
30天開發與部署 Laravel 專案30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言