Building a PHP development environment using Termux on Android devices: A mobile development guide
Core points
- Use powerful terminal emulator and Linux package collection Termux to build a PHP development environment on Android devices.
- Running Laravel on Android requires installing packages such as PHP, Git, and Composer, and verifying the PHP installation using a simple
phpinfo()
test. - Data persistence of Android devices can be achieved through SQLite, a lightweight serverless file-type database engine, which is ideal for storing small amounts of data.
- While Android devices cannot run complex test suites or MySQL, this setup is useful for small development tasks or emergency fixes, allowing PHP development anytime, anywhere without having to carry a laptop.
Not long ago, Christopher Pitt wrote an excellent article on how to write and run PHP code on iPad. After reading it, I thought to myself, “It’s cool to do the same thing on Android”, for example, you can write and edit code anytime on the go without having to carry your laptop with you. So I decided to do some research and see what I could come up with.
This tutorial is suitable for any type of Android device. I did this on my phone, but an Android tablet with a Bluetooth keyboard might be the ideal setup.
There are some different shell applications on Android. This tutorial will use an application called Termux.
Termux combines powerful terminal emulation and an extensive collection of Linux packages. It is also completely free and easy to use.
After installing Termux from the Play Store, first run the apt update
command. According to the documentation: "This command needs to be run immediately after installation and then run regularly to receive updates."
Now is the exciting part. The first two commands I want to discuss are the apt list
and apt list --installed
commands. The first command will list all packages available to Termux. We can see that it supports many different programming languages, text editors, and has some useful utility packages such as zip, tar, and so on. The second command will list all installed packages. We can see that Termux has pre-installed some packages such as apt and bash.
My goal when testing Termux was to see if I could assemble a suitable *PHP development environment, so I first installed a text editor. I prefer Vim, but there are some other options available, such as Emacs and Nano. Vim's learning curve is a bit steep, but once you get the basics of it, it becomes very comfortable. You can use the apt install vim
command to get Vim.
If you want to learn more about vim, here is a very good article, or, after installation, type vimtutor
to use the built-in tutorial.
If you test this on an Android phone, running vim will bring the first set of problems. How do I press the Escape key? Termux has a large list of shortcut keys that can be used to emulate unavailable buttons on Android keyboard:
命令 | 鍵 |
---|---|
Volume Up E | Escape鍵 |
Volume Up T | Tab鍵 |
Volume Up 1 | F1 (Volume Up 2 → F2,以此類推) |
Volume Up 0 | F10 |
Volume Up B | Alt B (使用readline時向后一個單詞) |
Volume Up F | Alt F (使用readline時向前一個單詞) |
Volume Up X | Alt X |
Volume Up W | 上箭頭鍵 |
Volume Up A | 左箭頭鍵 |
Volume Up S | 下箭頭鍵 |
Volume Up D | 右箭頭鍵 |
Volume Up L | | (管道字符) |
Volume Up U | _ (下劃線) |
Volume Up P | Page Up |
Volume Up N | Page Down |
Volume Up . | Ctrl (SIGQUIT) |
Volume Up V | 顯示音量控制 |
Now that our editor is up and running, it's time to install the packages we need: PHP, Git, and Composer.
apt install php apt install git
This will install the latest PHP and Git packages.
For Composer, we need to do some extra work. We need to go to the Composer download page and use the command line installation instructions:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"
This will download the installer, verify it, run it and delete it. If all goes well, we should be able to run Composer from Termux.
Now that we have all the tools installed, we should test if our PHP installation is running correctly. To do this, let's do a simple phpinfo()
test. Let's create a new folder and test our PHP installation.
mkdir test cd test echo "<?php phpinfo();" ?> > index.php php -S localhost:8080
This will create a new folder, and then create a phpinfo()
file containing the index.php
command. I echo it directly into the file, but you can do it with Vim. Finally, we use a PHP server to provide it to our localhost. When accessing localhost:8080 in the browser, we should see something like this:
We now have Composer for dependency management and git for version control. But I know what you're thinking: "We just did a simple phpinfo
test, what about the rest?"
Can we install Laravel on our Android device?
At this point, we have everything we need to install and run Laravel on our Android device. To create a new Laravel project, we need to run the following command:
php composer.phar create-project --prefer-dist laravel/laravel new_project
This will create a new Laravel project in the new_project
folder. The --prefer-dist
option is well documented here. It may take a while to install. Once done, we can use Laravel's own Artisan command line interface to run our newly created project. In the new_project
folder, we can run the following command:
php artisan serve
Accessing localhost:8000 URL in your browser should now display Laravel's homepage.
Success! Our Laravel installation is complete. We have successfully installed the tools we need to write and execute code. However, no development environment is complete without a method to persist data.
For Android devices, memory and storage capacity are practical issues in most cases. Therefore, Termux only provides SQLite as a way to persist data. SQLite is a serverless file-type database engine. It's lightweight and is perfect for a small amount of data, as you've read here and in this article that goes beyond the basics. First, we need to install it.
apt install php apt install git
Next, we need to configure our Laravel project to use SQLite. In the root directory of the project, we have a .env
file. This is the environment configuration file, the first file we need to edit. Use the editor of your choice to edit the following lines:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"
Then go to the config/database.php
file and turn the following line from:
mkdir test cd test echo "<?php phpinfo();" ?> > index.php php -S localhost:8080
Change to:
php composer.phar create-project --prefer-dist laravel/laravel new_project
This will make SQLite the default connection in the connections
array. Make sure the database.sqlite
file path is correctly pointing to your database file.
database.sqlite
The file does not exist yet, so we need to create it:
php artisan serve
This is all the configuration we need to tell Laravel to use SQLite - we can test it now. We will use Laravel's pre-built authentication system. To create a scaffold, we need to run the following command:
apt install sqliteAfter that, we will run the migration to build our database schema. This will create the
and users
tables. password_reset
<code>DB_CONNECTION=sqlite DB_DATABASE=homestead # 或者更改為你的數(shù)據(jù)庫名稱</code>If we run
again, we will see that we have the option to register and log in now. Our authentication CRUD has been successfully created! php artisan serve
Conclusion
I did all this on my Android phone. This setup is great for small development tasks, as it has all the tools you need to start developing on smaller devices without having to carry your laptop with you.While it is not the pinnacle of productivity, it comes in handy when it comes to an emergency repair or if you want to see how much PHP performance you can squeeze from your Android device.
Try it and tell us your thoughts, if you build something interesting with PHP on Android, please market to us your concept and we will write about it!
*"Appropriate" refers to the appropriateness for the context. Android phones don't run MySQL, and they can't run complex test suites, especially end-to-end tests, but some other things may run well enough to do some work.
(The FAQs part is omitted because it does not match the pseudo-original goal and is too long.)
The above is the detailed content of The Android Elephpant - Laravel on your Android Phone?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech
