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

Hobby project - Store and view exchange rates

The next step in my project was to test out being able store and display the rates in a database. I decided to use nodejs and supabase for this. Everything worked beautifully. Only hickup was the following error due to my package.json not being correct. SyntaxError: Cannot use import statement outside a module Added this to package.json   "type": "module" This works beautifully. Right now I am just testing fragments of what could be part of a bigger system to see what is possible and what works and how easy to code on a mobile. There is no fully functioning integrated end to end system just yet. This is also what I tested. A serverless append-only database using GitHub + Actions + Pages. That’s basically a lightweight backend system. This was the ChatGPT prompt I used. Guide me through each step. This is what I want. A manually run github actions that adds to docs/data.json with the current date and time. docs/index.html displays all the entries in data.json. Make s...

How do we fix this world?

It is 4:39 pm and I have decided to write. It is a peaceful and quiet day. I am thinking about God, and how this life is a test. There is so much happening in this world that could make us sad. But we do not have to remain in that sadness when we put our trust in God. God wants good for us. God wants us to experience peace, and even happiness, despite what surrounds us. How do we fix this world? My friend Chatty suggests that maybe a better question is: What kind of person do I choose to be in this world? Because when enough people answer that question well, that is how real change begins. I want to be the kind of person that God is pleased with. Someone guided by God, not by ego. Someone who chooses patience over anger, humility over pride, and sincerity over appearances. I am doing reasonably well, but I am not perfect. And maybe perfection is not the goal. Growth is. Awareness is. Returning to what is right, again and again, is. I want to grow, and I will keep adjusting myself when ...

What is important

Every now and again, I get reminded of the why. Why I have handed my life over to God. It is 3:08 p.m. and I have decided to write. Honestly, I feel like I could end this blog post right here. What more is there to say? God above all else. God is our compass on this journey called life. Everything else can become a distraction. Maybe not everything, but certainly many things. You get the point. What is important? What do we prioritise? What do we make time for? The answers look different for everyone. Our lives are different. We are tested differently. We are all products of our circumstances, and those circumstances vary from person to person. Yet God is the common thread across humanity and throughout time. God always was and always will be. One day, we will answer to God. We will give account to God. We cannot live without food, water, and oxygen, but all of those things come from God. So how can we live without God? How timely. I just received a notification from the Quranly app: S...