| Current Path : /pkg/zweb/1.0/bin/ |
| Current File : //pkg/zweb/1.0/bin/zweb-create |
#!/bin/bash
# TODO: quotas, restart nginx, test PHP logging, add custom rewrite rules
#
# configure storage quota
# configure let's encrypt
# move nginx config within user filesystem tree
# send email (end goal: link to initiate password & enable)
if ! echo $PATH | grep -q '/cmd'; then
PATH="/cmd:$PATH"
export PATH
fi
vhost=$1 # if with www, include without
user=$2 # add $user.zootzone.net to nginx vhost server_name
php_memory=$3
http_post=$4
#app=$5 # reserved for app installations, like WordPress, Drupal
usage() {
echo "usage: $0 <host> <user> <memory>"
exit 1
}
if (( $# != 3 )); then
usage
fi
errors=0
# global settings
cd /etc/zweb
panel_domain=$(head -1 panel_domain)
web_root=$(head -1 web_root)
http_user=$(head -1 http_user)
host_ip=$(head -1 host_ip)
if [ "$php_memory" == '' ]; then
php_memory=$(head -1 php_memory)
fi
if [ "$http_post" == '' ]; then
http_post=$(head -1 http_post)
fi
vhost_default="$vhost"
if echo "$vhost" | grep -q ^www; then
vhost=$(echo $vhost | sed 's/^www\.//')
vhost_nginx="$vhost www.$vhost"
echo -e "\n$vhost\n$vhost_nginx\n"
else
vhost_nginx="$vhost"
fi
if [ ! -L /pkg/php/default ]; then
echo "ERROR: no default PHP configured"
exit 1
fi
cd /pkg/php
php_version=$(readlink default)
# TODO: more error checking
if ! cat /etc/group | grep -q ^$user; then
echo "creating group $user"
groupadd $user
else
echo "group $user exists"
fi
if ! cat /etc/passwd | grep -q ^$user; then
echo "creating user $user"
useradd -g $user -s /bin/true -d $web_root/$user $user
else
echo "user $user exists"
fi
if cat /etc/group | grep ^web | grep -q "$user,"; then
echo "user $user already a member of group 'web'"
else
gpasswd -a $user web # to enable SFTP access (see /etc/ssh/sshd_config)
fi
user_passwd=$(zhost-password-random)
echo "Password is $user_passwd" # TODO: email/SMS
echo "$user:$user_passwd" | chpasswd
if [ -d $web_root/$user ]; then
echo "ERROR: $web_root/$user exists"
exit 1
fi
mkdir -p /etc/nginx/{hosts,hosts.tls,panel,panel.tls,snippet}
mkdir -p $web_root/$user
cd $web_root/$user
mkdir -p etc/{nginx,notify,panel,php,tls}
mkdir -p home/default/{log,private,public}
mkdir -p log/{panel,php}
mkdir -p tmp/panel/{sessions,tmp,uploads}
mkdir -p tmp/php/{sessions,tmp,uploads}
mkdir -p svc/panel/log
mkdir -p svc/php/log
mkdir sys var
chown -R root:$user etc var
chmod 750 etc var
chown -R $user:$user home log tmp
chown $user:$http_user home log sys tmp
chmod 750 home log sys tmp
for i in panel php; do
chown log:log svc/$i/log
done
chmod 750 home log tmp etc # TODO: add to cron job to check
chmod 755 svc # TODO: lock this down further
for p in php panel; do
echo "Setting up service $p"
cd $web_root/$user
if [ "$p" == 'panel' ]; then
php_memory='256'
http_post='100'
fi
cat << EOF > svc/$p/log/run
#!/bin/ash
exec 2>&1
user=\$(pwd | cut -d '/' -f4)
echo "user = \$user"
log_dir="$web_root/$user/log/svc/$p"
if [ ! -d $log_dir ]; then
mkdir -p $log_dir
fi
chown log:log $log_dir
chmod 755 $log_dir
conf="$web_root/\$user/etc/$p"
echo "conf = \$conf"
php_ver=\$(head -1 \$conf/version.value)
php_log_dir="/srv/web/\$user/log/$p"
if [ ! -d \$php_log_dir ]; then
mkdir -p \$php_log_dir
fi
touch \$php_log_dir/php.log
chown \$user:\$user \$php_log_dir/php.log
exec /pkg/php/\${php_ver}/sbin/php-fpm -n -y \$conf/php.conf -c \$conf/php.ini
EOF
cat << EOF > svc/$p/log/run
#!/bin/ash
exec chpst -u log svlogd -tt $web_root/$user/log/svc/$p
EOF
chmod 755 svc/$p/{run,log/run}
echo $php_version > etc/$p/version.value
cat << EOF > etc/$p/php.conf
[global]
daemonize = no
error_log = $web_root/$user/log/$p/php.log
[$user]
user = $user
group = $user
listen = $web_root/$user/svc/$p/sock
listen.owner = $user
listen.group = $http_user
pm = dynamic
pm.max_children = 4
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 2
pm.max_requests = 200
catch_workers_output = yes
php_admin_flag[expose_php] = off
php_admin_flag[log_errors] = on
php_admin_flag[display_errors] = off
php_admin_flag[display_startup_errors] = off
php_admin_value[memory_limit] = ${php_memory}M
php_admin_value[upload_max_filesize] = ${http_post}M
php_admin_value[post_max_size] = ${http_post}M
php_admin_value[sendmail_path] = /cmd/sendmail -t -i -f noreply
php_admin_value[error_log] = $web_root/$user/log/$p/php.log
php_admin_value[sys_temp_dir] = $web_root/$user/tmp/$p
php_admin_value[upload_tmp_dir] = $web_root/$user/tmp/$p/uploads
php_admin_value[session.save_path] = $web_root/$user/tmp/$p/sessions
php_value[date.timezone] = "Africa/Johannesburg"
EOF
cat << EOF > etc/$p/php.ini
include_path=".:/pkg/php/$php_version/lib/php"
extension="gettext.so"
extension="gmp.so"
EOF
# cd /run/sv
#
# if [ ! -d "$user-$p" ]; then
# ln -s $web_root/$user/svc/$p "$user-$p"
# sleep 6
# else
# echo "NOTICE: service /svc/$user-$p already running"
# fi
# svstat "/svc/$user-$p"
# if [ -f /etc/nginx/hosts/$user ]; then
# echo "ERROR: /etc/nginx/hosts/$user exists"
# exit 1
# fi
done
#location / {
#return 301 https://$vhost\$request_uri;
#}
cat << EOF > /etc/nginx/hosts/$user
server {
listen $host_ip:80;
server_name $vhost_nginx;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_comp_level 6;
gzip_types application/atom+xml application/font-otf application/font-ttf application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font application/x-font-opentype application/x-font-otf application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml font/opentype font/otf font/ttf image/svg+xml image/x-icon text/css text/html text/javascript text/plain text/xml;
root $web_root/$user/home/default/public;
access_log $web_root/$user/home/default/log/access.log combined;
error_log $web_root/$user/home/default/log/error.log error;
client_max_body_size ${http_post}m;
index index.php index.html;
include snippet/acme_challenge;
location = /xmlrpc.php {
return 444;
}
location / {
try_files \$uri \$uri/ @controller;
}
location @controller {
rewrite ^/(.*)$ /index.php?\$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:$web_root/$user/svc/php/sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_param TMPDIR $web_root/$user/tmp/php;
fastcgi_read_timeout 60;
}
}
EOF
cat << EOF > /etc/nginx/hosts.tls/$user
server {
listen $host_ip:80;
server_name $vhost_nginx;
include snippet/acme_challenge;
location / {
return 301 https://\$host\$request_uri;
}
}
server {
listen $host_ip:443 ssl;
server_name $vhost_nginx;
include snippet/ssl;
ssl_certificate ${web_root}/${user}/etc/tls/${vhost}.crt;
ssl_certificate_key ${web_root}/${user}/etc/tls/${vhost}.key;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_comp_level 6;
gzip_types application/atom+xml application/font-otf application/font-ttf application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font application/x-font-opentype application/x-font-otf application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml font/opentype font/otf font/ttf image/svg+xml image/x-icon text/css text/html text/javascript text/plain text/xml;
root $web_root/$user/home/default/public;
access_log $web_root/$user/home/default/log/access.log combined;
error_log $web_root/$user/home/default/log/error.log error;
client_max_body_size ${http_post}m;
index index.php index.html;
include snippet/acme_challenge;
location = /xmlrpc.php {
return 444;
}
location / {
try_files \$uri \$uri/ @controller;
}
location @controller {
rewrite ^/(.*)$ /index.php?\$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:$web_root/$user/svc/php/sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_param TMPDIR $web_root/$user/tmp/php;
fastcgi_read_timeout 60;
}
}
EOF
if [ -f /etc/nginx/auth/$user/panel ]; then
echo "ERROR: /etc/nginx/auth/$user/panel exists"
exit 1
fi
[[ ! -d /etc/nginx/auth/$user ]] && mkdir -p /etc/nginx/auth/$user
zweb-htpasswd $user $user_passwd > /etc/nginx/auth/$user/panel
[[ ! -d /etc/nginx/panel ]] && mkdir -p /etc/nginx/panel
# TODO: generate and test new letsencrypt cert
cat << EOF > /etc/nginx/panel/$user
server {
listen $host_ip:80;
server_name $user.$panel_domain;
access_log /srv/web/$user/log/panel/access.log combined;
error_log /srv/web/$user/log/panel/error.log error;
root /srv/web/share/hold;
index index.html;
error_page 401 =401 /error/401.html;
error_page 404 =404 /error/404.html;
location ^~ /error/ {
internal;
}
include snippet/acme_challenge;
}
EOF
cat << EOF > /etc/nginx/panel.tls/$user
server {
listen $host_ip:80;
server_name $user.$panel_domain;
include snippet/acme_challenge;
location / {
return 301 https://$user.$panel_domain\$request_uri;
}
}
server {
listen $host_ip:443 ssl;
server_name $user.$panel_domain;
include snippet/ssl;
ssl_certificate ${web_root}/${user}/etc/tls/${user}.${panel_domain}.crt;
ssl_certificate_key ${web_root}/${user}/etc/tls/${user}.${panel_domain}.key;
access_log /srv/web/$user/log/panel/access.log combined;
error_log /srv/web/$user/log/panel/error.log error;
client_max_body_size 100m;
root /srv/web/panel;
auth_basic_user_file /etc/nginx/auth/$user/panel;
index index.php index.html;
error_page 401 =401 /error/401.html;
error_page 404 =404 /error/404.html;
location ^~ /error/ {
auth_basic off;
internal;
}
include snippet/acme_challenge;
location / {
auth_basic "Panel";
}
location /files/home {
auth_basic "Panel";
alias /srv/web/$user/home;
}
location /files/log {
auth_basic "Panel";
alias /srv/web/$user/log;
}
location /files/sys {
auth_basic "Panel";
alias /srv/web/$user/sys;
}
location ~* \.php {
auth_basic "Panel";
fastcgi_pass unix:/srv/web/$user/svc/panel/sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_param TMPDIR /srv/web/$user/tmp/panel;
fastcgi_param HOME_ROOT /srv/web/$user/home;
fastcgi_param LOG_ROOT /srv/web/$user/log;
fastcgi_param SYS_ROOT /srv/web/$user/sys;
fastcgi_read_timeout 120s;
fastcgi_send_timeout 120s;
}
}
EOF
cat << EOF > $web_root/$user/home/default/public/index.html
<!DOCTYPE html>
<html>
<head>
<title>$vhost</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Varela+Round&display=swap" rel="stylesheet">
<style>
*{
margin: 0;
padding: 0;
font-family: "Varela Round", Verdana, Tahoma, arial, sans-serif;
box-sizing: border-box;
}
body {
font-size: 100%;
margin: 0;
background-color: #eee;
}
.header {
position: fixed;
display: block;
background-color: #1974bb;
height: 80px;
width: 100%;
text-align: center;
font-size: 2em;
font-weight: bold;
color: white;
padding-top: 20px;
padding-bottom: 10px;
}
a:link, a:visited {
color: #1974bb;
outline: 0;
text-decoration: none;
}
p {
padding-bottom: 40px;
}
.content {
color: #333;
text-align: center;
margin: auto;
width: 90%;
padding-top: 120px;
font-weight: bold;
}
.notice {
font-size: 1.5em;
}
.footer {
font-size: 1em;
}
</style>
</head>
<body>
<div class="header">
$vhost
</div>
<div class="content">
<p class="notice">
future web presence
</p>
<p class="footer">
hosted by<br/>
<a href="https://zootzone.com/support.pl">zootzone.com</a>
</p>
</div>
</body>
</html>
EOF
chown $user:$user $web_root/$user/home/default/public/index.html
cat << EOF > $web_root/$user/home/README.txt
Introduction
------------
Support: help@zootzone.com
This service uses the nginx web server,
so Apache .htaccess files will not work.
Request support to have equivalent
conditions configured.
Each account has its own pool of PHP
processes with strict resource allocation.
Request support for configuration changes.
The default host's document root is
./home/default/public
Take note of the locations of the server's
access and error logs, as well as the PHP
error log, below.
Storage quota is updated every 5 minutes to
the ./sys/storage file.
Additional hosts can be served from a single
web hosting account. Example ./home/host2/public
.
|-- etc
| \`-- php
| |-- php.conf
| |-- php.ini
| \`-- version
|-- home
| \`-- default
| |-- log
| | |-- access.log
| | \`-- error.log
| |-- private
| \`-- public
| \`-- index.html
|-- log
| \`-- php
| \`-- php.log
|-- sys
| \`-- storage
\`-- tmp
\`-- php
|-- sessions
\`-- uploads
EOF