From 2ed85e3e0ac1c8f511bea1d7653738b6e27e05d6 Mon Sep 17 00:00:00 2001 From: Antonio Carlos Ribeiro Date: Wed, 22 Feb 2017 11:43:37 -0300 Subject: [PATCH] Check nginx.conf for errors before restarting nginx --- cli/Valet/Nginx.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cli/Valet/Nginx.php b/cli/Valet/Nginx.php index a6ca0273f..4f63c8ff5 100644 --- a/cli/Valet/Nginx.php +++ b/cli/Valet/Nginx.php @@ -2,6 +2,8 @@ namespace Valet; +use DomainException; + class Nginx { var $brew; @@ -9,6 +11,7 @@ class Nginx var $files; var $configuration; var $site; + const NGINX_CONF = '/usr/local/etc/nginx/nginx.conf'; /** * Create a new Nginx instance. @@ -56,7 +59,7 @@ function installConfiguration() $contents = $this->files->get(__DIR__.'/../stubs/nginx.conf'); $this->files->putAsUser( - '/usr/local/etc/nginx/nginx.conf', + static::NGINX_CONF, str_replace(['VALET_USER', 'VALET_HOME_PATH'], [user(), VALET_HOME_PATH], $contents) ); } @@ -103,6 +106,19 @@ function installNginxDirectory() $this->rewriteSecureNginxFiles(); } + /** + * Check nginx.conf for errors. + */ + private function lint() + { + $this->cli->quietly( + 'sudo nginx -c '.static::NGINX_CONF.' -t', + function($exitCode, $outputMessage) { + throw new DomainException("Nginx cannot start, please check your nginx.conf [$exitCode: $outputMessage]."); + } + ); + } + /** * Generate fresh Nginx servers for existing secure sites. * @@ -122,6 +138,8 @@ function rewriteSecureNginxFiles() */ function restart() { + $this->lint(); + $this->brew->restartService($this->brew->nginxServiceName()); }