macOS·29 questions

How to run and configure a local web server using the PHP and Python interpreters built into macOS?

Answer

For quick web page verification or script testing, developers often do not need to install heavy server environments like Apache or Nginx. The macOS operating system already has built-in tools that allow you to launch a simple local server in a couple of seconds right from the terminal.

First of all, you will need to launch the standard Terminal application via Spotlight or from the Utilities folder. Using the cd command, navigate to your project folder where the site files or scripts you want to run locally are located.

If your project is written in pure HTML and PHP, you can use the system-built PHP server. To do this, enter the command php -S localhost:8000 in the terminal and press Enter. After that, the server will be successfully launched, and information about current connections will appear in the terminal window.

In situations where you need to spin up a simple static file server using Python, modern versions of macOS use the Python 3 interpreter. Navigate to the desired directory in the terminal and run the command python3 -m http.server 8000, which will instantly create a local server on the specified port.

After executing any of these commands, open your favorite web browser and enter http://localhost:8000 in the address bar. You will see the contents of your working directory or the main file of the site. To stop the server and free up the port, simply return to the terminal and press the Control plus C keyboard shortcut.

Was this answer helpful?

More questions in this topic

Related questions from other topics