Skip to content

Instantly share code, notes, and snippets.

@eduardoaugustojulio
Created November 28, 2016 13:38
Show Gist options
  • Save eduardoaugustojulio/f74a2af65de0af7a9b76032ca029c82d to your computer and use it in GitHub Desktop.
Save eduardoaugustojulio/f74a2af65de0af7a9b76032ca029c82d to your computer and use it in GitHub Desktop.
apache virtual host creator
#!/usr/bin/python
# Este programa cria uma virtual host e sua pagina de apresentação
# OBS: OS HOSTS CRIADOS TERAO NOME FINAL .COM.BR
# Utilizacao
# python create_virtual_host.py app_name porta 8080
# ou
# python create_virtual_host.py
import sys
import os.path
import pwd
import grp
import getpass
app_name = ''
port = ''
document_root = ''
index_file = ''
index_file_conteudo = ''
virtual_host_file = ''
virtual_host_conteudo = ''
if len(sys.argv) != 4:
app_name = input(u'Entre com o nome do app: ')
port = input (u'Entre com o numero da porta: ')
elif len(sys.argv) == 4:
app_name = str(sys.argv[1])
port = str(sys.argv[3])
document_root = '/var/www/html/'+str(app_name)+'.com.br/public_html'
index_file = document_root+'/index.htlm'
index_file_conteudo = ('<html>\n'
'\t<head>\n'
'\t\t<title>Welcome to '+str(app_name)+'.com.br!</title>\n'
'\t</head>\n'
'\t<body>'
'\t\t<h1>Success! The '+str(app_name)+'.com.br virtual host is working!</h1>\n'
'\t</body>\n'
'</html>\n')
virtual_host_file = '/etc/apache2/sites-available/'+str(app_name)+'.com.br.conf'
virtual_host_conteudo = ('<VirtualHost *:'+str(port)+'>\n'
'\tServerName '+str(app_name)+'.com.br\n'
'\tServerAlias www.'+str(app_name)+'.com.br\n'
'\tDocumentRoot /var/www/html/'+str(app_name)+'.com.br/public_html\n'
'\tErrorLog ${APACHE_LOG_DIR}/error.log\n'
'\tCustomLog ${APACHE_LOG_DIR}/access.log combined\n'
'</VirtualHost>\n')
if os.path.exists(document_root):
print ('Ja existe esse Document Root em: ', document_root)
else:
exist = 0
os.makedirs(document_root, 755, exist)
print ('criado pasta de recursos em: ',document_root,'\n')
f = open(index_file, 'w')
f.write(index_file_conteudo)
f.close()
print ('criado documento de aprensentacao default do dominio em: ',index_file,'\n')
print ('conteudo do arquivo de apresentacao default: ',index_file_conteudo,'\n')
# troca proprietario da pasta para o usuario atual
uid = pwd.getpwnam(getpass.getuser()).pw_uid
gid = grp.getgrnam(getpass.getuser()).gr_gid
os.chown(document_root, uid, gid)
f = open(virtual_host_file, 'w')
f.write(virtual_host_conteudo)
f.close
print ('criado documento de virtual host em: ',virtual_host_file,'\n')
print ('conteudo do arquivo de virtual host: ',virtual_host_conteudo,'\n')
print ('aplicando dominio no apache: \n')
os.system('a2ensite '+str(app_name)+'.com.br')
print ('restart no apache: \n')
os.system('service apache2 restart')
print ('\n fim do script')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment