├── .gitignore ├── LICENSE ├── README.md └── server /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | *.pid -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-built-in-server-manager 2 | Manage PHP built-in web server like a pro 3 | 4 | **usage**: `./server [: [router]]` 5 | 6 | **Available commands**: 7 | ``` 8 | start Starts PHP built-in web server server on specified hostname:port, default is localhost:8080 9 | router is a PHP script to which all requests will be redirected (if used) 10 | stop Stops the PHP built-in web server 11 | restart Stops and Starts on previously specified hostname:port 12 | status Status of the process 13 | log Show the PHP built-in web server logs. Use the -f option for a live update 14 | ``` 15 | 16 | ## How to Use it 17 | 18 | This is a very basic script so you're not limitted to any specific method. I suggest one of these two: 19 | 1. Copy `server` script to root of your PHP project. 20 | 2. Copy/Symlink to your one of $PATH directories like `~/bin` or `/usr/local/bin` and use it everywhere. 21 | 22 | **Note:** When starts, two files `server.pid` and `server.log` will be created in current working directory and when stopped, only `server.pid` will be deleted but `server.log` remains. 23 | 24 | the `server` script can be renamed to whatever you like. I personally prefer `pmserver`. 25 | `.pid` and `.log` files will have the same name as the filename e.g. when `server` renames to `pmserver` there will be `pmserver.pid` and `pmserver.log` files 26 | -------------------------------------------------------------------------------- /server: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # default hostname 4 | HOST=localhost 5 | # default port number 6 | PORT=8080 7 | # router PHP script (optional) 8 | ROUTER= 9 | # script name 10 | NAME=${0##*/} 11 | 12 | usage () { 13 | cat < [: []] 19 | 20 | Available commands: 21 | 22 | start Starts PHP built-in web server server on specified hostname:port, default is localhost:$PORT 23 | router is a PHP script to which all requests will be redirected (if used) 24 | stop Stops the PHP built-in web server 25 | restart Stops and Starts on previously specified hostname:port 26 | status Status of "$NAME" process 27 | log Show the PHP built-in web server logs. Use the -f option for a live update 28 | 29 | 30 | report bugs to me@cubny.com 31 | $NAME homepage: 32 | 33 | EOF 34 | return 0 35 | } 36 | 37 | setup_colors() { 38 | 39 | if which tput >/dev/null 2>&1; then 40 | ncolors=$(tput colors) 41 | fi 42 | if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then 43 | RED="$(tput setaf 1)" 44 | GREEN="$(tput setaf 2)" 45 | YELLOW="$(tput setaf 3)" 46 | BLUE="$(tput setaf 4)" 47 | BOLD="$(tput bold)" 48 | NORMAL="$(tput sgr0)" 49 | else 50 | RED="" 51 | GREEN="" 52 | YELLOW="" 53 | BLUE="" 54 | BOLD="" 55 | NORMAL="" 56 | fi 57 | } 58 | 59 | # if no command specified exit and show usage 60 | if [[ $# < 1 ]]; then 61 | echo $NAME: no command specified 62 | usage 63 | exit 1 64 | fi 65 | 66 | # if hostname:port specified override defaults 67 | if [[ $# > 1 ]]; then 68 | IFS=':' read -r -a hostport <<< "$2" 69 | if [[ ! -z "${hostport[0]}" ]]; then 70 | HOST=${hostport[0]} 71 | fi 72 | if [[ ! -z "${hostport[1]}" ]]; then 73 | PORT=${hostport[1]} 74 | fi 75 | fi 76 | 77 | # if router specified 78 | if [[ $# > 2 ]]; then 79 | read -r router <<< "$3" 80 | if [[ ! -z "${router}" ]]; then 81 | ROUTER=${router} 82 | fi 83 | fi 84 | 85 | # pidfile contents would be hostname:port:pid 86 | PIDFILE="$NAME".pid 87 | LOGFILE="$NAME".log 88 | 89 | validate_server () { 90 | which php &> /dev/null 91 | if [[ $? -eq 1 ]]; then 92 | printf "${YELLOW}Error: PHP not found. ${NORMAL}Please install PHP version 5.4 or greater!\n" 93 | return 1 94 | fi 95 | 96 | php -h | grep -q -- '-S' 97 | if [[ $? -eq 1 ]]; then 98 | printf "${YELLOW}Error: PHP version must be 5.4 or greater!${NORMAL}\n" 99 | return 1 100 | fi 101 | 102 | return 0 103 | } 104 | 105 | start_server () { 106 | 107 | validate_server 108 | if [[ $? -eq 1 ]]; then 109 | return 1 110 | fi 111 | 112 | if [[ -e "$PIDFILE" ]]; then 113 | printf "${YELLOW}Server seems to be running!${NORMAL}\n" 114 | echo 115 | echo if not, there is probably a zombie "$PIDFILE" in this directory. 116 | echo if you are sure no server is running just remove "$PIDFILE" manually and start again 117 | return 1 118 | else 119 | printf "${GREEN}"$NAME" started on $HOST:$PORT${NORMAL}\n" 120 | php -S "$HOST":"$PORT" $ROUTER >> "$LOGFILE" 2>&1 & 121 | echo "$HOST":"$PORT":$! > $PIDFILE 122 | return 0 123 | fi 124 | } 125 | 126 | read_pidfile() { 127 | if [[ -e "$PIDFILE" ]]; then 128 | PIDFILECONTENT=`cat "$PIDFILE"` 129 | IFS=: read HOST PORT PID <<< "$PIDFILECONTENT:" 130 | return 0 131 | else 132 | return 1 133 | fi 134 | 135 | } 136 | stop_server () { 137 | if read_pidfile; then 138 | kill -9 "$PID" 139 | rm -f "$PIDFILE" 140 | printf "${GREEN}"$NAME" stopped!${NORMAL}\n" 141 | return 0 142 | else 143 | printf "${YELLOW}"$NAME" is not running!${NORMAL}\n" 144 | return 1 145 | fi 146 | } 147 | 148 | status_server() { 149 | if read_pidfile && kill -0 "$PID" ; then 150 | printf "${BLUE}"$NAME" is running on ${HOST}:${PORT}${NORMAL}\n" 151 | else 152 | printf "${YELLOW}"$NAME" is not running!${NORMAL}\n" 153 | fi 154 | } 155 | 156 | 157 | log_server() { 158 | if read_pidfile && kill -0 "$PID" ; then 159 | if [[ "$1" = "-f" ]]; then 160 | TAIL_OPTS="-f" 161 | fi 162 | tail $TAIL_OPTS "$LOGFILE" 163 | else 164 | printf "${YELLOW}"$NAME" is not running!${NORMAL}\n" 165 | fi 166 | } 167 | 168 | 169 | setup_colors 170 | 171 | case $1 in 172 | start) start_server;; 173 | stop) stop_server;; 174 | restart) stop_server; start_server ;; 175 | status) status_server;; 176 | log) log_server $2;; 177 | -h) usage ;; 178 | --help) usage ;; 179 | *) usage;; 180 | esac 181 | 182 | --------------------------------------------------------------------------------