248

I'm trying to build a PHP site and I'm wanting to test my PHP files without uploading them to my host. Basically testing them on my own machine before I upload them. How do I do that?

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Kozy
  • 2,691
  • 3
  • 18
  • 11
  • 2
    use XAMPP and install PHP server. sometimes if skype uses port 80 & 443, apache server will not launch. Complete solution is provided here http://feelzdroid.com/2015/12/install-php-server-local-machine.html – Naruto Jan 13 '16 at 10:06

16 Answers16

566

PHP 5.4 and later have a built-in web server these days.

You simply run the command from the terminal:

cd path/to/your/app
php -S 127.0.0.1:8000

Then in your browser go to http://127.0.0.1:8000 and boom, your system should be up and running. (There must be an index.php or index.html file for this to work.)

You could also add a simple Router

<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
    return false;    // serve the requested resource as-is.
} else { 
    require_once('resolver.php');
}
?>

And then run the command

php -S 127.0.0.1:8000 router.php

References:

GardenRouteGold
  • 5,979
  • 2
  • 14
  • 23
  • 16
    In case anyone was wondering, the -S and a reference to other commands can be found at the link below. In particular, -S means "Run with built-in web server." http://php.net/manual/en/features.commandline.options.php – calipoop Jun 15 '17 at 15:37
  • but what about the Databases it should have place to be stored in! – Yousef Altaf Sep 10 '17 at 14:03
  • 4
    @YousefAltaf the OP does not specify weather or not they wanted a database to be used. They asked for a PHP Server and this simply is a PHP Server. If you want to run a MYSQL Server or PostGres Server then that would be another question and you can configure your PHP Application to use said server. Also for quick development purposes a SQLITE DB should do. – GardenRouteGold Oct 02 '17 at 14:12
  • 3
    PHP Built-in web server does not support parallel request: `The web server runs a only one single-threaded process, so PHP applications will stall if a request is blocked.`. This may cause some performance problem. – vikyd Apr 26 '18 at 02:59
  • 3
    @vikyd this isn't mean't for a full fledged application development it's just a quick way to test / debug features or code IMHO. – GardenRouteGold Nov 16 '18 at 09:26
  • @Dark-Reaper- ,yeah , it should be simple, but without supporting `parallel request` it is not a very `quick` way. – vikyd Nov 16 '18 at 10:55
  • @vikyd as of php 7.4 it can fork child processes : "Set the PHP_CLI_SERVER_WORKERS environment variable to the number of desired workers before starting the server. This is not supported on Windows. " https://www.php.net/manual/en/features.commandline.webserver.php – MichaelMoser Oct 31 '22 at 06:15
69

Install and run XAMPP:

Updated link for download https://www.apachefriends.org/download.html

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Lukman
  • 18,462
  • 6
  • 56
  • 66
  • 2
    Just adding this article for anyone who is going for XAMPP/MAMP: http://www.dwuser.com/education/content/why-you-need-a-testing-server-and-how-to-do-it/ – Richard Fu Aug 07 '17 at 09:24
  • 85
    Using `php -S localhost:8000` is an easier choice, without installing additional stuff. – Alan May 26 '19 at 06:04
  • 9
    @alanwsx, replying to your comment more than one year later, thanks for your comment to my answer more than 10 years ago. `php -S` was only added in php 5.4 which was released in 2012. So when I wrote my answer in 2009, there was no such option. And as of today in the year of 2020, the time machine has not been invented yet. BTW the topic of the year 2020 is COVID-19, just to remind you in case you reply years from today. Bless StackOverflow for its long life. – Lukman Oct 22 '20 at 01:08
  • 2
    @Lukman lol yeah I didn't notice that your answer is from 2009 – Alan Oct 23 '20 at 02:57
  • 1
    @Lukman (also @alanwsx) that keeps happening to me regularly: – Sedat Kilinc Jan 30 '21 at 21:21
  • 1
    @Lukman & @alanwsx) But I see StackOverflow not only as a forum but also as a reference book. In Future, there will be others who are looking for that solution, hit similiar or even same problems...that's why...Well, done...! – Sedat Kilinc Jan 30 '21 at 21:28
55

This is a simple, sure fire way to run your php server locally:

php -S 0.0.0.0:<PORT_NUMBER>

Where PORT_NUMBER is an integer from 1024 to 49151

Example: php -S 0.0.0.0:8000

Notes:

  1. If you use localhost rather than 0.0.0.0 you may hit a connection refused error.

  2. If want to make the web server accessible to any interface, use 0.0.0.0.

  3. If a URI request does not specify a file, then either index.php or index.html in the given directory are returned.

Given the following file (router.php)

<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
    return false;    // serve the requested resource as-is.
} else { 
    echo "<p>Welcome to PHP</p>";
}
?>

Run this ...

php -S 0.0.0.0:8000 router.php

... and navigate in your browser to http://localhost:8000/ and the following will be displayed:

Welcome to PHP

Reference:

Built-in web server

l3x
  • 30,760
  • 1
  • 55
  • 36
  • 6
    Thanks for the tip regarding 0.0.0.0 , what a magic fix it was - should be incorporated into the accepted answer or something... – calipoop Jun 15 '17 at 18:08
  • BTW `0.0.0.0:8000` will blind the port `8000` to PHP built-in server only on all networks address, dependence on your server configuration this may solve you problem or create it fro you, – Salem Sep 15 '21 at 12:38
  • THIS is the only thing that solves the error. Thanks a lot! – Filip Happy May 23 '22 at 19:16
23

I often use following command to spin my PHP Laravel framework :

$ php artisan serve --port=8080
or
$ php -S localhost:8080 -t public/

In above command : - Artisan is command-line interface included with Laravel which use serve to call built in php server

To Run with built-in web server.

 php -S <addr>:<port> -T

Here,
-S : Switch to Run with built-in web server.
-T : Switch to specify document root for built-in web server.

Amitesh Bharti
  • 14,264
  • 6
  • 62
  • 62
9

I use WAMP. One easy install wizard, tons of modules to for Apache and PHP preconfigured and easy to turn on and off to match your remote config.

Chris Sobolewski
  • 12,819
  • 12
  • 63
  • 96
9

If you want an all-purpose local development stack for any operating system where you can choose from different PHP, MySQL and Web server versions and are also not afraid of using Docker, you could go for the devilbox.

The devilbox is a modern and highly customisable dockerized PHP stack supporting full LAMP and MEAN and running on all major platforms. The main goal is to easily switch and combine any version required for local development. It supports an unlimited number of projects for which vhosts and DNS records are created automatically. Email catch-all and popular development tools will be at your service as well. Configuration is not necessary, as everything is pre-setup with mass virtual hosting.

Getting it up and running is pretty straight-forward:

# Get the devilbox
$ git clone https://github.com/cytopia/devilbox
$ cd devilbox

# Create docker-compose environment file
$ cp env-example .env

# Edit your configuration
$ vim .env

# Start all containers
$ docker-compose up

devilbox

Links:

cytopia
  • 413
  • 5
  • 15
6

Install XAMPP. If you're running MS Windows, WAMP is also an option.

outis
  • 75,655
  • 22
  • 151
  • 221
5

MAMP if you are on a MAC MAMP

Joey Blake
  • 3,451
  • 2
  • 18
  • 16
3

AppServ is a small program in Windows to run:

  • Apache
  • PHP
  • MySQL
  • phpMyAdmin

It will also give you a startup and stop button for Apache. Which I find very useful.

Mikael S
  • 435
  • 1
  • 4
  • 14
2

If you are using Windows, then the WPN-XM Server Stack might be a suitable alternative.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
1

Use Apache Friends XAMPP. It will set up Apache HTTP server, PHP 5 and MySQL 5 (as far as I know, there's probably some more than that). You don't need to know how to configure apache (or any of the modules) to use it.

You will have an htdocs directory which Apache will serve (accessible by http://localhost/) and should be able to put your PHP files there. With my installation, it is at C:\xampp\htdocs.

Carson Myers
  • 37,678
  • 39
  • 126
  • 176
1

If you have a local machine with the right software: web server with support for PHP, there's no reason why you can't do as you describe.

I'm doing it at the moment with XAMPP on a Windows XP machine, and (at home) with Kubuntu and a LAMP stack.

pavium
  • 14,808
  • 4
  • 33
  • 50
1

Another option is the Zend Server Community Edition.

Joe Internet
  • 571
  • 1
  • 3
  • 4
0

you can create your own server in php using code as well!

<?php
set_time_limit(0);

$address = '127.0.0.1';
$port =4444;
$server = '$address + $port';

// <-- Starts Server
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind to address');
echo "\n Server is running on port $port waiting for connection... \n\n";

while(1)
{
    socket_listen($sock);
    $client = socket_accept($sock);

    $input = socket_read($client, 443);

    $incoming = array();
    $incoming = explode("\r\n", $input);

    $fetchArray = array();
    $fetchArray = explode(" ", $incoming[0]);

    $file = $fetchArray[1];
    if($file == "/"){ 

   

$file = "src/browser.php";// this file is open with server when it starts!

    } else {

        $filearray = array();
        $filearray = explode("/", $file);
        $file = $filearray[1];
    }

echo $fetchArray[0] . " Request " . $file . "\n"; 

// <-- Control Header
$output = "";
$Header = "HTTP/1.1 200 OK \r\n" .
"Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n" .
"Content-Type: text/html \r\n\r\n";

$Content = file_get_contents($file);
$output = $Header . $Content;

    socket_write($client,$output,strlen($output));
    socket_close($client);

}
print('server running..');

run this code then open browser to localhost:443 or whichever port you chose

0

A clean way to do this, even if you have existing servers on your machine, is to use Docker. Run from any terminal via docker run with a single line:

docker run --name=php -d -it -p 80:80 --mount type=bind,source='/absolute/path/to/your/php/web/root/folder/',target=/app webdevops/php-nginx-dev

You will now have a running container named php serving requests on your localhost, port 80. You should be able to see your php scripts in any browser using the url http://127.0.0.1

Notes:

  • If you don't have Docker installed, instructions for Debian/Ubuntu and Windows 10+ are at the end. It can be installed on Windows 7 but it's quite annoying and not worth it. For windows 7, if you must, I'd just install Uniserver or XAMPP or something like that.

  • You can confirm that the container is live by running docker ps in a terminal on the host machine.

  • In order to keep your app/code modifications after the container is terminated/removed, the web root is bound to the folder where you ran the docker run command. To change it, specify the path to your local folder in the --mount source='[/local/path]' parameter. Note: Because the folder is bound to the container, changes you make in the container will also be made in the host folder.

  • Logs can be viewed using the following command (--follow is optional, ctrl+c to exit):

    docker logs php --follow
    
  • The web root folder in the container is /app. This may be helpful if you don't need to save anything and don't feel like specifying a bind mount in the docker run command.

  • The port is specified using the -p [host port]:80 parameters. You may have to explicitly specify -p 80:80 in order to be able to connect to the container from a web browser (at least on Windows 10).

  • To access the container's bash terminal run this from the host machine (type exit to return to host):

    docker exec -it php /bin/bash
    
  • You can install packages in the container's bash terminal the same way that you would on a native Debian/Ubuntu box (e.g. apt install -y nano).

  • Composer is already installed (run composer -v from container's terminal to inspect)

  • To launch an additional container, specify a different host port and container name using the --name=[new_name] and -p [host port]:80 parameters.

  • If you need a database or other server, do the same thing with a docker image for MySQL or MariaDB or whatever you need. Just remember to bind the data folder to a host folder so you don't lose it if you accidentally delete your docker image(s).

  • How to install Docker:

    Debian/Ubuntu as root (or add sudo before each of these commands):

    apt-get update
    apt install -y ca-certificates curl gnupg lsb-release
    mkdir -p /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    chmod a+r /etc/apt/keyrings/docker.gpg
    apt-get update
    apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
    service docker start
    systemctl enable docker
    

    Windows 10+ (tested on 10, should work on >10):

    Use Chocolatey, a command-line package manager for Windows. Chocolatey also has a gui if you insist. Once installed, run:

    choco install -y docker-desktop
    

    Mac, Chromebook, etc:

    You are alone. But we believe in you.

Peter Kionga-Kamau
  • 6,504
  • 2
  • 17
  • 13
0

its possible using the PHP built-in server. Make sure to place php.exe path in settings enter image description here then just select this option and it will run in ur browser. enter image description here