-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvhost
More file actions
128 lines (112 loc) · 3.27 KB
/
vhost
File metadata and controls
128 lines (112 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env php
<?php
$virtual_hosts_dir = '/etc/apache2/sites-available/';
if (!is_dir($virtual_hosts_dir) || !is_writable($virtual_hosts_dir)) {
print "You must run this script as root!\n";
exit;
}
$default_doc_root = '/var/www';
$server_alias = '';
$add_to_hosts = NULL;
$document_root = '';
if ($argc > 1) {
for ($i = 1; $i < $argc; $i++) {
$option = explode('=', $argv[$i]);
switch ($option[0]) {
case '--help':
print '-h / --add-to-hosts: Add to /etc/hosts file;' . PHP_EOL;
print '-n / --no-add-to-hosts: Not add to /etc/hosts file;' . PHP_EOL;
print '-a / --server-alias: Hostname;' . PHP_EOL;
print '-d / --document-root: Document root;' . PHP_EOL;
print '--help: Show this help.' . PHP_EOL;
exit;
case '-h':
case '--add-to-hosts':
$add_to_hosts = TRUE;
break;
case '-n':
case '--no-add-to-hosts':
$add_to_hosts = FALSE;
break;
case '-a':
case '--server-alias':
if (isset($option[1])) {
$server_alias = $option[1];
}
else {
echo "Wrong option: {$argv[$i]}\n";
}
break;
case '-d':
case '--document-root':
if (isset($option[1])) {
if ($option[1] == 'default') {
$document_root = $default_doc_root;
}
elseif (is_dir(dirname($option[1]))) {
$document_root = $option[1];
}
}
else {
echo "Wrong option: {$argv[$i]}\n";
}
break;
default:
if (substr($argv[$i], 1, 1) == '-') {
echo "Unknown option: {$argv[$i]}\n";
}
break;
}
}
}
while (!$server_alias) {
echo 'Enter your hostname: ';
$server_alias = trim(fgets(STDIN));
}
if ($add_to_hosts === NULL) {
print "Add $server_alias to your /etc/hosts ? (Y/N) [Y]: ";
$line = trim(fgets(STDIN));
if ($line == 'n' || $line == 'N') {
$add_to_hosts = FALSE;
}
else {
$add_to_hosts = TRUE;
}
}
if (!$document_root) {
$first_elem = explode('.', $server_alias);
$project_folder = array_shift($first_elem);
$default_doc_root = $default_doc_root . '/' . $project_folder;
print "Enter your document root [$default_doc_root]: ";
$line = trim(fgets(STDIN));
if ($line && is_dir(dirname($line))) {
$document_root = $line;
}
else {
$document_root = $default_doc_root;
}
}
if (!is_dir($document_root)) {
mkdir($document_root);
}
if ($add_to_hosts) {
$hosts = file_get_contents('/etc/hosts');
$hosts .= "127.0.0.1\t$server_alias\n";
file_put_contents('/etc/hosts', $hosts);
}
$host_template = <<<HOST
<VirtualHost *:80>
ServerAdmin admin@localhost
ServerName $server_alias
ServerAlias www.$server_alias
DocumentRoot $document_root
<Directory $document_root>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
LogLevel warn
</VirtualHost>
HOST;
file_put_contents("/etc/apache2/sites-available/$server_alias.conf", $host_template);
system("a2ensite $server_alias.conf && service apache2 restart");