顯示具有 chcweb 標籤的文章。 顯示所有文章
顯示具有 chcweb 標籤的文章。 顯示所有文章

2026年4月11日 星期六

想修改 chcweb 出現的問題解決集

系列文章:
1.Ubuntu 24.04 安裝 chcweb 專案的過程記錄
2.Ubuntu 24.04 安裝 laravel 5.7 的過程記錄
3.laravel 5.7 初始相關設定與注意事項
4.想修改 chcweb 出現的問題解決集
 

問題一: UnexpectedValueException : The stream or file "/home/webadmin/html/chcweb/storage/logs/laravel-2026-04-11.log" could not be opened in append mode: failed to open stream: Permission denied

解決方法:
錯誤意思是 Laravel 沒辦法寫入 log 檔,通常跟 目錄權限 有關。 

1.檢查檔案與目錄權限

Laravel 日誌檔案路徑:

/home/webadmin/html/chcweb/storage/logs/laravel-2026-04-16.log

需要 Web 伺服器用戶能夠寫入。通常 Linux 系統 Web 伺服器用戶是:

  • Debian/Ubuntu:www-data
  • CentOS/Fedora:apachenginx

檢查目錄權限:

ls -l /home/webadmin/html/chcweb/storage/logs/

2.設定正確的擁有者

假設你的 Web 伺服器用戶是 www-data

sudo chown -R www-data:www-data /home/webadmin/html/chcweb/storage
這樣 Web 伺服器就能寫入。

3.設定正確的權限

Laravel 需要 storagebootstrap/cache 可寫:
sudo chmod -R 775 /home/webadmin/html/chcweb/storage
sudo chmod -R 775 /home/webadmin/html/chcweb/bootstrap/cache
  • 775:擁有者與群組可讀寫執行,其他人可讀與執行
  • 如果 775 仍然不行,可以暫時用 777 測試,但不建議長期使用(安全性較低) 

4.清除快取(可選)

Laravel 有時會快取檔案路徑,建議清除:

php artisan config:clear
php artisan cache:clear

然後再重新載入網站看看是否正常。

小技巧
Laravel 的 storagebootstrap/cache 目錄必須可寫,這是最常見的「Permission denied」原因。 

一行命令,直接安全地修復 Laravel 所有目錄的權限和擁有者,幾乎可以立即解決這種錯誤:

sudo chown -R www-data:www-data /home/webadmin/html/chcweb && sudo find /home/webadmin/html/chcweb/storage -type d -exec chmod 775 {} \; && sudo find /home/webadmin/html/chcweb/storage -type f -exec chmod 664 {} \; && sudo find /home/webadmin/html/chcweb/bootstrap/cache -type d -exec chmod 775 {} \; && sudo find /home/webadmin/html/chcweb/bootstrap/cache -type f -exec chmod 664 {} \; 

問題二:要在資料表setups 新增遺個欄位 bg_image

1.建立 Migration 

php artisan make:migration add_bg_image_to_setups_table --table=setups

2. 修改 Migration 檔案

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddBgImageToSetupsTable extends Migration {
  public function up() {
      Schema::table('setups', function (Blueprint $table) {
          $table->string('bg_image', 255)->nullable()->after('bg_color')->comment('背景圖片');
      }); 
}

  public function down() {
       Schema::table('setups', function (Blueprint $table) {
          $table->dropColumn('bg_image');
       });
    }
}

3.執行 Migration

php artisan migrate 

問題三: UnexpectedValueException  : The stream or file "/home/webadmin/html/chcweb/storage/logs/laravel-2026-04-17.log" could not be opened in append mode: failed to open stream: Permission denied

問題解析:這個錯誤是 Laravel 在寫 log 檔案時發生的權限問題,完全跟你新增欄位無關,但它會阻止 migration 或任何程式寫入日誌。解決方式是調整 storagebootstrap/cache 的權限。

1.確認目錄

Laravel 需要以下目錄可讀寫:

  • storage
  • storage/logs
  • storage/framework
  • bootstrap/cache

 2.修改權限(Linux)

假設你的專案目錄是 /home/webadmin/html/chcweb

cd /home/webadmin/html/chcweb
# 設定 owner 為 web server 使用者,通常是 www-data (Ubuntu/Debian) 或 apache/nginx
sudo chown -R webadmin:www-data storage bootstrap/cache
# 設定目錄可寫權限
sudo chmod -R 775 storage bootstrap/cache
#注意:如果你的 web server 使用者不是 www-data,請改成實際使用者。

3.Laravel 快速清理快取(建議做)

php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear

4. 測試

重新執行 migration:

php artisan migrate

如果權限設定正確,就不會再出現 Permission denied

💡小技巧

在開發環境中,設定 storagebootstrap/cache 777(所有人可寫)雖然方便,但生產環境建議限制為 775 或更安全的方式,以免安全風險。 

 6.一行指令
既改權限,又保證 Laravel 的 storagebootstrap/cache 可以被 web server 寫入,同時保持相對安全(775)

sudo chown -R webadmin:www-data /home/webadmin/html/chcweb/storage /home/webadmin/html/chcweb/bootstrap/cache && sudo chmod -R 775 /home/webadmin/html/chcweb/storage /home/webadmin/html/chcweb/bootstrap/cache 

問題四:   ErrorException  : file_put_contents(/home/webadmin/html/chcweb/database/migrations/2026_04_18_132842_add_bg_image_to_setups_table.php): failed to open stream: No such file or directory

問題解析:

這個錯誤訊息告訴我們 Laravel 無法寫入 migration 檔案,因為目標目錄不存在或沒有權限。重點在這兩件事:

  1. 目錄 database/migrations 是否存在
  2. 你的使用者是否有寫入權限

問題解答:

1.確認目錄存在

執行:

ls -ld /home/webadmin/html/chcweb/database/migrations
  • 如果出現 No such file or directory,表示這個目錄真的不存在,需要建立:
mkdir -p /home/webadmin/html/chcweb/database/migrations

2.確認權限

Laravel 需要該目錄可寫入:

sudo chown -R webadmin:www-data /home/webadmin/html/chcweb/database/migrations
sudo chmod -R 775 /home/webadmin/html/chcweb/database/migrations

替換 webadminwww-data 為你的實際使用者與 web server 群組。

3. 再次建立 migration

php artisan make:migration add_bg_image_to_setups_table --table=setups

這次 Laravel 就能成功在 database/migrations 產生檔案了。

💡 小提醒:

  • storagebootstrap/cachedatabase/migrations 都需要對 Laravel 可寫。
  • 開發環境可以用 775,生產環境更安全的做法是限制非必要使用者寫入。

 4. 一次性指令

把 Laravel 專案的 storagebootstrap/cachedatabase/migrations 全部權限一次調整好。

sudo mkdir -p /home/webadmin/html/chcweb/database/migrations && sudo chown -R webadmin:www-data /home/webadmin/html/chcweb/storage /home/webadmin/html/chcweb/bootstrap/cache /home/webadmin/html/chcweb/database/migrations && sudo chmod -R 775 /home/webadmin/html/chcweb/storage /home/webadmin/html/chcweb/bootstrap/cache /home/webadmin/html/chcweb/database/migrations

 

 

 

 

2025年5月10日 星期六

Ubuntu 24.04 安裝 chcweb 專案的過程記錄

系列文章:
1.Ubuntu 24.04 安裝 chcweb 專案的過程記錄
2.Ubuntu 24.04 安裝 laravel 5.7 的過程記錄
3.laravel 5.7 初始相關設定與注意事項
4.想修改 chcweb 出現的問題解決集

感恩和東國小王麒富組長的指導

一、系統更新:
sudo apt-get update
sudo apt-get upgrade

二、安裝解壓縮工具、wget 與 git
sudo apt-get install unzip zip wget git

三、安裝apache2
sudo apt-get install apache2

四、安裝php 7.3 、擴充包與 apache2 模組
//安裝軟體源拓展工具
sudo apt -y install software-properties-common apt-transport-https lsb-release ca-certificates
//安裝php 7.3 ppa
sudo add-apt-repository ppa:ondrej/php
sudo apt update
//安裝php 7.3 、擴充包
sudo apt-get install php7.3 php7.3-fpm php7.3-cli php7.3-json php7.3-mbstring php7.3-gd php7.3-xml php7.3-ldap php7.3-mysql php7.3-curl php7.3-zip php7.3-imap -y
//安裝php 7.3  apache2 模組
sudo apt-get install libapache2-mod-php7.3 -y

//檢查 php 版本
php -v

五、安裝 mariadb
sudo apt-get install mariadb-server -y
sudo -i
//設定安全相關
mysql_secure_installation
exit

六、安裝composer
//下載composer
wget -c https://getcomposer.org/composer.phar

//將composer.phar設為可執行
chmod +x composer.phar

//移到/usr/local/bin,並改名composer,隨時可呼叫
sudo mv composer.phar /usr/local/bin/composer

//測試
composer

七、於html下安裝chcschool,放置在/home/webadmin/html/
cd /home/webadmin/
mkdir html
cd /var/www/
sudo mv html html2
sudo ln -s /home/webadmin/html /var/www/html    (此時 /var/www/ 有兩個目錄 html html2)
cd /home/webadmin/html/

八、建立資料庫chcweb
//建立資料庫chcweb
mysqladmin -uroot -p create chcweb

九、下載、匯入資料庫chcweb、安裝與設定 chcweb
//下載 chcweb
git clone https://github.com/wangchifu/chcweb.git
//進入chcweb目錄
cd /home/webadmin/html/chcweb
//匯入資料庫chcweb
mysql -uroot -p  chcweb < /home/webadmin/html/chcweb/database/chcweb.sql
//安裝
composer install
cp .env.example .env
sudo nano .env
.env 中 DB_DATABASE=chcweb
.env 中 DB_USERNAME 及 DB_PASSWORD 填上正確資料
php artisan key:generate
php artisan storage:link
sudo chmod 777 -R storage bootstrap/cache

帳密
帳號 admin 密碼 demo1234

十、加入apache虛擬主機
sudo nano /etc/apache2/sites-available/chcweb.conf 
寫入:
-------------------------------------------------------------------------------------
<VirtualHost *:80>
   ServerName localhost

   ServerAdmin webmaster@localhost
   DocumentRoot /home/webadmin/html/chcweb/public


<Directory /home/webadmin/html/chcweb/public>
         Options -Indexes
         AllowOverride All
         Require all granted
   
</Directory>

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

-------------------------------------------------------------------------------------

//如果要取消原本80網頁
sudo a2dissite 000-default.conf
//啟用新網頁
sudo a2ensite chcweb.conf
sudo a2enmod rewrite
sudo service apache2 restart
//更改特定目錄擁有者為www-data,及777
cd /home/webadmin/html/chcweb/
sudo chown -R www-data storage/ bootstrap/cache/
sudo chmod -R 777 storage/ bootstrap/cache/

注意網站運行的重大設定
/etc/php/7.3/apache2/php.ini
sudo nano /etc/php/7.3/apache2/php.ini
注意事項:
1.檢查已安裝的php 版本
update-alternatives --list php
2.切換php 版本
sudo update-alternatives --config php
3.確認是否切換成功
php -v
4.切換 Apache 用的 PHP 版本
(1). 停用舊版本
sudo a2dismod php8.4
(2). 啟用新版本
sudo a2enmod php7.3
(3). 重啟 Apache
sudo systemctl restart apache2
5.Mysql 注意事項
(1)創建該使用者並設置密碼:
CREATE USER 'webadmin'@'localhost' IDENTIFIED BY '密碼';
(2)如果想給這個使用者更多權限:
GRANT ALL PRIVILEGES ON *.* TO 'webadmin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;


資料來源:


在 192.168.91.x/24 網段,架設兩台DNS。當Master變更,讓client電腦對Slave 進行變更後的正解與反解

系列文章: 1.在 Ubuntu 26.04 架設網域名稱伺服器(DNS)及相關設定說明 2.在 192.168.91.x/24 網段中,架設DNS,讓client電腦進行正解與反解 3. 在 192.168.91.x/24 網段,架設兩台DNS。當Master變更,讓clien...