Last active
December 19, 2015 20:49
-
-
Save johndryan/6016071 to your computer and use it in GitHub Desktop.
Bash Script to automate creation of new websites on my Mediatemple Ubuntu server which runs Nginx as a Reverse Web Proxy. Such a pain setting up both and users, etc. each time, so this is a shortcut. Based on this script: http://bit.ly/1bmcJ7e
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
#!/bin/bash | |
# Author: Seb Dangerfield | |
# URL: http://sebdangerfield.me.uk | |
# ./create_site.sh example.com [email protected] | |
NGINX_ALL_VHOSTS='/etc/nginx/sites-available' | |
NGINX_ENABLED_VHOSTS='/etc/nginx/sites-enabled' | |
APACHE_ALL_VHOSTS='/etc/apache2/sites-available' | |
WEB_DIR='/var/www' | |
SED=`which sed` | |
NGINX=`sudo which nginx` | |
CURRENT_DIR=`dirname $0` | |
if [ -z $1 ]; then | |
echo "No domain name given" | |
exit 1 | |
fi | |
DOMAIN=$1 | |
if [ -z $2 ]; then | |
echo "No admin email given" | |
exit 1 | |
fi | |
ADMINEMAIL=$1 | |
# check the domain is valid! | |
PATTERN="^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$"; | |
if [[ "$DOMAIN" =~ $PATTERN ]]; then | |
DOMAIN=`echo $DOMAIN | tr '[A-Z]' '[a-z]'` | |
echo "Creating hosting for:" $DOMAIN | |
else | |
echo "invalid domain name" | |
exit 1 | |
fi | |
# Create a new user! | |
echo "Please specify the username for this site?" | |
read USERNAME | |
sudo adduser --home $WEB_DIR/$DOMAIN $USERNAME | |
#echo "Please enter a password for the user: $USERNAME" | |
#read -s PASS | |
#sudo echo $PASS | sudo passwd --stdin $USERNAME | |
# ---- FILES + FOLDERS ------------------------ | |
# put the template index.html file into the public_html dir | |
sudo mkdir /var/www/$DOMAIN/public_html | |
sudo mkdir /var/www/$DOMAIN/logs | |
sudo cp $CURRENT_DIR/index.html.template $WEB_DIR/$DOMAIN/public_html/index.html | |
sudo $SED -i "s/SITE/$DOMAIN/g" $WEB_DIR/$DOMAIN/public_html/index.html | |
sudo chown $USERNAME:$USERNAME $WEB_DIR/$DOMAIN/public_html -R | |
# ---- NGINX ---------------------------------- | |
# Now we need to copy the virtual host template | |
CONFIG=$NGINX_ALL_VHOSTS/$DOMAIN.conf | |
sudo cp $CURRENT_DIR/virtual_host_nginx.template $CONFIG | |
sudo $SED -i "s/DOMAIN/$DOMAIN/g" $CONFIG | |
sudo $SED -i "s/ADMINEMAIL/$ADMINEMAIL/g" $CONFIG | |
sudo $SED -i "s#ROOT#$WEB_DIR/$DOMAIN\/public_html#g" $CONFIG | |
# vv Necessary to add www-data user? vv | |
sudo usermod -aG $USERNAME www-data | |
sudo chmod g+rxs $WEB_DIR/$DOMAIN | |
sudo chmod 600 $CONFIG | |
sudo $NGINX -t | |
if [ $? -eq 0 ];then | |
# Create symlink | |
sudo ln -s $CONFIG $NGINX_ENABLED_VHOSTS/$DOMAIN.conf | |
else | |
echo "Could not create new vhost as there appears to be a problem with the newly created nginx config file: $CONFIG"; | |
exit 1; | |
fi | |
sudo /etc/init.d/nginx reload | |
# ---- APACHE2 -------------------------------- | |
# Now we need to copy the virtual host template | |
CONFIGAPACHE=$APACHE_ALL_VHOSTS/$DOMAIN | |
sudo cp $CURRENT_DIR/virtual_host_apache.template $CONFIGAPACHE | |
sudo $SED -i "s/DOMAIN/$DOMAIN/g" $CONFIGAPACHE | |
sudo $SED -i "s#ROOT#$WEB_DIR/$DOMAIN\/public_html#g" $CONFIGAPACHE | |
sudo a2ensite $DOMAIN | |
sudo /etc/init.d/apache2 reload | |
echo -e "\nSite Created for $DOMAIN" | |
echo "--------------------------" | |
echo "Host: "`hostname` | |
echo "URL: $DOMAIN" | |
echo "User: $USERNAME" | |
echo "--------------------------" | |
echo "Now restart apache & nginx" | |
echo "--------------------------" | |
exit 0; |
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
<html> | |
<head> | |
<title>SITE</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
</head> | |
<body> | |
Welcome to SITE, this site will be live shortly... | |
</body> | |
</html> |
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
<VirtualHost 127.0.0.1:80> | |
ServerAdmin ADMINEMAIL | |
ServerName DOMAIN | |
ServerAlias www.DOMAIN | |
DocumentRoot /var/www/DOMAIN/public_html/ | |
ErrorLog /var/www/DOMAIN/logs/error.log | |
CustomLog /var/www/DOMAIN/logs/access.log combined | |
</VirtualHost> |
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
### | |
# comment out this first stanza if you DO NOT want the leading www. stripped from the URI. | |
server { | |
listen 72.10.34.146:80; | |
server_name www.DOMAIN; | |
rewrite ^ http://DOMAIN$request_uri?; | |
} | |
server { | |
listen 72.10.34.146:80; | |
server_name DOMAIN; | |
server_name_in_redirect off; | |
access_log /var/www/DOMAIN/logs/access.log; | |
error_log /var/www/DOMAIN/logs/error.log; | |
location / { | |
proxy_pass http://127.0.0.1:80; | |
proxy_set_header Host $http_host; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
} | |
location ~* ^.+\.(jpg|jpeg|png|gif|swf|flv|mp4|mov|avi|wmv|m4v|mkv|ico|js|css|txt|html|htm)$ { | |
root /var/www/DOMAIN/public_html/; | |
access_log /var/www/DOMAIN/logs/access.log; | |
gzip on; | |
gzip_comp_level 2; | |
gzip_vary on; | |
gzip_proxied any; | |
gzip_types text/plain text/xml text/css application/x-javascript; | |
### | |
# uncomment the next line to enable expires headers | |
# expires 7d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment