Termux安装php


安装命令

pkg install php

查看php版本

php -v
PHP 8.3.10 (cli) (built: Aug 29 2024 15:46:34) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.10, Copyright (c) Zend Technologies

查看php安装的扩展库

php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
FFI
fileinfo
filter
gmp
hash
iconv
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
random
readline
Reflection
session
SimpleXML
sockets
SPL
sqlite3
standard
tidy
tokenizer
xml
xmlreader
xmlwriter
xsl
zip
zlib

[Zend Modules]

如果没有安装php-fpm,可以执行以下命令安装

pkg install php-fpm

php-fpm配置文件:/data/data/com.termux/files/usr/etc/php-fpm.d/www.conf

查看php-fpm监听地址

cat /data/data/com.termux/files/usr/etc/php-fpm.d/www.conf
listen = /data/data/com.termux/files/usr/var/run/php-fpm.sock

记住上面的地址,Nginx运行php需要配置那个地址。

修改Nginx配置文件,让其可以运行php

vim /data/data/com.termux/files/usr/etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  localhost;
        location / {
            root   /data/data/com.termux/files/usr/share/nginx/html;
            index  index.html index.htm index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /data/data/com.termux/files/usr/share/nginx/html;
        }
        location ~ \.php$ {
           root           /data/data/com.termux/files/usr/share/nginx/html;
           fastcgi_pass   unix:/data/data/com.termux/files/usr/var/run/php-fpm.sock;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /data/data/com.termux/files/usr/share/nginx/html$fastcgi_script_name;
           include        fastcgi_params;
        }
    }
}

主要是以下几个地方需要修改

启动php-fpm

php-fpm

查看php-fpm是否启动,可以执行top命令

top

重启Nginx

nginx -s reload

在Nginx网站目录下新建index.php文件

index.php文件目录:/data/data/com.termux/files/usr/share/nginx/html

代码:

<?php
phpinfo();

访问这个文件:http://192.168.1.101:8080/index.php

显示这个页面,说明php运行成功