Start the built-in PHP web server
Usually when you are building a website, you’ll have a web server such as Nginx or Apache that sits in front of the PHP service. Then, some configuration needs to be added to that web server to properly serve requests to handle PHP files.
You’ll definitely want to set up this external web server -slash- PHP combo on production websites, but right now you just need to learn PHP. So, we’ll use a much simpler method: use the built-in PHP web server!
Open up Terminal. You can do this right on your host with the Terminal app, or if you are using PhpStorm you can open the built-in Terminal by going to View > Tool Windows > Terminal.
We can see that this works the same way as the Terminal application on your host.
php -v
Let’s call the php
binary again, but this time pass in the --help
argument. This gives us the available arguments we can pass to this script. One of those is -S
, which runs the built-in PHP web server. This argument expects an address-colon-port combo.
So let’s call php
with this -S
flag:
php -S
The address will always be localhost
, then we will type a :
, followed by any available port. Note that you cannot use any port lower than 1024, as those are considered privileged ports that require higher security privileges:
php -S localhost:1000
It’s common to use port 8000
for this built-in web server. The default port for web servers is 80, so just adding a couple zeros on the end makes this easy to remember.
php -S localhost:8000
Now that we have our web server started, we can visit https://localhost:8000 in our web browser and we’ll see a “not found” page. Don’t worry, this is great! This purple background at the top lets us know that the built-in PHP server is serving up this request.
And if we check out our Terminal again, we’ll see that there was output to the console showing the web server accepting, serving up, and closing this web request.
Now that you have your editor set up and a web server running, it’s time to start writing some PHP.