php apache setup on android with Termux (without root)

Termux Setup Steps

  • update packages (If it asks you, choose yes in both cases.)
    apt update -y && apt upgrade -y
  • Install Apache and PHP 7

There is as of now a bundle that serves to introduce these two things together. That is, through apache PHP documents are prepared. To introduce our LAMPP on Android we will run:

apt install php-apache

That will install apache, PHP and a few libraries that will permit us to join the two things.

  • configure apache to process the PHP files

We are going to configure the httpd.conf file. Attention here, because the route is important. The apache configuration file is in /data/data/com.termux/files/usr/etc/apache2/httpd.conf

You can change to that folder with:

cd /data/data/com.termux/files/usr/etc/apache2/

Then open the file with nano:

nano httpd.conf
  • Load PHP module

on httpd.conf, scroll down until you see string start with LoadModule. Then add new line and type bellow code:

LoadModule php7_module /data/data/com.termux/files/usr/libexec/apache2/libphp7.so
  • Set handler

Add new line after LoadModule php7_module and type bellow code:

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

We are revealing to Apache that records that conform to a standard articulation/regex (where the document is one of PHP) are prepared by a controller.

Now above steps looks like: Preview

  • Change Index

Scroll again, and find text contains DirectoryIndex index.html. Looks like bellow:

<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

Change extension html to php, seem looks like bellow:

<IfModule dir_module>
DirectoryIndex index.php
</IfModule>

We do this to advise apache to serve index.php over index.html (this is a change and that doesn't mean we can not serve HTML).

For instance, in the event that we visit example.com apache will default to index.html, rather this change will serve index.php. Save changes and close the record. CRTL+X -> Save Dont Rename

  • Write first index.php on public directory

Our htdocs located in /data/data/com.termux/files/usr/share/apache2/default-site/htdocs. Just type bellow code:

cd /data/data/com.termux/files/usr/share/apache2/default-site/htdocs
nano index.php

Write your first php code or any text. I suggest type bellow code:

<?php
phpinfo();
?>

Now save it. CTRL+X -> Save don't rename

  • Start apache

To start apache daemon, type bellow commands:

apachectl start

On the off chance that it doesn't show errors at that point all is well.

apache is running a threaded mpm but your php module is not compiled to be threadsafe, How to fix ? See here
Fix error retrieving pid file apache. See here