Project

General

Profile

Download (11 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# rc.php_ini_setup
4
#
5
# part of pfSense (https://www.pfsense.org)
6
# Copyright (c) 2014-2016 Rubicon Communications, LLC (Netgate)
7
# All rights reserved.
8
#
9
# Redistribution and use in source and binary forms, with or without
10
# modification, are permitted provided that the following conditions are met:
11
#
12
# 1. Redistributions of source code must retain the above copyright notice,
13
#    this list of conditions and the following disclaimer.
14
#
15
# 2. Redistributions in binary form must reproduce the above copyright
16
#    notice, this list of conditions and the following disclaimer in
17
#    the documentation and/or other materials provided with the
18
#    distribution.
19
#
20
# 3. All advertising materials mentioning features or use of this software
21
#    must display the following acknowledgment:
22
#    "This product includes software developed by the pfSense Project
23
#    for use in the pfSense® software distribution. (http://www.pfsense.org/).
24
#
25
# 4. The names "pfSense" and "pfSense Project" must not be used to
26
#    endorse or promote products derived from this software without
27
#    prior written permission. For written permission, please contact
28
#    coreteam@pfsense.org.
29
#
30
# 5. Products derived from this software may not be called "pfSense"
31
#    nor may "pfSense" appear in their names without prior written
32
#    permission of the Electric Sheep Fencing, LLC.
33
#
34
# 6. Redistributions of any form whatsoever must retain the following
35
#    acknowledgment:
36
#
37
# "This product includes software developed by the pfSense Project
38
# for use in the pfSense software distribution (http://www.pfsense.org/).
39
#
40
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
# OF THE POSSIBILITY OF SUCH DAMAGE.
52
#
53

    
54
# Set our operating platform
55
PLATFORM=`/bin/cat /etc/platform`
56
VERSION=`/bin/cat /etc/version`
57
MIN_REALMEM_FOR_OPCACHE=512
58

    
59
EXTENSIONSDIR="/usr/local/lib/php/20131226/"
60

    
61
# Grab amount of memory that is detected
62
if [ -f /var/log/dmesg.boot ]; then
63
	AVAILMEM=`/bin/cat /var/log/dmesg.boot |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
64
else
65
	AVAILMEM=`/sbin/dmesg -a |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
66
fi
67

    
68
if [ -z "$AVAILMEM" ]; then
69
	MEM=`/sbin/sysctl hw.physmem | cut -d':' -f2`
70
	AVAILMEM=`/bin/expr $MEM / 1048576`
71
fi
72

    
73

    
74
# Get amount of ram installed on this system
75
REALMEM=`/sbin/sysctl hw.realmem | /usr/bin/awk '{print $2/1048576}' | /usr/bin/awk -F '.' '{print $1}'`
76
export REALMEM
77
export LOWMEM
78

    
79
if [  ${REALMEM} -lt $MIN_REALMEM_FOR_OPCACHE ]; then
80
	LOWMEM="TRUE"
81
	echo ">>> Under $MIN_REALMEM_FOR_OPCACHE megabytes of ram detected.  Not enabling opcache"
82
	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
83
else
84

    
85
	# Calculate opcache memory size according
86
	# to detected memory values
87
	if [ "$AVAILMEM" -gt "135" ]; then
88
		OPCACHEMEMSIZE="10"
89
	fi
90
	if [ "$AVAILMEM" -gt "256" ]; then
91
		OPCACHEMEMSIZE="20"
92
	fi
93
	if [ "$AVAILMEM" -gt "384" ]; then
94
		OPCACHEMEMSIZE="25"
95
	fi
96
	if [ "$AVAILMEM" -gt "512" ]; then
97
		OPCACHEMEMSIZE="30"
98
	fi
99
	if [ "$AVAILMEM" -gt "784" ]; then
100
		OPCACHEMEMSIZE="50"
101
	fi
102
fi
103

    
104
# Set upload directory
105
if [ "$PLATFORM" = "nanobsd" ]; then
106
	UPLOADTMPDIR=$(/usr/local/sbin/read_global_var upload_path "/root")
107
else
108
	UPLOADTMPDIR="/tmp"
109
fi
110

    
111
# Define php modules.  Do not add .so, it will
112
# be done automatically by the script below.
113
PHPMODULES="standard"
114
# Config read/write
115
PHPMODULES="$PHPMODULES xml libxml dom"
116
PHPMODULES="$PHPMODULES SimpleXML xmlreader xmlwriter"
117
# Downloading via HTTP/FTP (pkg mgr, etc)
118
PHPMODULES="$PHPMODULES curl date"
119
# Internationalization
120
PHPMODULES="$PHPMODULES gettext"
121
# User manager
122
PHPMODULES="$PHPMODULES ldap openssl pcntl"
123
PHPMODULES="$PHPMODULES hash"
124
PHPMODULES="$PHPMODULES mcrypt"
125
# Regexs, PERL style!
126
PHPMODULES="$PHPMODULES pcre"
127
# The mighty posix!
128
PHPMODULES="$PHPMODULES posix"
129
PHPMODULES="$PHPMODULES readline"
130
# Login sessions
131
PHPMODULES="$PHPMODULES session"
132
# Extra sanity seatbelts
133
PHPMODULES="$PHPMODULES suhosin"
134
# Firewall rules edit
135
PHPMODULES="$PHPMODULES ctype"
136
# firewall_rules_edit.php
137
PHPMODULES="$PHPMODULES mbstring"
138
# Synchronization primitives
139
PHPMODULES="$PHPMODULES shmop"
140
# Page compression
141
PHPMODULES="$PHPMODULES zlib"
142
# SQLlite & Database
143
PHPMODULES="$PHPMODULES spl"
144
PHPMODULES="$PHPMODULES PDO"
145
PHPMODULES="$PHPMODULES sqlite3"
146
# RADIUS
147
PHPMODULES="$PHPMODULES radius"
148
# ZeroMQ
149
PHPMODULES="$PHPMODULES zmq"
150
# SSH2
151
PHPMODULES="$PHPMODULES ssh2"
152
# pfSense extensions
153
PHPMODULES="$PHPMODULES pfSense"
154
# json
155
PHPMODULES="$PHPMODULES json"
156
# bcmath
157
PHPMODULES="$PHPMODULES bcmath"
158
# filter
159
PHPMODULES="$PHPMODULES filter"
160
# rrd
161
PHPMODULES="$PHPMODULES rrd"
162

    
163
PHP_ZEND_MODULES=""
164

    
165
# Modules previously included.
166
# can be turned on by touching
167
# /etc/php_dynamodules/$modulename
168
#	sysvmsg \
169
#	sysvsem \
170
#	sysvshm \
171
#	bcmath \
172
#	tokenizer \
173
#	uploadprogress \
174
#	sockets \
175
#	Reflection \
176
#	mysql \
177
#	bz2	\
178

    
179
# Clear the .ini file to make sure we are clean
180
if [ -f /usr/local/etc/php.ini ]; then
181
	/bin/rm /usr/local/etc/php.ini
182
fi
183
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/grep -v "\["`
184

    
185
unset TIMEZONE
186
# Fetch the timezone from /var/db/zoneinfo if present
187
if [ -f /var/db/zoneinfo ]; then
188
	TIMEZONE=$(cat /var/db/zoneinfo)
189
fi
190

    
191
if [ -z "${TIMEZONE}" ]; then
192
	# Second option is from config.xml
193
	TIMEZONE=$(/usr/local/sbin/read_xml_tag.sh string system/timezone)
194
fi
195

    
196
if [ -z "${TIMEZONE}" ]; then
197
	# Last option, use default value from $g or Etc/UTC
198
	TIMEZONE=$(/usr/local/sbin/read_global_var default_timezone "Etc/UTC")
199
fi
200

    
201
if echo "${VERSION}" | grep -q RELEASE; then
202
	error_reporting="error_reporting = E_ERROR | E_PARSE"
203
else
204
	error_reporting=""
205
fi
206

    
207
# Get a loaded module list in the stock php
208
# Populate a dummy php.ini to avoid
209
# the file being clobbered and the firewall
210
# not being able to boot back up.
211
/bin/cat >/usr/local/etc/php.ini <<EOF
212
; File generated from /etc/rc.php_ini_setup
213
output_buffering = "0"
214
expose_php = Off
215
implicit_flush = true
216
magic_quotes_gpc = Off
217
max_execution_time = 900
218
request_terminate_timeout = 900
219
max_input_time = 1800
220
max_input_vars = 5000
221
register_argc_argv = On
222
register_long_arrays = Off
223
variables_order = "GPCS"
224
file_uploads = On
225
upload_tmp_dir = ${UPLOADTMPDIR}
226
upload_max_filesize = 200M
227
post_max_size = 200M
228
html_errors = Off
229
zlib.output_compression = Off
230
zlib.output_compression_level = 1
231
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg:/usr/local/www/classes:/usr/local/www/classes/Form"
232
display_startup_errors=on
233
display_errors=on
234
log_errors=on
235
error_log=/tmp/PHP_errors.log
236
extension_dir=${EXTENSIONSDIR}
237
date.timezone="${TIMEZONE}"
238
session.hash_bits_per_character = 5
239
session.hash_function = 1
240
${error_reporting}
241

    
242
; Extensions
243

    
244
EOF
245

    
246
# Loop through and generate modules to load.
247
# Take into account modules built into php.
248
for EXT in $PHPMODULES; do
249
	SHOULDADD="true"
250
	# Check to see if module is compiled into php statically
251
	for LM in $LOADED_MODULES; do
252
		if [ "$EXT" = "$LM" ]; then
253
			SHOULDADD="false"
254
		fi
255
	done
256
	if [ "$SHOULDADD" = "true" ]; then
257
		# Ensure extension exists before adding.
258
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
259
			echo "extension=${EXT}.so" >> /usr/local/etc/php.ini
260
		fi
261
	fi
262
done
263

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

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

    
274
	/bin/cat >>/usr/local/etc/php.ini <<EOF
275

    
276
; opcache Settings
277
opcache.enabled="1"
278
opcache.enable_cli="0"
279
opcache.memory_consumption="${OPCACHEMEMSIZE}"
280

    
281
EOF
282
else
283
	/bin/cat >>/usr/local/etc/php.ini <<EOF
284
; opcache Settings
285
opcache.enabled="0"
286
EOF
287
fi
288

    
289
	/bin/cat >>/usr/local/etc/php.ini <<EOF
290

    
291
[suhosin]
292
suhosin.get.max_array_index_length = 256
293
suhosin.get.max_vars = 5000
294
suhosin.get.max_value_length = 500000
295
suhosin.post.max_array_index_length = 256
296
suhosin.post.max_vars = 5000
297
suhosin.post.max_value_length = 500000
298
suhosin.request.max_array_index_length = 256
299
suhosin.request.max_vars = 5000
300
suhosin.request.max_value_length = 500000
301
suhosin.memory_limit = 805306368
302

    
303
EOF
304

    
305

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

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

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

    
325
[nginx]
326
user = root
327
group = wheel
328
;mode = 0600
329

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

    
335
security.limit_extensions =
336

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

    
341
EOF
342

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

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

    
351
EOF
352

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

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

    
364
EOF
365
else
366

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

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

    
373
EOF
374

    
375
fi
376

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

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

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