-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.sh
executable file
·148 lines (128 loc) · 3.03 KB
/
server.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
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
#!/bin/bash
#
# Default config values
#
HTTPD=$(which httpd 2>/dev/null || which apache2 2>/dev/null)
if [ "x$HTTPD" = "x" ]; then
HTTPD=/usr/sbin/httpd
fi
TYPES_CONFIG=
PORT=4567
#
# Attempt to find the modules dir
#
MODULE_DIR=libexec/apache2
if [ `uname` = "Linux" ]; then
TYPES_CONFIG="TypesConfig \"/etc/mime.types\""
MODULE_DIR=/usr/lib/apache2/modules
if [ ! -d $MODULE_DIR ]; then
MODULE_DIR=/usr/lib/httpd/modules
fi
if [ ! -d $MODULE_DIR ]; then
MODULE_DIR=/usr/lib64/httpd/modules
fi
fi
DIR_MODULE="LoadModule dir_module $MODULE_DIR/mod_dir.so"
LOG_CONFIG_MODULE="LoadModule log_config_module $MODULE_DIR/mod_log_config.so"
MIME_MODULE="LoadModule mime_module $MODULE_DIR/mod_mime.so"
#
# Check existing modules and don't load the built-in modules
#
if [ `uname` = "Linux" ]; then
if [ ! -f "$MODULE_DIR/mod_dir.so" ]; then
DIR_MODULE=""
fi
if [ ! -f "$MODULE_DIR/mod_log_config.so" ]; then
LOG_CONFIG_MODULE=""
fi
if [ ! -f "$MODULE_DIR/mod_mime.so" ]; then
MIME_MODULE=""
fi
fi
#
# Print usage
#
function usage() {
echo "usage: server.sh [-h] [-p PORT] [-e HTTPD_EXECUTABLE] [-m APACHE_MODULE_DIR]"
}
#
# Parse args
#
while getopts ":e:p:m:h" opt; do
case $opt in
e)
HTTPD=$OPTARG
;;
p)
PORT=$OPTARG
;;
m)
MODULE_DIR=$OPTARG
;;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
esac
done
if [ ! -x "$HTTPD" ]; then
if [ `uname` = "Linux" ]; then
echo "If you are running Ubuntu or Debian, install Apache with: sudo apt-get update && sudo apt-get install apache2" >&2
echo "If you are running RedHat or CentOS, install Apache with: sudo yum install httpd" >&2
else
echo "Could not find Apache. Do you have it installed?" >&2
fi
echo "After installing Apache, please re-run this script." >&2
exit 2
fi
#
# Find project dir
#
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
CONF=$DIR/server/httpd.conf
ROOT=$DIR/source
#
# Create conf file
#
mkdir -p server
cat > $CONF <<EOF
Listen 127.0.0.1:$PORT
ServerName localhost
DocumentRoot "$ROOT"
PidFile "$DIR/server/httpd.pid"
LockFile "$DIR/server/accept.lock"
ErrorLog |/bin/cat
$TYPES_CONFIG
$DIR_MODULE
$LOG_CONFIG_MODULE
$MIME_MODULE
<Directory "$ROOT">
Options Indexes
</Directory>
<IfModule mime_module>
AddType text/html .html .htm
AddType application/javascript .js
AddType text/css .css
AddType image/png .png
</IfModule>
<IfModule log_config_module>
LogFormat "%t \"%r\" %>s %b" custom
CustomLog |/bin/cat custom
</IfModule>
EOF
#
# Run apache
#
echo "Starting server at http://localhost:$PORT ...";echo
$HTTPD -f $CONF -DNO_DETACH -DFOREGROUND