-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvirtualmin-nginx-uwsgi-lib.pl
146 lines (121 loc) · 3.73 KB
/
virtualmin-nginx-uwsgi-lib.pl
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use WebminCore;
&init_config();
&foreign_require("virtual-server", "virtual-server-lib.pl");
sub get_uwsgi_config {
my ($d) = @_;
my $lines = &read_file_lines("$config{'uwsgi_apps_available'}/$d->{'dom'}.ini");
my %conf = ();
foreach my $line (@$lines) {
my ($n, $v) = split(/\s+=\s+/, $line, 2);
if ($n) {
$conf{$n} = $v;
}
}
return %conf;
}
sub get_venv_config {
my ($d) = @_;
my %conf = ();
$conf{'python'} = &backquote_command("$d->{'home'}/venv/bin/python --version");
$conf{'modules'} = &backquote_command("$d->{'home'}/venv/bin/pip freeze");
return %conf;
}
sub get_nginx_config {
my ($d) = @_;
my $lines = &read_file_lines("$config{'nginx_sites_available'}/$d->{'dom'}.conf");
return $lines;
}
sub create_nginx_config {
my $d = shift;
my $conf = shift;
open(FILE, ">> $config{'nginx_sites_available'}/$d->{'dom'}.conf") or die ("Unable to create nginx config");
if ($d->{'ssl'}) {
print FILE <<"NGINX";
server {
server_name $d->{'dom'} www.$d->{'dom'};
listen $d->{'ip'};
rewrite ^ https://\$server_name\$request_uri? permanent;
}
server {
server_name $d->{'dom'} www.$d->{'dom'};
listen $d->{'ip'}:443 ssl;
ssl_certificate $d->{'ssl_cert'};
ssl_certificate_key $d->{'ssl_key'};
ssl_protocols TLSv1.1 TLSv1.2;
NGINX
} else {
print FILE <<"NGINX";
server {
server_name $d->{'dom'} www.$d->{'dom'};
listen $d->{'ip'};
NGINX
}
print FILE <<"NGINX";
root $d->{'home'}/public_html;
client_max_body_size $conf->{'client_max_body_size'}M;
location / {
uwsgi_pass unix:///var/run/uwsgi/app/$d->{'dom'}/socket;
include uwsgi_params;
}
NGINX
@static_paths = split(' ', $conf->{'static_paths'});
foreach $static_path(@static_paths) {
print FILE "\n location /$static_path {\n";
print FILE " root $d->{'home'}/public_html;\n";
print FILE " }\n";
}
print FILE <<"NGINX";
}
NGINX
close(FILE);
if($config{'nginx_sites_enabled'}) {
&symlink_file("$config{'nginx_sites_available'}/$d->{'dom'}.conf", "$config{'nginx_sites_enabled'}/$d->{'dom'}.conf");
}
}
sub create_uwsgi_ini {
my $d = shift;
my $conf = shift;
if (!-r "$d->{'home'}/.tmp") {
&make_dir("$d->{'home'}/.tmp", oct(755), 0);
&set_ownership_permissions($d->{'user'}, $d->{'group'}, undef, "$d->{'home'}/.tmp");
}
open(FILE, ">> $config{'uwsgi_apps_available'}/$d->{'dom'}.ini") or die ("Unable to create uwsgi ini for domain");
print FILE <<"uWSGI";
[uwsgi]
chdir = $d->{'home'}/public_html
home = $d->{'home'}/venv
pidfile = /tmp/uwsgi-$d->{'dom'}.pid
uid = $d->{'user'}
gid = $d->{'group'}
module = $conf->{'wsgi_module'}
master = True
vacuum = True
chmod-socket=660
chown-socket = $config{'nginx_user'}:$config{'nginx_user'}
uWSGI
close(FILE);
if($config{'uwsgi_apps_enabled'}) {
&symlink_file("$config{'uwsgi_apps_available'}/$d->{'dom'}.ini", "$config{'uwsgi_apps_enabled'}/$d->{'dom'}.ini");
}
}
sub setup_venv {
my $d = shift;
my $conf = shift;
&unlink_file("$d->{'home'}/venv");
print "<pre>";
print &backquote_command("virtualenv -p $conf->{'venv_python_version'} $d->{'home'}/venv");
print "</pre>";
&$virtual_server::second_print($text{'venv_requirements_install'});
@reqs = split(' ', $conf{'venv_requirements'});
foreach $req(@reqs) {
print "<pre>";
print &backquote_command("$d->{'home'}/venv/bin/pip install $req");
print "</pre>";
}
system("chown $d->{'user'}:$d->{'group'} $d->{'home'}/venv -R");
}
sub reload_services {
system("$config{'nginx_restart'} >/dev/null 2>&1");
# system("$config{'uwsgi_restart'} >/dev/null 2>&1");
}
1;