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
max_input_time = 1800
195
max_input_vars = 5000
196
register_argc_argv = On
197
register_long_arrays = Off
198
variables_order = "GPCS"
199
file_uploads = On
200
upload_tmp_dir = ${UPLOADTMPDIR}
201
upload_max_filesize = 200M
202
post_max_size = 200M
203
html_errors = Off
204
zlib.output_compression = Off
205
zlib.output_compression_level = 1
206
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg:/usr/local/www/classes:/usr/local/www/classes/Form"
207
display_startup_errors=on
208
display_errors=on
209
log_errors=on
210
error_log=/tmp/PHP_errors.log
211
extension_dir=${EXTENSIONSDIR}
212
date.timezone="${TIMEZONE}"
213
session.hash_bits_per_character = 5
214
session.hash_function = 1
215
${error_reporting}
216

    
217
; Extensions
218

    
219
EOF
220

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

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

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

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

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

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

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

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

    
278
EOF
279

    
280

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

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

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

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

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

    
310
security.limit_extensions =
311

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

    
316
EOF
317

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

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

    
326
EOF
327

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

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

    
339
EOF
340
else
341

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

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

    
348
EOF
349

    
350
fi
351

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

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

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