Playing
01. Using PHP's Built-In Web Server

Episodes

0%
Your progress
  • Total: 6m
  • Played: 0m
  • Remaining: 6m
Join or sign in to track your progress

Transcript

00:00
You may not have known that PHP has a built-in web server as of version 5.4. Now, of course, this doesn't mean that you can use it to host your site, but it's really useful for development and testing. And in this video, we'll be taking a look at exactly how to use it.
00:18
So let's start off in our terminal. I'm currently within the directory that I'm going to be working with. You'll obviously need PHP installed on your machine. And like I said, you'll need version 5.4 or greater.
00:31
You can check your PHP version by running PHP and then supplying the v option here. And you can see I'm running PHP 5.5. Now let's take a look at some of the options by running PHP and then the help command. The options that we're interested in here are s and t.
00:53
Now s lets you define the address and ports that you want your server to run on. So, for example, localhost and then you can choose a port name or port number rather. And then this can be accessed in your browser as you would normally with an Apache or Nginx server. The t option is optional.
01:14
But this allows you to define the path of the web route if you're not already in the directory that you want to run it from when you run the command we'll be looking at in a moment. So I'm currently in the directory I want to run as the web route. So I can leave the t option out.
01:31
But go ahead and use this option if you need and specify the path to the route that you want to serve. So let's run our web server on localhost or rather 127.0.0.1. And we will run it on port 1337. So all we do is port a PHP S option.
01:52
We give the host and then a colon and then the port we want to run it on and hit enter. And it is as easy as that. We now have a server running. So to test this out then within this directory PHP project, I'm going to create a new file.
02:09
And this is just going to be a PHP info file. So I'm going to call that phpinfo.php. And I'm going to call the phpinfo function to give us information about our PHP installation. So let's head over to the address here.
02:26
So we know it's 127.0.0.1. And that's on port 1337. You can see here we get a not found error because we're currently accessing the route directory which at the moment doesn't have an index.php file so it's not going to be served.
02:41
So we need to manually hit phpinfo.php. So we now see this file. And if we pull up our terminal, you'll notice that we see output here, including the file being accessed, the status code.
02:56
So let's just quickly mess up this file and see what happens. You can see here at the moment we're getting a 200. We had a 404 before, et cetera, et cetera. But if we just say write oops in here, which is obviously invalid code,
03:12
when we refresh this, we obviously get a parse error. But we actually see that reflected in our terminal as well. We're obviously still getting a 200, but we can see this error here. And it gives us the debugging info as well, so on line 3.
03:26
Now let's go and make things look a little bit nicer by adding some color to our output just to make it easier to look at while we are developing. So we can do this with the command line interface cliserver.color directive. So let's stop our server from running.
03:46
And we'll create this file somewhere in this directory. So this can be obviously anywhere on your machine. And we're going to call this phpcli, which stands for command line interface server. And this is an INI file.
04:01
So I'm going to save that out in there. And we can now change that cliserver.color directive that I just spoke about a minute ago. So we can say cliserver.color, and we can assign that the value on. And all this is going to do is it's going to enable colors in our console when we're actually running.
04:21
So we'll get different colors for errors, success, et cetera. So back over to the command line then, and let's restart our server. So we're just going to say phps127.0.0.1, same port that we had before. But this time, we're going to pass the configuration option,
04:41
and we're going to pass in that phpcliserver.ini file we created. So now what's going to happen then is when we access this file, we're obviously getting an error here. And inside of the terminal, you can see this is now highlighted in red,
04:57
which is pretty useful just to scan down. It's a little bit easier to look at. And if we were to go ahead and just put this file back to its original state, or we could just echo something out.
05:08
Why not? Then when we refresh here, we obviously see that. And then in our terminal, we see green. If we access a file that can't be found, so for example, at the moment,
05:21
just the root directory, there's no index.php file in there. You can see that we get this yellow color just here. So it's nicely highlighting the important parts of our output here. So that is pretty much it for running PHP's built-in web server.
05:39
Obviously, it doesn't do that much, but it's great for quickly developing on the go, just without having to set up our own server on our machine. Now, this obviously isn't recommended for a production environment. It's best to leave this up to Apache or Nginx or any other web server you're using.
05:56
But what about databases? Well, if you do need database support and you have MySQL installed on your system, you can connect to it directly with PHP just the usual way you would, specifying all the host details.
06:11
Of course, you could even use something like MySQL Lite if you don't have anything else installed. Obviously, that's a functionality that's built right into PHP. So next time you're quickly developing on the go, this might just help you out. It's always good to know it's there just in case you ever need it.
1 episode 6 mins

Overview

Did you know PHP has a built-in web server we can run from the command line? Let's take a look at how it works.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!