How to install PHP in your Android Device

How to install PHP in your Android Device

Table of contents

No heading

No headings in the article.

Hello everyone, so you want to start your backend journey and you've decided to learn PHP, but your system is having an issue ๐Ÿ˜ข, or you just saw one new function in a blog you read and you want to try out the function to have more understanding about it and how it works, the problem is you are not with your system and you are on the move. Don't freak out yet, it's possible to run PHP script from your Android phone. All you need to get started is to install Termux on Android, follow this link to learn how to install Termux on your Android phone.

So, you've now installed Termux, what next? We are going to install PHP using this command but before then we need to update and upgrade the package repository we are downloading PHP from.

pkg update && pkg upgrade

pkg-update.png

Once you've typed the above command into the terminal, hit enter, and a prompt message will pop up, it will ask if you want to continue, type y and hit enter to continue the update and upgrade.

type-y.png

Once the package repository is updated, next we install PHP by issuing this below command. apt-get install php

php-install.png

Hit enter, and within a few minutes depending on how fast your internet connection is, PHP will be installed.

To confirm that PHP has been installed, type php -v on the terminal.

php-version.png

If everything goes well, you should see the PHP version that was installed as it is with the image attached.

Next, let's try to run a simple hello world on the terminal and also on the browser

From your Termux terminal, type cd storage, hit enter from your keyboard, to see the list of folders in the storage directory, type ls, let's change the directory to the one we needed so that we can be able to easily access the folder from our Android phone file manager, type cd shared.

To create a new directory where we will save our project, use this command mkdir projectName, in my case, I will use test as a directory name, feel free to use any name you want.

From the storage directory where we created the test folder, cd into the test folder like this cd test, we will create a PHP file here, let's call it test.php. To create the test file from Termux terminal, use this command touch test.php. From my end I'm using Acode to edit PHP files on my Android phone, you can download Acode from Google PlayStore or F-Droid.

Now open the test.php in any of your favorite text editors, again in my case, I'm using Acode, so I will open it there, now enter the following code snippet there

<?php echo "Hello World"; ?>

To run this script in your mobile browser, go back to Termux terminal, in your current directory i.e storage/shared/test, type php -S localhost:8000, to start the in-built PHP server. Next, open your browser and type http://localhost/test.php. You should see HELLO WORLD in your browser.

You can also view this from the Termux terminal, make sure you are in the right directory, in our case it is storage/shared/test, type php test.php, you will see your result there.

That's it, you now have PHP installed on your Android phone, now go and create something beautiful with it, and also keep learning.

Thanks for reading.

ย