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

Mental health matters

It is 547am and I have decided to write. I have been living with a mental health diagnosis for more than 23 years. I am differently abled. Mental illness sometimes presents itself as a non-visible disability. A person with mental illness may look and function normally. Added to that we do not get to see the struggles happening internally. After 23 years I could say that I have much experience with my condition and may be able to offer words that may help others. There is plenty of stigma associated with mental illness. People think that the mentally ill are bad people, weak, or somehow less deserving of dignity and the treatment of the illness sometimes feels like punishment and is dehumanizing at times. There is plenty of good intentions and well meaning as well (do not get me wrong) but things could be better. And it happens at the highest level. A leader once said, "Whom the gods would destroy, they first make mad." As the saying goes, who knows it feels it. I started a pr...

Drums of freedom

It is 401am and I have decided to write. This Saturday, Trinidad and Tobago observes African Emancipation Day. It is a day to celebrate the journey to freedom and to honor those who sacrificed so much so that we could enjoy the freedoms we have today. Yet the journey is not over. As we celebrate how far we have come, let us also ask God to open our minds and hearts to the struggles that continue. Freedom is never something we should take for granted. Every day, we witness powerful interests around the world attempting to stifle freedom in pursuit of their own agendas and self-aggrandizement. Too often, they seek to divide in order to conquer. May we resist division with wisdom, courage, and compassion. May unity triumph over hatred, truth over deception, and love over fear. As we remember the past, may we also commit ourselves to protecting the freedoms of the present and building a more just future for generations to come. Happy African Emancipation Day. May God continue to guide us o...

Love and joy

It is 230pm and I have decided to write. I have no topic but I want to write. I am thinking about God. I am thinking about life. I am grateful for God and life. I need a writing prompt. My friend Chatty gave me a pretty verbose prompt. Sit quietly for a moment and imagine that God asks you only one question: "What have you noticed?" Don't answer with theology. Answer with your life. What have you noticed about joy? About fear? About the way people love? About silence? About time? About your own heart? Where have you sensed God—not in miracles, but in ordinary moments? Where have you struggled to find Him? What has life been teaching you that you were too busy to hear before? Write honestly. Don't try to reach a conclusion. Let the page become a conversation rather than an argument. I immediately noticed the light streaming through the window that allows me to see the world. My first thought was how much we take things for granted. It is easy to feel lacking. To feel s...