Last active
February 27, 2018 10:21
-
-
Save yvh/6749b315422e5f143eac to your computer and use it in GitHub Desktop.
Shell command for create basic vhost for apache 2.4.*
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Author Yannick Vanhaeren | |
# Version 1.0.0 | |
# Functions | |
############ | |
function usage | |
{ | |
echo "usage: create-vhost [-a email] [-p port] [-e env] [-h] -v vhost_name -d document_root" | |
} | |
# Main | |
####### | |
APACHE_SITE_DIR="/etc/apache2/sites-available" | |
server_admin="webmaster@localhost" | |
port=80 | |
listen_port="#Listen" | |
symfony_env="" | |
vhost="" | |
document_root="" | |
while getopts "a:p:e:d:v:h" opt; do | |
case $opt in | |
a) | |
# Set server admin | |
server_admin=$OPTARG | |
;; | |
p) | |
# Set port | |
port=$OPTARG | |
listen_port=${listen_port:1} | |
;; | |
e) | |
# Set ENV | |
symfony_env=$(printf "\n SetEnv SYMFONY_ENV %s\n " $OPTARG) | |
;; | |
v) | |
# Set vhost name | |
vhost=$OPTARG | |
;; | |
d) | |
# Set document root | |
document_root=$OPTARG | |
;; | |
h) | |
usage | |
exit | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
if [ -z "$vhost" ]; then | |
echo -e "\e[0;31mYou must specify a vhost name\e[0m" | |
exit 1 | |
fi | |
if [ -z "$document_root" ]; then | |
echo -e "\e[0;31mYou must specify a document root\e[0m" | |
exit 1 | |
fi | |
listen_port="$listen_port $port" | |
cat > /tmp/$$.tmp << EOF | |
$listen_port | |
<VirtualHost *:$port> | |
ServerAdmin $server_admin | |
DocumentRoot "$document_root" | |
<Directory "$document_root"> | |
Options Indexes FollowSymLinks | |
AllowOverride all | |
Require all granted | |
</Directory> | |
$symfony_env | |
ErrorLog \${APACHE_LOG_DIR}/${vhost}_error.log | |
<IfModule log_config_module> | |
CustomLog \${APACHE_LOG_DIR}/${vhost}_access.log combined | |
</IfModule> | |
</VirtualHost> | |
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet | |
EOF | |
sudo cp /tmp/$$.tmp $APACHE_SITE_DIR/$vhost.conf | |
sudo a2ensite $vhost | |
sudo systemctl restart apache2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment