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 hidev\modifiers\Sudo; |
14
|
|
|
use Yii; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* NGINX virtual host component. |
18
|
|
|
*/ |
19
|
|
|
class Vhost extends \hidev\base\Component |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Nginx |
23
|
|
|
*/ |
24
|
|
|
public $nginx; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var integer |
28
|
|
|
*/ |
29
|
|
|
public $timeout; |
30
|
|
|
|
31
|
|
|
public $ssl; |
32
|
|
|
|
33
|
|
|
public $_aliases = []; |
34
|
|
|
|
35
|
|
|
protected $_sslDir; |
36
|
|
|
protected $_ips = []; |
37
|
|
|
protected $_localIps = []; |
38
|
|
|
protected $_domain; |
39
|
|
|
protected $_webDir; |
40
|
|
|
protected $_logDir; |
41
|
|
|
protected $_fpmSocket; |
42
|
|
|
protected $_additionalConfig; |
43
|
|
|
|
44
|
|
|
public function chmodSSL() |
45
|
|
|
{ |
46
|
|
|
$dir = $this->getSslDir(); |
47
|
|
|
$this->passthru('chown', ['-R', 'www-data', $dir, Sudo::create()]); |
48
|
|
|
$this->passthru('chgrp', ['-R', 'www-data', $dir, Sudo::create()]); |
49
|
|
|
$this->passthru('chmod', ['-R', 'o-rwx', $dir, Sudo::create()]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function renderConf() |
53
|
|
|
{ |
54
|
|
|
return $this->nginx->render('default.twig', [ |
55
|
|
|
'this' => $this, |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setDomain($value) |
60
|
|
|
{ |
61
|
|
|
$this->_domain = trim($value); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setDomains($domains) |
65
|
|
|
{ |
66
|
|
|
if (!is_array($domains)) { |
67
|
|
|
$domains = preg_split('/[\s,]+/', trim($domains)); |
68
|
|
|
} |
69
|
|
|
$this->_domain = array_shift($domains); |
70
|
|
|
$this->_aliases = $domains; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getDomain() |
74
|
|
|
{ |
75
|
|
|
if ($this->_domain === null || $this->_domain === 'default') { |
76
|
|
|
$this->_domain = $this->takePackage()->name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->_domain; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setIp($value) |
83
|
|
|
{ |
84
|
|
|
$this->_ips = [$value]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setIps($value) |
88
|
|
|
{ |
89
|
|
|
$this->_ips = is_array($value) ? array_unique($value) : [$value]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getIps() |
93
|
|
|
{ |
94
|
|
|
if (empty($this->_ips)) { |
95
|
|
|
$this->_ips = $this->findIps(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->_ips; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function findIps() |
102
|
|
|
{ |
103
|
|
|
$ips = gethostbyname($this->getDomain()); |
104
|
|
|
if ($ips === $this->getDomain()) { |
105
|
|
|
$ips = ''; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return [$ips ?: '127.0.0.1']; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function setLocalIps($value) |
112
|
|
|
{ |
113
|
|
|
$this->_localIps = is_array($value) ? array_unique($value) : [$value]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getLocalIps() |
117
|
|
|
{ |
118
|
|
|
if (empty($this->_localIps)) { |
119
|
|
|
$this->_localIps = $this->ssl ? ['127.0.0.1'] : $this->getIps(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->_localIps; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setWebDir($value) |
126
|
|
|
{ |
127
|
|
|
$this->_webDir = Yii::getAlias($value); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getWebDir() |
131
|
|
|
{ |
132
|
|
|
if ($this->_webDir === null) { |
133
|
|
|
$this->_webDir = $this->takeGoal('start')->buildRootPath('web'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this->_webDir; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function setLogDir($value) |
140
|
|
|
{ |
141
|
|
|
$this->_logDir = Yii::getAlias($value); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getLogDir() |
145
|
|
|
{ |
146
|
|
|
if ($this->_logDir === null) { |
147
|
|
|
$this->_logDir = $this->nginx->getLogDir(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->_logDir; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setFpmSocket($value) |
154
|
|
|
{ |
155
|
|
|
$this->_fpmSocket = $value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getFpmSocket() |
159
|
|
|
{ |
160
|
|
|
if ($this->_fpmSocket === null) { |
161
|
|
|
$this->_fpmSocket = $this->nginx->getFpmSocket(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->_fpmSocket; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function setSslDir($value) |
168
|
|
|
{ |
169
|
|
|
$this->_sslDir = Yii::getAlias($value); |
170
|
|
|
if ($this->_sslDir[0] !== '/') { |
171
|
|
|
$this->_sslDir = $this->takeGoal('start')->buildRootPath($this->_sslDir); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getSslDir() |
176
|
|
|
{ |
177
|
|
|
if ($this->_sslDir === null) { |
178
|
|
|
$this->_sslDir = $this->takeGoal('start')->buildRootPath('ssl'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->_sslDir; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function setAliases($aliases) |
185
|
|
|
{ |
186
|
|
|
if (!is_array($aliases)) { |
187
|
|
|
$aliases = preg_split('/[\s,]+/', trim($aliases)); |
188
|
|
|
} |
189
|
|
|
$this->_aliases = $aliases; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getAliases() |
193
|
|
|
{ |
194
|
|
|
return $this->_aliases; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getDomains() |
198
|
|
|
{ |
199
|
|
|
$domains = $this->getAliases(); |
200
|
|
|
array_unshift($domains, $this->getDomain()); |
201
|
|
|
|
202
|
|
|
return $domains; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getServerName() |
206
|
|
|
{ |
207
|
|
|
return implode(' ', $this->getDomains()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getAdditionalConfig() |
211
|
|
|
{ |
212
|
|
|
return $this->_additionalConfig; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function setAdditionalConfig($additinalConfig) |
216
|
|
|
{ |
217
|
|
|
$this->_additionalConfig = $additinalConfig; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|