当前位置:首页 > PHP
使用旧手机搭建 PHP 服务器
来源:靑龍一笑的博客  作者:靑龍一笑  发布时间:2022-05-14 22:06:29  点击量:465  评论:0

一、安装 Termux

    在手机上安装 Termux,并启动。

1、安装基础软件包

$ pkg update
$ pkg install vim curl wget openssh

2、启动 ssh

$ sshd

3、查看当前系统帐号

$ whoami

4、设置登录密码

$ passwd

5、使用 ssh 客户端工具连接

    默认端口:8022

6、解决锁屏断网问题

$ termux-wake-lock

    命令输完后,在手机上允许。

7、设置自动开启 ssh,并解决锁屏断网问题

$ echo "termux-wake-lock; sshd" >> ~/.bashrc

8、允许访问手机存储

$ termux-setup-storage

    命令输完后,在手机上允许。

9、更换清华大学镜像源

$ sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
$ pkg update

10、升级版本

$ pkg upgrade

11、解决 vim 右键复制粘贴问题

$ vi ~/.vimrc

    添加如下内容:

set mouse-=a

二、安装 mysql

$ pkg install mariadb

    默认端口:3306

1、启动 mysql

$ nohup mysqld &

2、停止 mysql

$ kill -9 `pgrep mysql`

3、登录 mysql

$ mysql -u $(whoami)

    以当前系统用户身份登录 mysql,没有密码。

(1)查看用户

MariaDB [(none)]> select user();

(2)设置 root 密码

MariaDB [(none)]> use mysql
MariaDB [mysql]> set password for 'root'@'localhost'=password('p2022#DBA');
MariaDB [mysql]> grant all on *.* to root@'%' identified by 'p2022#DBA' with grant option;
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> quit

(3)使用 root 帐号登录 mysql

$ mysql -uroot -p

三、安装 nginx

$ pkg install nginx

    默认端口:8080

1、启动 nginx

$ nginx

2、停止 nginx

$ kill -9 `pgrep nginx`

四、安装 php7-fpm

$ pkg install php7-fpm

1、配置 php7-fpm

$ vi $PREFIX/etc/php-fpm.d/www.conf

    定位到 listen,即如下一行:

listen = /data/data/com.termux/files/usr/var/run/php-fpm.sock

    修改为:

listen = 127.0.0.1:9000

2、配置 nginx

$ vi $PREFIX/etc/nginx/nginx.conf

    定位到如下几行代码,标红为新增处:

location / {
    root   /data/data/com.termux/files/usr/share/nginx/html;
    index  index.html index.htm index.php;
}

    定位到如下几行代码,标红为修改处:

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /data/data/com.termux/files/usr/share/nginx/html$fastcgi_script_name;
    include        fastcgi_params;
}

3、添加测试页

    网站的根目录默认为:$PREFIX/share/nginx/html。当然,可以通过上述 nginx 配置文件进行修改。
    在根目录下添加一个 info.php 文件,内容如下:

<?php
phpinfo();

4、测试

    先启动 php-fpm,再启动 nginx:

$ php-fpm
$ nginx

    访问测试页:http://IP:8080/info.php

五、修改 nginx 配置

    为了方便部署,在家目录下进行如下操作:

1、创建网站目录、日志存放目录

$ mkdir -pv websites
$ mkdir -pv weblogs

2、创建 nginx 配置文件目录的软链接

$ ln -sf $PREFIX/etc/nginx nginx

3、创建站点配置文件存放目录

$ mkdir -pv nginx/vhosts

4、修改 nginx 配置文件

$ > nginx/nginx.conf
$ vi nginx/nginx.conf 

    内容如下:

worker_processes  1;

error_log  /data/data/com.termux/files/home/weblogs/error.log  crit;
pid  /data/data/com.termux/files/home/weblogs/nginx.pid;

worker_rlimit_nofile  51200;
events {
    worker_connections  51200;
}

http {
    include  mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  off;

    sendfile  on;
    tcp_nopush  on;
    tcp_nodelay  on;

    keepalive_timeout  65;
    client_header_timeout  15;
    client_body_timeout  15;
    reset_timedout_connection  on;
    send_timeout  15;

    server_names_hash_bucket_size  512;
    client_header_buffer_size  128k;
    large_client_header_buffers  4  128k;
    client_max_body_size  2000M;
    client_body_buffer_size  128K;    
    types_hash_max_size  2048;

    fastcgi_intercept_errors  on;
    fastcgi_connect_timeout  300;
    fastcgi_send_timeout  300;
    fastcgi_read_timeout  300;
    fastcgi_buffer_size  128k;
    fastcgi_buffers  8  128k;
    fastcgi_busy_buffers_size  256k;
    fastcgi_temp_file_write_size  256k;

    include  vhosts/*.conf;

    gzip  on;
    gzip_disable  "msie6";
    gzip_proxied  any;
    gzip_min_length  1k;
    gzip_buffers  16  64k;
    gzip_http_version  1.1;
    gzip_comp_level  6;
    gzip_types  text/plain  text/css application/javascript text/javascript application/json application/x-javascript text/xml application/xml application/xml+rss;
    gzip_vary  on;

}

5、创建站点配置文件

$ vi nginx/vhosts/127.0.0.1.conf

    内容如下:

server {
    listen  8080;
    server_name  localhost;
    access_log /data/data/com.termux/files/home/weblogs/127.0.0.1_access.log main;
    error_log /data/data/com.termux/files/home/weblogs/127.0.0.1_error.log crit;

    ssi  on;
    ssi_silent_errors  on;
    ssi_types  text/shtml;

    error_page  403 /403.html;
    error_page  404 /404.html;
    error_page  500 502 503 504 /50x.html;

    location / {
        root  /data/data/com.termux/files/home/websites;
        index  index.html index.htm index.php;
    }

    location = /50x.html {
        root  /data/data/com.termux/files/home/websites;
    }

    location = /403.html {
        root  /data/data/com.termux/files/home/websites;
        allow  all;
    }

    location = /404.html {
        root  /data/data/com.termux/files/home/websites;
    }

    location ~ \.php$ {
        root  /data/data/com.termux/files/home/websites;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /data/data/com.termux/files/home/websites$fastcgi_script_name;
        include  fastcgi_params;
    }
}

版权所有 © 2005-2023 靑龍一笑的博客  Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn  All Rights Reserved.

欢迎光临本站,这里是靑龍一笑的博客。

因资金匮乏,本站已迁到国外的免费空间,可能导致本站的访问速度较慢,由此给您带来的不便,敬请谅解。

您可以通过下方的“支持本站建设”链接,给本站提供资金支持。

Free Web Hosting