-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkvhost
152 lines (123 loc) · 4.82 KB
/
mkvhost
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
################################################################################
### CHECK SCRIPT INVOCATION AND SET CONSTANTS
################################################################################
### CHECK FOR ROOT USER
if [[ $(whoami) != "root" ]]; then
echo "ERROR! Script needs root access. Please run as sudo";
exit 0;
fi;
### ENSURE THAT THE SCRIPT IS CALLED WITH PARAMETERS
if [[ $1 == "" ]]; then
echo "Usage: mkvhostrepo <username> <vhostname>";
echo "Example: mkvhostrepo username www.example.com";
echo " will create the virtual host for www.example.com in the correct user's Sites folder";
echo " and set up a local repo";
exit 0;
fi
if [[ $2 == "" ]]; then
echo "Usage: mkvhostrepo <username> <vhostname>";
echo "Example: mkvhostrepo www.example.com";
echo " will create the virtual host for www.example.com in the correct user's Sites folder";
echo " and set up a local repo";
exit 0;
fi
### CREATE VARIABLES
USERNAME=$1;
VIRTUALHOST=$2;
VHOSTS="/etc/apache2/vhosts";
SITES="/Users/${USERNAME}/Sites";
IPV4="127.0.0.1";
IPV6="fe80::1%lo0";
DBUSER="root";
DBPASSWORD="br-6004w-g1";
DBPREFIX="symphony_";
DBNAME=${VIRTUALHOST//'.'/""};
SQL="CREATE DATABASE ${DBPREFIX}${DBNAME}";
################################################################################
### HTTPD VIRTUAL HOST
################################################################################
### CREATE THE VIRTUALHOST CONF
if [[ -e ${VHOSTS}/${VIRTUALHOST}.conf ]]; then
echo "${VHOSTS}/${VIRTUALHOST}.conf already exists. Skipping.";
else
if [[ ! -e ${VHOSTS}/template.http ]]; then
echo "${VHOSTS}/template.http does not exist. Cannot create ${SITESENABLED}/${VIRTUALHOST}.";
else
cat ${VHOSTS}/template.http | sed "s/VHOSTNAME/${VIRTUALHOST}/g" | sed "s/USERNAME/${USERNAME}/g" | sed "s/MHOSTNAME/$(hostname)/g" > ${VHOSTS}/${VIRTUALHOST}.conf
fi
fi
### CREATE A RECORD IN HOSTS FILE
grep "127.0.0.1 ${VIRTUALHOST}" /etc/hosts;
if [[ $? -eq 0 ]]; then
echo "Hostname already in hosts file. Skipping."
else
if [[ ${IPV6} != "" ]]; then
echo "${IPV6} ${VIRTUALHOST}" >> /etc/hosts
fi
echo "${IPV4} ${VIRTUALHOST}" >> /etc/hosts
echo "";
fi
### CREATE THE ERROR LOG FILE
if [[ -e ${SITES}/__logs/vhosts/${VIRTUALHOST}-error.log ]]; then
echo "${SITES}/__logs/vhosts/${VIRTUALHOST}-error.log already exists. Skipping.";
else
touch ${SITES}/__logs/vhosts/${VIRTUALHOST}-error.log
fi
################################################################################
### FILESYSTEM VIRTUAL HOST
################################################################################
if [[ ! -e ${SITES}/${VIRTUALHOST} ]]; then
### CREATE THE VIRTUALHOST
bool="n";
echo -n "Should I create ${SITES}/${VIRTUALHOST} folder for you? ";
read bool;
if [[ ${bool} == "y" || ${bool} == "Y" || ${bool} == "yes" || ${bool} == "YES" || ${bool} == "Yes" ]]; then
if [[ -e ${SITES}/${VIRTUALHOST} ]]; then
echo "${SITES}/${VIRTUALHOST} already exists.";
else
mkdir -p ${SITES}/${VIRTUALHOST};
chmod -R 775 ${SITES}/${VIRTUALHOST};
fi
fi
### CREATE THE HTDOCS FOLDER
bool="n";
echo -n "Should I create ${SITES}/${VIRTUALHOST}/htdocs folder for you? ";
read bool;
if [[ ${bool} == "y" || ${bool} == "Y" || ${bool} == "yes" || ${bool} == "YES" || ${bool} == "Yes" ]]; then
if [[ -e ${SITES}/${VIRTUALHOST}/htdocs ]]; then
echo "${SITES}/${VIRTUALHOST}/htdocs already exists.";
else
mkdir -p ${SITES}/${VIRTUALHOST}/htdocs;
chmod -R 775 ${SITES}/${VIRTUALHOST}/htdocs;
fi
fi
fi
### SET PERMISSIONS ON FILES AND FOLDERS
echo -n "Should I run owner and permissions fixes on ${SITES}/${VIRTUALHOST} folder for you? ";
read bool;
if [[ ${bool} == "y" || ${bool} == "Y" || ${bool} == "yes" || ${bool} == "YES" || ${bool} == "Yes" ]]; then
fixperms ${USERNAME} ${SITES}/${VIRTUALHOST};
fi
################################################################################
### FILESYSTEM VIRTUAL HOST
################################################################################
echo -n "Should I run create a database for you? ";
read bool;
if [[ ${bool} == "y" || ${bool} == "Y" || ${bool} == "yes" || ${bool} == "YES" || ${bool} == "Yes" ]]; then
mysql -u ${DBUSER} -p${DBPASSWORD} -e "${SQL}";
fi
################################################################################
### RELOAD APACHE
################################################################################
bool="n";
echo -n "Should I reload Apache for you? ";
read bool;
if [[ ${bool} == "y" || ${bool} == "Y" || ${bool} == "yes" || ${bool} == "YES" || ${bool} == "Yes" ]]; then
echo "Running: sudo apachectl -k graceful";
apachectl -k graceful;
fi
################################################################################
### FINISHED OUTPUT
################################################################################
echo "Completed.";