Project

General

Profile

Download (9.82 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
EXTENSIONSDIR="/usr/local/lib/php/20131226/"
33

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

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

    
46

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

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

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

    
77
/usr/bin/sort -u /usr/local/etc/php/extensions.ini > /tmp/extensions.ini
78
/bin/mv /tmp/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

    
137
PHP_ZEND_MODULES=""
138

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

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

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

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

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

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

    
208
; Extensions
209

    
210
EOF
211

    
212
# Loop through and generate modules to load.
213
# Take into account modules built into php.
214
for EXT in $PHPMODULES; do
215
	SHOULDADD="true"
216
	# Check to see if module is compiled into php statically
217
	for LM in $LOADED_MODULES; do
218
		if [ "$EXT" = "$LM" ]; then
219
			SHOULDADD="false"
220
		fi
221
	done
222
	if [ "$SHOULDADD" = "true" ]; then
223
		# Ensure extension exists before adding.
224
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
225
			echo "extension=${EXT}.so" >> /usr/local/etc/php.ini
226
		fi
227
	fi
228
done
229

    
230
# Zend modules
231
for EXT in $PHP_ZEND_MODULES; do
232
	# Ensure extension exists before adding.
233
	if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
234
		echo "zend_extension=${EXT}.so" >> /usr/local/etc/php.ini
235
	fi
236
done
237

    
238
if [ "$LOWMEM" != "TRUE" ]; then
239

    
240
	/bin/cat >>/usr/local/etc/php.ini <<EOF
241

    
242
; opcache Settings
243
opcache.enabled="1"
244
opcache.enable_cli="0"
245
opcache.memory_consumption="${OPCACHEMEMSIZE}"
246

    
247
EOF
248
else
249
	/bin/cat >>/usr/local/etc/php.ini <<EOF
250
; opcache Settings
251
opcache.enabled="0"
252
EOF
253
fi
254

    
255
	/bin/cat >>/usr/local/etc/php.ini <<EOF
256

    
257
[suhosin]
258
suhosin.get.max_array_index_length = 256
259
suhosin.get.max_vars = 5000
260
suhosin.get.max_value_length = 500000
261
suhosin.post.max_array_index_length = 256
262
suhosin.post.max_vars = 5000
263
suhosin.post.max_value_length = 500000
264
suhosin.request.max_array_index_length = 256
265
suhosin.request.max_vars = 5000
266
suhosin.request.max_value_length = 500000
267
suhosin.memory_limit = 512435456
268

    
269
EOF
270

    
271

    
272
PHPFPMMAX=3
273
if [ $REALMEM -lt 250 ]; then
274
	PHPFPMMAX=2
275
elif [ ${REALMEM} -gt 1000 ]; then
276
	PHPFPMMAX=4
277
fi
278

    
279
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
280

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

    
291
[nginx]
292
user = root
293
group = wheel
294
;mode = 0600
295

    
296
listen = /var/run/php-fpm.socket
297
listen.owner = root
298
listen.group = wheel
299
listen.mode = 0600
300

    
301
security.limit_extensions =
302

    
303
; Pass environment variables
304
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
305
env[LOGNAME] = root
306

    
307
EOF
308

    
309
if [ $REALMEM -lt 350 ]; then
310
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
311

    
312
pm = ondemand
313
pm.process_idle_timeout = 5
314
pm.max_children = $PHPFPMMAX
315
pm.max_requests = 500
316

    
317
EOF
318

    
319
elif [ $REALMEM -gt 1000 ]; then
320
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
321

    
322
pm = dynamic
323
pm.process_idle_timeout = 5
324
pm.max_children = $PHPFPMMAX
325
pm.start_servers = 1
326
pm.max_requests = 500
327
pm.min_spare_servers=1
328
pm.max_spare_servers=1
329

    
330
EOF
331
else
332

    
333
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
334

    
335
pm = static
336
pm.max_children = $PHPFPMMAX
337
pm.max_requests = 500
338

    
339
EOF
340

    
341
fi
342

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

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

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