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
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="."
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
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
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
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?
- Convenience: Saves time by reducing the need to repeatedly type out the full PHP server command.
- Flexibility: Allows for quick changes to the port and document root.
- Port Handling: Automatically handles port conflicts by incrementing the port number.
How to Use the Script
-
Save the Script: Copy the script and save it as
phloc
(or any preferred name) in your desired directory. -
Make It Executable: Run
chmod +x phloc
to make the script executable. -
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)
As a top tip, bash (and zsh) can use default substitutions, so this:
could be rweritten as this:
or even this (factoring the default port out to a global):
Nicee, it will simplfy the code
Thanks
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
Use this
Making the Script Globally Accessible
Now you can run the script from anywhere in your terminal like this:
Or simply