Termux

How to install Apache and PHP 7 on Android with Termux

Install web server and process PHP files: build a LAMPP stack on Android

This is not a post of those who use tricks to attract visitors, we are really going to install an Apache server on Android, configure it to process PHP files and finally write a Hello World: everything running from our mobile device.

PHP running on Android

Summary

You will learn to configure Apache and PHP in Android without the phone being rooted and without you dying in the attempt, thanks to the application of Termux and some packages.

Requirements

Look at this tutorial to install and configure Termux, as well as to learn its basics and take out the help keys.

Step 1: update packages

When you have installed termux, execute these commands:

apt update

And then:

apt upgrade

If it asks you, choose yes in both cases.

Step 2: Install Apache and PHP 7

There is already a package that serves to install these two things together. That is, through apache PHP files are processed. To install our LAMPP on Android we will run:

apt install php-apache

That will install apache, PHP and some libraries that will allow us to combine both things.

Install PHP & Apache on Android

With that we have everything ready, we need to fine-tune the configurations.

Step 3: Install Nano

Nano is a text editor that works in the terminal. It is important to install it because we will have to modify the apache configuration file. To install it simply run this command:

apt install nano

The basic commands are to press Ctrl + o to save changes and Ctrl + x to exit. Ctrl + w is used to search the file; and if you want to learn more, look for the manual in Google.

For the keys like TAB and CTRL to appear, look at the link that I left above for you to configure Termux as it should.

Step 4: 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 dir with:

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

Then open the file with nano:

nano httpd.conf

Here is a screenshot of what the directory where this important file looks like:

httpd.conf file

In this file we will modify some things.

4.1 Load PHP module

In the file that we are editing we will look for a section where the modules are loaded, you should see something like this:

Where to load PHP 7 module

Right there we are going to add a new line with this content:

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

What we are doing is loading the PHP 7 module that is in the path that is above. Although the image does not look complete and you see that there is a line break ignore it; put the command that I put as is.

Load PHP 7 module on httpd.conf

4.2 Set handler

That’s not all, now below where we load the module we add this:

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

We are telling Apache that files that comply with a regular expression (where the file is one of PHP) are processed by a handler.

Set handler for PHP files

4.3 Change index

To finish (and this is not so necessary) we will look for the fragment where is a code like the following:

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

We are going to change it to PHP so that it looks like this:

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

We do this to tell apache to serve index.php over index.html (this is an adjustment and that does not mean we can not serve HTML).

For example, if we visit site.com apache will default to index.html, instead this adjustment will serve index.php. Save changes and close the file.

Step 5: write hello world PHP and see public directory

Our htdocs folder is located in (I mean, its route is):

/data/data/com.termux/files/usr/share/apache2/default-site/htdocs

So whatever thing or file we put there will be served. And if we put a PHP file it will be processed. We navigate there and create the index.php with:

nano index.php

Inside it we put the following content:

<?php
phpinfo();

Then we save it.

htdocs path on Termux

The image above is only to show where is htdocs located.

Step 6: Start apache

What remains is to start the apache daemon. For this we use the command:

apachectl start

If it does not show errors (apart from the one that says that you can not determine the domain server as seen in the image) then everything is fine.

Start Apache server on Android

Update 2020: error starting apache on android

These days I have been reported some errors. If the following appears to you, please continue reading this section; If not, continue with the next step:

apache is running a threaded mpm but your php module is not compiled to be threadsafe

To fix it, we need to edit the httpd.conf file mentioned above, on the lines that contain “LoadModule”. We must comment the following line:

LoadModule mpm_worker_module libexec/apache2/mod_mpm_worker.so

So it looks like this:

#LoadModule mpm_worker_module libexec/apache2/mod_mpm_worker.so

In addition, we uncomment the following line:

#LoadModule mpm_prefork_module libexec/apache2/mod_mpm_prefork.so

So it looks like this:

LoadModule mpm_prefork_module libexec/apache2/mod_mpm_prefork.so

Here is an image of what it should look like:

LoadModule httpd.conf – fix error apache on Android

That should fix the error.

See server on web browser

We open Chrome (or our browser) and we go to:

localhost:8080/index.php

You will see something like this:

PHP running on Android

We have achieved it; that is the information of the PHP version that we have installed on our Android system.

As you can see, we have PHP version 7.2.12; and the Apache server obviously works. In fact, from another computer that is in the same network of your Android you can put the IP address of the same in port 8080 and you will see that it connects:

Connect to remote Apache server running on Android

Conclusions and notes that you should not ignore

Remember that you can run PHP files from the terminal as we do in Windows (with php -f). You can also enter the interactive mode with php -a

To work with the apache daemon you can run apachectl start, apachectl stop and apachectl restart to start it, stop it and restart it respectively.

It should be mentioned that in the htdocs folder you can put any file (image, video, text file, HTML file, etc.) and it will be served as it is normally done.

With this you already have a LAMPP stack on Android: Linux, Apache, MySQL and PHP. You can mount any application that you would mount on a server with Linux.

If you want to know how to install the MariaDB server on Android visit: install MySQL on Android.


I am available for hiring if you need help! I can help you with your project or homework feel free to contact me.
If you liked the post, show your appreciation by sharing it, or making a donation

parzibyte

Freelancer programmer ready to work with you. Web, mobile and desktop applications. PHP, Java, Go, Python, JavaScript, Kotlin and more :) https://parzibyte.me/

Ver comentarios

  • thank you parzibyte for this tutorial, can you help me for this error : AH00526: Syntax error on line 224 of data/data/com.termux/files/usr/etc/apache2/httpd.conf:
    invalid command 'Require' perhaps misspelled or defined by a module not included in ther serber configuration.
    and this line error in httpd.conf
    222:
    223: AllowOverride none
    224: Require all granted
    225:

  • Me again. On restarting termux, apache2 is reporting an error with the httpd.pid file: AH00058 : Error retrieving pid file /ar/run/apache2/httpd.pid. and the php server will not run. I have been able to fix this by renaming the httpd.pid to httpd.pid.bak, and then restarting the httpd service. If the problem persists I will probably use Termux:Boot to run a script at startup. UNless you know of a way to prevent this from happening ?

  • You forgot to mention the need to replace php7_module to php_module and libphp7.so to libphp.so like this:

    LoadModule php_module libexec/apache2/libphp.so

    replacing the old line:

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

    I hope this helps someone installing ew versions of termux and php-apache respectively.

  • update for php 8

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

    be

    LoadModule php_module /data/data/com.termux/files/usr/libexec/apache2/libphp.so

Entradas recientes

Receipt designer for thermal printers – Free and open source

In the last months I have been working on a ticket designer to print on…

11 months hace

JavaScript: store and read files with the Origin Private File System

In this post you will learn how to use the Origin Private File System with…

1 year hace

JavaScript: download file with fetch

In this post you will learn how to download a file in the background using…

1 year hace

SQLite3 with vanilla JavaScript and OPFS – Hello world

In this post I will show you how to use SQLite3 directly in the web…

1 year hace

Python Thermal Printing: A Comprehensive Guide for Printing on Thermal Printers

In this tutorial, we'll explore how to effortlessly print receipts, invoices, and tickets on a…

1 year hace

Image printing on Thermal printer

When printing receipts on thermal printers (ESC POS) sometimes it is needed to print images…

1 year hace

Esta web usa cookies.