Skip to content

Instantly share code, notes, and snippets.

@michaelcullum
Last active January 5, 2017 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelcullum/65cf1328eef82887affe578b56f62aaf to your computer and use it in GitHub Desktop.
Save michaelcullum/65cf1328eef82887affe578b56f62aaf to your computer and use it in GitHub Desktop.
A quick and dirty script to set up multiple PHP CLI servers

CLI Servers Script

This script creates a series of web servers defined in a config file on different ports.

Usage

  • Copy cliServers.sh from below to the root directory in which you have all your php projects (e.g. /var/www/ or ~/code/)
  • In that directory run mkdir logs
  • Create a file in that directory called config and add configuration for each web server as detailed below and exemplified in the config file in this gist.
  • Run sh cliServers.sh when you wish to start your web servers. They will run in the background until their processes are killed (by using kill or a reboot).

Config file

Format your config file with each line as a web server to start. config shows an example file.

Each line should then be in the format port:project-root:web-root.

For the web root it should be the directory including your front controller (e.g. public for laravel or web for symfony standard edition).

Killing processes

To kill the processes run kill <pid> where <pid> is the process ID.

Different PHP web servers

It uses the standard php binary in your path. If you want a different php binary (e.g. for a different php version for different projects) then use https://gist.github.com/michaelcullum/65cf1328eef82887affe578b56f62aaf#file-differentphpbinaries-md

Warnings

Be careful you don't try and create multiple servers on the same port (inc. running the script twice without killing the old processes)

Use at risk, things might break. I don't know what I'm doing. ;)

Ocassionally clear out /logs/. You shouldn't ever get clashing file names but they'll build up (one log file is generated per web server per script run). So run it once a day for 2 months with 10 scripts and you have 600 log files....

Domains

I strongly recommend when browsing to these webservers not using the ip of 127.0.0.1:<port> but instead using dnsmasq to set *.local to redirect locally. Then for each server just pick a name for it. See https://github.com/michaelcullum/setup-scripts/blob/master/localDomains.sh for how to set that up. You can just navigate then to whateveryoulike.local:<port> and your project will be there (you don't need to change anything here as using 127.0.0.1 for the web server instead of localhost means it will respond to different domains, so long as the port is correct and it's resolving to 127.0.0.1 (which dnsmasq handles for you).

basedir=`pwd`
echo "Setup the following web servers":
while IFS=':' read a b c; do
cd $b
nohup php -S 127.0.0.1:$a -t $c > $basedir/logs/$b-`date +%s`.log 2>&1 &
echo "$b to 127.0.0.1:$a. PID: $!"
cd $basedir
done < config
8000:auth:web
8888:api:www
8080:app:public

Shell file

basedir=`pwd`

echo "Setup the following web servers":
while IFS=':' read a b c d; do
  cd $b
  nohup $d -S 127.0.0.1:$a -t $c > $basedir/logs/$b-`date +%s`.log 2>&1 &
  echo "$b to 127.0.0.1:$a using $d. PID: $!"
  cd $basedir
done < config

Config File

8000:auth:web:php
8888:api:www:php70
8080:app:public:php71
Setup the following web servers:
auth to 127.0.0.1:8000. PID: 57176
api to 127.0.0.1:8888. PID: 57177
app to 127.0.0.1:8080. PID: 57178
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment