Home > Backend Development > PHP Problem > How to build php in Android shell

How to build php in Android shell

PHPz
Release: 2023-03-29 13:54:47
Original
1042 people have browsed it

In the current Internet era, web applications are ubiquitous. As one of the languages, PHP language is widely popular among developers. In development, many students use headless environment tools such as virtual machines or Docker prepared for Linux or Mac system users, but most of the time, they are too cumbersome and cumbersome. So in this article I will introduce a simpler method to build a PHP environment using Android Shell.

First of all, we need to prepare some necessary software and environment:

  1. An Android phone or Android emulator
  2. Install the Termux Terminal Emulator application
  3. PHP installation package of Termux Extra application package

After installing the Termux Terminal Emulator application, you need to download the "Termux Extra application package" in the Termux application.

Enter the following command in Termux:

pkg install php
Copy after login

The Termux package manager will automatically install the PHP and PHP-FPM services. Once installed, you can run PHP and set it to start with the system.

Next, we need to edit the configuration file of PHP-FPM so that when we access PHP files, Nginx can correctly send requests and communicate with the PHP-FPM process.

In Termux, enter the PHP directory:

cd /data/data/com.termux/files/usr/etc/php-fpm.d
Copy after login

Open the www.conf file with a text editor:

nano www.conf
Copy after login

Set user and group to the UID of the current Termux user:

user = ${UID}
group = ${GID}
Copy after login

Find the listen variable, set it to UNIX Socket mode, and set it to /data/data/com.termux/files/usr/var/run/php-fpm.sock:

#(打开的时候前面的 ; 号必须删掉)

listen = /data/data/com.termux/files/usr/var/run/php-fpm.sock
listen.owner = ${UID}
listen.group = ${GID}
listen.mode = 0660
Copy after login

Save the file and close the Nano editor: Ctrl X, Y, Enter

Next, we need to configure the Nginx server to communicate with the above PHP-FPM process. You can download and install Nginx from the official website, or you can download and install it from the Termux package manager:

pkg install nginx
Copy after login

After installation, run the following command in Termux to start the Nginx server:

nginx
Copy after login

At this point, Nginx will start and listen for requests on port 8080.

You can test whether Nginx starts successfully. Open your browser and visit http://localhost:8080. Nginx will return the default Welcome To Nginx text.

Now, we will create a simple PHP web page to test whether our CMS is installed successfully. Open your text editor and create the following index.php file:

<?php
echo "Hello, PHP on Android!"
?>
Copy after login

Save the above file to /data/data/com.termux/files/home/my-www (please create this first in Termux Table of contents).

Next, we need to create Nginx’s virtual host configuration file to direct the Nginx server to find our web page and pass it to the PHP-FPM process. Enter the following command in the Termux command prompt:

nano /data/data/com.termux/files/usr/etc/nginx/conf.d/default.conf
Copy after login

Add the following content to the file, configuring the root directory to the directory created in the previous step: /data/data/com.termux/files/home/ my-www/

server {
  listen 8080;
  root /data/data/com.termux/files/home/my-www;
  index index.php;
  server_name localhost;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/data/data/com.termux/files/usr/var/run/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}
Copy after login

Exit the Nano editor (Ctrl X, Y, Enter), and then restart Nginx through the following command:

nginx -s reload
Copy after login

At this time, enter http:// in the browser localhost:8080/index.php You can see the output: Hello, PHP on Android!

Here, we have used a simple method to upload the configuration and established a lightweight PHP environment that can be convenient for desktop or mobile web development. If you need to deploy PHP in a production environment, there are additional security and scaling considerations.

Quote:
1.https://www.linuxidc.com/Linux/2019-09/160469.htm

2.https ://www.guru99.com/php-installation-and-configuration-in-android-termux.html

The above is the detailed content of How to build php in Android shell. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template