1#!/bin/bash 2 3# https://github.com/cubny/php-built-in-server-manager/blob/9a5cbeaad50a108d6058b882b83ba23fbd7722a9/server 4 5# default hostname 6HOST=localhost 7# default port number 8PORT=8080 9# script name 10NAME=${0##*/} 11 12usage () { 13 cat <<EOF 14 15 $NAME (PHP built-in web server manager) Version 0.2.0 16 PHP builtin server manager on port $PORT 17 18 usage: ./$NAME <command> [<hostname>:<port>] 19 20 Available commands: 21 22 start Starts PHP built-in web server server on specified hostname:port, default is localhost:$PORT 23 stop Stops the PHP built-in web server 24 restart Stops and Starts on previously specified hostname:port 25 status Status of "$NAME" process 26 log Show the PHP built-in web server logs. Use the -f option for a live update 27 28 29 report bugs to me@cubny.com 30 $NAME homepage: <https://github.com/cubny/php-built-in-server-manager> 31 32EOF 33return 0 34} 35 36setup_colors() { 37 38if which tput >/dev/null 2>&1; then 39 ncolors=$(tput colors) 40 fi 41 if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then 42 RED="$(tput setaf 1)" 43 GREEN="$(tput setaf 2)" 44 YELLOW="$(tput setaf 3)" 45 BLUE="$(tput setaf 4)" 46 BOLD="$(tput bold)" 47 NORMAL="$(tput sgr0)" 48 else 49 RED="" 50 GREEN="" 51 YELLOW="" 52 BLUE="" 53 BOLD="" 54 NORMAL="" 55 fi 56} 57 58# if no command specified exit and show usage 59if [[ $# < 1 ]]; then 60 echo $NAME: no command specified 61 usage 62 exit 1 63fi 64 65# if hostname:port specified override defaults 66if [[ $# > 1 ]]; then 67 IFS=':' read -r -a hostport <<< "$2" 68 if [[ ! -z "${hostport[0]}" ]]; then 69 HOST=${hostport[0]} 70 fi 71 if [[ ! -z "${hostport[1]}" ]]; then 72 PORT=${hostport[1]} 73 fi 74fi 75 76# pidfile contents would be hostname:port:pid 77PIDFILE=.build/server/server.pid 78LOGFILE=.build/server/server.log 79 80validate_server () { 81 which php &> /dev/null 82 if [[ $? -eq 1 ]]; then 83 printf "${YELLOW}Error: PHP not found. ${NORMAL}Please install PHP version 5.4 or greater!\n" 84 return 1 85 fi 86 87 php -h | grep -q -- '-S' 88 if [[ $? -eq 1 ]]; then 89 printf "${YELLOW}Error: PHP version must be 5.4 or greater!${NORMAL}\n" 90 return 1 91 fi 92 93 return 0 94} 95 96start_server () { 97 validate_server 98 99 if [[ $? -eq 1 ]]; then 100 return 1 101 fi 102 103 if [[ -e "$PIDFILE" ]]; then 104 printf "${YELLOW}Server seems to be running!${NORMAL}\n" 105 echo 106 echo if not, there is probably a zombie "$PIDFILE" in this directory. 107 echo if you are sure no server is running just remove "$PIDFILE" manually and start again 108 return 1 109 else 110 printf "${GREEN}"$NAME" started on $HOST:$PORT${NORMAL}\n" 111 mkdir -p $(dirname "$LOGFILE") 112 php -S "$HOST":"$PORT" -c tests/php.ini >> "$LOGFILE" 2>&1 & 113 mkdir -p $(dirname "$PIDFILE") 114 echo "$HOST":"$PORT":$! > $PIDFILE 115 return 0 116 fi 117} 118 119read_pidfile() { 120 if [[ -e "$PIDFILE" ]]; then 121 PIDFILECONTENT=`cat "$PIDFILE"` 122 IFS=: read HOST PORT PID <<< "$PIDFILECONTENT:" 123 return 0 124 else 125 return 1 126 fi 127} 128 129stop_server () { 130 if read_pidfile; then 131 kill -9 "$PID" 132 rm -f "$PIDFILE" 133 printf "${GREEN}"$NAME" stopped!${NORMAL}\n" 134 return 0 135 else 136 printf "${YELLOW}"$NAME" is not running!${NORMAL}\n" 137 return 1 138 fi 139} 140 141status_server() { 142 if read_pidfile && kill -0 "$PID" ; then 143 printf "${BLUE}"$NAME" is running on ${HOST}:${PORT}${NORMAL}\n" 144 else 145 printf "${YELLOW}"$NAME" is not running!${NORMAL}\n" 146 fi 147} 148 149 150log_server() { 151 if read_pidfile && kill -0 "$PID" ; then 152 if [[ "$1" = "-f" ]]; then 153 TAIL_OPTS="-f" 154 fi 155 tail $TAIL_OPTS "$LOGFILE" 156 else 157 printf "${YELLOW}"$NAME" is not running!${NORMAL}\n" 158 fi 159} 160 161 162setup_colors 163 164case $1 in 165 start) start_server;; 166 stop) stop_server;; 167 restart) stop_server; start_server ;; 168 status) status_server;; 169 log) log_server $2;; 170 -h) usage ;; 171 --help) usage ;; 172 *) usage;; 173esac 174