Project

General

Profile

Download (10.9 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
MIN_REALMEM_FOR_APC=512
31

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

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

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

    
50

    
51
# Get amount of ram installed on this system
52
REALMEM=`/sbin/sysctl hw.realmem | /usr/bin/awk '{print $2/1048576}' | /usr/bin/awk -F '.' '{print $1}'`
53
export REALMEM
54
export LOWMEM
55

    
56
if [  ${REALMEM} -lt $MIN_REALMEM_FOR_APC ]; then
57
	LOWMEM="TRUE"
58
	echo ">>> Under $MIN_REALMEM_FOR_APC megabytes of ram detected.  Not enabling APC."
59
	echo ">>> Under $MIN_REALMEM_FOR_APC megabytes of ram detected.  Not enabling APC." | /usr/bin/logger -p daemon.info -i -t rc.php_ini_setup
60
else
61

    
62
	# Calculate APC SHM size according 
63
	# to detected memory values
64
	if [ "$AVAILMEM" -gt "135" ]; then
65
		APCSHMEMSIZE="10M"
66
	fi
67
	if [ "$AVAILMEM" -gt "256" ]; then
68
		APCSHMEMSIZE="20M"
69
	fi
70
	if [ "$AVAILMEM" -gt "384" ]; then
71
		APCSHMEMSIZE="25M"
72
	fi
73
	if [ "$AVAILMEM" -gt "512" ]; then
74
		APCSHMEMSIZE="30M"
75
	fi
76
	if [ "$AVAILMEM" -gt "784" ]; then
77
		APCSHMEMSIZE="50M"
78
	fi
79
fi
80

    
81
# Set upload directory
82
if [ "$PLATFORM" = "embedded" -o "$PLATFORM" = "nanobsd" ]; then
83
	UPLOADTMPDIR=`/usr/bin/grep upload_path /etc/inc/globals.inc | /usr/bin/cut -d'"' -f4`
84
else 
85
	UPLOADTMPDIR="/tmp"
86
fi
87

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

    
139
PHP_ZEND_MODULES="ioncube_loader"
140
PHP_ZEND_MODULES_TS="ioncube_loader_ts"
141

    
142
# Modules previously included.
143
# can be turned on by touching
144
# /etc/php_dynamodules/$modulename
145
#	sysvmsg \
146
#	sysvsem \
147
#	sysvshm \
148
#	bcmath \
149
#	tokenizer \
150
#	uploadprogress \
151
#	sockets \
152
#	Reflection \
153
#	mysql \
154
#	bz2	\
155

    
156
# Clear the .ini file to make sure we are clean
157
if [ -f /usr/local/etc/php.ini ]; then
158
	/bin/rm /usr/local/etc/php.ini
159
fi
160
if [ -f /usr/local/lib/php.ini ]; then
161
	/bin/rm /usr/local/lib/php.ini
162
fi
163
LOADED_MODULES=`/usr/local/bin/php -m | /usr/bin/grep -v "\["`
164

    
165
# Fetch the timezone from the XML and set it here. We set it later too in the running scripts
166
TIMEZONE=`cat /conf/config.xml | egrep -E '<timezone>(.*?)</timezone>' | awk -F'>' '{print $2}'|awk -F'<' '{print $1}'`
167

    
168
# Get a loaded module list in the stock php
169
# Populate a dummy php.ini to avoid
170
# the file being clobbered and the firewall
171
# not being able to boot back up.
172
/bin/cat >/usr/local/lib/php.ini <<EOF
173
; File generated from /etc/rc.php_ini_setup
174
output_buffering = "0"
175
expose_php = Off
176
implicit_flush = true
177
magic_quotes_gpc = Off
178
max_execution_time = 900
179
max_input_time = 1800
180
register_argc_argv = On
181
register_long_arrays = Off
182
variables_order = "GPCS"
183
file_uploads = On
184
upload_tmp_dir = ${UPLOADTMPDIR}
185
upload_max_filesize = 200M
186
post_max_size = 200M
187
html_errors = Off
188
zlib.output_compression = Off
189
zlib.output_compression_level = 1
190
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg"
191
display_startup_errors=on
192
display_errors=on
193
log_errors=on
194
error_log=/tmp/PHP_errors.log
195
extension_dir=${EXTENSIONSDIR}
196
date.timezone="${TIMEZONE}"
197

    
198
; Extensions
199

    
200
EOF
201

    
202
# Copy php.ini file to etc/ too (cli)
203
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
204

    
205
# Ensure directory exists
206
if [ ! -d /etc/php_dynamodules ]; then
207
	/bin/mkdir /etc/php_dynamodules
208
fi
209
if [ ! -d /etc/php_dynamodules_zend ]; then
210
	/bin/mkdir /etc/php_dynamodules_zend
211
fi
212
if [ ! -d /etc/php_dynamodules_zend_ts ]; then
213
	/bin/mkdir /etc/php_dynamodules_zend_ts
214
fi
215

    
216
# Read in dynamodules
217
if [ -d /etc/php_dynamodules ]; then
218
	DYNA_MODULES=`/bin/ls -Utr /etc/php_dynamodules/`
219
	PHPMODULES="$PHPMODULES $DYNA_MODULES"
220
fi
221

    
222
# Read in zend modules
223
if [ -d /etc/php_dynamodules_zend ]; then
224
	DYNA_MODULES=`/bin/ls /etc/php_dynamodules_zend/`
225
	PHP_ZEND_MODULES="$PHP_ZEND_MODULES $DYNA_MODULES"
226
fi
227

    
228
# Read in zend threaded modules
229
if [ -d /etc/php_dynamodules_zend_ts ]; then
230
	DYNA_MODULES=`/bin/ls /etc/php_dynamodules_zend_ts/`
231
	PHP_ZEND_MODULES_TS="$PHP_ZEND_MODULES_TS $DYNA_MODULES"
232
fi
233

    
234
# Loop through and generate modules to load.
235
# Take into account modules built into php.
236
for EXT in $PHPMODULES; do
237
	SHOULDADD="true"
238
	# Check to see if module is compiled into php statically
239
	for LM in $LOADED_MODULES; do
240
		if [ "$EXT" = "$LM" ]; then
241
			SHOULDADD="false"
242
		fi
243
	done
244
	if [ "$SHOULDADD" = "true" ]; then
245
		# Ensure extension exists before adding.
246
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
247
			echo "extension=${EXT}.so" >> /usr/local/lib/php.ini
248
		fi
249
	fi
250
done
251

    
252
# Zend modules
253
for EXT in $PHP_ZEND_MODULES; do
254
	# Ensure extension exists before adding.
255
	if [ -f "${EXTENSIONSDIR}/ioncube/${EXT}.so" ]; then
256
		echo "zend_extension=${EXTENSIONSDIR}/ioncube/${EXT}.so" >> /usr/local/lib/php.ini
257
	fi
258
done
259

    
260
# Zend threaded modules
261
for EXT in $PHP_ZEND_MODULES_TS; do
262
	# Ensure extension exists before adding.
263
	if [ -f "${EXTENSIONSDIR}/ioncube/${EXT}.so" ]; then
264
		echo "zend_extension_ts=${EXTENSIONSDIR}/ioncube/${EXT}.so" >> /usr/local/lib/php.ini
265
	fi
266
done
267

    
268

    
269
if [ "$LOWMEM" != "TRUE" ]; then
270

    
271
	/bin/cat >>/usr/local/lib/php.ini <<EOF
272

    
273
; APC Settings
274
apc.enabled="1"
275
apc.enable_cli="0"
276
apc.shm_size="${APCSHMEMSIZE}"
277

    
278
EOF
279
fi
280

    
281
	/bin/cat >>/usr/local/lib/php.ini <<EOF
282

    
283
[suhosin]
284
suhosin.get.max_array_depth = 5000
285
suhosin.get.max_array_index_length = 256
286
suhosin.get.max_vars = 5000
287
suhosin.get.max_value_length = 500000
288
suhosin.post.max_array_depth = 5000
289
suhosin.post.max_array_index_length = 256
290
suhosin.post.max_vars = 5000
291
suhosin.post.max_value_length = 500000
292
suhosin.request.max_array_depth = 5000
293
suhosin.request.max_array_index_length = 256
294
suhosin.request.max_vars = 5000
295
suhosin.request.max_value_length = 500000
296
suhosin.memory_limit = 512435456
297

    
298
EOF
299

    
300

    
301
PHPFPMMAX=3
302
if [ $REALMEM -lt 250 ]; then
303
	PHPFPMMAX=2
304
elif [ ${REALMEM} -gt 1000 ]; then
305
	PHPFPMMAX=4
306
fi
307

    
308
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
309

    
310
[global]
311
pid = run/php-fpm.pid
312
error_log=syslog
313
syslog.facility = daemon
314
syslog.ident = system
315
log_level = notice
316
daemonize = yes
317
events.mechanism = kqueue
318
process.max = ${PHPFPMMAX}
319

    
320
[lighty]
321
user = root
322
group = wheel
323
;mode = 0600
324

    
325
listen = /var/run/php-fpm.socket
326
listen.owner = root
327
listen.group = wheel
328
listen.mode = 0600
329

    
330
security.limit_extensions =
331

    
332
; Pass environment variables
333
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
334

    
335
EOF
336

    
337
if [ $REALMEM -lt 350 ]; then
338
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
339

    
340
pm = ondemand
341
pm.process_idle_timeout = 5
342
pm.max_children = $PHPFPMMAX
343
pm.max_requests = 500
344

    
345
EOF
346

    
347
elif [ $REALMEM -gt 1000 ]; then
348
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
349

    
350
pm = dynamic
351
pm.process_idle_timeout = 5
352
pm.max_children = $PHPFPMMAX
353
pm.start_servers = 1
354
pm.max_requests = 500
355
pm.min_spare_servers=1
356
pm.max_spare_servers=1
357

    
358
EOF
359
else
360

    
361
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
362

    
363
pm = static
364
pm.max_children = $PHPFPMMAX
365
pm.max_requests = 500
366

    
367
EOF
368

    
369
fi
370

    
371
# Copy php.ini file to etc/ too (cli)
372
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
373

    
374
# Remove old log file if it exists.
375
if [ -f /var/run/php_modules_load_errors.txt ]; then
376
	/bin/rm /var/run/php_modules_load_errors.txt
377
fi 
378

    
379
for EXT in $PHPMODULES; do
380
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
381
done
382

    
383
# Check loaded modules and remove anything that did not load correctly
384
LOADED_MODULES=`/usr/local/bin/php -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\["`
385
for EXT in $PHPMODULESLC; do
386
	SHOULDREMOVE="true"
387
	for LM in $LOADED_MODULES; do
388
		if [ "$EXT" = "$LM" ]; then
389
			SHOULDREMOVE="false"
390
		fi		
391
	done
392
	# Handle low memory situations
393
	if [ "$LOWMEM" = "TRUE" ]; then
394
		if [ "$EXT" = "apc" ]; then
395
			SHOULDREMOVE="true"
396
		fi
397
		if [ "$EXT" = "xcache" ]; then
398
			SHOULDREMOVE="true"
399
		fi
400
	fi
401
	if [ "$SHOULDREMOVE" = "true" ]; then
402
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
403
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
404
			/bin/cat /usr/local/lib/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
405
			/bin/rm -f /usr/local/lib/php.ini
406
			/bin/mv /tmp/php.ini /usr/local/lib/php.ini
407
		fi
408
	fi
409
done
410

    
411
# Copy php.ini file to etc/ too (cli)
412
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
(86-86/111)