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

Failure

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for failure. Thank you for the good days and the bad days. Thank you for the journey. There are no failures, only lessons learnt. Better to try and fail than fail to try. For this chapter I made a bunch of notes and pieced them together. If mosaic was a style of writing, this would be it for me. Weirdly enough I am not afraid of failure as much as I am afraid of success. I want to live an ordinary and simple life. Much of what society deems as success is really the rat race. I guess I do not have a problem with success but the mainstream definitions of success. What does failure teach us? Failure teaches us to never give up because success can be right around the corner. Define your own definition of success. Enjoy the journey. Focus on learning. Focus on gaining experience. Focus on becoming wise. Today I learnt how a pearl is formed. A pearl forms when an oyster responds...

Wisdom

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for wisdom. Age brings wisdom. At least it should. Everyday we are learning. Everyday we are growing. What is the wisest thing ever said? I think one of the wisest things ever said is something my grandfather told my mother which is, "Dont make nothing a problem." Let me digest what was meant by this. Life happens. Fix what we can. Let go of what we cannot fix. Fix what is within our control. Get help when needed. Let go and let God. There are no problems only solutions waiting to be found and lessons waiting to be learnt. Every problem has a solution. Do not look at the problem, look at the solution. Do not see the problem, see the solution. What defines the situation is not the problem but the solution. We get to choose how we react to what we face in life. They say that laughter is the best medicine. If we do not laugh we will cry. So sometimes wisdom comes wi...

Faith

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for faith. We say we want to meet God but we already do in some ways. We meet God in his creation. We meet God every time we meet someone. We meet God every time we meet the animals. We meet God every time we meet nature. God is all around. Faith is when we can see this. This is God's world. We are God's work. We are testimony of his greatness. Everyday that goes by I think about God. Everyday I thank God for his mercy. I take my problems to God. I pray to God. God is at the center of my life. Nothing happens without the will of God and therefore I would be wise to submit to the will of God. God is in the everyday moments. God loves when we love. What is the simplest definition of faith? I think it is found in the phrase, let go and let God. I asked my friend the same question. My friend says that at its simplest, faith is trust in what you cannot see or prove. But...