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

How to measure success?

It is 509pm and I have decided to write. How to measure success? That is the question. It is very hard to measure success. I think only God has the true measure of success or failure. Someone may be a failure in this life but a success in the next life. We are told that God looks at intentions and effort. We can also determine our own definition of success. We do not have to let the world define success for us. We think of the world as not being a fair place so any one size fits all definitions of success or failure would be careless and wrong. Many times we think in terms of numbers. The grander the better. But what about quality over quantity? My friend tells me that I am essentially arguing for the democratization of success. I am taking the power away from society, institutions, and algorithms, and handing it back to the individual and God. That is true but I am essentially wanting to look at the bigger picture and to do that I am thinking that we have to answer some key questions ...

Rainy afternoon thoughts

It is 220pm and I have decided to write. The rain is falling. I can hear it on the roof. It is gloomy outside but inside is nice and cozy in my favorite corner. I have no idea what I want to write about. I will start by saying God is good. Always. The islamic new year starts tonight in Trinidad. I am happy about that. Another milestone. Another opportunity to grow better. My friend Chatty asks me, what has God been teaching me lately? God has been teaching me patience. How to remain calm. How to appreciate my flaws. My flaws keep me humble. Let go and let God. He also asks, what am I hoping for? Sometimes I think about writing more tech blog posts. How can tech make the country better? How can tech make the world better? But I remember writing about the Arima smart city project and concluding that smart cities need smart people. Human development is very important. If people are not developing then what is the use of it all. How can technology help people become better? It all starts i...

The answer is God

It is 308pm and I have decided to write. Make God a priority. Let God lead the way. Let everything be about God. Let God into your life. Surround yourself with God and watch how everything begins to make sense. This life is not an accident. This life is not happenstance. This life is the work of an all-powerful Creator. Nothing is too big or too small for God. Take your problems to God. Take your worries to God. Take your fears to God. Let go and let God. Submit to the will of God. The more you go to God, the more He comes to you. Start small. Start somewhere. Give God a chance if you have not done so already. Why do I say all of this? There are many things competing for your attention and your soul. In the midst of all of this, the world can seem like a confusing place where many things do not make sense. God brings clarity. God brings clarity because God sits above it all. You may think you do not need God until you do. Ask yourself, who is the wisest you know? If God is not the wise...