nginx+
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
a2enmod ssl
|
||||
a2enmod rewrite
|
||||
systemctl restart apache2
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
777$7II7III??+,.......:+~=7$$Z$?$OOZOZ77?$$I+??I$ZOD8DD8DD888O$7$77$Z$$$ZZZZZOOO
|
||||
$$$777$$77II??~.,..,.=?777$ZZ==$OZZ$I?=+?Z+==~=7Z7Z$OO8888DDDD8ZOZ$$$7Z$ZOZ$ZZOO
|
||||
$$$7ZZ$Z$$77II=~:.,.:??+IZZZ+?O$ZI+?+:~+7I~~,:~?Z$88O88D88DDDDD8OZOZ$$$ZZZZZOOZZ
|
||||
$$$$ZZ$$$$777I??:::,:7ZZ8OZ$$$7?=::,:~??~:,.....7$8$IZDDD8DDDNDDOZZO$$ZZZZZOZOOO
|
||||
$$$77$7$7III$II+==+$$$OOO8OZ?=:......~. ........,?ZOI$88DOD8DDN8D888$7$ZZ$$Z$$ZO
|
||||
$$7$$$77I77$ZZ$7,:Z$OZOOOOOI. ...,~:.....:~~+:===$Z$7OOD8DDD88O8D888O$7$Z$$$$OZ
|
||||
$$77$$77I$$ZOZI,.7Z88O88O8?. .,:~~::,,.,,,+?$ZZZZZ8OZZ$8$8NDDDD88ODO8ZZ$$$OZZZOO
|
||||
Z$Z$$$77$Z$$O$~.~?$ZOOOOOZ, ,?7$7II+:..,~+$III777I77I$O8OD8DD8D8DO8D88$77$$$ZZZZ
|
||||
OOOOZc$ZOZOZZ?..+??IZO8NO$I?===+=~~~,...,~II???+~:+~:+ZO8D88DOODDDDD8D88OZ77$ZZZ
|
||||
ZOZOOZZZZZOZ$II.~?I$ZZO8ZO~. ..~?+~~.. .,?$I$ZO888O?~$Z88D888O8888DONDD88Z7777$
|
||||
OZZ8O8OZZOZZZZ7I7I$ZZ7$OZ7:.=7ZDO7?~.. ,::+O7$O8O78O7IOD88N8D8DD88DD8OOOO7$$7Z
|
||||
OOOZ88888OOOOZZI~?I$Z$OZI+?77,?$I?:.. .,:,,~,,~+++~+I$ZZ8DDD8DNDD8DDOOOZ7?$$7$
|
||||
OOOZO88OO8OZ7=:,.?7IZZOZI,~=~. ... . ..,,.. ...=:..~I$Z8DDDD8D8DDOD88a7II777$
|
||||
$OOOOOOO8O+.,.:++:++$ZOO7 .,. .,,,.... ..:~.:$Z8DD8DDD88DDD8O$?????I7
|
||||
OO8O8O8OZ$.,:?=..::?IZZ8Z .. ..,.,=,.. ..:I??7O8DD88D888D8OOI?II77II
|
||||
OOOO8OZZ7=,~7?~,,,~~?$7OZ,. .,:~=:,.,:, ..:=+:~788DDD88Z888OZ$+I?III$7
|
||||
ZOZZO$$$ZZZZ$I:~:~,,7=77$~:.. .:$$==IOO7=,.,....,:,:=+$D8DD8888D887II?7777$$
|
||||
ZOOZZZ$$ZZZZ7+::=:::::II~.:.. ..=+?+IIII:...,,..,,,:==$DDD88888DD8$ZZ$$ZOOOO
|
||||
7ZO$n7I$$$Z$7I~,,:,,.,=?:,,.. . ,:::~~I?:,......,,:,:=ZO888D88D8DO88O8888888
|
||||
77777$77$$7I?~,.. .,,,=+~.. ~+=:~+:+II+=~:,:..,,::~?ZODO888D88DD8DDDD8D88N
|
||||
?77???I?II?~:,.....=.:?+~. ., .:?7I:::~::=+~~...,:~=?$OOOZ888OO8OO8888D8D8D
|
||||
7II??I+++~:,.. .,==$7+,. .IZ7~~~~,~:++=:..::~++7ZZODDD8D888a8OOD88D88
|
||||
I???+~:,,,... .=:=~I$$+, .$7?+++++=+==+~,,:==+?78DDDDDDDO88OZOO88O888
|
||||
??+::,,... ... .78Z7IZO=...... ,77:..:::.,~?+~::=++~?$8DDDN8D8O88O88OOOOO88
|
||||
~=~:,,,.,.... .:OO$Z$7... ?$~ .......,:=~,:++==?$88D88D8ZOZOOZOOOO8DOO
|
||||
:~=+:,,.. . ..,?$$+.. =...::,... ,~::~?===+OD88888OZOOOZO$ZOOZZZ8
|
||||
,~++++==.. ....~ZZO~. .:~=~,,...:=~~$7?+?$DD8D8OOZZOZOZOOOO8O8O8
|
||||
.:=+==++,. ...,.:+8DZ...I$7:.....,~=:,....+~?I7=?=$O8DDOOZ88OOO8OOOOOZZO88
|
||||
,:~===?d, .:,:?OD8O...ZOO$$?I,...=~:,,,,~===?+?+77Z8OZ$Z$OOZZOOO8OOOOO8O8
|
||||
,:~=~~=+.. ..=Z8888$.+OZ8OOZ8O7,.::=:~~=~:+????I$$?7$$ZZZOOOOOOOOO8O8OOOOO
|
||||
,,:~~~+=.. ~ZO87I7Z,:7OOZOO?OD$~,:~:+?+==+?+=+77$?IOOZZZOOOOO8OOOOOOOOO888
|
||||
..,,~=+~.. . =$OO:..+?.IZOOZOZ$OZ?=~=~=++=+:?III7$7I=7DD8ZZOO8OO88OO8ZOZ$ZOOO
|
||||
..... .... . ,~?7OZ7..:~I+ZZOO$OOOZZ$+7++?+??I777$$Z7I?~Z8D8OO$Z888O8888ZOZOZOO8
|
||||
,.....,~~==:=?$ZOZ7 .,~7IZZOZZZZOOOZO$I$Z$$Z$Z$ZZ7$I+==~DDDN8DDO$O88D8DDZO8OZ888
|
||||
=+=:~::=~~:+$ZZZ$+...I77$OZ$$IIZ$?$Z7=======~=++??==,::=D8D88D8D8ZOODOOOZZ8ZOaOO
|
||||
Executable
+224
@@ -0,0 +1,224 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys, os, shutil, platform, subprocess, re, argparse
|
||||
|
||||
# only support linux / Debian, Ubuntu / nginx, apache2
|
||||
platform_system_support = ['Linux']
|
||||
platform_version_support = ['DEBIAN', 'UBUNTU']
|
||||
server_support = ['NGINX', "APACHE2"]
|
||||
|
||||
server_kind = None
|
||||
|
||||
html_dir_path = ""
|
||||
logs_dir_path = ""
|
||||
|
||||
re_domain = r'^(?=.{1,253}$)(?!.*\.\..*)(?!\..*)([a-zA-Z0-9-]{,63}\.){,127}[a-zA-Z0-9-]{1,63}$'
|
||||
|
||||
def y_n_question(question_str: str) -> bool:
|
||||
|
||||
yes = {'yes','y', 'ye', ''}
|
||||
no = {'no','n'}
|
||||
|
||||
while True:
|
||||
sys.stdout.write(question_str + " [Y/n]: ")
|
||||
choice = input().lower()
|
||||
if choice in yes:
|
||||
return True
|
||||
elif choice in no:
|
||||
return False
|
||||
else:
|
||||
sys.stdout.write("\nPlease respond with 'yes' or 'no'\n")
|
||||
continue
|
||||
|
||||
def check_server_version() -> str:
|
||||
|
||||
a = subprocess.call("sudo apache2 -v", shell=True)
|
||||
if not a:
|
||||
return "APACHE2"
|
||||
|
||||
n = subprocess.call("sudo nginx -v", shell=True)
|
||||
if not n:
|
||||
return "NGINX"
|
||||
|
||||
return None
|
||||
|
||||
def check_distribution_version() -> str:
|
||||
|
||||
dist = platform.version().lower()
|
||||
|
||||
if 'debian' in dist:
|
||||
return "DEBIAN"
|
||||
|
||||
if 'ubuntu' in dist:
|
||||
return "UBUNTU"
|
||||
|
||||
return None
|
||||
|
||||
def sanity_check_system():
|
||||
|
||||
# check platform
|
||||
if platform.system() not in platform_system_support:
|
||||
sys.exit(f"Platform {platform.system()} not supported. Aborting...")
|
||||
|
||||
dist = check_distribution_version()
|
||||
if not dist:
|
||||
sys.exit("OS distribution not supported. Aborting...")
|
||||
|
||||
sv = check_server_version()
|
||||
if not sv:
|
||||
sys.exit("Server distribution not supported. Aborting...")
|
||||
|
||||
if dist in platform_version_support and sv == "APACHE2":
|
||||
# check apache2 (ubuntu or debian)
|
||||
u = os.path.exists('/etc/apache2/sites-available/')
|
||||
if not u:
|
||||
sys.exit(f"Apache2 ({dist}) not installed on your system. Aborting...")
|
||||
|
||||
if dist in platform_version_support and sv in server_support:
|
||||
# check apache2 (ubuntu or debian)
|
||||
u = os.path.exists(f'/etc/{sv.lower()}/sites-available/')
|
||||
if not u:
|
||||
sys.exit(f"{sv} ({dist}) not installed on your system. Aborting...")
|
||||
|
||||
return sv, dist
|
||||
|
||||
|
||||
def sanity_check_platform():
|
||||
|
||||
global html_dir_path, logs_dir_path
|
||||
|
||||
usr = os.getlogin()
|
||||
|
||||
html_dir_path = os.path.join('/home', usr, 'html')
|
||||
logs_dir_path = os.path.join('/home', usr, 'logs')
|
||||
|
||||
if not os.path.exists(html_dir_path):
|
||||
if y_n_question("Path - " + html_dir_path + ' - does not exists. Create it?'):
|
||||
os.makedirs(html_dir_path)
|
||||
else:
|
||||
sys.exit("Can not configure platform. Aborting...")
|
||||
|
||||
if not os.path.exists(logs_dir_path):
|
||||
if y_n_question("Path - " + logs_dir_path + ' - does not exists. Create it?'):
|
||||
os.makedirs(logs_dir_path)
|
||||
else:
|
||||
sys.exit("Can not configure platform. Aborting...")
|
||||
|
||||
def vhost_add(domain: str, sv: str, dist: str):
|
||||
|
||||
global html_dir_path, logs_dir_path, re_domain
|
||||
|
||||
print(" adding vhost domain - " + domain)
|
||||
|
||||
if re.match(re_domain, domain) is None:
|
||||
print("Invalid domain name: " + domain + " -> pass")
|
||||
|
||||
www = os.path.join(html_dir_path, domain)
|
||||
os.makedirs(www, exist_ok=True)
|
||||
|
||||
logs = os.path.join(logs_dir_path, domain)
|
||||
os.makedirs(logs, exist_ok=True)
|
||||
|
||||
# debug: this file might not be here...........
|
||||
if sv == "APACHE2":
|
||||
with open('vhost_tmpl_apache2') as vhost_tmpl_fp:
|
||||
vhost_tmpl = vhost_tmpl_fp.read()
|
||||
|
||||
elif sv == "NGINX":
|
||||
with open('vhost_tmpl_nginx') as vhost_tmpl_fp:
|
||||
vhost_tmpl = vhost_tmpl_fp.read()
|
||||
|
||||
else:
|
||||
sys.exit(f"{sv} not recognised. Aborting...")
|
||||
|
||||
usr = os.getlogin()
|
||||
|
||||
vhost = vhost_tmpl.replace("%domain?", domain).replace("%user?", usr)
|
||||
|
||||
# debug: write file directly to '/etc/apache2/sites-available/' ?
|
||||
vhost_file = os.path.join(www, domain + '.conf')
|
||||
|
||||
with open(vhost_file, 'w+') as vhost_file_fp:
|
||||
vhost_file_fp.write(vhost)
|
||||
|
||||
# debug: this file might not be here...........
|
||||
shutil.copyfile('c.txt', os.path.join(www, 'c.txt'))
|
||||
|
||||
|
||||
# check is ssl cert exists
|
||||
cert = os.path.join('/etc/letsencrypt/live', domain)
|
||||
if not os.path.exists(cert):
|
||||
print(" warning: SSL certificates do not exist for domain - " + domain + " - this will problaly cause errors...")
|
||||
print(" warning: Please make sure to place them in " + cert + " to allow secure https connection to your site.")
|
||||
|
||||
# mv conf file to apache?
|
||||
if dist in platform_version_support:
|
||||
if y_n_question(f"Move {vhost_file} to /etc/{sv.lower()}/sites-available/ ?"):
|
||||
vhost_conf_file = os.path.join(f'/etc/{sv.lower()}/sites-available/', f'{domain}.conf')
|
||||
subprocess.call(['sudo', 'mv', vhost_file, vhost_conf_file])
|
||||
|
||||
# enable site?
|
||||
if y_n_question(f"Enable {domain}?"):
|
||||
if sv == "APACHE2":
|
||||
subprocess.call(['sudo', 'a2ensite', f'{domain}.conf'])
|
||||
|
||||
if sv == "NGINX":
|
||||
vhost_conf_file_enabled = os.path.join(f'/etc/nginx/sites-enabled/', f'{domain}.conf')
|
||||
subprocess.call(['sudo', 'ln', '-s', vhost_conf_file, vhost_conf_file_enabled])
|
||||
|
||||
|
||||
def vhost_remove(domain: str, sv: str, dist: str):
|
||||
print(" removing domain — " + domain)
|
||||
|
||||
vhost_conf_file = os.path.join(f'/etc/{sv.lower()}/sites-available/{domain}.conf')
|
||||
if os.path.exists(vhost_conf_file):
|
||||
if y_n_question("Delete " + vhost_conf_file + " ?"):
|
||||
subprocess.call(['sudo', 'rm', vhost_conf_file])
|
||||
if sv == "APACHE2":
|
||||
subprocess.call(['sudo', 'a2dissite', vhost_conf_file])
|
||||
if sv == "NGINX":
|
||||
vhost_conf_file_enabled = os.path.join(f'/etc/nginx/sites-enabled/', f'{domain}.conf')
|
||||
subprocess.call(['sudo', 'rm', vhost_conf_file_enabled])
|
||||
|
||||
|
||||
www = os.path.join(html_dir_path, domain)
|
||||
if os.path.exists(www):
|
||||
if y_n_question("Delete " + www + " ?"):
|
||||
subprocess.call(['sudo', 'rm', '-r', www])
|
||||
|
||||
logs = os.path.join(logs_dir_path, domain)
|
||||
if os.path.exists(logs):
|
||||
if y_n_question("Delete " + logs + " ?"):
|
||||
subprocess.call(['sudo', 'rm', '-r', logs])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
p = argparse.ArgumentParser(description='vhost helper')
|
||||
p.add_argument('domain', metavar="domain", help="vhost domain(s)", nargs="+")
|
||||
g = p.add_mutually_exclusive_group()
|
||||
g.add_argument('-a', '--add', action='store_true', help="adds vhost for given domain(s)")
|
||||
g.add_argument('-r', '--remove', action='store_true', help="removes vhost for given domain(s)")
|
||||
|
||||
args = p.parse_args()
|
||||
|
||||
print('1. sanity checks')
|
||||
|
||||
sv, dist = sanity_check_system()
|
||||
sanity_check_platform()
|
||||
|
||||
print('2. vhosting')
|
||||
|
||||
for d in args.domain:
|
||||
if args.add:
|
||||
vhost_add(d, sv, dist)
|
||||
elif args.remove:
|
||||
vhost_remove(d, sv, dist)
|
||||
|
||||
if y_n_question(f"Reload {sv}?"):
|
||||
subprocess.call(['sudo', 'service', sv.lower(), 'reload'])
|
||||
|
||||
print('done.')
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<VirtualHost *:80>
|
||||
ServerAdmin webmaster@localhost
|
||||
ServerName %domain?
|
||||
ServerAlias www.%domain? %domain?
|
||||
DocumentRoot /home/%user?/html/%domain?
|
||||
|
||||
<Directory /home/%user?/html/%domain?>
|
||||
Options Indexes FollowSymLinks
|
||||
AllowOverride all
|
||||
Require all granted
|
||||
</Directory>
|
||||
|
||||
ErrorLog /home/%user?/logs/%domain?/error.log
|
||||
CustomLog /home/%user?/logs/%domain?/access.log combined
|
||||
|
||||
RewriteEngine on
|
||||
RewriteCond %{SERVER_NAME} =%domain?
|
||||
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
|
||||
</VirtualHost>
|
||||
|
||||
<VirtualHost *:443>
|
||||
ServerName %domain?
|
||||
ServerAlias www.%domain? %domain?
|
||||
DocumentRoot /home/%user?/html/%domain?
|
||||
|
||||
<Directory /home/%user?/html/%domain?>
|
||||
Options Indexes FollowSymLinks
|
||||
AllowOverride all
|
||||
Require all granted
|
||||
</Directory>
|
||||
|
||||
ErrorLog /home/%user?/logs/%domain?/error.log
|
||||
CustomLog /home/%user?/logs/%domain?/access.log combined
|
||||
|
||||
SSLEngine on
|
||||
SSLCertificateFile /etc/letsencrypt/live/%domain?/fullchain.pem
|
||||
SSLCertificateKeyFile /etc/letsencrypt/live/%domain?/privkey.pem
|
||||
Include /etc/letsencrypt/options-ssl-apache.conf
|
||||
</VirtualHost>
|
||||
@@ -0,0 +1,25 @@
|
||||
server {
|
||||
listen 80;
|
||||
server_name %domain?;
|
||||
rewrite ^ https://%domain?$request_uri? permanent;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
|
||||
ssl_certificate /etc/letsencrypt/live/%domain?/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/%domain?/privkey.pem;
|
||||
ssl_stapling on;
|
||||
|
||||
server_name %domain?;
|
||||
|
||||
location / {
|
||||
root /home/%user?/html/%domain?;
|
||||
index index.html;
|
||||
}
|
||||
|
||||
|
||||
gzip on;
|
||||
error_log /home/%user?/logs/%domain?/error.log;
|
||||
access_log /home/%user?/logs/%domain?/access.log;
|
||||
}
|
||||
Reference in New Issue
Block a user