Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Justin Hill
Justin Hill
16,642 Points

When to use PHP Built-In Server (for development)

Hey there, I had a question about using the built-in web server functionality that PHP added in ver. 5.4 (here's the page explaining how it works: http://php.net/manual/en/features.commandline.webserver.php).

So far, if I understand correctly, most people working on the PHP courses here just create different folders for PHP projects within their Apache Document Root (on Ubuntu, this is /var/www/html/, so I would keep a project in directory called, for instance, /var/www/html/shirts4mike), and then when they need to view their project in-browser they just navigate to the localhost/shirts4mike.

My question is, are there downsides to using the PHP built in web-server functionality discussed in the link above? I'm kind of an organization nut, so I'd much rather have a "Projects" folder in my home directory with all of my projects in it, and then when I need to view a PHP project, I could just navigate to the project folder and run the command php -S localhost:8000.

So the basic question is, is there any particular reason I would need to keep my PHP projects in the /var/www/html/ directory, or why it is a better solution than using the PHP Built-In server? Or is it all a matter of personal preference?

Thanks!

2 Answers

Ryan Watson
Ryan Watson
5,471 Points

In my opinion, I believe that you should develop in an environment that is as close to the production environment as possible. In most cases your webpages are going to be served by a webserver like Apache or ngnix. These webservers are more robust and fully featured than the functionality offered by the php web server, and for these reasons, I would recommend using something like Apache as a development platform.

As Nick pointed out, if you wish to serve your webpages from another directory, you can change this is the apache config file.

The PHP server that you are referencing seems like it would be useful for something like prototyping a web service that is meant to serve a specific purpose like building a php based authentication service that only processes requests triggered by a specific event such as a from submission from a login form. Such a request could be sent to this web service via an AJAX call and pointed to a specific port such as 8000. The PHP web service would be listening for requests pointed to :8000 and would process the request.

Justin Hill
Justin Hill
16,642 Points

Ryan Watson, that makes a lot of sense... and thank you for the 2nd example, that's an interesting use case and I see the value.

I think what I'll probably do is just create a directory structure something like ~/Projects/PHPsites, or something like that, and then just change my Apache document root directory to be the PHPsites directory using the instructions here: https://help.ubuntu.com/community/ApacheMySQLPHP#Virtual_Hosts

That way, I'll have all of my projects (php and otherwise) in one spot in my home directory... I don't know why but it's just odd to me to have stuff I'm working on buried up in the /var directory, but that's probably just me being weird. Thanks again.

I think it's just convention. You can actually change the public directory in the apache config file. In Ubuntu, that config file is /etc/apache2/apache2.conf