diff --git a/cli/Valet/Nginx.php b/cli/Valet/Nginx.php index 90a3409ab..7094c8840 100644 --- a/cli/Valet/Nginx.php +++ b/cli/Valet/Nginx.php @@ -37,7 +37,7 @@ function __construct(Ubuntu $ubuntu, CommandLine $cli, Filesystem $files, */ function install() { - $this->ubuntu->ensureInstalled('nginx'); + $this->ubuntu->ensureInstalled('nginx-core'); $this->installConfiguration(); $this->installServer(); @@ -66,10 +66,10 @@ function installConfiguration() */ function installServer() { - $this->files->ensureDirExists('/etc/nginx/valet'); + // $this->files->ensureDirExists('/etc/nginx/valet'); $this->files->putAsUser( - '/etc/nginx/valet/valet.conf', + '/etc/nginx/sites-available/valet.conf', str_replace( ['VALET_HOME_PATH', 'VALET_SERVER_PATH'], [VALET_HOME_PATH, VALET_SERVER_PATH], @@ -77,6 +77,12 @@ function installServer() ) ); + if ($this->files->exists('/etc/nginx/sites-enabled/default')) { + $this->cli->run('rm -f /etc/nginx/sites-enabled/default'); + } + + $this->cli->run('ln -snf /etc/nginx/sites-available/valet.conf /etc/nginx/sites-enabled/valet.conf'); + $this->files->putAsUser( '/etc/nginx/fastcgi_params', $this->files->get(__DIR__.'/../stubs/fastcgi_params') diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index 1469a6463..a8c6ada3b 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -32,16 +32,17 @@ public function __construct(Ubuntu $ubuntu, CommandLine $cli, Filesystem $files) */ public function install() { - if (! $this->ubuntu->installed(get_config('php71')['name']) && - ! $this->ubuntu->installed(get_config('php70')['name']) && - ! $this->ubuntu->installed(get_config('php56')['name']) && - ! $this->ubuntu->installed(get_config('php55')['name'])) { - $this->ubuntu->ensureInstalled(get_config('php70')['name']); + if (! $this->ubuntu->installed(get_config('php71')['fpm']) && + ! $this->ubuntu->installed(get_config('php70')['fpm']) && + ! $this->ubuntu->installed(get_config('php56')['fpm']) && + ! $this->ubuntu->installed(get_config('php55')['fpm']) && + ! $this->ubuntu->installed(get_config('php5')['fpm'])) { + $this->ubuntu->ensureInstalled(get_config('php70')['fpm']); } $this->files->ensureDirExists('/var/log', user()); - $this->updateConfiguration(); + $this->installConfiguration(); $this->restart(); } @@ -51,15 +52,14 @@ public function install() * * @return void */ - public function updateConfiguration() + public function installConfiguration() { - $contents = $this->files->get($this->fpmConfigPath()); + $contents = $this->files->get(__DIR__.'/../stubs/fpm.conf'); - $contents = preg_replace('/^user = .+$/m', 'user = '.user(), $contents); - $contents = preg_replace('/^listen.owner = .+$/m', 'listen.owner = '.user(), $contents); - $contents = preg_replace('/^listen = .+$/m', 'listen = '.VALET_HOME_PATH.'/valet.sock', $contents); - - $this->files->put($this->fpmConfigPath(), $contents); + $this->files->putAsUser( + $this->fpmConfigPath(), + str_replace(['VALET_USER', 'VALET_HOME_PATH'], [user(), VALET_HOME_PATH], $contents) + ); } /** @@ -81,7 +81,7 @@ public function restart() */ public function stop() { - $this->ubuntu->stopService($this->ubuntu->linkedPhp()['service']); + $this->ubuntu->stopService($this->ubuntu->linkedPhp()['fpm']); } /** diff --git a/cli/Valet/Ubuntu.php b/cli/Valet/Ubuntu.php index a5da44d4a..b11fccf74 100644 --- a/cli/Valet/Ubuntu.php +++ b/cli/Valet/Ubuntu.php @@ -41,10 +41,11 @@ function installed($package) */ function hasInstalledPhp() { - return $this->installed(get_config('php71')['name']) - || $this->installed(get_config('php70')['name']) - || $this->installed(get_config('php56')['name']) - || $this->installed(get_config('php55')['name']); + return $this->installed(get_config('php71')['cli']) + || $this->installed(get_config('php70')['cli']) + || $this->installed(get_config('php56')['cli']) + || $this->installed(get_config('php55')['cli']) + || $this->installed(get_config('php5')['cli']); } /** @@ -133,14 +134,16 @@ function linkedPhp() $resolvedPath = $this->files->readLink(get_config('php-bin')); - if (strpos($resolvedPath, get_config('php71')['name']) !== false) { + if (strpos($resolvedPath, get_config('php71')['bin']) !== false) { return get_config('php71'); - } elseif (strpos($resolvedPath, get_config('php70')['name']) !== false) { + } elseif (strpos($resolvedPath, get_config('php70')['bin']) !== false) { return get_config('php70'); - } elseif (strpos($resolvedPath, get_config('php56')['name']) !== false) { + } elseif (strpos($resolvedPath, get_config('php56')['bin']) !== false) { return get_config('php56'); - } elseif (strpos($resolvedPath, get_config('php55')['name']) !== false) { + } elseif (strpos($resolvedPath, get_config('php55')['bin']) !== false) { return get_config('php55'); + } elseif (strpos($resolvedPath, get_config('php5')['bin']) !== false) { + return get_config('php5'); } else { throw new DomainException("Unable to determine linked PHP."); } @@ -153,6 +156,6 @@ function linkedPhp() */ function restartLinkedPhp() { - $this->restartService($this->linkedPhp()['service']); + $this->restartService($this->linkedPhp()['fpm']); } } diff --git a/cli/stubs/fastcgi_params b/cli/stubs/fastcgi_params index b4726b776..097553857 100644 --- a/cli/stubs/fastcgi_params +++ b/cli/stubs/fastcgi_params @@ -1,20 +1,27 @@ -fastcgi_param QUERY_STRING $query_string; -fastcgi_param REQUEST_METHOD $request_method; -fastcgi_param CONTENT_TYPE $content_type; -fastcgi_param CONTENT_LENGTH $content_length; -fastcgi_param SCRIPT_FILENAME $request_filename; -fastcgi_param SCRIPT_NAME $fastcgi_script_name; -fastcgi_param REQUEST_URI $request_uri; -fastcgi_param DOCUMENT_URI $document_uri; -fastcgi_param DOCUMENT_ROOT $document_root; -fastcgi_param SERVER_PROTOCOL $server_protocol; -fastcgi_param GATEWAY_INTERFACE CGI/1.1; -fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; -fastcgi_param REMOTE_ADDR $remote_addr; -fastcgi_param REMOTE_PORT $remote_port; -fastcgi_param SERVER_ADDR $server_addr; -fastcgi_param SERVER_PORT $server_port; -fastcgi_param SERVER_NAME $server_name; -fastcgi_param HTTPS $https if_not_empty; -fastcgi_param REDIRECT_STATUS 200; -fastcgi_param HTTP_PROXY ""; + +fastcgi_param QUERY_STRING $query_string; +fastcgi_param REQUEST_METHOD $request_method; +fastcgi_param CONTENT_TYPE $content_type; +fastcgi_param CONTENT_LENGTH $content_length; + +fastcgi_param SCRIPT_FILENAME $request_filename; +fastcgi_param SCRIPT_NAME $fastcgi_script_name; +fastcgi_param REQUEST_URI $request_uri; +fastcgi_param DOCUMENT_URI $document_uri; +fastcgi_param DOCUMENT_ROOT $document_root; +fastcgi_param SERVER_PROTOCOL $server_protocol; +fastcgi_param REQUEST_SCHEME $scheme; +fastcgi_param HTTPS $https if_not_empty; +fastcgi_param HTTP_PROXY ""; + +fastcgi_param GATEWAY_INTERFACE CGI/1.1; +fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; + +fastcgi_param REMOTE_ADDR $remote_addr; +fastcgi_param REMOTE_PORT $remote_port; +fastcgi_param SERVER_ADDR $server_addr; +fastcgi_param SERVER_PORT $server_port; +fastcgi_param SERVER_NAME $server_name; + +# PHP only, required if PHP was built with --enable-force-cgi-redirect +fastcgi_param REDIRECT_STATUS 200; diff --git a/cli/stubs/fpm.conf b/cli/stubs/fpm.conf new file mode 100644 index 000000000..663692a1c --- /dev/null +++ b/cli/stubs/fpm.conf @@ -0,0 +1,13 @@ +[valet] + +user = VALET_USER +group = www-data +listen = VALET_HOME_PATH/valet.sock + +listen.owner = VALET_USER +listen.group = www-data + +pm = ondemand +pm.max_children = 5 +pm.process_idle_timeout = 10s +pm.max_requests = 500 diff --git a/cli/stubs/nginx.conf b/cli/stubs/nginx.conf index b6c601a8c..40082f403 100644 --- a/cli/stubs/nginx.conf +++ b/cli/stubs/nginx.conf @@ -1,18 +1,25 @@ user VALET_USER www-data; worker_processes auto; +pid /run/nginx.pid; events { - worker_connections 1024; + worker_connections 1024; + # multi_accept on; } http { - include mime.types; - default_type application/octet-stream; + include /etc/nginx/mime.types; + default_type application/octet-stream; sendfile on; - keepalive_timeout 65; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + # server_tokens off; - gzip on; + gzip on; + gzip_disable "msie6"; gzip_comp_level 5; gzip_min_length 256; gzip_proxied any; @@ -35,7 +42,7 @@ http { text/plain text/x-component; + include /etc/nginx/conf.d/*.conf; + include /etc/nginx/sites-enabled/*; include VALET_HOME_PATH/Nginx/*; - include servers/*; - include valet/valet.conf; } diff --git a/cli/valet.php b/cli/valet.php index 469c5860c..5a3eef529 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -92,16 +92,10 @@ * Remove the current working directory to the paths configuration. */ $app->command('status', function () { - passthru('systemctl status nginx.service php*-fpm.service'); + passthru('service nginx status'); + passthru('service php*-fpm status'); })->descriptions('View Valet service status'); -/** - * Reload systemd services - */ -$app->command('reload', function () { - passthru('systemctl daemon-reload'); -})->descriptions('Reload Valet services'); - /** * Register a symbolic link with Valet. */ diff --git a/config.php b/config.php index 4e7cd466a..68d0797b6 100644 --- a/config.php +++ b/config.php @@ -12,35 +12,44 @@ function get_config($value) // PHP binary path "php-bin" => "/usr/bin/php", - // Systemd - "systemd-fpm" => "/var/run/php/php7.0-fpm.sock", - // PHP 7.1 "php71" => [ - "name" => "php7.1", - "service" => "php7.1-fpm", - "fpm-config" => "/etc/php/7.1/fpm/pool.d/www.conf", + "bin" => "php7.1", + "cli" => "php7.1-cli", + "fpm" => "php7.1-fpm", + "fpm-config" => "/etc/php/7.1/fpm/pool.d/valet.conf", ], // PHP 7.0 "php70" => [ - "name" => "php7.0", - "service" => "php7.0-fpm", - "fpm-config" => "/etc/php/7.0/fpm/pool.d/www.conf" + "bin" => "php7.0", + "cli" => "php7.0-cli", + "fpm" => "php7.0-fpm", + "fpm-config" => "/etc/php/7.0/fpm/pool.d/valet.conf" ], // PHP 5.6 "php56" => [ - "name" => "php5.6", - "service" => "php5.6-fpm", - "fpm-config" => "/etc/php/5.6/php-fpm.conf" + "bin" => "php5.6", + "cli" => "php5.6-cli", + "fpm" => "php5.6-fpm", + "fpm-config" => "/etc/php/5.6/fpm/pool.d/valet.conf" ], - // PHP 5.5 + // PHP 5.5.38 (Ondrej PPA) "php55" => [ - "name" => "php5.5", - "service" => "php5.5-fpm", - "fpm-config" => "/etc/php/5.5/php-fpm.conf" + "bin" => "php5.5", + "cli" => "php5.5-cli", + "fpm" => "php5.5-fpm", + "fpm-config" => "/etc/php/5.5/fpm/pool.d/valet.conf" + ], + + // PHP 5.5.9 (Ubuntu 14.04 default) + "php5" => [ + "bin" => "php5", + "cli" => "php5-cli", + "fpm" => "php5-fpm", + "fpm-config" => "/etc/php5/fpm/pool.d/valet.conf" ], ]; diff --git a/readme.md b/readme.md index a4af4e9f2..5db358983 100644 --- a/readme.md +++ b/readme.md @@ -38,10 +38,10 @@ After upgrading, it may be necessary to re-park or re-link your sites. ## Requirements - - Ubuntu >= 15.04 + - Ubuntu >= 14.04 - Dependencies: `sudo apt-get install libnss3-tools jq xsel` - PHP >= 5.6 - - PHP Packages: `php*-cli php*-common php*-curl php*-json php*-mbstring php*-mcrypt php*-opcache php*-readline php*-xml php*-zip` + - PHP Packages: `php*-cli php*-curl php*-mbstring php*-mcrypt php*-xml php*-zip` - Optional PHP Packages: `php*-sqlite3 php*-mysql php*-pgsql` **Replace the star _(*)_ with your php version** diff --git a/tests/NginxTest.php b/tests/NginxTest.php index df1ca4314..d10c62e39 100644 --- a/tests/NginxTest.php +++ b/tests/NginxTest.php @@ -38,7 +38,7 @@ public function test_install_nginx_configuration_places_nginx_base_configuration } - public function test_install_caddy_directories_creates_location_for_site_specific_configuration() + public function test_install_nginx_directories_creates_location_for_site_specific_configuration() { $files = Mockery::mock(Filesystem::class); $files->shouldReceive('isDir')->with(VALET_HOME_PATH.'/Nginx')->andReturn(false); diff --git a/tests/PhpFpmTest.php b/tests/PhpFpmTest.php index 4653a21d7..1c0217c7c 100644 --- a/tests/PhpFpmTest.php +++ b/tests/PhpFpmTest.php @@ -23,13 +23,14 @@ public function tearDown() } - public function test_update_configuration_replaces_user_and_group_in_config_file() + public function test_install_configuration_replaces_user_and_sock_in_config_file() { copy(__DIR__.'/files/fpm.conf', __DIR__.'/output/fpm.conf'); - resolve(StubForUpdatingFpmConfigFiles::class)->updateConfiguration(); + resolve(StubForUpdatingFpmConfigFiles::class)->installConfiguration(); $contents = file_get_contents(__DIR__.'/output/fpm.conf'); - $this->assertTrue(strpos($contents, 'user = '.user()) !== false); - $this->assertTrue(strpos($contents, 'listen.owner = '.user()) !== false); + $this->assertContains(sprintf("\nuser = %s", user()), $contents); + $this->assertContains(sprintf("\nlisten.owner = %s", user()), $contents); + $this->assertContains("\nlisten = ".VALET_HOME_PATH."/valet.sock", $contents); } } diff --git a/tests/UbuntuTest.php b/tests/UbuntuTest.php index e62246eef..dba52cd83 100644 --- a/tests/UbuntuTest.php +++ b/tests/UbuntuTest.php @@ -31,18 +31,18 @@ public function test_installed_returns_true_when_given_formula_is_installed() { $cli = Mockery::mock(CommandLine::class); $cli->shouldReceive('run')->once() - ->with('dpkg -l | grep '.get_config('php70')['name'].' | sed \'s_ _\t_g\' | cut -f 2') - ->andReturn(get_config('php70')['name']); + ->with('dpkg -l | grep '.get_config('php70')['bin'].' | sed \'s_ _\t_g\' | cut -f 2') + ->andReturn(get_config('php70')['bin']); swap(CommandLine::class, $cli); - $this->assertTrue(resolve(Ubuntu::class)->installed(get_config('php70')['name'])); + $this->assertTrue(resolve(Ubuntu::class)->installed(get_config('php70')['bin'])); $cli = Mockery::mock(CommandLine::class); $cli->shouldReceive('run')->once() - ->with('dpkg -l | grep '.get_config('php70')['name'].' | sed \'s_ _\t_g\' | cut -f 2') + ->with('dpkg -l | grep '.get_config('php70')['bin'].' | sed \'s_ _\t_g\' | cut -f 2') ->andReturn('php7.0-mcrypt php7.0'); swap(CommandLine::class, $cli); - $this->assertTrue(resolve(Ubuntu::class)->installed(get_config('php70')['name'])); + $this->assertTrue(resolve(Ubuntu::class)->installed(get_config('php70')['bin'])); } @@ -50,50 +50,53 @@ public function test_installed_returns_false_when_given_formula_is_not_installed { $cli = Mockery::mock(CommandLine::class); $cli->shouldReceive('run')->once() - ->with('dpkg -l | grep '.get_config('php70')['name'].' | sed \'s_ _\t_g\' | cut -f 2') + ->with('dpkg -l | grep '.get_config('php70')['bin'].' | sed \'s_ _\t_g\' | cut -f 2') ->andReturn(''); swap(CommandLine::class, $cli); - $this->assertFalse(resolve(Ubuntu::class)->installed(get_config('php70')['name'])); + $this->assertFalse(resolve(Ubuntu::class)->installed(get_config('php70')['bin'])); $cli = Mockery::mock(CommandLine::class); $cli->shouldReceive('run')->once() - ->with('dpkg -l | grep '.get_config('php70')['name'].' | sed \'s_ _\t_g\' | cut -f 2') + ->with('dpkg -l | grep '.get_config('php70')['bin'].' | sed \'s_ _\t_g\' | cut -f 2') ->andReturn('php7.0-mcrypt'); swap(CommandLine::class, $cli); - $this->assertFalse(resolve(Ubuntu::class)->installed(get_config('php70')['name'])); + $this->assertFalse(resolve(Ubuntu::class)->installed(get_config('php70')['bin'])); $cli = Mockery::mock(CommandLine::class); $cli->shouldReceive('run')->once() - ->with('dpkg -l | grep '.get_config('php70')['name'].' | sed \'s_ _\t_g\' | cut -f 2') + ->with('dpkg -l | grep '.get_config('php70')['bin'].' | sed \'s_ _\t_g\' | cut -f 2') ->andReturn('php7.0-mcrypt php7.0-something-else php7'); swap(CommandLine::class, $cli); - $this->assertFalse(resolve(Ubuntu::class)->installed(get_config('php70')['name'])); + $this->assertFalse(resolve(Ubuntu::class)->installed(get_config('php70')['bin'])); } public function test_has_installed_php_indicates_if_php_is_installed_via_apt() { $apt = Mockery::mock(Ubuntu::class.'[installed]', [new CommandLine, new Filesystem]); - $apt->shouldReceive('installed')->once()->with(get_config('php71')['name'])->andReturn(true); - $apt->shouldReceive('installed')->with(get_config('php70')['name'])->andReturn(true); - $apt->shouldReceive('installed')->with(get_config('php56')['name'])->andReturn(true); - $apt->shouldReceive('installed')->with(get_config('php55')['name'])->andReturn(true); + $apt->shouldReceive('installed')->once()->with(get_config('php71')['cli'])->andReturn(true); + $apt->shouldReceive('installed')->with(get_config('php70')['cli'])->andReturn(true); + $apt->shouldReceive('installed')->with(get_config('php56')['cli'])->andReturn(true); + $apt->shouldReceive('installed')->with(get_config('php55')['cli'])->andReturn(true); + $apt->shouldReceive('installed')->with(get_config('php5')['cli'])->andReturn(true); $this->assertTrue($apt->hasInstalledPhp()); $apt = Mockery::mock(Ubuntu::class.'[installed]', [new CommandLine, new Filesystem]); - $apt->shouldReceive('installed')->once()->with(get_config('php71')['name'])->andReturn(true); - $apt->shouldReceive('installed')->with(get_config('php70')['name'])->andReturn(false); - $apt->shouldReceive('installed')->with(get_config('php56')['name'])->andReturn(false); - $apt->shouldReceive('installed')->with(get_config('php55')['name'])->andReturn(false); + $apt->shouldReceive('installed')->once()->with(get_config('php71')['cli'])->andReturn(true); + $apt->shouldReceive('installed')->with(get_config('php70')['cli'])->andReturn(false); + $apt->shouldReceive('installed')->with(get_config('php56')['cli'])->andReturn(false); + $apt->shouldReceive('installed')->with(get_config('php55')['cli'])->andReturn(false); + $apt->shouldReceive('installed')->with(get_config('php5')['cli'])->andReturn(false); $this->assertTrue($apt->hasInstalledPhp()); $apt = Mockery::mock(Ubuntu::class.'[installed]', [new CommandLine, new Filesystem]); - $apt->shouldReceive('installed')->once()->with(get_config('php71')['name'])->andReturn(false); - $apt->shouldReceive('installed')->once()->with(get_config('php70')['name'])->andReturn(false); - $apt->shouldReceive('installed')->once()->with(get_config('php56')['name'])->andReturn(false); - $apt->shouldReceive('installed')->once()->with(get_config('php55')['name'])->andReturn(false); + $apt->shouldReceive('installed')->once()->with(get_config('php71')['cli'])->andReturn(false); + $apt->shouldReceive('installed')->once()->with(get_config('php70')['cli'])->andReturn(false); + $apt->shouldReceive('installed')->once()->with(get_config('php56')['cli'])->andReturn(false); + $apt->shouldReceive('installed')->once()->with(get_config('php55')['cli'])->andReturn(false); + $apt->shouldReceive('installed')->once()->with(get_config('php5')['cli'])->andReturn(false); $this->assertFalse($apt->hasInstalledPhp()); } @@ -122,13 +125,13 @@ public function test_linked_php_returns_linked_php_formula_name() $files->shouldReceive('isLink')->once()->with(get_config('php-bin'))->andReturn(true); $files->shouldReceive('readLink')->once()->with(get_config('php-bin'))->andReturn('/test/path/php7.0/test'); swap(Filesystem::class, $files); - $this->assertSame(get_config('php70')['name'], resolve(Ubuntu::class)->linkedPhp()['name']); + $this->assertSame(get_config('php70')['bin'], resolve(Ubuntu::class)->linkedPhp()['bin']); $files = Mockery::mock(Filesystem::class); $files->shouldReceive('isLink')->once()->with(get_config('php-bin'))->andReturn(true); $files->shouldReceive('readLink')->once()->with(get_config('php-bin'))->andReturn('/test/path/php5.6/test'); swap(Filesystem::class, $files); - $this->assertSame(get_config('php56')['name'], resolve(Ubuntu::class)->linkedPhp()['name']); + $this->assertSame(get_config('php56')['bin'], resolve(Ubuntu::class)->linkedPhp()['bin']); } @@ -140,7 +143,7 @@ public function test_linked_php_throws_exception_if_no_php_link() $files = Mockery::mock(Filesystem::class); $files->shouldReceive('isLink')->once()->with(get_config('php-bin'))->andReturn(false); swap(Filesystem::class, $files); - resolve(Ubuntu::class)->linkedPhp()['name']; + resolve(Ubuntu::class)->linkedPhp()['bin']; } @@ -153,7 +156,7 @@ public function test_linked_php_throws_exception_if_unsupported_php_version_is_l $files->shouldReceive('isLink')->once()->with(get_config('php-bin'))->andReturn(true); $files->shouldReceive('readLink')->once()->with(get_config('php-bin'))->andReturn('/test/path/php42/test'); swap(Filesystem::class, $files); - resolve(Ubuntu::class)->linkedPhp()['name']; + resolve(Ubuntu::class)->linkedPhp()['bin']; } diff --git a/tests/files/fpm.conf b/tests/files/fpm.conf index 0c3bfa0bf..ae965bb71 100644 --- a/tests/files/fpm.conf +++ b/tests/files/fpm.conf @@ -1,7 +1,7 @@ Test contents -user = something -listen.owner = something-else -listen = /potentially/some.socket +user = VALET_USER +listen.owner = VALET_USER +listen = VALET_HOME_PATH/valet.socket more contents