Skip to content

Commit

Permalink
Modify FPM and Nginx config files to take advantage of default 'inclu…
Browse files Browse the repository at this point in the history
…de' folders.
  • Loading branch information
cpriego committed Dec 11, 2016
1 parent d242f87 commit 58a6085
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 114 deletions.
12 changes: 9 additions & 3 deletions cli/Valet/Nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -66,17 +66,23 @@ 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],
$this->files->get(__DIR__.'/../stubs/valet.conf')
)
);

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')
Expand Down
28 changes: 14 additions & 14 deletions cli/Valet/PhpFpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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)
);
}

/**
Expand All @@ -81,7 +81,7 @@ public function restart()
*/
public function stop()
{
$this->ubuntu->stopService($this->ubuntu->linkedPhp()['service']);
$this->ubuntu->stopService($this->ubuntu->linkedPhp()['fpm']);
}

/**
Expand Down
21 changes: 12 additions & 9 deletions cli/Valet/Ubuntu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -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.");
}
Expand All @@ -153,6 +156,6 @@ function linkedPhp()
*/
function restartLinkedPhp()
{
$this->restartService($this->linkedPhp()['service']);
$this->restartService($this->linkedPhp()['fpm']);
}
}
47 changes: 27 additions & 20 deletions cli/stubs/fastcgi_params
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions cli/stubs/fpm.conf
Original file line number Diff line number Diff line change
@@ -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
21 changes: 14 additions & 7 deletions cli/stubs/nginx.conf
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
10 changes: 2 additions & 8 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
41 changes: 25 additions & 16 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
];

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
2 changes: 1 addition & 1 deletion tests/NginxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions tests/PhpFpmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Loading

0 comments on commit 58a6085

Please sign in to comment.