How to run a webphar PHP file from built-in PHP 5.4 webserver?
Asked Answered
G

8

16

As per title, I created a very simple test phar. I'd like to test it with the built-in webserver(PHP 5.4) but it seems impossible.

php -S localhost:80 /path/to/myphar.php

Result: blank page (in the phar front controller is index.php doing some HTML output).

php -S localhost:80 -t /path/to/folder_with_index_and_phar

With loader script:

<?php

require 'phar://myphar.php'

Result in a blank page, again.

Is this even possible or I have to use Apache?

EDIT: after an year I need this again. Here is what's happening (from answers):

php -S localhost:80 -t /path/to/app.phar
php -S localhost:80 -t /path/to/app.phar

Results in an error: /path/to/app.phar is not a directory.

While:

php -S localhost:80 -t .
http://localost/app.phar/foo/bar

Works, but I don't need the app.phar "prefix"!

Using Apache and setting the directory index to app.phar works as expected.

Giron answered 28/5, 2014 at 21:42 Comment(3)
Do you get errors from the loader script option, perhaps in your logs?Wimmer
Pherhaps there are some grammar errors in your myphar.php. What happen if you excute echo "<?php phpinfo();" > myphar.phpBedtime
check this github.com/phpDocumentor/phpDocumentor2/issues/1004 hope it helps atleast a bit :)Verbid
B
5

php -S localhost:80 -t /path/to/phar isn't going to work since the -t argument is meant to be used for directories. The trick is that the php webserver supports passing a router as argument. Therefore, suppose your phar is in /tmp (it can be anywhere), this is how you create a simple router, and setup the webserver:

echo '<?php require("/path/to/phar");'>/tmp/router.php
php -S localhost:80 /tmp/router.php
Baca answered 8/7, 2015 at 13:47 Comment(0)
C
5
php -S localhost:8080 phpinfo.phar

That seemed to work for me just fine from a simple phar file I created to out put phpinfo();

I did notice in the PHP docs, when creating the PHAR file, that you seem to need to setup stubs for CLI and WWW interfaces.

http://php.net/manual/en/phar.buildfromdirectory.php

<?php
// create with alias "project.phar"
$phar = new Phar('project.phar', 0, 'project.phar');
// add all files in the project
$phar->buildFromDirectory(dirname(__FILE__) . '/project');
$phar->setStub($phar->createDefaultStub('cli/index.php', 'www/index.php'));

EDIT

Specifically, this is what I did.

#!/usr/bin/env php
<?php

$phar = new Phar('phpinfo.phar', 0, 'phpinfo.phar');

$phar->buildFromDirectory(__DIR__ . '/phpinfo');
$phar->setStub($phar->createDefaultStub('index.php'));

to create a phar file of a directory:

phpinfo/
└── index.php

and index.php was just:

<?php phpinfo();
Cobweb answered 14/7, 2015 at 19:10 Comment(0)
R
3

After hours of messing around with built-in web server, finally found the solution to what you are looking for. The key is router file for the web server. Follow below procedure

1) Create a router.php file for built-in web server with following content

    $path = $_SERVER["REQUEST_URI"];
    if($pathPosition = strpos($path,'/myapp') !== false){
        //Request has been made to Phar-ed application
        $pathPosition += strlen('/myapp');
        $file = substr($path, $pathPosition);

        //Here include the base path of your Phar-ed application with file path
        //I placed myphar.phar and router.php in the root directory for this example
        require_once("phar://myphar.phar/$file");
        exit;
    }
    return FALSE;

What this does: It checks the Uri for your application name, in my case "/myapp" and then extract the file path you want to access. Then includes the desired phared file into current script and exits. Else process the request as normal.

2) Start the built-in web server as follows:

     php -S localhost:80 -t . router.php

What this does: Fires up the built-in webserver making current directory as document root folder and uses router.php as router file (Placed in the same folder)

3) Make a request in browser like this:

    http://localhost/myapp/foo/bar

You will get the Phar-ed file from you application .phar file

Let me know in case of questions.

Note: You can also use regex to match the Uri pattern in router.php

Rossetti answered 10/7, 2015 at 23:42 Comment(0)
B
2

So, I do not understand why you need the Apache server. After the command

php -S localhost:80 -t /path/to/myphar.php

PHP 5.5.9-1ubuntu4 Development Server started at Fri Jun  6 22:27:49 2014
Listening on http://localhost:80
Document root is /tmp/http
Press Ctrl-C to quit.

means all is well, you go to your browser's address bar write

http://localhost:80

and see the result of the script execution

Breathy answered 6/6, 2014 at 15:37 Comment(1)
Sorry for answering so late but this won't work: webserver says: /path/to/myphar.php is not a directory!Giron
T
2

If you are willing to be a bit more flexible you could do what you want by creating an index.php file and then including your .phar which is possible according to the documentation which states that:

Using a Phar archive library is identical to using any other PHP library:

Therefore you can just include it normally like such:

<?php require_once "app.phar";

After you run php -S localhost:80 the index.php will be executed and you can just access it via http://localhost.

Tallu answered 8/7, 2015 at 9:52 Comment(0)
S
1

On windows if you are using xampp serve then you can use this command in command line.

C:\xampp\php>"C:\xampp\php\php.exe" "C:\xampp\php\pharcommand.phar"
Solenoid answered 16/7, 2015 at 21:54 Comment(0)
A
0

What about creating an index.php file in the same directory as that app.phar. the contents of it should be :

<?php
require "myapp.phar";
?>

Then access it by serving that index.php file using the command php -S localhost:80 /path/to/pharapp/index.php

Airlee answered 14/7, 2015 at 17:41 Comment(0)
P
-1

You can run a webphar with the built in web server by renaming the phar to have a .php extension (e.g. mv app.phar app.php), pointing to that directory:

php -S localhost:80 -t /path/to/phar

and accessing it as http://localhost/app.php/someroute.php. The tricky part is usually setting up the stub, munging the environment, and generating the phar.

Pharr answered 12/5, 2015 at 1:7 Comment(2)
Sorry for answering so late but this won't work: webserver says: /path/to/myphar.php is not a directory!Giron
@Giron Make sure "-t /path/to/phar" is only pointing to the directory containing the phar, not the phar itself (e.g., NOT "/path/to/phar/myphar.php"). If you are running this command in the same directory as the phar you can leave the "-t" bit off.Pharr

© 2022 - 2024 — McMap. All rights reserved.