1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-05 16:40:05 +01:00

Serve static files through Caddy instead of PHP

This commit is contained in:
Adam Wathan
2016-09-24 15:21:06 -04:00
parent 7176c54c31
commit 73a205aa81
5 changed files with 49 additions and 22 deletions

View File

@@ -46,12 +46,13 @@ function install()
$this->files->ensureDirExists('/usr/local/var/log', user()); $this->files->ensureDirExists('/usr/local/var/log', user());
$this->updateConfiguration(); $this->updateConfiguration();
$this->updateIni();
$this->restart(); $this->restart();
} }
/** /**
* Update the PHP FPM configuration to use the current user. * Update the PHP FPM configuration.
* *
* @return void * @return void
*/ */
@@ -66,6 +67,20 @@ function updateConfiguration()
$this->files->put($this->fpmConfigPath(), $contents); $this->files->put($this->fpmConfigPath(), $contents);
} }
/**
* Update the PHP FPM ini.
*
* @return void
*/
function updateIni()
{
$contents = $this->files->get($this->fpmIniPath());
$contents = preg_replace('/^default_mimetype = .+$/m', 'default_mimetype = ""', $contents);
$this->files->put($this->fpmIniPath(), $contents);
}
/** /**
* Restart the PHP FPM process. * Restart the PHP FPM process.
* *
@@ -95,16 +110,30 @@ function stop()
*/ */
function fpmConfigPath() function fpmConfigPath()
{ {
if ($this->brew->linkedPhp() === 'php71') { $confLookup = [
return '/usr/local/etc/php/7.1/php-fpm.d/www.conf'; 'php71' => '/usr/local/etc/php/7.1/php-fpm.d/www.conf',
} elseif ($this->brew->linkedPhp() === 'php70') { 'php70' => '/usr/local/etc/php/7.0/php-fpm.d/www.conf',
return '/usr/local/etc/php/7.0/php-fpm.d/www.conf'; 'php56' => '/usr/local/etc/php/5.6/php-fpm.conf',
} elseif ($this->brew->linkedPhp() === 'php56') { 'php55' => '/usr/local/etc/php/5.5/php-fpm.conf',
return '/usr/local/etc/php/5.6/php-fpm.conf'; ];
} elseif ($this->brew->linkedPhp() === 'php55') {
return '/usr/local/etc/php/5.5/php-fpm.conf'; return $confLookup[$this->brew->linkedPhp()];
} else { }
throw new DomainException('Unable to find php-fpm config.');
} /**
* Get the path to the FPM ini file for the current PHP version.
*
* @return string
*/
function fpmIniPath()
{
$versionLookup = [
'php71' => '/usr/local/etc/php/7.1/php.ini',
'php70' => '/usr/local/etc/php/7.0/php.ini',
'php56' => '/usr/local/etc/php/5.6/php.ini',
'php55' => '/usr/local/etc/php/5.5/php.ini',
];
return $versionLookup[$this->brew->linkedPhp()];
} }
} }

View File

@@ -119,15 +119,7 @@ public function mutateUri($uri)
*/ */
public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri) public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri)
{ {
$extension = strtolower(pathinfo($staticFilePath)['extension']); header('X-Accel-Redirect: ' . $staticFilePath);
$mimes = require(__DIR__.'/../mimes.php');
$mime = isset($mimes[$extension]) ? $mimes[$extension] : 'application/octet-stream';
header('Content-Type: '. $mime);
readfile($staticFilePath);
} }
/** /**

View File

@@ -7,7 +7,7 @@
* Define the ~/.valet path as a constant. * Define the ~/.valet path as a constant.
*/ */
define('VALET_HOME_PATH', $_SERVER['HOME'].'/.valet'); define('VALET_HOME_PATH', $_SERVER['HOME'].'/.valet');
define('VALET_SERVER_PATH', '/server.php'); define('VALET_SERVER_PATH', realpath(__DIR__ . '/../../server.php'));
/** /**
* Output the given text to the console. * Output the given text to the console.

View File

@@ -1,6 +1,9 @@
import VALET_HOME_PATH/Caddy/* import VALET_HOME_PATH/Caddy/*
:80 { :80 {
root /
internal /dev/null
fastcgi / /var/run/fpm-valet.socket php { fastcgi / /var/run/fpm-valet.socket php {
index VALET_SERVER_PATH index VALET_SERVER_PATH
} }

View File

@@ -3,6 +3,9 @@ http://VALET_SITE:80 {
} }
https://VALET_SITE:443 { https://VALET_SITE:443 {
root /
internal /dev/null
tls VALET_CERT VALET_KEY tls VALET_CERT VALET_KEY
fastcgi / /var/run/fpm-valet.socket php { fastcgi / /var/run/fpm-valet.socket php {