php apache setup on android with Termux(without root)
***downloading and intalling termux
go to given link and choose a suitable version of termux for your device.
then allow installation from unknwon sources and install the termux app. After installation continue the other steps:
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.
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
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:
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.
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.
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:
Then we save it.
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.
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
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:
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.
No comments:
Post a Comment