php 命令启动静态服务

目标:

  1. 在当前目录下启动一个静态文件服务器
  2. 设定一个50000端口为默认静态服务端口
  3. 如果当前端口已经被占用,需要向上找一个可以使用的端口

写在前面的完整示例:

#!/bin/bash

docfile() {
  loop() {
    if [  $(lsof -i:$1 | wc -l) -eq 0 ]; then
    	chrome --new-window http://127.0.0.1:$1;
        php -S 127.0.0.1:$1 -t $2;
    else
        loop $(($1+1)) $2;
    fi
  }
  if [ -z $2 ]; then
    loop $1 $(pwd);
  else
    loop $1 $2;
  fi
};

## 示例1 有目标目录
docfile 50000 /Users/rc/Documents/JSDoc/jsdoc.github.io
## 示例2 无目标目录
docfile 50000 

命令别名

alias docfile='docfile() { loop() { if [  $(lsof -i:$1 | wc -l) -eq 0 ]; then chrome --new-window http://127.0.0.1:$1; php -S 127.0.0.1:$1 -t $2; else loop $(($1+1)) $2; fi }; if [ -z $2 ]; then loop $1 $(pwd); else loop $1 $2; fi }; docfile 50000 '

结果输出

➜  ~ alias docfile
docfile='docfile() { loop() { if [  $(lsof -i:$1 | wc -l) -eq 0 ]; then chrome --new-window http://127.0.0.1:$1; php -S 127.0.0.1:$1 -t $2; else loop $(($1+1)) $2; fi }; if [ -z $2 ]; then loop $1 $(pwd); else loop $1 $2; fi }; docfile 50000 '
➜  ~ docfile
[Fri Apr 29 12:59:14 2022] PHP 8.1.5 Development Server (http://127.0.0.1:50002) started

设计

1. 查看端口是否被占用

➜  ~ lsof -i:50000
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
php     63637   rc    5u  IPv4 0x2f9d5a5323563b45      0t0  TCP localhost:50000 (LISTEN)
➜  ~

2. 如果端口不可用则更换一个端口

3. 默认以当前目录为静态服务器目录, 也可指定文件目录

-z 判断当前值是否为null

  if [ -z $2 ]; then
    loop $1 $(pwd);
  else
    loop $1 $2;
  fi

启动静态服务

查看所有命令

➜  ~ php --help
Usage: php [options] [-f] <file> [--] [args...]
   php [options] -r <code> [--] [args...]
   php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
   php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
   php [options] -S <addr>:<port> [-t docroot] [router]
   php [options] -- [args...]
   php [options] -a

  -a               Run as interactive shell
  -c <path>|<file> Look for php.ini file in this directory
  -n               No configuration (ini) files will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse and execute <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
  -R <code>        Run PHP <code> for every input line
  -F <file>        Parse and execute <file> for every input line
  -E <end_code>    Run PHP <end_code> after processing all input lines
  -H               Hide any passed arguments from external tools.
  -S <addr>:<port> Run with built-in web server.
  -t <docroot>     Specify document root <docroot> for built-in web server.
  -s               Output HTML syntax highlighted source.
  -v               Version number
  -w               Output source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin

  --ini            Show configuration file names

  --rf <name>      Show information about function <name>.
  --rc <name>      Show information about class <name>.
  --re <name>      Show information about extension <name>.
  --rz <name>      Show information about Zend extension <name>.
  --ri <name>      Show configuration for extension <name>.

完整示例

php -S 127.0.0.1:8081   -t /Users/shang/Downloads/opencv-4.5.5

示例解读

将 静态目录 /Users/shang/Downloads/opencv-4.5.5 挂载到 127.0.0.1:8081

示例展示 在这里插入图片描述

使用docfile直接启动静态服务

➜  jsdoc.github.io git:(master)pwd
/Users/rc/Documents/JSDoc/jsdoc.github.io
➜  jsdoc.github.io git:(master) ✗ docfile
[Fri Apr 29 13:40:50 2022] PHP 8.1.5 Development Server (http://127.0.0.1:50006) started

在这里插入图片描述

当前目标下再次启动静态服务

➜  jsdoc.github.io git:(master) ✗ docfile
[Fri Apr 29 13:42:30 2022] PHP 8.1.5 Development Server (http://127.0.0.1:50007) started

端口自动增加1
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从未、淡定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值