|
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 $_ip; |
|
33
|
|
|
protected $_domain; |
|
34
|
|
|
protected $_webDir; |
|
35
|
|
|
protected $_logDir; |
|
36
|
|
|
protected $_fpmSocket; |
|
37
|
|
|
|
|
38
|
|
|
public function actionChmodSsl() |
|
39
|
|
|
{ |
|
40
|
|
|
$dir = $this->getSslDir(); |
|
41
|
|
|
$this->passthru('chown', ['-R', 'www-data', $dir, Sudo::create()]); |
|
42
|
|
|
$this->passthru('chgrp', ['-R', 'www-data', $dir, Sudo::create()]); |
|
43
|
|
|
$this->passthru('chmod', ['-R', 'o-rwx', $dir, Sudo::create()]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function renderConf() |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->nginx->render('default.twig', [ |
|
49
|
1 |
|
'this' => $this, |
|
50
|
1 |
|
]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
public function setDomain($value) |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->_domain = $value; |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function getDomain() |
|
59
|
|
|
{ |
|
60
|
1 |
|
if ($this->_domain === null || $this->_domain === 'default') { |
|
61
|
1 |
|
$this->_domain = $this->takePackage()->name; |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return $this->_domain; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function setIp($value) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->_ip = $value; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function getIp() |
|
73
|
|
|
{ |
|
74
|
1 |
|
if ($this->_ip === null) { |
|
75
|
1 |
|
$this->_ip = $this->findIp(); |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
return $this->_ip; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function findIp() |
|
82
|
|
|
{ |
|
83
|
1 |
|
return gethostbyname($this->getDomain()) ?: '127.0.0.1'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function setWebDir($value) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->_webDir = Yii::getAlias($value); |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function getWebDir() |
|
92
|
|
|
{ |
|
93
|
1 |
|
if ($this->_webDir === null) { |
|
94
|
|
|
$this->_webDir = $this->takeGoal('start')->buildRootPath('web'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
return $this->_webDir; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
public function setLogDir($value) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$this->_logDir = Yii::getAlias($value); |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
public function getLogDir() |
|
106
|
|
|
{ |
|
107
|
1 |
|
if ($this->_logDir === null) { |
|
108
|
1 |
|
$this->_logDir = $this->nginx->getLogDir(); |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
return $this->_logDir; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
public function setFpmSocket($value) |
|
115
|
|
|
{ |
|
116
|
1 |
|
$this->_fpmSocket = $value; |
|
117
|
1 |
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
public function getFpmSocket() |
|
120
|
|
|
{ |
|
121
|
1 |
|
if ($this->_fpmSocket === null) { |
|
122
|
1 |
|
$this->_fpmSocket = $this->nginx->getFpmSocket(); |
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
return $this->_fpmSocket; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
public function setSslDir($value) |
|
129
|
|
|
{ |
|
130
|
1 |
|
$this->_sslDir = Yii::getAlias($value); |
|
131
|
1 |
|
if ($this->_sslDir[0] !== '/') { |
|
132
|
|
|
$this->_sslDir = $this->takeGoal('start')->buildRootPath($this->_sslDir); |
|
133
|
|
|
} |
|
134
|
1 |
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
public function getSslDir() |
|
137
|
|
|
{ |
|
138
|
1 |
|
if ($this->_sslDir === null) { |
|
139
|
|
|
$this->_sslDir = $this->takeGoal('start')->buildRootPath('ssl'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
return $this->_sslDir; |
|
143
|
|
|
} |
|
144
|
|
|
public function setAliases($aliases) |
|
145
|
|
|
{ |
|
146
|
|
|
if (!is_array($aliases)) { |
|
147
|
|
|
$aliases = preg_split('/[\s,]+/', trim($aliases)); |
|
148
|
|
|
} |
|
149
|
|
|
$this->_aliases = $aliases; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
public function getAliases() |
|
153
|
|
|
{ |
|
154
|
1 |
|
return $this->_aliases; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
public function getDomains() |
|
158
|
|
|
{ |
|
159
|
1 |
|
$domains = $this->getAliases(); |
|
160
|
1 |
|
array_unshift($domains, $this->getDomain()); |
|
161
|
|
|
|
|
162
|
1 |
|
return $domains; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
1 |
|
public function getServerName() |
|
166
|
|
|
{ |
|
167
|
1 |
|
return implode(' ', $this->getDomains()); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|