GithubHelp home page GithubHelp logo

sindresorhus / php-server Goto Github PK

View Code? Open in Web Editor NEW
135.0 4.0 9.0 14 KB

Start a PHP server

License: MIT License

JavaScript 90.37% TypeScript 9.63%
php-server php development nodejs npm-package webserver

php-server's Introduction

php-server

Start a PHP server

Uses PHP's built-in development web server (not for production use).

The Node.js process is automatically kept alive as long as the PHP server is running.

Install

npm install php-server

Usage

import phpServer from 'php-server';

const server = await phpServer();
console.log(`PHP server running at ${server.url}`);

API

phpServer(options?)

Returns an object with the following properties:

options

Type: object

port

Type: number
Default: 0

The port on which you want to access the server.

Specify 0 to use a random port.

hostname

Type: string
Default: '127.0.0.1' (Usually the same as localhost)

The hostname the server will use.

Use '0.0.0.0' if you want it to be accessible from the outside.

base

Type: string
Default: '.'

The directory the server will serve from.

open

Type: boolean | string
Default: false

Open the server URL in the browser.

Can be one of the following:

  • true: Opens the default server URL (http://${hostname}${port}).
  • A relative URL: Opens that URL in the browser. Useful when testing pages that are not the default.
env

Type: object
Default: {}

Set environment variables for the PHP process.

router

Type: string

Optionally specify the path to a router script that is run at the start of each HTTP request. If this script returns false, the requested resource is returned as-is. Otherwise, the script's output is returned to the browser.

Example router script:

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

Type: string
Default: 'php' (The one in your $PATH)

The path to the PHP binary.

Can be useful if you have multiple versions of PHP installed.

ini

Type: string
Default: The built-in php.ini

A path to a custom php.ini config file.

directives

Type: object
Default: {}

Add custom INI directives.

php-server's People

Contributors

richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php-server's Issues

isServerRunning hides application errors

Hi,

So I was getting the following error: Server docroot returned 500-level response. Please check your configuration for possible errors. and, after trying some things out, I finally decided to comment out the reject on line 31 of index.js and replace it with a resolve call, after doing that I could see that the cause of the error was an undefined function, so it was an application problem not a server, or configuration one.

I believe the error message is misleading and that errors of this type should still resolve the promise instead of reject it. Maybe I'm missing the reason why you're checking for if (statusCodeType === 5) and calling reject instead of just resolving and letting the user see the error for themselves.

Please let me know your thoughts on this and thank you for your time and effort ๐Ÿ™Œ.

Inconsistent behavior between `hostname` and `open`.

Below was pretty confusing. When setting open to a string I would expect it to be check relative vs absolute.

hostname: '0.0.0.0'
open: true
Opens browser with 0.0.0.0:8080

hostname: '0.0.0.0'
open: 'http://127.0.0.1:8080',
Opens browser with 0.0.0.0:8080/http://127.0.0.1:8080

hostname: 'localhost' (resolves to 127.0.0.1)
open: true
Errors with Warning: Could not start the PHP server: connect ECONNREFUSED 127.0.0.1:9119 Use --force to continue.
Likely would of opened 127.0.0.1:9119:8080 instead of localhost:8080

Javascripts not loading - Error 404

Hello,

I am using VSC and phpserver whilst testing some PHP files.

I have come across an issue where the JS files are not being loaded, such as:

The requested resource /js/jquery.min.js was not found on this server.
Loading failed for the <script> with source โ€œhttp://localhost:3000/js/jquery.min.jsโ€.

The file exists in that folder and I am not sure what to do.

Many Thanks.

Is there a way to keep the server alive?

Hi @sindresorhus,

thanks for the package! We have one problem. Maybe you can help us.

The server seems the be paused from time to time. Is there an option or another way to keep the server alive until I close the terminal?

System: macOS 11.6, Node v14.17.3, npm 6.14.13

Every support is welcome!

Dosen't work when packaged

Thanks for the awesome library. I have integrated this to my electron app and it works perfectly fine when running before packing. But when packaged using electron-builder, it dosen't seems to start. Any help on how i can correct it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.