How Do I Run PHP? Every Method Explained (Web Server, CLI, and Local Stacks)
If you have written your first `` and double-clicked the file only to see raw source code in your browser, you have already discovered the single most important fact about PHP: it does not run by itself the way an HTML file opens by itself. PHP is a server-side language, which means something has to *execute* it before a human ever sees the result.
The question “how do I run PHP” sounds simple, but it actually maps to several different setups depending on what you are trying to do. Running PHP for a live website is fundamentally different from running PHP scripts on your own machine for testing. This guide walks through every method, shows you the exact commands, and clears up the confusion that trips up nearly everyone at the start.
Key Takeaways
• You rarely “run” PHP yourself for a website. Your web host’s PHP engine executes your `.php` files automatically whenever a visitor requests them.
• The PHP CLI (`php script.php`) runs scripts directly from the command line — ideal for development, automation, and testing.
• `php -S localhost:8000` spins up a built-in development server in seconds, but it is for local testing only, never production.
• Local stacks like XAMPP, MAMP, and Laragon bundle Apache, PHP, and MySQL so you can simulate a real server on your computer.
• Always check your version first with `php -v` — behaviour and available features depend heavily on it.
What does “running PHP” actually mean?
PHP is an interpreted language. There is no double-click-to-launch executable for your script. Instead, a program called the PHP interpreter reads your code, processes it line by line, and produces output — usually HTML that a browser then displays.
That interpreter can be invoked in two broad contexts:
- By a web server, when someone visits a URL that maps to a `.php` file. This is how websites, WordPress, Laravel apps, and online stores work.
- By you, directly, on the command line, when you want to run a script for development, a scheduled task, or a quick test.
Understanding which of these you need is the whole game. Let me show you both.
The hidden confusion behind “how do I run PHP”: This single question disguises two completely different goals that people constantly conflate. The first goal is running a PHP-powered website — and here is the twist: *you don’t really run PHP yourself at all.* Your web host’s PHP engine executes your `.php` files automatically the moment a visitor requests the page. You just upload the files (or install WordPress) and they work. The second goal is running PHP scripts for development or testing, where you genuinely invoke `php` by hand on the command line. Knowing which one you actually want saves enormous frustration: for a live site you need PHP hosting and an upload, full stop — the CLI and the built-in server are developer tools, not the mechanism by which production websites serve pages. People burn hours trying to “start PHP” on their server when their host was already running it for them.
How do I check if PHP is installed and which version I have?
Before running anything, confirm PHP exists on your system and find out the version. Open a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS/Linux) and run:
“`bash php -v “`
You should see something like:
“`text PHP 8.3.6 (cli) (built: Apr 11 2024 …) Copyright (c) The PHP Group Zend Engine v4.3.6 … “`
If instead you get `’php’ is not recognized` or `command not found`, PHP either is not installed or is not on your system PATH. The version number matters a great deal — language features, performance, and security support all depend on it.
“`bash
which php # macOS / Linux where php # Windows
php -i | more “`
For a deeper walkthrough of getting the interpreter onto your machine, see . To understand which release to target and why, see .
How do I run a PHP website on a web server (the normal way)?
This is what most people mean, even if they do not realise it. For a live website, you do not start PHP manually. Your hosting environment already has a web server (Apache, Nginx, or LiteSpeed) wired to a PHP engine. The flow looks like this:
- You write a file, for example `index.php`.
- You upload it to your hosting account (via FTP, the file manager, or Git).
- A visitor opens `https://yoursite.com/index.php`.
- The web server hands the file to the PHP engine, which executes it.
- The engine returns plain HTML, and the browser displays the page.
Your `index.php` can mix HTML and PHP freely:
“`php
Welcome
The time on the server is .
“`
The visitor never sees the `` part — only the rendered result. That is the entire model behind WordPress, e-commerce platforms, and custom apps. You manage files; the host runs the engine. If a page shows raw PHP code instead of output, the server is not configured to execute PHP for that file — a hosting configuration issue, not a code issue. (For diagnosing what goes wrong, see .)
This is also why you cannot just open a `.php` file from your desktop in a browser and expect it to work — there is no PHP engine in that path. You need either a host or a local server, which brings us to the developer methods.
How do I run a PHP script from the command line (PHP CLI)?
The PHP CLI (Command Line Interface) runs scripts directly, with no web server involved. This is perfect for testing logic, running scheduled jobs (cron), building tools, or just learning. Create a file called `hello.php`:
“`php
Then run it:
“`bash php hello.php “`
Output:
“`text Hello World from the command line! “`
You can pass arguments, run inline code, or open an interactive shell:
“`bash
php -r “echo 2 + 2;” # prints 4
php -a
php greet.php Ravi “`
“`php
The CLI is how professional developers test code in isolation and how servers run background tasks. It is not how a website serves pages to visitors — keep that distinction firmly in mind.
How do I use PHP’s built-in development server?
Since PHP 5.4, the interpreter ships with a tiny built-in web server. It lets you preview a PHP site in a browser without installing Apache or Nginx. From the folder containing your `.php` files, run:
“`bash php -S localhost:8000 “`
Now open `http://localhost:8000` in your browser. If your folder has an `index.php`, it serves it. You can also point it at a specific document root:
“`bash
php -S localhost:8000 -t public “`
This is fantastic for quick local development. But the official documentation is blunt about it: the built-in server is designed for development and testing only. It is single-threaded, lacks the hardening of a real server, and must never be used to serve a public, production site. Use it to build; use real PHP hosting to ship.
When should I use a local stack like XAMPP, MAMP, or Laragon?
When your project needs a database, an Apache/Nginx configuration that mirrors production, or multiple moving parts, a bundled local stack is the cleaner choice. These packages install Apache + PHP + MySQL/MariaDB (and often more) in one go:
- XAMPP — cross-platform (Windows, macOS, Linux), the long-time standard.
- MAMP — popular on macOS, also available for Windows.
- Laragon — Windows-focused, lightweight, developer-friendly.
The typical workflow: install the stack, start the services from its control panel, then drop your project into the web root (for XAMPP that is the `htdocs` folder). Visit `http://localhost/yourproject` and the bundled Apache + PHP serves it exactly as a real host would — database included.
A local stack is the closest match to a production environment, which is why it is the go-to for building WordPress sites, Laravel apps, or anything touching MySQL before deploying to a real host.
Which method should I use? A quick comparison
| Method | Best use case | Command / action |
|---|---|---|
| Web host PHP engine | Live, public websites and apps | Upload `.php` files; engine runs them automatically on request |
| PHP CLI | Scripts, automation, cron jobs, testing logic | `php script.php` |
| Built-in dev server | Quick local preview, no setup | `php -S localhost:8000` |
| Local stack (XAMPP/MAMP/Laragon) | Full local dev with database | Start services, place files in web root |
| Check installation | Verify PHP and version | `php -v` |
Here is the same idea framed by *what you are building*:
| Your goal | What you actually need |
|---|---|
| A public website or WordPress site | PHP hosting + upload your files |
| Test a script or build a CLI tool | PHP CLI on your machine |
| Preview a small PHP app locally | `php -S` built-in server |
| Develop with a database before launch | Local stack (XAMPP/MAMP/Laragon) |
How does embedding PHP inside HTML work?
PHP shines because you can drop it directly into HTML using `` tags. Everything outside the tags is sent to the browser untouched; everything inside is executed:
“`php
- {$fruit}
“; } ?>
“`
When executed, the visitor receives clean HTML:
“`html
- Apples
- Mangoes
- Bananas
“`
This blend of markup and logic is exactly why PHP powers such a large share of the web. But remember — for this to render, the file must pass through a PHP engine, whether that is your host, the built-in server, or a local stack.
Running PHP for DarazHost
When your goal is a real, live website, the simplest path is to let your host run PHP for you. DarazHost runs your PHP automatically — upload your `.php` files or install WordPress in a couple of clicks, and the server’s PHP engine executes them on every request. You can select your PHP version per site directly in cPanel, and each environment ships with performance features like OPcache and LiteSpeed already in place. There is no interpreter to install, no server to configure, and no command to “start” — it is just working PHP hosting, backed by 24/7 support if anything needs a hand. You write the code; the engine does the running.
For the full picture of versions, configuration, performance tuning, and security, read our complete guide to PHP hosting. And once your site is live, knowing helps you reason about performance and debugging.
Frequently asked questions
Why does my browser show PHP code instead of running it? Because you opened the `.php` file directly (via `file://` or a double-click) instead of through a PHP engine. Serve it through a web host, `php -S`, or a local stack so the interpreter can process it first.
Do I need to install PHP to run a website? Not on a host — your hosting provider already has PHP installed and configured. You only install PHP locally when you want to develop or test on your own machine.
What is the difference between `php script.php` and `php -S`? `php script.php` runs a single script once and exits (CLI mode). `php -S localhost:8000` starts a long-running web server that handles browser requests until you stop it. The first is for scripts; the second is for previewing web pages locally.
Can I use PHP’s built-in server for my live site? No. It is explicitly meant for development and testing only. It is single-threaded and not hardened for production traffic. Use proper PHP hosting for anything public.
How do I stop the built-in PHP server? Press `Ctrl + C` in the terminal window where it is running. That terminates the process and frees the port.