Project

General

Profile

Download (10 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_OPCACHE=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_OPCACHE ]; then
59
	LOWMEM="TRUE"
60
	echo ">>> Under $MIN_REALMEM_FOR_OPCACHE megabytes of ram detected.  Not enabling opcache"
61
	echo ">>> Under $MIN_REALMEM_FOR_OPCACHE megabytes of ram detected.  Not enabling opcache" | /usr/bin/logger -p daemon.info -i -t rc.php_ini_setup
62
else
63

    
64
	# Calculate opcache memory size according
65
	# to detected memory values
66
	if [ "$AVAILMEM" -gt "135" ]; then
67
		OPCACHEMEMSIZE="10"
68
	fi
69
	if [ "$AVAILMEM" -gt "256" ]; then
70
		OPCACHEMEMSIZE="20"
71
	fi
72
	if [ "$AVAILMEM" -gt "384" ]; then
73
		OPCACHEMEMSIZE="25"
74
	fi
75
	if [ "$AVAILMEM" -gt "512" ]; then
76
		OPCACHEMEMSIZE="30"
77
	fi
78
	if [ "$AVAILMEM" -gt "784" ]; then
79
		OPCACHEMEMSIZE="50"
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
# Config read/write
94
PHPMODULES="$PHPMODULES xml libxml dom"
95
PHPMODULES="$PHPMODULES SimpleXML xmlreader xmlwriter"
96
# Downloading via HTTP/FTP (pkg mgr, etc)
97
PHPMODULES="$PHPMODULES curl date"
98
# Internationalization
99
PHPMODULES="$PHPMODULES gettext"
100
# User manager
101
PHPMODULES="$PHPMODULES ldap openssl pcntl"
102
PHPMODULES="$PHPMODULES hash"
103
PHPMODULES="$PHPMODULES mcrypt"
104
# Regexs, PERL style!
105
PHPMODULES="$PHPMODULES pcre"
106
# The mighty posix!
107
PHPMODULES="$PHPMODULES posix"
108
PHPMODULES="$PHPMODULES readline"
109
# Login sessions
110
PHPMODULES="$PHPMODULES session"
111
# Extra sanity seatbelts
112
PHPMODULES="$PHPMODULES suhosin"
113
# Firewall rules edit
114
PHPMODULES="$PHPMODULES ctype"
115
# firewall_rules_edit.php
116
PHPMODULES="$PHPMODULES mbstring"
117
# Synchronization primitives
118
PHPMODULES="$PHPMODULES shmop"
119
# Page compression
120
PHPMODULES="$PHPMODULES zlib"
121
# SQLlite & Database
122
PHPMODULES="$PHPMODULES spl"
123
PHPMODULES="$PHPMODULES PDO"
124
PHPMODULES="$PHPMODULES sqlite3"
125
# RADIUS
126
PHPMODULES="$PHPMODULES radius"
127
# ZeroMQ
128
PHPMODULES="$PHPMODULES zmq"
129
# SSH2
130
PHPMODULES="$PHPMODULES ssh2"
131
# pfSense extensions
132
PHPMODULES="$PHPMODULES pfSense"
133
# json
134
PHPMODULES="$PHPMODULES json"
135
# bcmath
136
PHPMODULES="$PHPMODULES bcmath"
137
# filter
138
PHPMODULES="$PHPMODULES filter"
139

    
140
PHP_ZEND_MODULES=""
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-cgi -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
# Loop through and generate modules to load.
206
# Take into account modules built into php.
207
for EXT in $PHPMODULES; do
208
	SHOULDADD="true"
209
	# Check to see if module is compiled into php statically
210
	for LM in $LOADED_MODULES; do
211
		if [ "$EXT" = "$LM" ]; then
212
			SHOULDADD="false"
213
		fi
214
	done
215
	if [ "$SHOULDADD" = "true" ]; then
216
		# Ensure extension exists before adding.
217
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
218
			echo "extension=${EXT}.so" >> /usr/local/lib/php.ini
219
		fi
220
	fi
221
done
222

    
223
# Zend modules
224
for EXT in $PHP_ZEND_MODULES; do
225
	# Ensure extension exists before adding.
226
	if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
227
		echo "zend_extension=${EXT}.so" >> /usr/local/lib/php.ini
228
	fi
229
done
230

    
231
if [ "$LOWMEM" != "TRUE" ]; then
232

    
233
	/bin/cat >>/usr/local/lib/php.ini <<EOF
234

    
235
; opcache Settings
236
opcache.enabled="1"
237
opcache.enable_cli="0"
238
opcache.memory_consumption="${OPCACHEMEMSIZE}"
239

    
240
EOF
241
else
242
	/bin/cat >>/usr/local/lib/php.ini <<EOF
243
; opcache Settings
244
opcache.enabled="0"
245
EOF
246
fi
247

    
248
	/bin/cat >>/usr/local/lib/php.ini <<EOF
249

    
250
[suhosin]
251
suhosin.get.max_array_depth = 5000
252
suhosin.get.max_array_index_length = 256
253
suhosin.get.max_vars = 5000
254
suhosin.get.max_value_length = 500000
255
suhosin.post.max_array_depth = 5000
256
suhosin.post.max_array_index_length = 256
257
suhosin.post.max_vars = 5000
258
suhosin.post.max_value_length = 500000
259
suhosin.request.max_array_depth = 5000
260
suhosin.request.max_array_index_length = 256
261
suhosin.request.max_vars = 5000
262
suhosin.request.max_value_length = 500000
263
suhosin.memory_limit = 512435456
264

    
265
EOF
266

    
267

    
268
PHPFPMMAX=3
269
if [ $REALMEM -lt 250 ]; then
270
	PHPFPMMAX=2
271
elif [ ${REALMEM} -gt 1000 ]; then
272
	PHPFPMMAX=4
273
fi
274

    
275
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
276

    
277
[global]
278
pid = run/php-fpm.pid
279
error_log=syslog
280
syslog.facility = daemon
281
syslog.ident = system
282
log_level = error
283
daemonize = yes
284
events.mechanism = kqueue
285
process.max = ${PHPFPMMAX}
286

    
287
[lighty]
288
user = root
289
group = wheel
290
;mode = 0600
291

    
292
listen = /var/run/php-fpm.socket
293
listen.owner = root
294
listen.group = wheel
295
listen.mode = 0600
296

    
297
security.limit_extensions =
298

    
299
; Pass environment variables
300
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
301
env[LOGNAME] = root
302

    
303
EOF
304

    
305
if [ $REALMEM -lt 350 ]; then
306
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
307

    
308
pm = ondemand
309
pm.process_idle_timeout = 5
310
pm.max_children = $PHPFPMMAX
311
pm.max_requests = 500
312

    
313
EOF
314

    
315
elif [ $REALMEM -gt 1000 ]; then
316
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
317

    
318
pm = dynamic
319
pm.process_idle_timeout = 5
320
pm.max_children = $PHPFPMMAX
321
pm.start_servers = 1
322
pm.max_requests = 500
323
pm.min_spare_servers=1
324
pm.max_spare_servers=1
325

    
326
EOF
327
else
328

    
329
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
330

    
331
pm = static
332
pm.max_children = $PHPFPMMAX
333
pm.max_requests = 500
334

    
335
EOF
336

    
337
fi
338

    
339
# Copy php.ini file to etc/ too (cli)
340
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
341

    
342
# Remove old log file if it exists.
343
if [ -f /var/run/php_modules_load_errors.txt ]; then
344
	/bin/rm /var/run/php_modules_load_errors.txt
345
fi
346

    
347
for EXT in $PHPMODULES; do
348
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
349
done
350

    
351
# Check loaded modules and remove anything that did not load correctly
352
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\["`
353
for EXT in $PHPMODULESLC; do
354
	SHOULDREMOVE="true"
355
	for LM in $LOADED_MODULES; do
356
		if [ "$EXT" = "$LM" ]; then
357
			SHOULDREMOVE="false"
358
		fi
359
	done
360
	# Handle low memory situations
361
	if [ "$LOWMEM" = "TRUE" ]; then
362
		if [ "$EXT" = "opcache" ]; then
363
			SHOULDREMOVE="true"
364
		fi
365
		if [ "$EXT" = "xcache" ]; then
366
			SHOULDREMOVE="true"
367
		fi
368
	fi
369
	if [ "$SHOULDREMOVE" = "true" ]; then
370
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
371
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
372
			/bin/cat /usr/local/lib/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
373
			/bin/rm -f /usr/local/lib/php.ini
374
			/bin/mv /tmp/php.ini /usr/local/lib/php.ini
375
		fi
376
	fi
377
done
378

    
379
# Copy php.ini file to etc/ too (cli)
380
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
(83-83/105)