How Do I Run PHP? A Hands-On Guide to Every Method

So you have a `.php` file sitting in a folder, and you’re staring at it wondering: how do I actually *run* this thing? Good news — there are several ways, and once you understand the difference between them, PHP stops feeling like a mystery. In this guide we’ll walk through each method together, type the actual commands, and look at what you should see on screen. By the end you’ll know exactly which approach to reach for.

Key Takeaways
• PHP runs in two fundamentally different ways: from the command line (for scripts, cron jobs, and testing) and through a web server (for pages a browser requests).
• To run a script in your terminal, use `php script.php`. To check PHP is installed, run `php -v`.
• PHP’s built-in server (`php -S localhost:8000`) spins up an instant local site for testing — but never use it in production.
• Real websites serve PHP through Apache (mod_php or PHP-FPM) or Nginx + PHP-FPM.
• On shared hosting, you just upload `.php` files to `public_html` and the host’s web server runs them for you.

Is PHP even installed on my machine?

Before you run anything, let’s confirm PHP exists. Open your terminal and type:

“`bash php -v “`

You should see something like this:

“` PHP 8.3.6 (cli) (built: Apr 15 2026 10:22:31) (NTS) Copyright (c) The PHP Group Zend Engine v4.3.6, Copyright (c) Zend Technologies “`

If you get that version banner, you’re ready. If instead you see *”command not found”* or *”php is not recognized”*, PHP isn’t installed (or isn’t on your PATH). On most Linux systems you’d install it with your package manager, on macOS with Homebrew (`brew install php`), and on Windows you can grab a build from the official site or use a bundle. Once `php -v` prints a version, come back here.

How do I run a PHP script from the command line?

This is the most direct way to run PHP, and it’s where a lot of beginners *should* start. Create a file called `hello.php` with this content:

“`php

Now run it:

“`bash php hello.php “`

You should see:

“` Hello from the command line! “`

That’s it — PHP read the file, executed the code, and printed the output straight to your terminal. Notice there’s no browser, no `localhost`, no web server. This mode is perfect for testing logic quickly, cron jobs, data processing scripts, and CLI tools.

Want to run a tiny snippet without even making a file? Use the `-r` flag for a one-liner:

“`bash php -r ‘echo “Quick test: ” . (2 + 2) . “\n”;’ “`

Output:

“` Quick test: 4 “`

I use `php -r` constantly to sanity-check a function or test a quick calculation. It’s the PHP equivalent of scribbling on a notepad.

How do I run PHP as a website with the built-in server?

Command-line scripts are great, but what if your `.php` file is meant to produce a *web page* you open in a browser? PHP ships with a small built-in development server that’s perfect for this. Make a file called `index.php`:

“`php Hello from the browser!

“; echo “

The time is ” . date(“H:i:s”) . “

“; “`

Now, from inside that folder, run:

“`bash php -S localhost:8000 “`

You should see:

“` [Fri Jun 27 09:14:02 2026] PHP 8.3.6 Development Server (http://localhost:8000) started “`

Open your browser and visit http://localhost:8000. You’ll see your heading and the current time rendered as an actual web page. Refresh it — the time updates, because PHP re-runs the script on every request.

This is fantastic for local development. But the name says it all: it’s a development server. It handles one request at a time and has no hardening, so never point real traffic at it. Press `Ctrl+C` in the terminal to stop it.

The one thing that confuses every PHP beginner

Here’s the insight that makes everything click: PHP runs in two completely separate worlds, and beginners constantly conflate them.

  1. From the command line — you type `php script.php`, PHP executes the file, and output goes to your terminal. This is for scripts, cron jobs, queue workers, and testing. No browser involved.
  2. Through a web server — a browser requests a `.php` page, the web server hands that request to PHP, PHP runs the script, and the output comes back as a web page. This is how real websites work.

`php -S` is the quick *bridge* between these worlds for local testing — it lets you preview web pages without setting up a full server. But a production website does not run on `php -S`. It runs through Apache or Nginx paired with PHP-FPM. When you’re deciding how to run PHP, the very first question to ask yourself is: *do I need a terminal script, or a served web page?* Answer that, and the right method is obvious.

How do I run PHP on a real web server (Apache or Nginx)?

For a production website, you need a proper web server that listens for traffic and passes PHP requests to the PHP engine. There are two common setups:

Apache with mod_php or PHP-FPM. Apache can either embed PHP directly (the classic `mod_php`) or hand requests off to PHP-FPM (FastCGI Process Manager), which is the modern, more efficient approach. You drop your `.php` files into Apache’s web root — typically `/var/www/html` — and when a browser requests `example.com/index.php`, Apache recognizes the `.php` extension and routes it to PHP.

Nginx + PHP-FPM. Nginx doesn’t run PHP itself at all; it always pairs with PHP-FPM. A snippet of the Nginx config makes the handoff explicit:

“`nginx location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/run/php/php8.3-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } “`

That `fastcgi_pass` line is the key: it tells Nginx, *”for any `.php` request, send it to the PHP-FPM socket and give me back the result.”* PHP-FPM runs the script and returns HTML, which Nginx delivers to the browser.

The workflow is the same either way: put your `.php` files in the web root, and the server runs them automatically whenever someone visits the matching URL. You don’t type `php` yourself — the server does it for every request.

How do I run PHP on shared hosting?

For most people, this is the easiest path of all — and you barely have to think about web servers at all. On shared hosting, the host has already installed and configured a web server (usually Apache or LiteSpeed) plus PHP. Your job is simply to upload your files.

The steps:

  1. Connect to your hosting account via the file manager or an FTP/SFTP client.
  2. Place your `.php` files inside the `public_html` folder (your site’s web root).
  3. Visit your domain in a browser — `https://yourdomain.com/index.php`.

The host’s web server detects the `.php` file and runs it automatically. No commands, no config files, no PHP-FPM setup. This is why shared hosting is so popular for WordPress and other PHP apps: the *”how do I run PHP”* question is answered for you.

A quick comparison of every method

Method Command / Action Best for Production-ready?
CLI script `php script.php` Testing, cron jobs, CLI tools Yes (for scripts)
One-liner `php -r ‘…’` Quick snippets, sanity checks No
Built-in server `php -S localhost:8000` Local web development No
Apache + PHP-FPM Files in web root Production websites Yes
Nginx + PHP-FPM Files in web root + config High-traffic production sites Yes
Shared hosting Upload to `public_html` Most websites, fastest start Yes

Running PHP on DarazHost

Once you’ve moved past local testing and want your PHP site live, the hosting layer is where things either stay simple or get complicated. On DarazHost shared and cloud hosting, running PHP is the easy path: upload your `.php` files to `public_html` and the optimized web server runs them automatically — no manual server configuration required. You can select your exact PHP version through cPanel’s MultiPHP Manager, so a legacy app on PHP 7.4 and a modern app on PHP 8.3 can happily coexist on the same account.

Need full control over both the command line and the web server — say, to run cron-driven CLI scripts alongside a custom Nginx + PHP-FPM stack? A DarazHost VPS with root access gives you the whole toolkit. Either way you get reliable PHP hosting and 24/7 support for when a `.php` page doesn’t behave the way your terminal test promised it would.

Frequently Asked Questions

Q: What’s the difference between `php script.php` and `php -S`? A: `php script.php` runs a single script once and prints the output to your terminal — ideal for CLI tasks and cron jobs. `php -S localhost:8000` starts a small web server so you can open `.php` pages in a browser for local development. One is for the terminal, the other simulates a website.

Q: Can I use PHP’s built-in server (`php -S`) for a live website? A: No. The built-in server is explicitly a development tool. It handles requests one at a time and lacks the security and performance features of a real server. For production, use Apache or Nginx with PHP-FPM, or simply use shared hosting.

Q: Do I need to install Apache or Nginx to run PHP locally? A: Not just to test. PHP’s built-in server (`php -S`) is enough for local web development, and `php script.php` covers command-line work. You only need a full web server when you’re replicating a production environment or going live.

Q: How do I check which PHP version I’m running? A: Run `php -v` on the command line. To check the version your *website* uses (which can differ), create a file with `

Q: Why does my PHP file download instead of running in the browser? A: That almost always means the web server isn’t configured to pass `.php` files to PHP — common when PHP isn’t installed or the handler is missing. On shared hosting this is set up for you; on a self-managed server, confirm PHP-FPM is running and your server config routes `.php` requests to it.

Wrapping up

Running PHP comes down to one decision: terminal script or served web page? For scripts and quick tests, `php script.php` and `php -r` are your friends. For local web previews, `php -S localhost:8000` is a one-command wonder. For real websites, Apache or Nginx with PHP-FPM does the heavy lifting — and on shared hosting, you just upload files and you’re done. Pick the method that matches what you’re building, and PHP will do exactly what you ask.

About the Author

Leave a Reply