program:php:linux_執行_php_檔案
目錄表
Linux 執行 php 檔案
在一般情況下 PHP 是用在網站上,而有些情況需要在文字模式下執行,例如用 PHP 寫 shell script 或需要用 crontab 排程執行等。而在文字模式下執行 PHP 也很方便
以下是幾種實現方法:
方法一:
- 在 PHP 程式的第一行加入:
#!/usr/bin/php -q <?php $foo = 123; ?>
請留意,我的 PHP 是路徑在 /usr/bin/php,請根據你的 PHP 執行檔位置作出修改。
- 將 PHP 程式給予可執行權限:
chmod +x testing.php
- d\n
./testing.php # 可以像其他 shell script 般執行
方法二:
/usr/local/php/bin/php -q shell_script.php
如果你覺得 /usr/local/php/bin/php 很長的話,可以將 /usr/local/php/bin/php 覆製到 /usr/bin/ 目錄下:
cp /usr/local/php/bin/php /usr/bin/php php -q shell_script.php # 現在可以這樣執行
方法三:
透過像一般網頁的方法載入 PHP,可以用 curl 或 lynx 等工具,以下會以 curl 為例子。
如果沒有安裝 curl, 首先需要先安裝 curl:
RHEL / CentOS:
# yum install curl
Debian / Ubuntu / Mint:
# apt-get install curl
安裝好 curl 後,執行並輸入以下內容:
curl -l http://www.yourdomain.com/testing.php > /dev/null 2>&2
program/php/linux_執行_php_檔案.txt · 上一次變更: 2019/11/16 08:12 由 127.0.0.1