|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* HiDev Nginx plugin |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/hidev-nginx |
|
7
|
|
|
* @package hidev-nginx |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hidev\nginx\controllers; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
use hidev\base\File; |
|
16
|
|
|
use hidev\modifiers\Sudo; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Goal for Nginx. |
|
21
|
|
|
*/ |
|
22
|
|
|
class NginxController extends \hidev\controllers\CommonController |
|
23
|
|
|
{ |
|
24
|
|
|
use \hiqdev\yii2\collection\ManagerTrait; |
|
25
|
|
|
|
|
26
|
|
|
protected $_logDir; |
|
27
|
|
|
protected $_etcDir; |
|
28
|
|
|
protected $_fpmSocket; |
|
29
|
|
|
|
|
30
|
|
|
public $defaultClass = VhostController::class; |
|
31
|
|
|
|
|
32
|
1 |
|
public function actionDoDump() |
|
33
|
|
|
{ |
|
34
|
1 |
|
foreach ($this->getItems() as $vhost) { |
|
35
|
1 |
|
$conf = $vhost->renderConf(); |
|
36
|
1 |
|
file_put_contents($vhost->getDomain() . '.conf', $conf); |
|
37
|
1 |
|
} |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function actionDump() |
|
41
|
|
|
{ |
|
42
|
1 |
|
return $this->perform('do-dump'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function actionDeploy($aliases = []) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
return $this->perform('do-deploy'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function actionDoDeploy() |
|
51
|
|
|
{ |
|
52
|
|
|
$etcDir = $this->getEtcDir(); |
|
53
|
|
|
if (!is_dir($etcDir)) { |
|
54
|
|
|
throw new InvalidParamException("Non existing Nginx etcDir: $etcDir"); |
|
55
|
|
|
} |
|
56
|
|
|
$enabledDir = $etcDir . DIRECTORY_SEPARATOR . 'sites-enabled'; |
|
57
|
|
|
$availableDir = $etcDir . DIRECTORY_SEPARATOR . 'sites-available'; |
|
58
|
|
|
static::mkdir($enabledDir); |
|
59
|
|
|
static::mkdir($availableDir); |
|
60
|
|
|
foreach ($this->getItems() as $vhost) { |
|
61
|
|
|
$conf = $vhost->renderConf(); |
|
62
|
|
|
$name = $vhost->getDomain() . '.conf'; |
|
63
|
|
|
$file = File::plain($availableDir . DIRECTORY_SEPARATOR . $name); |
|
64
|
|
|
$file->save($conf); |
|
65
|
|
|
$file->symlink($enabledDir . DIRECTORY_SEPARATOR . $name); |
|
66
|
|
|
} |
|
67
|
|
|
$this->actionRestart(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function actionStart() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->make('start'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function actionStop() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->make('stop'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function actionReload() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->make('reload'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function actionRestart() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->make('restart'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function actionStatus() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->make('status', false); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function make($operation, $sudo = true) |
|
96
|
|
|
{ |
|
97
|
|
|
$args = ['nginx', $operation]; |
|
98
|
|
|
if ($sudo) { |
|
99
|
|
|
array_push($args, Sudo::create()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->passthru('service', $args); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function actionMake() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->make($this->performName ?: 'status'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function actionLetsencrypt() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->perform('do-letsencrypt'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function actionDoLetsencrypt() |
|
116
|
|
|
{ |
|
117
|
|
|
foreach ($this->getItems() as $vhost) { |
|
118
|
|
|
$domain = $vhost->getDomain(); |
|
119
|
|
|
$sslDir = $vhost->getSslDir(); |
|
120
|
|
|
$args = ['certonly', '-a', 'webroot', '--webroot-path=' . $vhost->getWebDir()]; |
|
121
|
|
|
foreach (array_reverse($vhost->getDomains()) as $name) { |
|
122
|
|
|
array_push($args, '-d'); |
|
123
|
|
|
array_push($args, $name); |
|
124
|
|
|
} |
|
125
|
|
|
if ($this->passthru('/opt/letsencrypt/letsencrypt-auto', $args)) { |
|
126
|
|
|
throw new Exception('failed letsencrypt'); |
|
127
|
|
|
} |
|
128
|
|
|
static::mkdir($sslDir); |
|
129
|
|
|
$this->passthru('sh', ['-c', "cp /etc/letsencrypt/live/$domain/* $sslDir", Sudo::create()]); |
|
130
|
|
|
$vhost->actionChmodSsl(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function actionChmodSsl() |
|
135
|
|
|
{ |
|
136
|
|
|
foreach ($this->getItems() as $vhost) { |
|
137
|
|
|
$vhost->actionChmodSsl(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public static function mkdir($path) |
|
142
|
|
|
{ |
|
143
|
|
|
if (file_exists($path)) { |
|
144
|
|
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return mkdir($path, 0777, true); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Prepares item config. |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function getItemConfig($name = null, array $config = []) |
|
154
|
|
|
{ |
|
155
|
1 |
|
return array_merge([ |
|
156
|
1 |
|
'domain' => $name, |
|
157
|
1 |
|
'nginx' => $this, |
|
158
|
1 |
|
'class' => $this->defaultClass, |
|
159
|
1 |
|
], $config); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
public function createItem($id, $config = []) |
|
163
|
|
|
{ |
|
164
|
1 |
|
return Yii::createObject($this->getItemConfig($id, $config), [$id, Yii::$app]); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function setLogDir($value) |
|
168
|
|
|
{ |
|
169
|
|
|
$this->_logDir = $value; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
public function getLogDir() |
|
173
|
|
|
{ |
|
174
|
1 |
|
if ($this->_logDir === null) { |
|
175
|
1 |
|
$this->_logDir = '/var/log/nginx'; |
|
176
|
1 |
|
} |
|
177
|
|
|
|
|
178
|
1 |
|
return $this->_logDir; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function setEtcDir($value) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->_etcDir = $value; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function getEtcDir() |
|
187
|
|
|
{ |
|
188
|
|
|
if ($this->_etcDir === null) { |
|
189
|
|
|
$this->_etcDir = $this->findEtcDir(); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $this->_etcDir; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function findEtcDir() |
|
196
|
|
|
{ |
|
197
|
|
|
$dirs = ['/etc/nginx', '/usr/local/etc/nginx']; |
|
198
|
|
|
foreach ($dirs as $dir) { |
|
199
|
|
|
if (is_dir($dir)) { |
|
200
|
|
|
return $dir; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return reset($dirs); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
1 |
|
public function setFpmSocket($value) |
|
208
|
|
|
{ |
|
209
|
1 |
|
$this->_fpmSocket = $value; |
|
210
|
1 |
|
} |
|
211
|
|
|
|
|
212
|
1 |
|
public function getFpmSocket() |
|
213
|
|
|
{ |
|
214
|
1 |
|
if ($this->_fpmSocket === null) { |
|
215
|
|
|
$this->_fpmSocket = 'unix:' . $this->findFpmSocketFile(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
1 |
|
return $this->_fpmSocket; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function findFpmSocketFile() |
|
222
|
|
|
{ |
|
223
|
|
|
$files = ['/run/php/php7.0-fpm.sock', '/var/run/php5-fpm.sock', '/tmp/php-fpm.sock']; |
|
224
|
|
|
foreach ($files as $file) { |
|
225
|
|
|
if (file_exists($file)) { |
|
226
|
|
|
return $file; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return reset($files); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.