我以為安裝 php 和安裝 python 一樣簡單, 到 php 網站點2下就解決, 結果到了 php 網站, 完全看不懂要下載那一個才是 windows 的安裝檔, 結果一問 AI, 原來要用第三方的工具才是正解, 太神奇了。
xampp windows 下載點:
https://sourceforge.net/projects/xampp/files/XAMPP%20Windows/
實際使用之後, 發現在 Win11 上使用 xampp 很容易 crash, 請改用 xampp-lite 64bit
https://sourceforge.net/projects/xampplite/
如果你想讓整個 windows 任意路徑都可以執行 php, 請增加 path 變數:
C:\xampp_lite\apps\php;C:\xampp_lite\apps\mysql\bin;
如果安裝 xampp-lite, 請在系統變數增加這個項目:
XAMPP_LITE_ROOT=c:/xampp_lite
不知道為什麼 xampp_lite 的 apache 一直遇到問題, 請改用下列的 httpd.conf 設定檔:
Define SRVROOT "C:/xampp_lite/apps/apache"
ServerRoot "C:/xampp_lite/apps/apache"
Listen 80
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule allowmethods_module modules/mod_allowmethods.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule dav_lock_module modules/mod_dav_lock.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule include_module modules/mod_include.so
LoadModule info_module modules/mod_info.so
LoadModule isapi_module modules/mod_isapi.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule cache_disk_module modules/mod_cache_disk.so
LoadModule mime_module modules/mod_mime.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule status_module modules/mod_status.so
LoadModule version_module modules/mod_version.so
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>
ServerAdmin postmaster@localhost
ServerName localhost:80
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "C:/your-path/web"
<Directory "C:/your-path/web">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error.log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access.log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "C:/xampp_lite/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "C:/xampp_lite/cgi-bin">
AllowOverride All
Options None
Require all granted
</Directory>
<IfModule headers_module>
RequestHeader unset Proxy early
</IfModule>
<IfModule mime_module>
TypesConfig conf/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddHandler cgi-script .cgi .pl .asp
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
<IfModule mime_magic_module>
MIMEMagicFile "conf/magic"
</IfModule>
Include conf/extra/httpd-mpm.conf
Include conf/extra/httpd-autoindex.conf
Include conf/extra/httpd-languages.conf
Include conf/extra/httpd-userdir.conf
Include conf/extra/httpd-info.conf
Include conf/extra/httpd-vhosts.conf
Include "conf/extra/httpd-default.conf"
Include "conf/extra/httpd-xampp-lite.conf"
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>
Include conf/extra/httpd-ssl.conf
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
AcceptFilter http none
AcceptFilter https none
<IfModule mod_proxy.c>
<IfModule mod_proxy_ajp.c>
</IfModule>
</IfModule>
請手動修改上面的 C:/your-path/web 為你實際的 web root path.
在 Windows 上安裝 PHP 其實不難,主要有手動安裝和使用整合軟體兩種方式。如果你是初學者,建議用整合軟體會省掉很多設定麻煩。
使用整合軟體(推薦初學者)
這類軟體通常叫做 XAMPP 或 WAMP。它們把 PHP、資料庫 MySQL 和網頁伺服器 Apache 打包在一起,點幾下滑鼠就裝好了。
- 到 XAMPP 官網下載安裝檔。
- 執行安裝程式,基本上一直點下一步就好。
- 預設會裝在 C 槽的 xampp 資料夾。
- 安裝完打開 XAMPP Control Panel,點擊 Apache 後面的 Start。
- 把你的 PHP 檔案丟到 C:\xampp\htdocs,用瀏覽器打開 localhost 就能看到結果。
手動安裝步驟
如果你想要自己掌控版本,可以選擇手動安裝。這會需要一點點電腦路徑的操作。
- 到 PHP for Windows 官網下載 Zip 壓縮檔,記得選 Thread Safe 版本。
- 在 C 槽建立一個資料夾叫 php,把壓縮檔裡的所有東西解壓縮進去。
- 進入該資料夾,把 php.ini-development 改名成 php.ini。
- 設定環境變數。打開電腦的進階系統設定,找到環境變數裡的 Path。
- 新增一條路徑,填入 C:\php。這讓你能在任何地方用終端機執行 PHP 指令。
- 打開 CMD 輸入 php -v,如果有顯示版本號碼就代表成功了。
手動安裝雖然比較乾淨,但如果你還要架資料庫,建議還是用整合包比較快。
想了解怎麼在 VS Code 裡面跑 PHP 嗎?
如果你在安裝過程中遇到困難,這段影片示範了如何在 Windows 10 或 11 上手動下載並安裝 PHP 的完整流程:Windows PHP 手動安裝教學