Simplify Your PHP Development with a Handy Bash Script
Tags: bash, php, opensource, productivity
Published: 2024-05-28 19:04:42

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:

```bash
#!/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
```

## 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

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

doc_root="."
```

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

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

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

### Step 4: Starting the PHP Server

```bash
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
```

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

```bash
  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
```

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!