Project

General

Profile

Download (11.1 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 Electric Sheep Fencing, LLC
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
/bin/chmod 0644 /usr/local/etc/php/extensions.ini
105
/usr/bin/sort -u -o /usr/local/etc/php/extensions.ini /usr/local/etc/php/extensions.ini
106

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

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

    
166
PHP_ZEND_MODULES=""
167

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

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

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

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

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

    
204
if echo "${VERSION}" | grep -q RELEASE; then
205
	error_reporting="error_reporting = E_ERROR | E_PARSE"
206
else
207
	error_reporting=""
208
fi
209

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

    
245
; Extensions
246

    
247
EOF
248

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

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

    
275
if [ "$LOWMEM" != "TRUE" ]; then
276

    
277
	/bin/cat >>/usr/local/etc/php.ini <<EOF
278

    
279
; opcache Settings
280
opcache.enabled="1"
281
opcache.enable_cli="0"
282
opcache.memory_consumption="${OPCACHEMEMSIZE}"
283

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

    
292
	/bin/cat >>/usr/local/etc/php.ini <<EOF
293

    
294
[suhosin]
295
suhosin.get.max_array_index_length = 256
296
suhosin.get.max_vars = 5000
297
suhosin.get.max_value_length = 500000
298
suhosin.post.max_array_index_length = 256
299
suhosin.post.max_vars = 5000
300
suhosin.post.max_value_length = 500000
301
suhosin.request.max_array_index_length = 256
302
suhosin.request.max_vars = 5000
303
suhosin.request.max_value_length = 500000
304
suhosin.memory_limit = 536870912
305

    
306
EOF
307

    
308

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

    
316
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
317

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

    
328
[nginx]
329
user = root
330
group = wheel
331
;mode = 0600
332

    
333
listen = /var/run/php-fpm.socket
334
listen.owner = root
335
listen.group = wheel
336
listen.mode = 0600
337

    
338
security.limit_extensions =
339

    
340
; Pass environment variables
341
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
342
env[LOGNAME] = root
343

    
344
EOF
345

    
346
if [ $REALMEM -lt 350 ]; then
347
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
348

    
349
pm = ondemand
350
pm.process_idle_timeout = 5
351
pm.max_children = $PHPFPMMAX
352
pm.max_requests = 500
353

    
354
EOF
355

    
356
elif [ $REALMEM -gt 1000 ]; then
357
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
358

    
359
pm = dynamic
360
pm.process_idle_timeout = 5
361
pm.max_children = $PHPFPMMAX
362
pm.start_servers = 1
363
pm.max_requests = 500
364
pm.min_spare_servers=1
365
pm.max_spare_servers=1
366

    
367
EOF
368
else
369

    
370
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
371

    
372
pm = static
373
pm.max_children = $PHPFPMMAX
374
pm.max_requests = 500
375

    
376
EOF
377

    
378
fi
379

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

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

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