Skip to content

Instantly share code, notes, and snippets.

@xctheo
Forked from MauJFernandez/addwebsite
Last active May 12, 2017 01:49
Show Gist options
  • Select an option

  • Save xctheo/e98ebd3f8d1fd25a5c446494b6721a99 to your computer and use it in GitHub Desktop.

Select an option

Save xctheo/e98ebd3f8d1fd25a5c446494b6721a99 to your computer and use it in GitHub Desktop.
Apache auto-vhost
#!/bin/sh
WEBROOT="$HOME/www/"
VHOSTDIR="/etc/apache2/sites-available/"
NGINXDIR="/etc/nginx/sites-available/"
NGINXENABLEDDIR="/etc/nginx/sites-available/"
EXTENSION=".conf"
RESTARTCMD="/usr/bin/sudo service apache2 reload"
RESTARTCMD_NGINX="/usr/bin/sudo service nginx reload"
if [ "$1" != '' ]; then
if [ ! -f "$VHOSTDIR$1.conf" ]; then
sudo cp "$VHOSTDIR/template.conf" "$VHOSTDIR$1$EXTENSION"
echo "created $VHOSTDIR$1$EXTENSION"
else
sudo mv "$VHOSTDIR$1.conf" "$VHOSTDIR$1$EXTENSION.bak"
sudo cp "$VHOSTDIR/template.conf" "$VHOSTDIR$1$EXTENSION"
echo "created $VHOSTDIR$1$EXTENSION and made a backup of the existing conf"
fi
sudo find "$VHOSTDIR$1$EXTENSION" -type f -exec sed -i -e "s,SITENAME,$1,g" -e "s,PATH,$WEBROOT,g" {} \;
if [ ! -f "$NGINXDIR$1.conf" ]; then
sudo cp "$NGINXDIR/template.conf" "$NGINXDIR$1$EXTENSION"
echo "created $NGINXDIR$1$EXTENSION"
else
sudo mv "$NGINXDIR$1.conf" "$NGINXDIR$1$EXTENSION.bak"
sudo cp "$NGINXDIR/template.conf" "$NGINXDIR$1$EXTENSION"
echo "created $NGINXDIR$1$EXTENSION and made a backup of the existing conf"
fi
sudo find "$NGINXDIR$1$EXTENSION" -type f -exec sed -i -e "s,SITENAME,$1,g" -e "s,PATH,$WEBROOT,g" {} \;
sudo ln -s "$NGINXDIR$1$EXTENSION" "$NGINXENABLEDDIR$1$EXTENSION"
if [ ! -d "$WEBROOT$1/" ]; then
sudo mkdir -p "$WEBROOT$1/public"
sudo chown -R www-data:www-data "$WEBROOT$1/"
echo "created $WEBROOT$1/"
else
echo "$WEBROOT$1/ already exists"
fi
sudo a2ensite $1
$RESTARTCMD
echo "reloaded apache"
$RESTARTCMD_NGINX
echo "reloaded nginx"
elif [ "$1" = 'help' ] || [ "$1" = '' ]; then
echo "usage:"
echo "sudo addwebsite "
echo "example: to create hostname.localhost just run the command 'sudo addwebsite hostname.localhost'"
fi
sudo chown -R www-data:www-data $WEBROOT
# sudo chgrp -R www-data $WEBROOT
sudo chmod -R g+rwxs $WEBROOT
# sed -i "127.0.0.1 $1.local" /etc/hosts
#!/bin/sh
WEBROOT="$HOME/www/"
VHOSTDIR="/etc/apache2/sites-available/"
NGINXDIR="/etc/nginx/sites-available/"
NGINXENABLEDDIR="/etc/nginx/sites-enabled/"
EXTENSION=".conf"
RESTARTCMD="/usr/bin/sudo service apache2 reload"
RESTARTCMD_NGINX="/usr/bin/sudo service nginx reload"
if [ "$1" != '' ]; then
if [ ! -f "$VHOSTDIR$1.conf" ]; then
sudo cp "$VHOSTDIR/template.conf" "$VHOSTDIR$1$EXTENSION"
echo "created $VHOSTDIR$1$EXTENSION"
else
sudo mv "$VHOSTDIR$1.conf" "$VHOSTDIR$1$EXTENSION.bak"
sudo cp "$VHOSTDIR/template.conf" "$VHOSTDIR$1$EXTENSION"
echo "created $VHOSTDIR$1$EXTENSION and made a backup of the existing conf"
fi
sudo find "$VHOSTDIR$1$EXTENSION" -type f -exec sed -i -e "s,SITENAME,$1,g" -e "s,PATH,$WEBROOT,g" {} \;
if [ ! -f "$NGINXDIR$1.conf" ]; then
sudo cp "$NGINXDIR/template.conf" "$NGINXDIR$1$EXTENSION"
echo "created $NGINXDIR$1$EXTENSION"
else
sudo mv "$NGINXDIR$1.conf" "$NGINXDIR$1$EXTENSION.bak"
sudo cp "$NGINXDIR/template.conf" "$NGINXDIR$1$EXTENSION"
echo "created $NGINXDIR$1$EXTENSION and made a backup of the existing conf"
fi
sudo find "$NGINXDIR$1$EXTENSION" -type f -exec sed -i -e "s,SITENAME,$1,g" -e "s,PATH,$WEBROOT,g" {} \;
sudo ln -s "$NGINXDIR$1$EXTENSION" "$NGINXENABLEDDIR$1$EXTENSION"
if [ ! -d "$WEBROOT$1/" ]; then
sudo mkdir -p "$WEBROOT$1/public"
sudo chown -R www-data:www-data "$WEBROOT$1/"
echo "created $WEBROOT$1/"
else
echo "$WEBROOT$1/ already exists"
fi
sudo a2ensite $1
$RESTARTCMD
echo "reloaded apache"
$RESTARTCMD_NGINX
echo "reloaded nginx"
elif [ "$1" = 'help' ] || [ "$1" = '' ]; then
echo "usage:"
echo "sudo addwebsite "
echo "example: to create hostname.localhost just run the command 'sudo addwebsite hostname.localhost'"
fi
sudo chown -R www-data:www-data $WEBROOT
# sudo chgrp -R www-data $WEBROOT
sudo chmod -R g+rwxs $WEBROOT
# sed -i "127.0.0.1 $1.local" /etc/hosts
server {
listen 80;
server_name SITENAME www.SITENAME;
root PATH/SITENAME/public;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
<VIRTUALHOST *:80>
ServerAdmin webmaster@SITENAME
ServerName SITENAME
ServerAlias www.SITENAME
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
DocumentRoot PATH/SITENAME/public
<Directory PATH/SITENAME/public>
# enable the .htaccess rewrites
AllowOverride All
Require all granted
Order allow,deny
Allow from All
</Directory>
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
ErrorLog /var/log/apache2/SITENAME_error.log
CustomLog /var/log/apache2/SITENAME_access.log combined
</VIRTUALHOST>
<VIRTUALHOST *:80>
ServerAdmin webmaster@SITENAME
ServerName SITENAME
ServerAlias www.SITENAME
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
DocumentRoot PATH/SITENAME/public
<Directory PATH/SITENAME/public>
# enable the .htaccess rewrites
AllowOverride All
Require all granted
Order allow,deny
Allow from All
</Directory>
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
ErrorLog /var/log/apache2/SITENAME_error.log
CustomLog /var/log/apache2/SITENAME_access.log combined
</VIRTUALHOST>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment