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="/var/www/"
VHOSTDIR="/etc/apache2/sites-available/"
EXTENSION=".conf"
RESTARTCMD="/usr/bin/sudo service apache2 reload"
if [ "$1" != '' ]; then
if [ ! -f "$VHOSTDIR$1.conf" ]; then
cp "$VHOSTDIR/skeleton" "$VHOSTDIR$1$EXTENSION"
echo "created $VHOSTDIR$1$EXTENSION"
else
mv "$VHOSTDIR$1.conf" "$VHOSTDIR$1$EXTENSION.bak"
cp "$VHOSTDIR/skeleton" "$VHOSTDIR$1$EXTENSION"
echo "created $VHOSTDIR$1$EXTENSION and made a backup of the existing conf"
fi
find "$VHOSTDIR$1$EXTENSION" -type f -exec sed -i "s/SKELETON/$1/" {} \;
if [ ! -d "$WEBROOT$1/" ]; then
mkdir "$WEBROOT$1/"
chown -R mauro:mauro "$WEBROOT$1/"
echo "created $WEBROOT$1/"
else
echo "$WEBROOT$1/ already exists"
fi
sudo a2ensite $1
$RESTARTCMD
echo "reloaded apache"
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
sed -i "127.0.0.1 $1.local" /etc/hosts
#!/bin/sh
WEBROOT="/var/www/"
VHOSTDIR="/etc/apache2/sites-available/"
EXTENSION="-dev.conf"
RESTARTCMD="/usr/bin/sudo service apache2 reload"
if [ "$1" != '' ]; then
if [ ! -f "$VHOSTDIR$1$EXTENSION" ]; then
cp "$VHOSTDIR/skeletondev" "$VHOSTDIR$1$EXTENSION"
echo "created $VHOSTDIR$1$EXTENSION"
else
mv "$VHOSTDIR$1.conf" "$VHOSTDIR$1$EXTENSION.bak"
cp "$VHOSTDIR/skeletondev" "$VHOSTDIR$1$EXTENSION"
echo "created $VHOSTDIR$1$EXTENSION and made a backup of the existing conf"
fi
find "$VHOSTDIR$1$EXTENSION" -type f -exec sed -i "s/SKELETON/$1/" {} \;
if [ ! -d "$WEBROOT$1/" ]; then
mkdir "$WEBROOT$1/"
chown -R mauro:mauro "$WEBROOT$1/"
echo "created $WEBROOT$1/"
else
echo "$WEBROOT$1/ already exists"
fi
sudo a2ensite $1$EXTENSION
$RESTARTCMD
echo "reloaded apache"
elif [ "$1" = 'help' ] || [ "$1" = '' ]; then
echo "usage:"
echo "sudo addwebsitedev "
echo "example: to create hostname.localhost just run the command 'sudo addwebsite hostname.localhost'"
fi
sed -i "127.0.0.1 $1.dev.local" /etc/hosts
<VIRTUALHOST *:80>
ServerAdmin webmaster@localhost
ServerName SKELETON.local
<DIRECTORY />
Options FollowSymLinks
AllowOverride None
</DIRECTORY>
DocumentRoot /var/www/SKELETON/web
<DIRECTORY /var/www/SKELETON/web>
# enable the .htaccess rewrites
AllowOverride All
Order allow,deny
Allow from All
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
</DIRECTORY>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VIRTUALHOST>
<VIRTUALHOST *:80>
ServerAdmin webmaster@localhost
ServerName SKELETON.dev.local
<DIRECTORY />
Options FollowSymLinks
AllowOverride None
</DIRECTORY>
DocumentRoot /var/www/SKELETON/web
<DIRECTORY /var/www/SKELETON/web>
# enable the .htaccess rewrites
AllowOverride All
Order allow,deny
Allow from All
DirectoryIndex app_dev.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app_dev.php [QSA,L]
</IfModule>
</DIRECTORY>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VIRTUALHOST>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment