Building a PHP development environment using Termux on Android devices: A mobile development guide
Core points
phpinfo()
test. 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 sqlite
and users
tables. password_reset
<code>DB_CONNECTION=sqlite DB_DATABASE=homestead # 或者更改为你的数据库名称</code>
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!
(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!