Projet

Général

Profil

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

univnautes / etc / rc.php_ini_setup @ c28da0a7

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
# filter
141
PHPMODULES="$PHPMODULES filter"
142

    
143
PHP_ZEND_MODULES="ioncube_loader"
144
PHP_ZEND_MODULES_TS="ioncube_loader_ts"
145

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

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

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

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

    
202
; Extensions
203

    
204
EOF
205

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

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

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

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

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

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

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

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

    
272

    
273
if [ "$LOWMEM" != "TRUE" ]; then
274

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

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

    
282
EOF
283
fi
284

    
285
	/bin/cat >>/usr/local/lib/php.ini <<EOF
286

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

    
302
EOF
303

    
304

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

    
312
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
313

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

    
324
[lighty]
325
user = root
326
group = wheel
327
;mode = 0600
328

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

    
334
security.limit_extensions =
335

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

    
340
EOF
341

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

    
345
pm = ondemand
346
pm.process_idle_timeout = 5
347
pm.max_children = $PHPFPMMAX
348
pm.max_requests = 500
349

    
350
EOF
351

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

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

    
363
EOF
364
else
365

    
366
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
367

    
368
pm = static
369
pm.max_children = $PHPFPMMAX
370
pm.max_requests = 500
371

    
372
EOF
373

    
374
fi
375

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

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

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

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

    
416
# Copy php.ini file to etc/ too (cli)
417
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
(80-80/102)