-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewsite.sh
88 lines (67 loc) · 2.99 KB
/
newsite.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#------------------------------------------------------------------
#
# BASH script to automate setting up a new virtual host in MAMP
# (please note that this script assumes a standard installation of MAMP)
#
# Adapted from https://github.com/alanauckland86/MAMP-newsite-setup-script-/blob/master/newsite.sh
#
# To run type: sudo sh newsite.sh
#
# Or make executable with: chmod a+x newsite.sh
# then to run type: sudo ./newsite.sh
#
#------------------------------------------------------------------
# 1 Get the name for the new site
#================================
read -p "New site name: " SITE
# 2 Set up variables
#===================
# 2.1 Determine account under which this site will be set up
if [ $SUDO_USER ]
then LOGGEDINUSER=$SUDO_USER
else LOGGEDINUSER=`whoami`
fi
# 2.2 Get the document root and append site name to create path to new directory
DOCROOT=$(grep "DocumentRoot \"" /Applications/MAMP/conf/apache/httpd.conf) # Find line with DocumentRoot in httpd.conf
DOCROOT=$(echo "${DOCROOT##* }" | tr -d '"') # Isolate DocumentRoot path and strip off quotemarks
SITEPATH="${DOCROOT}/${SITE}"
# 2.3 Path to httpd-vhosts.conf
VHOSTSFILE="/Applications/MAMP/conf/apache/extra/httpd-vhosts.conf"
# 2.4 TLD (edit this if you want a different TLD)
TLD="local"
# 2.5 Path to the test file that will be created
TESTFILE="${SITEPATH}/index.html"
# 3 Make new directory and change owner (so not owned by 'root')
#======================================
mkdir $SITEPATH
chown $LOGGEDINUSER $SITEPATH
# 4 Add new entry in /etc/hosts and flush DNS cache
#==================================================
echo "127.0.0.1\t${SITE}.${TLD}" >> /etc/hosts
dscacheutil -flushcache
# 5 Add new virtual host to http-vhosts.conf
#===========================================
echo "\n<VirtualHost *:80>" >> $VHOSTSFILE
echo "\tServerAdmin webmaster@${SITE}.${TLD}" >> $VHOSTSFILE
echo "\tDocumentRoot \"${SITEPATH}\"" >> $VHOSTSFILE
echo "\tServerName ${SITE}.${TLD}" >> $VHOSTSFILE
echo "\tErrorLog \"/Applications/MAMP/logs/${SITE}-error.log\"" >> $VHOSTSFILE
echo "\tCustomLog \"/Applications/MAMP/logs/${SITE}-access.log\" common" >> $VHOSTSFILE
echo "</VirtualHost>" >> $VHOSTSFILE
# 6 Create index.html file for testing
#=====================================
echo "<!DOCTYPE html>\n<html>\n<head>\n<title>Test page</title>\n</head>\n" >> $TESTFILE
echo "<body>\n<h1>TEST PAGE</h1>\n<p>Hooray - your site called ${SITE} is working!</p>\n</body>\n</html>" >> $TESTFILE
chown $LOGGEDINUSER $TESTFILE # so owned by user running sudo rather than owned by 'root'
# 7 Restart Apache
#=================
/Applications/MAMP/bin/apache2/bin/apachectl restart;
# 8 Prompt for and create database
#================================
read -p "Create MySQL database (y/n): " DB
if [ $DB == "y" ] || [ $DB == "Y" ]
then /Applications/MAMP/Library/bin/mysql -uroot -p -e 'create database '${SITE}
fi
# 9 Prompt user to test
#======================
echo "To confirm that the new site is working open web browser and type ${SITE}.${TLD}/ in address bar."