Skip to content

How to Host a Website on Localhost (PHP Built-in Server, Docker or XAMPP)

2 min read

Applies to: Windows, macOS and Linux; tested with the PHP 8.3 built-in server and the php:8.3-apache Docker image (Apache 2.4.68)

“Hosting on localhost” means running a website on your own computer, reachable at http://localhost, to build and test it before it goes online. Other people can’t see it. These are three simple ways, from quickest to most complete.

Option 1: PHP’s built-in server (quickest for PHP sites)

With PHP installed, open a terminal in your site’s folder:

php -S localhost:8000

Open http://localhost:8000. Our test printed PHP 8.3.33 Development Server (http://localhost:8000) started and logged each request. It serves index.php and static files, but has no .htaccess support and handles one request at a time, so it is for development only. For a static HTML site without PHP, python3 -m http.server 8000 works the same way.

Option 2: Docker (closest to a real server)

docker run -d --name mysite -p 8080:80 -v "$PWD":/var/www/html php:8.3-apache

Open http://localhost:8080: Apache 2.4 with PHP 8.3, including .htaccess. Add a database with a MariaDB container and Docker Compose when you need one (Docker Compose basics).

Pick an image that includes PHP: when we mounted the same folder into the plain httpd:2.4 image, Apache sent index.php as plain text (the PHP source), because that image has no PHP.

Option 3: XAMPP (installer for Windows, macOS and Linux)

XAMPP bundles Apache, MariaDB and PHP with a control panel. Its latest release when we checked was 8.2.12 with PHP 8.2, which receives security fixes only until the end of 2026. It still works for learning, but Docker or a current PHP installation is the better long-term choice.

localhost or 127.0.0.1?

Both mean “this computer”. A server started on localhost or 127.0.0.1 can’t be reached from other devices. Starting it on 0.0.0.0 makes it reachable from your network too, which you rarely want for development.

Use a local domain name

To open the site as http://mysite.test:8080, add a line to your hosts file (/etc/hosts on Linux and macOS, C:\Windows\System32\drivers\etc\hosts on Windows, edited as administrator):

127.0.0.1   mysite.test

When it is ready

Upload the site to hosting with SFTP or Git and point your domain at it. See our web hosting, or a KVM VPS for your own server. Check the PHP settings online afterwards: create a phpinfo page.

Was this answer helpful?

Still stuck? Open a ticket