Projet

Général

Profil

Télécharger (11 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / rc.php_ini_setup @ 75b8eb83

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/20121212 ]; then
33
	EXTENSIONSDIR="/usr/local/lib/php/20121212/"
34
elif [ -d /usr/local/lib/php/20090626 ]; then
35
	EXTENSIONSDIR="/usr/local/lib/php/20090626/"
36
else
37
	EXTENSIONSDIR="/usr/local/lib/php/20060613/"
38
fi
39

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

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

    
52

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

    
58
if [  ${REALMEM} -lt $MIN_REALMEM_FOR_APC ]; then
59
	LOWMEM="TRUE"
60
	echo ">>> Under $MIN_REALMEM_FOR_APC megabytes of ram detected.  Not enabling APC."
61
	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
62
else
63

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

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

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

    
141
PHP_ZEND_MODULES="ioncube_loader"
142
PHP_ZEND_MODULES_TS="ioncube_loader_ts"
143

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

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

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

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

    
200
; Extensions
201

    
202
EOF
203

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

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

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

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

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

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

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

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

    
270

    
271
if [ "$LOWMEM" != "TRUE" ]; then
272

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

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

    
280
EOF
281
fi
282

    
283
	/bin/cat >>/usr/local/lib/php.ini <<EOF
284

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

    
300
EOF
301

    
302

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

    
310
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
311

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

    
322
[lighty]
323
user = root
324
group = wheel
325
;mode = 0600
326

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

    
332
security.limit_extensions =
333

    
334
; Pass environment variables
335
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
336
env[LOGNAME] = root
337

    
338
EOF
339

    
340
if [ $REALMEM -lt 350 ]; then
341
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
342

    
343
pm = ondemand
344
pm.process_idle_timeout = 5
345
pm.max_children = $PHPFPMMAX
346
pm.max_requests = 500
347

    
348
EOF
349

    
350
elif [ $REALMEM -gt 1000 ]; then
351
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
352

    
353
pm = dynamic
354
pm.process_idle_timeout = 5
355
pm.max_children = $PHPFPMMAX
356
pm.start_servers = 1
357
pm.max_requests = 500
358
pm.min_spare_servers=1
359
pm.max_spare_servers=1
360

    
361
EOF
362
else
363

    
364
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
365

    
366
pm = static
367
pm.max_children = $PHPFPMMAX
368
pm.max_requests = 500
369

    
370
EOF
371

    
372
fi
373

    
374
# Copy php.ini file to etc/ too (cli)
375
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
376

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

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

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

    
414
# Copy php.ini file to etc/ too (cli)
415
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
(81-81/104)