Project

General

Profile

Download (10.9 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-2018 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
# pfSense extensions
151
PHPMODULES="$PHPMODULES pfSense"
152
# json
153
PHPMODULES="$PHPMODULES json"
154
# bcmath
155
PHPMODULES="$PHPMODULES bcmath"
156
# filter
157
PHPMODULES="$PHPMODULES filter"
158
# rrd
159
PHPMODULES="$PHPMODULES rrd"
160

    
161
PHP_ZEND_MODULES=""
162

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

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

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

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

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

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

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

    
240
; Extensions
241

    
242
EOF
243

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

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

    
270
if [ "$LOWMEM" != "TRUE" ]; then
271

    
272
	/bin/cat >>/usr/local/etc/php.ini <<EOF
273

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

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

    
287
	/bin/cat >>/usr/local/etc/php.ini <<EOF
288

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

    
301
EOF
302

    
303

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

    
311
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
312

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

    
323
[nginx]
324
user = root
325
group = wheel
326
;mode = 0600
327

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

    
333
security.limit_extensions =
334

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

    
339
EOF
340

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

    
344
pm = ondemand
345
pm.process_idle_timeout = 5
346
pm.max_children = $PHPFPMMAX
347
pm.max_requests = 500
348

    
349
EOF
350

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

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

    
362
EOF
363
else
364

    
365
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
366

    
367
pm = static
368
pm.max_children = $PHPFPMMAX
369
pm.max_requests = 500
370

    
371
EOF
372

    
373
fi
374

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

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

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