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