DEV Community

Cover image for Simplify Your PHP Development with a Handy Bash Script
Zxce3
Zxce3

Posted on • Updated on

Simplify Your PHP Development with a Handy Bash Script

As developers, we often need to quickly spin up a local PHP server for testing and development purposes. While PHP's built-in server is quite handy, repeatedly typing out the full command can become tedious. To streamline this process, I've created a simple Bash script called phloc that starts a PHP server with minimal fuss.

In this article, I'll guide you through the script step by step, explaining how it works and why it's beneficial for your development workflow.

What Does the Script Do?

The phloc script simplifies starting a PHP server by allowing you to specify a port number and document root directory. If you don't provide these options, it defaults to port 8080 and the current directory. The script also checks if the specified port is already in use and increments the port number if necessary.

The Script

Here's the complete script:

#!/bin/bash
# Script name: phloc
# Usage:
#   phloc [port] [doc_root]
#   port: port number to listen on (default: 8080)
#   doc_root: document root (default: current directory)
# Description:
#   Starts a PHP server on the specified port and document root.
#   If no port is specified, the default port 8080 is used.
#   If no document root is specified, the current directory is used.
#   The script is intended to be used for local development.
if [ $# -eq 0 ]; then
  port=8080
else
  port=$1
fi

doc_root="."

if [ $# -eq 2 ]; then
  doc_root=$2
fi

while true; do
  echo "Starting PHP server on port $port with document root $doc_root..."
  php -S 0.0.0.0:$port -t $doc_root

  # Check if the port is already in use
  if [ $? -eq 0 ]; then
    break
  else
    # Increment the port number and try again
    echo "Port $port is already in use, trying port $((port+1))..."
    port=$((port+1))
  fi
done
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Breakdown

Step 1: Shebang and Script Metadata

The script starts with the shebang (#!/bin/bash) which tells the system to execute the script using the Bash shell. This is followed by comments that provide usage instructions and a brief description of what the script does.

Step 2: Default Port and Document Root

if [ $# -eq 0 ]; then
  port=8080
else
  port=$1
fi

doc_root="."
Enter fullscreen mode Exit fullscreen mode

Here, the script checks if any arguments were passed. If no arguments are provided, it sets the default port to 8080. The document root is set to the current directory by default.

Step 3: Handling Optional Document Root Argument

if [ $# -eq 2 ]; then
  doc_root=$2
fi
Enter fullscreen mode Exit fullscreen mode

If two arguments are provided, the second argument is used as the document root.

Step 4: Starting the PHP Server

while true; do
  echo "Starting PHP server on port $port with document root $doc_root..."
  php -S 0.0.0.0:$port -t $doc_root
Enter fullscreen mode Exit fullscreen mode

The script enters a while loop to start the PHP server. The echo command outputs the port and document root for transparency.

Step 5: Port Conflict Handling

  if [ $? -eq 0 ]; then
    break
  else
    # Increment the port number and try again
    echo "Port $port is already in use, trying port $((port+1))..."
    port=$((port+1))
  fi
done
Enter fullscreen mode Exit fullscreen mode

The script checks the exit status of the php -S command. If the command succeeds ($? -eq 0), the loop breaks. If the port is already in use, the script increments the port number and tries again.

Why Use This Script?

  1. Convenience: Saves time by reducing the need to repeatedly type out the full PHP server command.
  2. Flexibility: Allows for quick changes to the port and document root.
  3. Port Handling: Automatically handles port conflicts by incrementing the port number.

How to Use the Script

  1. Save the Script: Copy the script and save it as phloc (or any preferred name) in your desired directory.
  2. Make It Executable: Run chmod +x phloc to make the script executable.
  3. Run the Script: Use ./phloc [port] [doc_root] to start your PHP server. If no arguments are provided, it defaults to port 8080 and the current directory.

By incorporating this script into your development toolkit, you can streamline your PHP development process, making it more efficient and less prone to human error.

Happy coding!

Top comments (3)

Collapse
 
moopet profile image
Ben Sinclair

As a top tip, bash (and zsh) can use default substitutions, so this:

if [ $# -eq 0 ]; then
  port=8080
else
  port=$1
fi
Enter fullscreen mode Exit fullscreen mode

could be rweritten as this:

port=${1:-8080}
Enter fullscreen mode Exit fullscreen mode

or even this (factoring the default port out to a global):

DEFAULT_PORT=8080

# [...snip]

port=${1:-$DEFAULT_PORT}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zxce3 profile image
Zxce3

Nicee, it will simplfy the code

Thanks

Collapse
 
hrrarya profile image
Hridoy Mozumder

Great script. However, to change the doc_root without changing the port, you currently need to specify the port as well.

Improved Script (php_dev.sh)

instead of this portion of the script

if [ $# -eq 0 ]; then
  port=8080
else
  port=$1
fi

doc_root="."

if [ $# -eq 2 ]; then
  doc_root=$2
fi
Enter fullscreen mode Exit fullscreen mode

Use this

port=8080
doc_root="."
while getopts p:d: flag
do
    case "${flag}" in
        p) port=${OPTARG};;
        d) doc_root=${OPTARG};;
    esac
done

Enter fullscreen mode Exit fullscreen mode

Making the Script Globally Accessible

  1. Move the Script to /usr/local/bin:
sudo mv ./php_dev.sh /usr/local/bin/php_dev

Enter fullscreen mode Exit fullscreen mode
  1. Ensure the Script is Executable:
sudo chmod +x /usr/local/bin/php_dev

Enter fullscreen mode Exit fullscreen mode
  1. Optional: Create an Alias in .bashrc: You can add an alias to your .bashrc .bash_profile or .zshrc file.
echo 'alias php_dev="/usr/local/bin/php_dev"' >> ~/.bashrc
source ~/.bashrc

Enter fullscreen mode Exit fullscreen mode

Now you can run the script from anywhere in your terminal like this:

php_dev -p 8081 -d ./test/

Enter fullscreen mode Exit fullscreen mode

Or simply

php_dev

Enter fullscreen mode Exit fullscreen mode