Project

General

Profile

Download (7.75 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
#	rc.php_ini_setup
4
#	Copyright (C) 2010 Scott Ullrich <sullrich@gmail.com>
5
#	All rights reserved.
6
#
7
#	Redistribution and use in source and binary forms, with or without
8
#	modification, are permitted provided that the following conditions are met:
9
#
10
#	1. Redistributions of source code must retain the above copyright notice,
11
#	   this list of conditions and the following disclaimer.
12
#
13
#	2. Redistributions in binary form must reproduce the above copyright
14
#	   notice, this list of conditions and the following disclaimer in the
15
#	   documentation and/or other materials provided with the distribution.
16
#
17
#	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
#	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19
#	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
#	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
#	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
#	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
#	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
#	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
#	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
#	POSSIBILITY OF SUCH DAMAGE.
27

    
28
if [ -f /usr/local/etc/php.ini ]; then
29
	rm /usr/local/etc/php.ini
30
fi
31
if [ -f /usr/local/lib/php.ini ]; then
32
	rm /usr/local/lib/php.ini
33
fi
34

    
35
# Set our operating platform
36
PLATFORM=`cat /etc/platform`
37
EXTENSIONSDIR="/usr/local/lib/php/20060613/"
38

    
39
# Grab amount of memory that is detected
40
if [ -f /var/log/dmesg.boot ]; then
41
	AVAILMEM=`/bin/cat /var/log/dmesg.boot |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
42
else 
43
	AVAILMEM=`/sbin/dmesg -a |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
44
fi
45

    
46
if [ -z "$AVAILMEM" ]; then
47
	MEM=`sysctl hw.physmem | cut -d':' -f2`
48
	AVAILMEM=`expr $MEM / 1048576`
49
fi
50

    
51
# Calculate APC SHM size according 
52
# to detected memory values
53
if [ "$AVAILMEM" -lt "65" ]; then
54
	APCSHMEMSIZE="1"
55
fi
56
if [ "$AVAILMEM" -lt "96" ]; then
57
	APCSHMEMSIZE="5"
58
fi
59
if [ "$AVAILMEM" -lt "128" ]; then
60
	APCSHMEMSIZE="10"
61
fi
62
if [ "$AVAILMEM" -gt "128" ]; then
63
	APCSHMEMSIZE="15"
64
fi
65
if [ "$AVAILMEM" -gt "256" ]; then
66
	APCSHMEMSIZE="20"
67
fi
68
if [ "$AVAILMEM" -gt "384" ]; then
69
	APCSHMEMSIZE="25"
70
fi
71
if [ "$AVAILMEM" -gt "512" ]; then
72
	APCSHMEMSIZE="30"
73
fi
74
if [ "$AVAILMEM" -gt "784" ]; then
75
	APCSHMEMSIZE="35"
76
fi
77

    
78
# Set upload directory
79
if [ "$PLATFORM" = "embedded" -o "$PLATFORM" = "nanobsd" ]; then
80
	UPLOADTMPDIR="/root"
81
else 
82
	UPLOADTMPDIR="/tmp"
83
fi
84

    
85
# Define php modules.  Do not add .so, it will  
86
# be done automatically by the script below.
87
PHPMODULES="apc \
88
# Downloading via HTTP/FTP (pkg mgr, etc)
89
	curl \
90
	date \
91
# Internationalization 
92
	gettext \
93
# User manager
94
	ldap \
95
	openssl \
96
	pcntl \
97
# Regexs, PERL style!
98
	pcre \
99
# The mighty posix!
100
	posix \
101
	readline \
102
# Login sessions
103
	session \
104
	standard \
105
# Extra sanity seatbelts
106
	suhosin \
107
	pfSense \
108
# Firewall rules edit
109
	ctype \
110
# Config read/write
111
	xml \
112
	xmlreader \
113
	xmlwriter \
114
	libxml \
115
# user manager
116
	mhash \
117
# firewall_rules_edit.php
118
	mbstring \
119
	shmop \
120
# Page compression
121
	zlib \
122
# SQLlite
123
	simplexml \
124
	spl \
125
	pdo \
126
	sqlite \
127
# ZeroMQ
128
	zmq"
129

    
130
# Modules previously included.
131
# can be turned on by touching
132
# /etc/php_dynamodules/$modulename
133
#	sysvmsg \
134
#	sysvsem \
135
#	sysvshm \
136
#	bcmath \
137
#	tokenizer \
138
#	uploadprogress \
139
#	sockets \
140
#	Reflection \
141
#	mysql \
142
#	bz2	\
143
#	json \
144

    
145
# Get a loaded module list in the stock php
146
if [ -f /usr/local/etc/php.ini ]; then
147
	rm /usr/local/etc/php.ini
148
fi
149
if [ -f /usr/local/lib/php.ini ]; then
150
	rm /usr/local/lib/php.ini
151
fi
152
LOADED_MODULES=`php -m | grep -v "\["`
153

    
154
# Populate a dummy php.ini to avoid
155
# the file being clobbered and the firewall
156
# not being able to boot back up.
157
cat >/usr/local/lib/php.ini <<EOF
158
; File generated from /etc/rc.php_ini_setup
159
output_buffering = "0"
160
expose_php = Off
161
implicit_flush = true
162
magic_quotes_gpc = Off
163
max_execution_time = 99999999
164
max_input_time = 99999999
165
set_time_limit = 0
166
register_argc_argv = On
167
file_uploads = On
168
upload_tmp_dir = ${UPLOADTMPDIR}
169
upload_max_filesize = 100M
170
post_max_size = 100M
171
html_errors = Off
172
zlib.output_compression = Off
173
zlib.output_compression_level = 1
174
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg"
175
extension_dir=${EXTENSIONSDIR}
176

    
177
; Extensions
178

    
179
EOF
180

    
181
# Ensure directory exists
182
if [ ! -d /etc/php_dynamodules ]; then
183
	mkdir /etc/php_dynamodules
184
fi
185

    
186
# Read in dynamodules
187
if [ -d /etc/php_dynamodules ]; then
188
	DYNA_MODULES=`ls /etc/php_dynamodules/`
189
	PHPMODULES="$PHPMODULES $DYNA_MODULES"
190
fi
191

    
192
# Loop through and generate modules to load.
193
# Take into account modules built into php.
194
for EXT in $PHPMODULES; do
195
	SHOULDADD="true"
196
	# Check to see if module is compiled into php statically
197
	for LM in $LOADED_MODULES; do
198
		if [ "$EXT" = "$LM" ]; then
199
			SHOULDADD="false"
200
		fi
201
	done
202
	if [ "$SHOULDADD" = "true" ]; then
203
		# Ensure extension exists before adding.
204
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
205
			echo "extension=${EXT}.so" >> /usr/local/lib/php.ini
206
		fi
207
	fi
208
done
209

    
210
# Get amount of ram installed on this system
211
RAM=`sysctl hw.realmem | awk '{print $2/1000000}' | awk -F '.' '{print $1}'`
212
export RAM
213
export LOWMEM
214
if [ "$RAM" -lt "97" ]; then
215
	LOWMEM="TRUE"
216
	cat >>/usr/local/lib/php.ini <<EOF
217

    
218
[suhosin]
219
suhosin.get.max_array_depth = 5000
220
suhosin.get.max_array_index_length = 256
221
suhosin.get.max_vars = 5000
222
suhosin.get.max_value_length = 500000
223
suhosin.post.max_array_depth = 5000
224
suhosin.post.max_array_index_length = 256
225
suhosin.post.max_vars = 5000
226
suhosin.post.max_value_length = 500000
227
suhosin.request.max_array_depth = 5000
228
suhosin.request.max_array_index_length = 256
229
suhosin.request.max_vars = 5000
230
suhosin.request.max_value_length = 500000
231

    
232
EOF
233

    
234
fi
235
if [  "$RAM" -gt 96 ]; then
236

    
237
	cat >>/usr/local/lib/php.ini <<EOF
238

    
239
; APC Settings
240
apc.enabled="1"
241
apc.enable_cli="0"
242
apc.shm_size="${APCSHMEMSIZE}"
243

    
244
[suhosin]
245
suhosin.get.max_array_depth = 5000
246
suhosin.get.max_array_index_length = 256
247
suhosin.get.max_vars = 5000
248
suhosin.get.max_value_length = 500000
249
suhosin.post.max_array_depth = 5000
250
suhosin.post.max_array_index_length = 256
251
suhosin.post.max_vars = 5000
252
suhosin.post.max_value_length = 500000
253
suhosin.request.max_array_depth = 5000
254
suhosin.request.max_array_index_length = 256
255
suhosin.request.max_vars = 5000
256
suhosin.request.max_value_length = 500000
257

    
258
EOF
259

    
260
else 
261

    
262
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC."
263
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC." | logger -p daemon.info -i -t rc.php_ini_setup
264

    
265
fi
266

    
267
# Copy php.ini file to etc/ too (cli)
268
cp /usr/local/lib/php.ini /usr/local/etc/php.ini
269

    
270
# Remove old log file if it exists.
271
if [ -f /var/run/php_modules_load_errors.txt ]; then
272
	rm /var/run/php_modules_load_errors.txt
273
fi 
274

    
275
for EXT in $PHPMODULES; do
276
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | tr '[:upper:]' '[:lower:]'`"
277
done
278

    
279
# Check loaded modules and remove anything that did not load correctly
280
LOADED_MODULES=`php -m | tr '[:upper:]' '[:lower:]' 2>/dev/null | grep -v "\["`
281
for EXT in $PHPMODULESLC; do
282
	SHOULDREMOVE="true"
283
	for LM in $LOADED_MODULES; do
284
		if [ "$EXT" = "$LM" ]; then
285
			SHOULDREMOVE="false"
286
		fi		
287
	done
288
	# Handle low memory situations
289
	if [ "$LOWMEM" = "TRUE" ]; then
290
		if [ "$EXT" = "apc" ]; then
291
			SHOULDREMOVE="true"
292
		fi
293
		if [ "$EXT" = "xcache" ]; then
294
			SHOULDREMOVE="true"
295
		fi
296
	fi
297
	if [ "$SHOULDREMOVE" = "true" ]; then
298
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
299
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
300
			cat /usr/local/lib/php.ini | grep -v $EXT > /tmp/php.ini
301
			mv /tmp/php.ini /usr/local/lib/php.ini
302
		fi
303
	fi
304
done
305

    
306
# Copy php.ini file to etc/ too (cli)
307
cp /usr/local/lib/php.ini /usr/local/etc/php.ini
308

    
309

    
310

    
311

    
(75-75/98)