Ubuntu Server 20.04.1 使用 WordOps NGINX 建立設定 Laravel 環境

Laravel

在 Ubuntu Server 20.04.1 使用 WordOps 快速及簡化網站安裝流程,並自動設定 NGINX 最佳化,來建置 Laravel 環境教學。

WordOps

安裝及設定可參考 WordOps 快速建立高效能 WordPress 和管理教學

新建網站

建立一般網站:

wo site create api.footmark.com.tw --php74 -le

Laravel 8 NGINX 設定

編輯獨立網站設定

編輯獨立網站 NGINX 設定檔,存檔後 WordOps 會自動重新載入 NGINX 設定檔 (參考 How to create custom nginx configurations? - WordOps Community Forum):

wo site edit api.footmark.com.tw
server {
    server_name api.footmark.com.tw www.api.footmark.com.tw;

    access_log /var/log/nginx/api.footmark.com.tw.access.log rt_cache;
    error_log /var/log/nginx/api.footmark.com.tw.error.log;

    #root /var/www/api.footmark.com.tw/htdocs;
    root /var/www/api.footmark.com.tw/htdocs/public;

    # Laravel
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    #index index.php index.html index.htm;
    index index.php

    # Laravel
    charset utf-8;
    error_page 404 /index.php;

    include common/php74.conf;

    include common/locations-wo.conf;
    include /var/www/api.footmark.com.tw/conf/nginx/*.conf;
}

自訂共用設定

使用 WordOps 預設方式,新增 php74.conf.custom 自訂設定,來取代 php74.conf 預設設定檔 (因 WordOps 更新,預設設定檔會被清除):

sudo vim /etc/nginx/common/php74.conf.custom
# PHP NGINX CONFIGURATION - WordOps 3.13.2
# DO NOT MODIFY, ALL CHANGES WILL BE LOST AFTER AN WordOps (wo) UPDATE
location / {
  try_files $uri $uri/ /index.php$is_args$args;
}

// Laravel
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

location ~ \.php$ {
  try_files $uri =404;

  // Laravel
  fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;

  include fastcgi_params;
  fastcgi_pass php74;
}

// Laravel
location ~ /\.(?!well-known).* {
    deny all;
}

重新載入 NGINX 設定檔:

wo stack reload --nginx

Laravel 8

安裝及設定可參考 Laravel 安裝和 NGINX 設定 for CentOS 8

新增專案

先切換至欲建立 Laravel 8 專案網站根目錄:

cd /var/www/api.footmark.com.tw/htdocs/

新建 Lavavel 8 版本專案:

  • ./:表示在當前目錄 (也可指定路徑)。
sudo composer create-project --prefer-dist laravel/laravel=8.* ./

目錄和檔案權限設定

Laravel 8

NGINX 網頁伺服器必須要有寫入 storage 和 bootstrap/cache 目錄的權限,否則 Laravel 8 就會無法執行:

sudo chmod -R 777 storage
sudo chmod -R 777 bootstrap/cache/

網站目錄、檔案

遞迴更改網站目錄下,所有目錄的權限設定:

sudo find /var/www/api.footmark.com.tw/htdocs/ -type d -exec chmod 2775 {} \;

遞迴更改網站目錄下,所有檔案的權限設定:

sudo find /var/www/api.footmark.com.tw/htdocs/ -type f -exec chmod 664 {} \;

擁有者

遞迴更改網站所有目錄和檔案的擁有者為 NGINX 網頁伺服器使用者帳號 www-data

sudo chown www-data:www-data -R /var/www/laravel8-jwt.footmark.com.tw/

測試

在瀏覽器輸入您的網址出現下圖,表示 Laravel 8 已建置完成,可以進行開發了。

Laravel 8 專案建置完成首頁
Laravel 8 專案建置完成首頁

參考

發表留言