1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nginx plugin for HiDev. |
4
|
|
|
* |
5
|
|
|
* @see https://github.com/hiqdev/hidev-nginx |
6
|
|
|
* @package hidev-nginx |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hidev\nginx\components; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use hidev\base\File; |
15
|
|
|
use hidev\modifiers\Sudo; |
16
|
|
|
use Yii; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* nginx component. |
20
|
|
|
*/ |
21
|
|
|
class Nginx extends \hidev\base\Component |
22
|
|
|
{ |
23
|
|
|
use \hiqdev\yii2\collection\ManagerTrait; |
24
|
|
|
|
25
|
|
|
protected $_logDir; |
26
|
|
|
protected $_etcDir; |
27
|
|
|
protected $_fpmSocket; |
28
|
|
|
|
29
|
|
|
public $defaultClass = Vhost::class; |
30
|
|
|
|
31
|
|
|
public function dump() |
32
|
|
|
{ |
33
|
|
|
foreach ($this->getItems() as $vhost) { |
34
|
|
|
$conf = $vhost->renderConf(); |
35
|
|
|
File::plain($vhost->getDomain() . '.conf')->save($conf); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function deploy() |
40
|
|
|
{ |
41
|
|
|
$etcDir = $this->getEtcDir(); |
42
|
|
|
if (!is_dir($etcDir)) { |
43
|
|
|
throw new InvalidParamException("Non existing Nginx etcDir: $etcDir"); |
44
|
|
|
} |
45
|
|
|
$enabledDir = $etcDir . DIRECTORY_SEPARATOR . 'sites-enabled'; |
46
|
|
|
$availableDir = $etcDir . DIRECTORY_SEPARATOR . 'sites-available'; |
47
|
|
|
static::mkdir($enabledDir); |
|
|
|
|
48
|
|
|
static::mkdir($availableDir); |
|
|
|
|
49
|
|
|
foreach ($this->getItems() as $vhost) { |
50
|
|
|
$conf = $vhost->renderConf(); |
51
|
|
|
$name = $vhost->getDomain() . '.conf'; |
52
|
|
|
$file = File::plain($availableDir . DIRECTORY_SEPARATOR . $name); |
53
|
|
|
$file->save($conf); |
54
|
|
|
$file->symlink($enabledDir . DIRECTORY_SEPARATOR . $name); |
55
|
|
|
} |
56
|
|
|
$this->restart(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function start() |
60
|
|
|
{ |
61
|
|
|
return $this->run('start'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function stop() |
65
|
|
|
{ |
66
|
|
|
return $this->run('stop'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function Reload() |
70
|
|
|
{ |
71
|
|
|
return $this->run('reload'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function Restart() |
75
|
|
|
{ |
76
|
|
|
return $this->run('restart'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function Status() |
80
|
|
|
{ |
81
|
|
|
return $this->run('status', false); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function run($operation, $sudo = true) |
85
|
|
|
{ |
86
|
|
|
$args = ['nginx', $operation]; |
87
|
|
|
if ($sudo) { |
88
|
|
|
array_push($args, Sudo::create()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->passthru('service', $args); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function letsencrypt() |
95
|
|
|
{ |
96
|
|
|
foreach ($this->getItems() as $vhost) { |
97
|
|
|
$domain = $vhost->getDomain(); |
98
|
|
|
$sslDir = $vhost->getSslDir(); |
99
|
|
|
$args = ['certonly', '-a', 'webroot', '--webroot-path=' . $vhost->getWebDir()]; |
100
|
|
|
foreach (array_reverse($vhost->getDomains()) as $name) { |
101
|
|
|
array_push($args, '-d'); |
102
|
|
|
array_push($args, $name); |
103
|
|
|
} |
104
|
|
|
if ($this->passthru('/opt/letsencrypt/letsencrypt-auto', $args)) { |
105
|
|
|
throw new Exception('failed letsencrypt'); |
106
|
|
|
} |
107
|
|
|
static::mkdir($sslDir); |
|
|
|
|
108
|
|
|
$this->passthru('sh', ['-c', "cp /etc/letsencrypt/live/$domain/* $sslDir", Sudo::create()]); |
109
|
|
|
$vhost->chmodSSL(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function chmodSSL() |
114
|
|
|
{ |
115
|
|
|
foreach ($this->getItems() as $vhost) { |
116
|
|
|
$vhost->chmodSSL(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private static function mkdir($path) |
121
|
|
|
{ |
122
|
|
|
if (file_exists($path)) { |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return mkdir($path, 0777, true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Prepares item config. |
131
|
|
|
*/ |
132
|
|
|
public function getItemConfig($name = null, array $config = []) |
133
|
|
|
{ |
134
|
|
|
return array_merge([ |
135
|
|
|
'domain' => $name, |
136
|
|
|
'nginx' => $this, |
137
|
|
|
'class' => $this->defaultClass, |
138
|
|
|
], $config); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function createItem($id, $config = []) |
142
|
|
|
{ |
143
|
|
|
return Yii::createObject($this->getItemConfig($id, $config)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function setLogDir($value) |
147
|
|
|
{ |
148
|
|
|
$this->_logDir = $value; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getLogDir() |
152
|
|
|
{ |
153
|
|
|
if ($this->_logDir === null) { |
154
|
|
|
$this->_logDir = '/var/log/nginx'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->_logDir; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setEtcDir($value) |
161
|
|
|
{ |
162
|
|
|
$this->_etcDir = $value; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getEtcDir() |
166
|
|
|
{ |
167
|
|
|
if ($this->_etcDir === null) { |
168
|
|
|
$this->_etcDir = $this->findEtcDir(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->_etcDir; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function findEtcDir() |
175
|
|
|
{ |
176
|
|
|
$dirs = ['/etc/nginx', '/usr/local/etc/nginx']; |
177
|
|
|
foreach ($dirs as $dir) { |
178
|
|
|
if (is_dir($dir)) { |
179
|
|
|
return $dir; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return reset($dirs); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setFpmSocket($value) |
187
|
|
|
{ |
188
|
|
|
$this->_fpmSocket = $value; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function getFpmSocket() |
192
|
|
|
{ |
193
|
|
|
if ($this->_fpmSocket === null) { |
194
|
|
|
$this->_fpmSocket = 'unix:' . $this->findFpmSocketFile(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this->_fpmSocket; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function findFpmSocketFile() |
201
|
|
|
{ |
202
|
|
|
$files = ['/run/php/php7.0-fpm.sock', '/var/run/php5-fpm.sock', '/tmp/php-fpm.sock']; |
203
|
|
|
foreach ($files as $file) { |
204
|
|
|
if (file_exists($file)) { |
205
|
|
|
return $file; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return reset($files); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: