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 hidev\base\File; |
15
|
|
|
use hidev\modifiers\Sudo; |
16
|
|
|
use Exception; |
17
|
|
|
use Yii; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Goal for Nginx. |
21
|
|
|
*/ |
22
|
|
|
class NginxController extends \hidev\controllers\AbstractController |
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
|
|
|
public function actionDump() |
33
|
|
|
{ |
34
|
|
|
foreach ($this->getItems() as $vhost) { |
35
|
|
|
$conf = $vhost->renderConf(); |
36
|
|
|
file_put_contents($vhost->getDomain() . '.conf', $conf); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function actionDeploy() |
41
|
|
|
{ |
42
|
|
|
if (!file_exists($this->getEtcDir())) { |
43
|
|
|
throw new InvalidParamException('Non existing Nginx etcDir: ' . $this->getEtcDir()); |
44
|
|
|
} |
45
|
|
|
$enabledDir = $this->getEtcDir() . '/sites-enabled'; |
46
|
|
|
$availableDir = $this->getEtcDir() . '/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
|
|
|
$dest = $enabledDir . '/' . $name; |
|
|
|
|
53
|
|
|
$file = File::plain($availableDir . '/' . $name); |
|
|
|
|
54
|
|
|
$file->save($conf); |
55
|
|
|
$file->symlink($enabledDir . '/' . $name); |
56
|
|
|
} |
57
|
|
|
$this->actionRestart(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function actionStart() |
61
|
|
|
{ |
62
|
|
|
return $this->actionPerform('start'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function actionStop() |
66
|
|
|
{ |
67
|
|
|
return $this->actionPerform('stop'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function actionReload() |
71
|
|
|
{ |
72
|
|
|
return $this->actionPerform('reload'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function actionRestart() |
76
|
|
|
{ |
77
|
|
|
return $this->actionPerform('restart'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function actionStatus() |
81
|
|
|
{ |
82
|
|
|
return $this->actionPerform('status', false); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function actionPerform($operation, $sudo = true) |
86
|
|
|
{ |
87
|
|
|
$args = ['nginx', $operation]; |
88
|
|
|
if ($sudo) { |
89
|
|
|
array_push($args, Sudo::create()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->passthru('service', $args); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function actionLetsencrypt($aliases = []) |
96
|
|
|
{ |
97
|
|
|
foreach ($this->getItems() as $vhost) { |
98
|
|
|
$domain = $vhost->getDomain(); |
99
|
|
|
$sslDir = $vhost->getSslDir(); |
100
|
|
|
if (!$vhost->ssl || !$sslDir) { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
$args = [ |
104
|
|
|
'certonly', '-a', 'webroot', |
105
|
|
|
'--webroot-path=' . $vhost->getWebDir(), |
106
|
|
|
'-d', $domain, |
107
|
|
|
]; |
108
|
|
|
if (!is_array($aliases)) { |
109
|
|
|
$aliases = explode(',', trim($aliases)); |
110
|
|
|
} |
111
|
|
|
foreach ($aliases as $alias) { |
112
|
|
|
$alias = trim($alias); |
113
|
|
|
if ($alias) { |
114
|
|
|
array_push($args, '-d'); |
115
|
|
|
array_push($args, $alias); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
if ($this->passthru('/opt/letsencrypt/letsencrypt-auto', $args)) { |
119
|
|
|
throw new Exception('failed letsencrypt'); |
120
|
|
|
} |
121
|
|
|
static::mkdir($sslDir); |
122
|
|
|
$this->passthru('sh', ['-c', "cp /etc/letsencrypt/live/$domain/* $sslDir", Sudo::create()]); |
123
|
|
|
$vhost->actionChmodSsl(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function actionChmodSsl() |
128
|
|
|
{ |
129
|
|
|
foreach ($this->getItems() as $vhost) { |
130
|
|
|
$vhost->actionChmodSsl(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public static function mkdir($path) |
135
|
|
|
{ |
136
|
|
|
if (file_exists($path)) { |
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return mkdir($path, 0777, true); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Prepares item config. |
145
|
|
|
*/ |
146
|
|
|
public function getItemConfig($name = null, array $config = []) |
147
|
|
|
{ |
148
|
|
|
return array_merge([ |
149
|
|
|
'domain' => $name, |
150
|
|
|
'nginx' => $this, |
151
|
|
|
'class' => $this->defaultClass, |
152
|
|
|
], $config); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function createItem($id, $config = []) |
156
|
|
|
{ |
157
|
|
|
return Yii::createObject($this->getItemConfig($id, $config), [$id, Yii::$app]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setLogDir($value) |
161
|
|
|
{ |
162
|
|
|
$this->_logDir = $value; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getLogDir() |
166
|
|
|
{ |
167
|
|
|
if ($this->_logDir === null) { |
168
|
|
|
$this->_logDir = '/var/log/nginx'; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->_logDir; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function setEtcDir($value) |
175
|
|
|
{ |
176
|
|
|
$this->_etcDir = $value; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function getEtcDir() |
180
|
|
|
{ |
181
|
|
|
if ($this->_etcDir === null) { |
182
|
|
|
$this->_etcDir = $this->findEtcDir(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this->_etcDir; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function findEtcDir() |
189
|
|
|
{ |
190
|
|
|
$dirs = ['/etc/nginx', '/usr/local/etc/nginx']; |
191
|
|
|
foreach ($dirs as $dir) { |
192
|
|
|
if (is_dir($dir)) { |
193
|
|
|
return $dir; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return reset($dirs); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function setFpmSocket($value) |
201
|
|
|
{ |
202
|
|
|
$this->_fpmSocket = $value; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getFpmSocket() |
206
|
|
|
{ |
207
|
|
|
if ($this->_fpmSocket === null) { |
208
|
|
|
$this->_fpmSocket = 'unix:' . $this->findFpmSocketFile(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $this->_fpmSocket; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function findFpmSocketFile() |
215
|
|
|
{ |
216
|
|
|
$files = ['/run/php/php7.0-fpm.sock', '/var/run/php5-fpm.sock']; |
217
|
|
|
foreach ($files as $file) { |
218
|
|
|
if (file_exists($file)) { |
219
|
|
|
return $file; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return reset($files); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.