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