简体   繁体   中英

How can I use PHP's built-in webserver with Symfony on Windows, with a specified hostname and nonstandard port?

EDIT: Sorry everyone, this isn't something you could have fixed! The AppKernel class had been modified to change the cache directory, as below:

public function getCacheDir()
{
    if(isset($_SERVER['HTTP_HOST']))
    {
        return $this->rootDir.'/cache/'.$this->environment.'/'.$_SERVER['HTTP_HOST'];
    }
    else{
        return $this->rootDir.'/cache/'.$this->environment.'/default';
    }
 }

So not down to Symfony, or PHP, but a previous developer (presumably not on Windows!). Thanks for all your help, +1s all round.


I'm hoping there's a simple answer to this, but right now I can't see it!

Windows 10
Symfony 2.8.11
PHP 5.5.9

For convenience, I'd like to use PHP's built-in webserver (via the Symfony Console) to run a Symfony (2.8) application, on a port other than 80. I have a colleague successfully doing this, but he's using Linux, and I'm on Windows 10. The issue is that, on anything other than the standard port 80, when Symfony builds its cache the port is appended to one of the directory names, with a colon, which is illegal in Windows filenames (although not elsewhere). The cache build process fails, and the app doesn't run.

I'm starting the PHP server via Symfony's Console, like so:

php app/console server:run appname.local

The directory it's trying to build is:

C:\git\appname\app/cache/dev/appname.local:8000

And so I get the error:

RuntimeException in bootstrap.php.cache line 2763:
Unable to create the cache directory C:\git\appname\app/cache/dev/appname.local:8000)

I'd just use the standard port (this does work), but in fact I want to run several things at once, and they can't all be on 80.

Is there any way I can run a Symfony site on PHP's webserver, on Windows, on a non-standard port, in such a way that Symfony doesn't choke at the point of building the cache? For clarity, I could change webserver, and I could change OS, but for the purposes of this question assume that those are fixed. I'd prefer not to switch off the cache (it's slow enough as it is!) but that's an option if it would help.

EDIT: it seems like this works for at least some people, so there must be something different about my config. Best bet is probably the PHP version, which is quite old (not for any particular reason, just laziness).

Symfony has a command to run a webserver (which uses the php built-in PHP server)

php bin/console server:start

This command will start the server on port 8000 (default config)

Have a look here for more information about available options : https://symfony.com/doc/current/setup/built_in_web_server.html

to start the server on a particular port specify the port after the IP address from the command line. Using the IP address is useful too, because you can access from a different host.

For example, let's say you run ipconfig /all and you see your IPv$ address is 192.168.1.100 . Then you can run:

php bin/console server:start 192.168.1.100:8888

This starts Symfony's built-in web server on port 8888 on IP address 192.168.1.100 . So in a browser you can enter: http://192.168.1.100:8888/ where / is the route you want to access.

To stop the built-in server enter:

php bin console server:stop 192.168.1.100:8888

You'll see messages on the command line showing the stopping/starting of the built-in web server.

As in my question edit, this was down to developer action rather than a Symfony or PHP issue to be solved by the community.

public function getCacheDir()
{
    if(isset($_SERVER['HTTP_HOST']))
    {
        return $this->rootDir.'/cache/'.$this->environment.'/'.$_SERVER['HTTP_HOST']; //KABOOM
    }
    else{
        return $this->rootDir.'/cache/'.$this->environment.'/default';
    }
}

Thanks for all your efforts!

public function getCacheDir()
{
    if(isset($_SERVER['HTTP_HOST']))
    {
        return str$this->rootDir.'/cache/'.$this->environment.'/'.str_replace(':','_', $_SERVER['HTTP_HOST']);
    }
    else{
        return $this->rootDir.'/cache/'.$this->environment.'/default';
    }
 }

a colon isnt a valid character for filesystem filename, so replace it with underscore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM