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
VERSION=`/bin/cat /etc/version`
31
MIN_REALMEM_FOR_OPCACHE=512
32

    
33
EXTENSIONSDIR="/usr/local/lib/php/20131226/"
34

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

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

    
47

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

    
53
if [  ${REALMEM} -lt $MIN_REALMEM_FOR_OPCACHE ]; then
54
	LOWMEM="TRUE"
55
	echo ">>> Under $MIN_REALMEM_FOR_OPCACHE megabytes of ram detected.  Not enabling opcache"
56
	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
57
else
58

    
59
	# Calculate opcache memory size according
60
	# to detected memory values
61
	if [ "$AVAILMEM" -gt "135" ]; then
62
		OPCACHEMEMSIZE="10"
63
	fi
64
	if [ "$AVAILMEM" -gt "256" ]; then
65
		OPCACHEMEMSIZE="20"
66
	fi
67
	if [ "$AVAILMEM" -gt "384" ]; then
68
		OPCACHEMEMSIZE="25"
69
	fi
70
	if [ "$AVAILMEM" -gt "512" ]; then
71
		OPCACHEMEMSIZE="30"
72
	fi
73
	if [ "$AVAILMEM" -gt "784" ]; then
74
		OPCACHEMEMSIZE="50"
75
	fi
76
fi
77

    
78
/usr/bin/sort -u -o /usr/local/etc/php/extensions.ini /usr/local/etc/php/extensions.ini
79

    
80
# Set upload directory
81
if [ "$PLATFORM" = "nanobsd" ]; then
82
	UPLOADTMPDIR=$(/usr/local/sbin/read_global_var upload_path "/root")
83
else
84
	UPLOADTMPDIR="/tmp"
85
fi
86

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

    
139
PHP_ZEND_MODULES=""
140

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

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

    
161
unset TIMEZONE
162
# Fetch the timezone from /var/db/zoneinfo if present
163
if [ -f /var/db/zoneinfo ]; then
164
	TIMEZONE=$(cat /var/db/zoneinfo)
165
fi
166

    
167
if [ -z "${TIMEZONE}" ]; then
168
	# Second option is from config.xml
169
	TIMEZONE=$(/usr/local/sbin/read_xml_tag.sh string system/timezone)
170
fi
171

    
172
if [ -z "${TIMEZONE}" ]; then
173
	# Last option, use default value from $g or Etc/UTC
174
	TIMEZONE=$(/usr/local/sbin/read_global_var default_timezone "Etc/UTC")
175
fi
176

    
177
if echo "${VERSION}" | grep -q RELEASE; then
178
	error_reporting="error_reporting = E_ERROR | E_PARSE"
179
else
180
	error_reporting=""
181
fi
182

    
183
# Get a loaded module list in the stock php
184
# Populate a dummy php.ini to avoid
185
# the file being clobbered and the firewall
186
# not being able to boot back up.
187
/bin/cat >/usr/local/etc/php.ini <<EOF
188
; File generated from /etc/rc.php_ini_setup
189
output_buffering = "0"
190
expose_php = Off
191
implicit_flush = true
192
magic_quotes_gpc = Off
193
max_execution_time = 900
194
request_terminate_timeout = 900
195
max_input_time = 1800
196
max_input_vars = 5000
197
register_argc_argv = On
198
register_long_arrays = Off
199
variables_order = "GPCS"
200
file_uploads = On
201
upload_tmp_dir = ${UPLOADTMPDIR}
202
upload_max_filesize = 200M
203
post_max_size = 200M
204
html_errors = Off
205
zlib.output_compression = Off
206
zlib.output_compression_level = 1
207
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg:/usr/local/www/classes:/usr/local/www/classes/Form"
208
display_startup_errors=on
209
display_errors=on
210
log_errors=on
211
error_log=/tmp/PHP_errors.log
212
extension_dir=${EXTENSIONSDIR}
213
date.timezone="${TIMEZONE}"
214
session.hash_bits_per_character = 5
215
session.hash_function = 1
216
${error_reporting}
217

    
218
; Extensions
219

    
220
EOF
221

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

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

    
248
if [ "$LOWMEM" != "TRUE" ]; then
249

    
250
	/bin/cat >>/usr/local/etc/php.ini <<EOF
251

    
252
; opcache Settings
253
opcache.enabled="1"
254
opcache.enable_cli="0"
255
opcache.memory_consumption="${OPCACHEMEMSIZE}"
256

    
257
EOF
258
else
259
	/bin/cat >>/usr/local/etc/php.ini <<EOF
260
; opcache Settings
261
opcache.enabled="0"
262
EOF
263
fi
264

    
265
	/bin/cat >>/usr/local/etc/php.ini <<EOF
266

    
267
[suhosin]
268
suhosin.get.max_array_index_length = 256
269
suhosin.get.max_vars = 5000
270
suhosin.get.max_value_length = 500000
271
suhosin.post.max_array_index_length = 256
272
suhosin.post.max_vars = 5000
273
suhosin.post.max_value_length = 500000
274
suhosin.request.max_array_index_length = 256
275
suhosin.request.max_vars = 5000
276
suhosin.request.max_value_length = 500000
277
suhosin.memory_limit = 536870912
278

    
279
EOF
280

    
281

    
282
PHPFPMMAX=3
283
if [ $REALMEM -lt 250 ]; then
284
	PHPFPMMAX=2
285
elif [ ${REALMEM} -gt 1000 ]; then
286
	PHPFPMMAX=4
287
fi
288

    
289
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
290

    
291
[global]
292
pid = run/php-fpm.pid
293
error_log=syslog
294
syslog.facility = daemon
295
syslog.ident = system
296
log_level = error
297
daemonize = yes
298
events.mechanism = kqueue
299
process.max = ${PHPFPMMAX}
300

    
301
[nginx]
302
user = root
303
group = wheel
304
;mode = 0600
305

    
306
listen = /var/run/php-fpm.socket
307
listen.owner = root
308
listen.group = wheel
309
listen.mode = 0600
310

    
311
security.limit_extensions =
312

    
313
; Pass environment variables
314
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
315
env[LOGNAME] = root
316

    
317
EOF
318

    
319
if [ $REALMEM -lt 350 ]; then
320
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
321

    
322
pm = ondemand
323
pm.process_idle_timeout = 5
324
pm.max_children = $PHPFPMMAX
325
pm.max_requests = 500
326

    
327
EOF
328

    
329
elif [ $REALMEM -gt 1000 ]; then
330
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
331

    
332
pm = dynamic
333
pm.process_idle_timeout = 5
334
pm.max_children = $PHPFPMMAX
335
pm.start_servers = 1
336
pm.max_requests = 500
337
pm.min_spare_servers=1
338
pm.max_spare_servers=1
339

    
340
EOF
341
else
342

    
343
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
344

    
345
pm = static
346
pm.max_children = $PHPFPMMAX
347
pm.max_requests = 500
348

    
349
EOF
350

    
351
fi
352

    
353
# Remove old log file if it exists.
354
if [ -f /var/run/php_modules_load_errors.txt ]; then
355
	/bin/rm /var/run/php_modules_load_errors.txt
356
fi
357

    
358
for EXT in $PHPMODULES; do
359
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
360
done
361

    
362
# Check loaded modules and remove anything that did not load correctly
363
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\["`
364
for EXT in $PHPMODULESLC; do
365
	SHOULDREMOVE="true"
366
	for LM in $LOADED_MODULES; do
367
		if [ "$EXT" = "$LM" ]; then
368
			SHOULDREMOVE="false"
369
			break
370
		fi
371
	done
372
	# Handle low memory situations
373
	if [ "$LOWMEM" = "TRUE" ]; then
374
		if [ "$EXT" = "opcache" ]; then
375
			SHOULDREMOVE="true"
376
		fi
377
		if [ "$EXT" = "xcache" ]; then
378
			SHOULDREMOVE="true"
379
		fi
380
	fi
381
	if [ "$SHOULDREMOVE" = "true" ]; then
382
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
383
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
384
			/bin/cat /usr/local/etc/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
385
			/bin/rm -f /usr/local/etc/php.ini
386
			/bin/mv /tmp/php.ini /usr/local/etc/php.ini
387
		fi
388
	fi
389
done
(74-74/94)