|
#! /bin/bash |
|
|
|
source flash.sh |
|
|
|
serverUser="user" |
|
defaultIP="127.0.0.1" |
|
confVirtualHost=$(cat <<EOF |
|
<VirtualHost {ip}:80> |
|
ServerAdmin [email protected] |
|
ServerName {domain} |
|
ServerAlias www.{domain} |
|
|
|
DocumentRoot {documentRoot} |
|
|
|
<Directory "{documentRoot}"> |
|
Options -Indexes FollowSymLinks MultiViews |
|
AllowOverride All |
|
Order allow,deny |
|
Allow from all |
|
</Directory> |
|
|
|
ErrorLog /home/{serverUser}/logs/{domain_slug}-error_log |
|
CustomLog /home/{serverUser}/logs/{domain_slug}-access_log combined |
|
</VirtualHost> |
|
|
|
EOF |
|
) |
|
confVirtualHostSSL=$(cat <<EOF |
|
<VirtualHost {ip}:443> |
|
ServerAdmin [email protected] |
|
ServerName {domain} |
|
ServerAlias www.{domain} |
|
|
|
DocumentRoot {documentRoot} |
|
|
|
<Directory "{documentRoot}"> |
|
Options -Indexes FollowSymLinks MultiViews |
|
AllowOverride All |
|
Order allow,deny |
|
Allow from all |
|
</Directory> |
|
|
|
ErrorLog {directoryLogs}/ssl_{domain_slug}-error_log |
|
CustomLog {directoryLogs}/ssl_{domain_slug}-access_log combined |
|
|
|
SSLEngine on |
|
SSLCertificateKeyFile {SSLCertificateKeyFile} |
|
SSLCertificateFile {SSLCertificateFile} |
|
</VirtualHost> |
|
|
|
EOF |
|
) |
|
directoryVirtualHost="/etc/httpd/vhost/" |
|
|
|
|
|
# Server Username |
|
# ------------------------------------------------------------------------------ |
|
echo -n "Server user (${serverUser}):" |
|
read newServerUser |
|
if [ ! -z "$newServerUser" ]; then |
|
serverUser=$newServerUser |
|
fi |
|
|
|
documentRoot="/home/${serverUser}/public_html/" |
|
if [ ! -d $documentRoot ]; then |
|
flashError "The ${documentRoot} directory does not exist." |
|
exit |
|
fi |
|
|
|
directoryLogs="/home/${serverUser}/logs" |
|
if [ ! -d $directoryLogs ]; then |
|
sudo -u $serverUser mkdir ${directoryLogs} |
|
fi |
|
|
|
|
|
|
|
# Server IP |
|
# ------------------------------------------------------------------------------ |
|
echo -n "IP (${defaultIP}):" |
|
read newDomainIP |
|
if [ ! -z "$newDomainIP" ]; then |
|
defaultIP=$newDomainIP |
|
fi |
|
|
|
|
|
# Domain |
|
# ------------------------------------------------------------------------------ |
|
echo -n "Domain: " |
|
read newDomain |
|
if [ -z "$newDomain" ]; then |
|
flashError "Domain invalid." |
|
exit |
|
fi |
|
domain_slug=$(sed -e "s/\./_/g" <<< $newDomain) |
|
|
|
filenameVirtualHost=$newDomain'.conf' |
|
pathVirtualHost=$directoryVirtualHost$filenameVirtualHost |
|
if [ -s $pathVirtualHost ]; then |
|
flashError "The VirtualHost has already been created." |
|
exit |
|
fi |
|
|
|
documentRoot=$documentRoot$newDomain |
|
if [ -d $documentRoot ]; then |
|
flashError "The Directory ${documentRoot} has already been created." |
|
exit |
|
fi |
|
|
|
|
|
# Use SSL |
|
# ------------------------------------------------------------------------------ |
|
echo -n "SSL [y|n] (n):" |
|
read newDomainSSL |
|
if [ -z "$newDomainSSL" ]; then |
|
newDomainSSL="n" |
|
elif [ "y" != "$newDomainSSL" ] && [ "n" != "$newDomainSSL" ]; then |
|
flashError "Argument invalid." |
|
exit |
|
fi |
|
|
|
if [ "y" == $newDomainSSL ]; then |
|
SSLCertificateKeyFile="/home/${serverUser}/ssl/keys/${domain_slug}.key" |
|
if [ ! -s $SSLCertificateKeyFile ]; then |
|
flashError "Error: file does not exist." |
|
flashError "SSLCertificateKeyFile: ${SSLCertificateKeyFile}." |
|
exit |
|
fi |
|
|
|
SSLCertificateFile="/home/${serverUser}/ssl/certs/${domain_slug}.crt" |
|
if [ ! -s $SSLCertificateFile ]; then |
|
flashError "Error: file does not exist." |
|
flashError "SSLCertificateFile: ${SSLCertificateFile}." |
|
exit |
|
fi |
|
fi |
|
|
|
|
|
function addVirtualHost() { |
|
echo -n "$confVirtualHost" > $pathVirtualHost |
|
|
|
if [ "y" == $newDomainSSL ]; then |
|
echo -n "$confVirtualHostSSL" >> $pathVirtualHost |
|
fi |
|
|
|
sed -i "s/{ip}/$defaultIP/g" $pathVirtualHost |
|
sed -i "s/{domain}/$newDomain/g" $pathVirtualHost |
|
sed -i "s/{domain_slug}/${domain_slug}/g" $pathVirtualHost |
|
sed -i "s|{documentRoot}|$documentRoot|g" $pathVirtualHost |
|
sed -i "s|{serverUser}|$serverUser|g" $pathVirtualHost |
|
sed -i "s|{directoryLogs}|$directoryLogs|g" $pathVirtualHost |
|
sed -i "s|{SSLCertificateKeyFile}|${SSLCertificateKeyFile}|g" $pathVirtualHost |
|
sed -i "s|{SSLCertificateFile}|${SSLCertificateFile}|g" $pathVirtualHost |
|
|
|
flashSuccess "VirtualHost created ${filenameVirtualHost}." |
|
|
|
generate_directory |
|
|
|
service httpd restart |
|
} |
|
|
|
function generate_directory() { |
|
sudo -u $serverUser mkdir $documentRoot |
|
sudo -u $serverUser touch $documentRoot/index.php |
|
echo '<h1>It works!</h1>' > $documentRoot/index.php |
|
|
|
flashSuccess "Directory created ${documentRoot}." |
|
} |
|
|
|
addVirtualHost |