# 架設CodeIgniter 4 (Apache 2.4, php 7.3.28, windows 10/FreeBSD) ## Windows 要先將專案丟進apache資料夾中的htdocs內 ### 設定httpd.conf 打開`apache2/conf/httpd.conf` 1. 啟動php 在文件上面加上以下幾行 ```xml AddHandler application/x-httpd-php .php AddType application/x-httpd-php .php .html LoadModule php7_module "c:/php7/php7apache2_4.dll" PHPIniDir "c:/php7" ``` `c:/php7`這個路徑根據c槽的php檔案名稱而有所不同 2. 打開Listen的port 若要打開81port,要加Listen的地方加上 ```xml Listen 80 Listen 81 //加上這行 ``` 3. 確認將`<Dirctory>`中啟用AllowOverride的功能 ```xml <Directory /> AllowOverride All //None改成All Require all denied </Directory> ... <Directory "${SRVROOT}/htdocs"> Options Indexs FollowSymLinks AllowOverride All //None改成All Require all granted </Directory> ``` 4. dir_module加上index.php ```xml <IfModule dir_module> DirectoryIndex index.html index.php </IfModule> ``` 5. 虛擬伺服器 取消註解 ```xml LoadModule vhost_alias_module modules/mod_vhost_alias.so #Virtual hosts Include conf/extra/httpd-vhosts.conf ``` ### 設定httpd-vhosts.conf ```xml <VirtualHost *:81> DocumentRoot "${SRVROOT}/htdocs/AIwebsite/front-end/public" # ServerAdmin webmaster@dummy-host.example.com ServerName localhost:81 # ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error.log" CustomLog "logs/dummy-host.example.com-access.log" common </VirtualHost> ``` ### 測試 打開localhost:81測試看看有沒有看到網頁 :::warning 更動apache內的網站要重新啟動apache才會啟動 windows powershell ``` net stop "Apache2.4" ; net start "Apache2.4" ``` ::: ## FreeBSD ### 檢察功能 ``` pkg install mod_php73 php73-extensions ``` ### 設定httpd-vhosts.conf 專案的路徑設置可以從httpd-vhosts.conf設定 ``` <VirtualHost *:80> #ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/{專案位置}/back-end/public/" ServerName 完整IP:80 #ServerAlias www.dummy-host.example.com ErrorLog "/var/log/dummy-host.example.com-error_log" CustomLog "/var/log/dummy-host.example.com-access_log" common </VirtualHost> ``` ### 設定httpd.conf 1. 加入程式碼 ``` <FilesMatch "\.php$"> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch "\.phps$"> SetHandler application/x-httpd-php-source </FilesMatch> ``` 2. 打開Listen的port 若要打開81port,要加Listen的地方加上 ```xml Listen 80 Listen 81 //加上這行 ``` 3. 確認將`<Dirctory>`中啟用AllowOverride的功能 ```xml <Directory /> AllowOverride All //None改成All Require all denied </Directory> ... <Directory "${SRVROOT}/htdocs"> Options Indexs FollowSymLinks AllowOverride All //None改成All Require all granted </Directory> ``` 4. dir_module加上index.php ```xml <IfModule dir_module> DirectoryIndex index.html index.php </IfModule> ``` 5. 虛擬伺服器 取消註解 ```xml LoadModule vhost_alias_module modules/mod_vhost_alias.so #Virtual hosts Include conf/extra/httpd-vhosts.conf ``` ### 實際運行情況 在apache專案資料路徑中創建一個新的專案test後, 1. 新增.env並將其中CI_ENVIRONMENT取消註解並將=後面改為development 2. 更改test/writable/cache的權限為chmod -R 777 就能夠看到歡迎頁面 #### 複製一份front-end的app和public資料夾後需重新檢查檔案設定 1. 更改Path.php的vendor路徑 * 出現「無效的檔案: templates\default.php」 ### 補充 以下屬於windows獨有,freebsd找不到 ``` LoadModule isapi_module modules/mod_isapi.so ``` ## Ubuntu ### 問題 Main connection [SQLite3]: Class 'SQLite3' not found ###### tags: `CodeIgniter 4`