How To Configure PHP-Apache To Run PHP Scripts In Apache2 Server In Termux?Poated on by
Learn to set up PHP-Apache in Termux for running php scripts as by default Apache serves only static files. check out both auto and manual configuration.
Table Of Contents
What Is PHP-Apache In Termux Android?
PHP-Apache in Termux Android is a popular Open Source Software PHP and Apache2 HTTP Server for Web Development. PHP is Server side Scripting Language that developers used to create dynamic webpages, while Apache2 is web server used to host and run php scripts and handle http request and responce mechanism.
In Termux on Android device the php-apache package installs all the componants but you have to configure it before you run PHP scripts in Apache2. In this blog post I explained the easiest method to configure and explained to make PHP-Aapache run in Termux on Android Device. So lets get dive.
The PHP-Apache can be installed in Termux so you have to download Termux first on your Android Device.
Update and Upgrade Termux repository by running these commands.
Install PHP-Apache Package
pkg install php-apache
This will install all componants in Termux now you just need to configure Apache2 configuration.
Insted of touching main config file $PREFIX/etc/apache2/httpd.conf we will create new config file that will be included in main config file. By this way we can avoid unexpected errors.
Create new file in $PREFIX/etc/apache2/conf.d/
Copy the following code to that file and save.
AddHandler php-script .php ServerName 0.0.0.0 <IfModule dir_module> DirectoryIndex index.php </IfModule> LoadModule php_module libexec/apache2/libphp.so <IfModule !mpm_worker_module> LoadModule mpm_prefork_module libexec/apache2/mod_mpm_prefork.so </IfModule>
Press CTRL + S and CTRL + X to save and exit nano text editor.
Run apachectl command, there is high chance you encounter the following error.
This is because Apache is running the mpm_worker_module and the PHP Module you get in termux is not compatible with it. I already added the compatible module mpm_prefork_module in the configuration but we need to remove mpm_worker_module from original httpd.conf file. Because there should be exactly one mpm module loaded.
Run following command to fix this error.
sed -i 's|LoadModule mpm_worker|#LoadModule mpm_worker|g' $PREFIX/etc/apache2/httpd.conf
Boom!!! The configuration is done now the Apache2 HTTP Server is able to run PHP Scripts.
The default Document Root of Apache2 HTTP Server is $PREFIX/share/apache2/default-site/htdocs/
Create sample php script to test PHP-Apache.
Write following code and save it.
<?php phpinfo(); ?>
Start Apache2 Server
apachectl start Or simply apachectl
Open the following link in your web browser http://localhost:8080
There is high chance that the Apache2 Server Loads html file not php. Try http://localhost:8080/index.php or simply remove index.html file.
After opening the link in web browser you can see PHP Information. Here you can see the Server information on which PHP is running.
In conclusion, installing PHP-Apache in Termux on Android provides a way to develop and test web applications locally on Android. It’s a convenient option for learning and experimentation for those who love to learn but does not effort computers.