Project

General

Profile

Download (9.38 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
# Set our operating platform
29
PLATFORM=`/bin/cat /etc/platform`
30

    
31
if [ -d /usr/local/lib/php/20090626 ]; then
32
	EXTENSIONSDIR="/usr/local/lib/php/20090626/"
33
else
34
	EXTENSIONSDIR="/usr/local/lib/php/20060613/"
35
fi
36

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

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

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

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

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

    
128
PHP_ZEND_MODULES="ioncube_loader"
129
PHP_ZEND_MODULES_TS="ioncube_loader_ts"
130

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

    
146
# Clear the .ini file to make sure we are clean
147
if [ -f /usr/local/etc/php.ini ]; then
148
	/bin/rm /usr/local/etc/php.ini
149
fi
150
if [ -f /usr/local/lib/php.ini ]; then
151
	/bin/rm /usr/local/lib/php.ini
152
fi
153
LOADED_MODULES=`/usr/local/bin/php -m | /usr/bin/grep -v "\["`
154

    
155
# Get a loaded module list in the stock php
156
# Populate a dummy php.ini to avoid
157
# the file being clobbered and the firewall
158
# not being able to boot back up.
159
/bin/cat >/usr/local/lib/php.ini <<EOF
160
; File generated from /etc/rc.php_ini_setup
161
output_buffering = "0"
162
expose_php = Off
163
implicit_flush = true
164
magic_quotes_gpc = Off
165
max_execution_time = 99999999
166
max_input_time = 99999999
167
set_time_limit = 0
168
register_argc_argv = On
169
file_uploads = On
170
upload_tmp_dir = ${UPLOADTMPDIR}
171
upload_max_filesize = 128M
172
post_max_size = 128M
173
html_errors = Off
174
zlib.output_compression = Off
175
zlib.output_compression_level = 1
176
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg"
177
display_startup_errors=on
178
display_errors=on
179
log_errors=on
180
error_log=/tmp/PHP_errors.log
181
extension_dir=${EXTENSIONSDIR}
182

    
183
; Extensions
184

    
185
EOF
186

    
187
# Copy php.ini file to etc/ too (cli)
188
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
189

    
190
# Ensure directory exists
191
if [ ! -d /etc/php_dynamodules ]; then
192
	/bin/mkdir /etc/php_dynamodules
193
fi
194
if [ ! -d /etc/php_dynamodules_zend ]; then
195
	/bin/mkdir /etc/php_dynamodules_zend
196
fi
197
if [ ! -d /etc/php_dynamodules_zend_ts ]; then
198
	/bin/mkdir /etc/php_dynamodules_zend_ts
199
fi
200

    
201
# Read in dynamodules
202
if [ -d /etc/php_dynamodules ]; then
203
	DYNA_MODULES=`/bin/ls -Utr /etc/php_dynamodules/`
204
	PHPMODULES="$PHPMODULES $DYNA_MODULES"
205
fi
206

    
207
# Read in zend modules
208
if [ -d /etc/php_dynamodules_zend ]; then
209
	DYNA_MODULES=`/bin/ls /etc/php_dynamodules_zend/`
210
	PHP_ZEND_MODULES="$PHP_ZEND_MODULES $DYNA_MODULES"
211
fi
212

    
213
# Read in zend threaded modules
214
if [ -d /etc/php_dynamodules_zend_ts ]; then
215
	DYNA_MODULES=`/bin/ls /etc/php_dynamodules_zend_ts/`
216
	PHP_ZEND_MODULES_TS="$PHP_ZEND_MODULES_TS $DYNA_MODULES"
217
fi
218

    
219
# Loop through and generate modules to load.
220
# Take into account modules built into php.
221
for EXT in $PHPMODULES; do
222
	SHOULDADD="true"
223
	# Check to see if module is compiled into php statically
224
	for LM in $LOADED_MODULES; do
225
		if [ "$EXT" = "$LM" ]; then
226
			SHOULDADD="false"
227
		fi
228
	done
229
	if [ "$SHOULDADD" = "true" ]; then
230
		# Ensure extension exists before adding.
231
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
232
			echo "extension=${EXT}.so" >> /usr/local/lib/php.ini
233
		fi
234
	fi
235
done
236

    
237
# Zend modules
238
for EXT in $PHP_ZEND_MODULES; do
239
	# Ensure extension exists before adding.
240
	if [ -f "${EXTENSIONSDIR}/ioncube/${EXT}.so" ]; then
241
		echo "zend_extension=${EXTENSIONSDIR}/ioncube/${EXT}.so" >> /usr/local/lib/php.ini
242
	fi
243
done
244

    
245
# Zend threaded modules
246
for EXT in $PHP_ZEND_MODULES_TS; do
247
	# Ensure extension exists before adding.
248
	if [ -f "${EXTENSIONSDIR}/ioncube/${EXT}.so" ]; then
249
		echo "zend_extension_ts=${EXTENSIONSDIR}/ioncube/${EXT}.so" >> /usr/local/lib/php.ini
250
	fi
251
done
252

    
253
# Get amount of ram installed on this system
254
RAM=`/sbin/sysctl hw.realmem | /usr/bin/awk '{print $2/1000000}' | /usr/bin/awk -F '.' '{print $1}'`
255
export RAM
256
export LOWMEM
257
if [  "$RAM" -gt 135 ]; then
258

    
259
	/bin/cat >>/usr/local/lib/php.ini <<EOF
260

    
261
; APC Settings
262
apc.enabled="1"
263
apc.enable_cli="0"
264
apc.shm_size="${APCSHMEMSIZE}"
265

    
266
EOF
267

    
268
else
269
	LOWMEM="TRUE"
270
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC."
271
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC." | /usr/bin/logger -p daemon.info -i -t rc.php_ini_setup
272
fi
273

    
274
	/bin/cat >>/usr/local/lib/php.ini <<EOF
275

    
276
[suhosin]
277
suhosin.get.max_array_depth = 5000
278
suhosin.get.max_array_index_length = 256
279
suhosin.get.max_vars = 5000
280
suhosin.get.max_value_length = 500000
281
suhosin.post.max_array_depth = 5000
282
suhosin.post.max_array_index_length = 256
283
suhosin.post.max_vars = 5000
284
suhosin.post.max_value_length = 500000
285
suhosin.request.max_array_depth = 5000
286
suhosin.request.max_array_index_length = 256
287
suhosin.request.max_vars = 5000
288
suhosin.request.max_value_length = 500000
289
suhosin.memory_limit = 512435456
290

    
291
EOF
292

    
293

    
294
# Copy php.ini file to etc/ too (cli)
295
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
296

    
297
# Remove old log file if it exists.
298
if [ -f /var/run/php_modules_load_errors.txt ]; then
299
	/bin/rm /var/run/php_modules_load_errors.txt
300
fi 
301

    
302
for EXT in $PHPMODULES; do
303
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
304
done
305

    
306
# Check loaded modules and remove anything that did not load correctly
307
LOADED_MODULES=`/usr/local/bin/php -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\["`
308
for EXT in $PHPMODULESLC; do
309
	SHOULDREMOVE="true"
310
	for LM in $LOADED_MODULES; do
311
		if [ "$EXT" = "$LM" ]; then
312
			SHOULDREMOVE="false"
313
		fi		
314
	done
315
	# Handle low memory situations
316
	if [ "$LOWMEM" = "TRUE" ]; then
317
		if [ "$EXT" = "apc" ]; then
318
			SHOULDREMOVE="true"
319
		fi
320
		if [ "$EXT" = "xcache" ]; then
321
			SHOULDREMOVE="true"
322
		fi
323
	fi
324
	if [ "$SHOULDREMOVE" = "true" ]; then
325
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
326
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
327
			/bin/cat /usr/local/lib/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
328
			/bin/rm -f /usr/local/lib/php.ini
329
			/bin/mv /tmp/php.ini /usr/local/lib/php.ini
330
		fi
331
	fi
332
done
333

    
334
# Copy php.ini file to etc/ too (cli)
335
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
(82-82/107)