2 Easy Ways to Serve PHP Localhost

Sam Lim
3 min readAug 8, 2021

PHP is a programming language that is widely used for web development. Over 70% of websites used PHP as the backbone. Therefore, there is a needs to ease PHP developer life with rapid development tools. So I’m going to show you how to serve PHP in your localhost environment, and speed up the development.

  1. Serve localhost
php -S 127.0.0.1:3000 -t public

If you’re new to PHP, you probably using this method state above. This is a command that we type and enter in terminal(Mac) or command prompt(windows) to initiate a localhost server for your PHP project. A quick explanation for the command:

  • php : It is the command header to let shell understood you are calling php command
  • -S : Flag that tell shell to serve a server
  • 127.0.0.1 : location to serve the server
  • 3000 : Port for the server
  • -t public : Target to public folder (only for Laravel framework)

The concept is to use your laptop or computer, a machine to serve your PHP code. Since it is your local machine, so it is called localhost.

2. Sites Folder (Mac)

For mac users, there is another way to serve localhost, which is using Apache. By default, your mac machine should have Apache installed, if not, may download it here.

The benefit of this method is that once you have done setup, you can just open browser and access your project without further command to serve your PHP server like shown below.

http://localhost/~yourUsername/yourProjectName/

However, this method required some understanding of how to use command-line programs (Terminal), basic knowledge of how web servers work, and basic usage of text editors like vi or nano.

Well, let me share some simple steps of setup. There is a default directory called Sites, locate in User directory. You need to put your project folder in Sites, your directory should look something like this.

/Users/yourUsername/Sites/yourProject

Now, we have to enable personal website by editing the apache config file. Uncomment the following line in /etc/apache2/httpd.conf by removing the #. You may open up the file with sudo nano httpd.conf.

LoadModule php7_module libexec/apache2/libphp7.soLoadModule userdir_module libexec/apache2/mod_userdir.soInclude /private/etc/apache2/extra/httpd-userdir.conf

Save and quit with control + x , it will prompt you to save, just respond with “Y” which represent Yes then enter. Then go to edit /etc/apache2/extra/httpd-userdir.conf and uncomment the following, save and exit.

Include /private/etc/apache2/users/*.conf

Almost there, hang on with me. Edit /etc/apache2/users/yourUsername.conf and change according to the below lines stated.

#For Apache 2.2
<Directory "/Users/yourUsername/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Order allow,deny
Allow from all
</Directory>
#For Apache 2.4
<Directory "/Users/yourUsername/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>

That’s it, you can start or restart your apache server with one of the command below. Then you should be able to access your localhost with the URL I stated above.

sudo apachectl start
sudo apachectl restart

Conclusion : Both methods have it’s pro and cons. If you don’t know how to setup the config for Apache server, may just stick with simple php serve command. If you have more knowledge of web server, the second method is probably gonna save your time and trouble. The configuration steps may be different depends on your machine situation. I have attach some helpful discussion or blog that helping or explaining the config setup, hope it helps. Thank you and happy coding.

Any freelance work, please give me a try. Contact me at samlim9343@gmail.com.

--

--

Sam Lim

Hi guys, I’m Sam from Malaysia. I start my career as a web developer since 2017. Major in PHP, NodeJS, Javascript. I had experience with Flutter, AWS as well.