<!--
  canonical-url: https://www.avonture.be/blog/docker-php-run-script-or-website
  published:     2023-11-02
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# The easiest way to run a PHP script / website

> The easiest way to run a standard PHP script or website instantly using Docker. Just one docker run command and your code is live for quick testing.

---
![The easiest way to run a PHP script / website](/img/v2/php_tips.webp)

**TL;DR**

This article shows the fastest way to run a standard PHP script or site locally: drop the code into a folder and run `docker run -d -p 8080:80 -u ${UID}:${GID} -v "$PWD":/var/www/html php:8.3-apache`, then browse to `http://127.0.0.1:8080` — no local PHP or Apache install needed, and swapping the image tag switches PHP versions instantly.

Imagine this situation: you wish to run a piece of standard PHP code (no dependency) like the one below:

**index.php**

```php
<?php

echo "<h2>Incorrect, silent bug</h2>";

print_r(json_decode(utf8_decode("Ipso lorem"), true));

echo "<h2>Incorrect, we got an exception</h2>";

print_r(json_decode(utf8_decode("Ipso lorem"), true, 512, JSON_THROW_ON_ERROR));
```

The easiest way is to:

* Create a temporary directory with `mkdir /tmp/snippet && cd $_`,
* Create an `index.php` file there (with your snippet like above),
* Run this command in the console: `docker run -d -p 8080:80 -u ${UID}:${GID} -v "$PWD":/var/www/html php:8.3-apache` and,
* Browse to `http://127.0.0.1:8080`.

It's done.

Explanations for the `docker run -d -p 8080:80 -u ${UID}:${GID} -v "$PWD":/var/www/html php:8.3-apache` command:

* We wish to make the local website accessible on port 8080 (so `http://127.0.0.1:8080`),
* We wish to synchronize the current folder (i.e. `/tmp/snippet`) with the Docker container so changes in any file in `/tmp/snippet` will be immediately reflected in Docker and thus in your browser,
* And we wish to use `php:8.3-apache`. Just replace with e.g. `php:8.4-apache` to switch to a newer version.

Three things you'll want next: [Update php.ini when using a Docker image](/blog/docker-php-ini) when the default settings are too tight, [Docker init now supports PHP](/blog/docker-init) to turn this one-liner into a real `Dockerfile` + `compose.yaml`, and [jakzal/phpqa](/blog/php-jakzal-phpqa) to run every PHP quality tool on that code — still without installing anything.

*For a site with no PHP at all, [Running an HTML site in seconds using Docker](/blog/docker-html-site) is the even lighter version.*
