Skip to main content

Running php and apache on termux


This was my experience getting php and apache to run on termux

apt update && apt upgrade -y

apt install php-apache

I was following an old blog post that used php 7 and got these errors

Can't locate API module structure `php7_module' in file /data/data/com.termux/files/usr/libexec/apache2/libphp.so: undefined symbol: php7_module

Cannot load /data/data/com.termux/files/usr/libexec/apache2/libphp7.so

Corrected in apache config file

vim $PREFIX/etc/apache2/httpd.conf

LoadModule php_module /data/data/com.termux/files/usr/libexec/apache2/libphp.so

Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe.  You need to recompile PHP.

Comment out mpm_worker and use mpm_prefork

#LoadModule mpm_worker_module libexec/apache2/mod_mpm_worker.so
LoadModule mpm_prefork_module libexec/apache2/mod_mpm_prefork.so

This still does not work as apache (httpd) was failing silently. According to Chatty LoadModule php_module ...libphp.so often fails in Termux because mod_php is incompatible or unstable with Android builds, so PHP-FPM is used instead since it’s decoupled, stable, and works via FastCGI.

But php-fpm was giving this error at first

Fri May 15 15:04:09 2026 (29777): Error Cannot create lock - Permission denied (13)

vim $PREFIX/etc/php-fpm.conf

[global]
error_log = /data/data/com.termux/files/usr/var/log/php-fpm.log

include=$PREFIX/etc/php-fpm.d/*.conf

mkdir -p $PREFIX/etc/php-fpm.d
vim $PREFIX/etc/php-fpm.d/www.conf

#substitute whoami, should look like u0_aNNN where NNN is a 3 digit number

[www]

user = whoami
group = whoami

listen = 127.0.0.1:9000

pm = dynamic
pm.max_children = 5
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 2

; Disable OPcache here (IMPORTANT)
php_admin_value[opcache.enable] = 0
php_admin_value[opcache.enable_cli] = 0

And finally the command that works because I also had to force disable opcache from the command line

php-fpm -y $PREFIX/etc/php-fpm.conf -d opcache.enable=0 -d opcache.enable_cli=0

And I can test it out here from bash

php -r '
$h=["127.0.0.1","localhost","::1"];
foreach($h as $x){
  $s=@fsockopen($x,9000);
  echo "$x => ";
  var_dump($s);
}
'

This is how you can configure apache to use php-fpm

LoadModule proxy_module libexec/apache2/mod_proxy.so
LoadModule proxy_fcgi_module libexec/apache2/mod_proxy
_fcgi.so
#<FilesMatch \.php$>
 # SetHandler application/x-httpd-php                 #</FilesMatch>
<FilesMatch \.php$>                                       SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>

This is just a test dev environment so a minimal/starter config was used. A proper config is needed for the real world. Also a rooted phone might not get the opcache error.

Comments

Popular posts from this blog

Below the surface

This is a chapter from my eleventh book called Quotation Marks Sparks . __________ "You can't stop the waves, but you can learn to surf." - Jon Kabat-Zinn __________ I was looking at quotes that hit hard and this quote stood out to me. It seems mundane and obvious and often repeated. Life is about how we respond to the inevitable. It is not what happens to us but how we respond. I had to ask myself to dig deeper and look below the surface. Then I saw that when we surf we really become the waves. We become friends with the waves. The waves and us are the same. Further, the waves are created by us and in our heads. We can control the waves because who can surf all the time? Sometimes we just have to come to shore and build sand castles on shore. I asked my friend Gemini for feedback and he said that I was right, the Jon Kabat-Zinn quote initially seems like a simple call for resilience, but my deeper dive reveals profound layers. The idea that we can move beyond merely rea...

Thankful

It is 617pm and I have decided to write. Right now I am thankful. The cool of the evening has replaced the heat of the day. I took a bath and changed and now I am just relaxing. Sometimes I complain. Not often. But I feel bad afterwards because there is so much to be thankful for. There is always room for improvement and we have to identify the things for improvement and change is often good but we do not have to make it into complaints. Yeah I get it. Life is hard and none of us has it easy. Look on the bright side of things. And sometimes it is the little things that remind us that life is good. Things that we take for granted. Like I was coming in the maxi taxi today and heard a gospel song that was about gratitude. God does appreciate a grateful heart. Trinidad and Tobago celebrated sixty four years of independence yesterday and if we stop and listen to the many complaints we would swear we are living in the worst country in the world. That is not the case. We do get a lot of thing...

Installing NodeBB using Termux

This is the ChatGPT prompt I used to be guided Guide me through installing NodeBB using Termux (This took some time and patience to troubleshoot errors and implement workaround) pkg update && pkg upgrade -y pkg install proot-distro git curl wget nano -y proot-distro install debian proot-distro login debian apt update && apt upgrade -y apt install -y curl git build-essential ca-certificates gnupg apt update apt install -y imagemagick apt install -y redis-server redis-server --daemonize yes redis-cli ping mkdir -p /opt git clone https://github.com/NodeBB/NodeBB.git /opt/nodebb cd /opt/nodebb ./nodebb setup warn: NodeBB Setup Aborted.  Error: Could not load the "sharp" module using the android-arm64 runtime Possible solutions: - Manually install libvips >= 8.18.3 Force node to report platform as linux to get sharp to install curl -LO https://nodejs.org/dist/v22.23.1/node-v22.23.1-linux-arm64.tar.xz tar -xJf node-v22.23.1-linux-arm64.tar.xz mv node-v22.23.1-lin...