Project

General

Profile

Download (11.5 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
7
# Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
8
# All rights reserved.
9
#
10
# Licensed under the Apache License, Version 2.0 (the "License");
11
# you may not use this file except in compliance with the License.
12
# You may obtain a copy of the License at
13
#
14
# http://www.apache.org/licenses/LICENSE-2.0
15
#
16
# Unless required by applicable law or agreed to in writing, software
17
# distributed under the License is distributed on an "AS IS" BASIS,
18
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
# See the License for the specific language governing permissions and
20
# limitations under the License.
21
#
22

    
23
# Set our operating platform
24
VERSION=$(/bin/cat /etc/version)
25
MIN_REALMEM_FOR_OPCACHE=512
26

    
27
if /usr/local/sbin/pkg-static info -e php83; then
28
	EXTENSIONSDIR="/usr/local/lib/php/20230831/"
29
elif /usr/local/sbin/pkg-static info -e php82; then
30
	EXTENSIONSDIR="/usr/local/lib/php/20220829/"
31
elif /usr/local/sbin/pkg-static info -e php81; then
32
	EXTENSIONSDIR="/usr/local/lib/php/20210902/"
33
elif /usr/local/sbin/pkg-static info -e php74; then
34
	EXTENSIONSDIR="/usr/local/lib/php/20190902/"
35
elif /usr/local/sbin/pkg-static info -e php73; then
36
	EXTENSIONSDIR="/usr/local/lib/php/20180731/"
37
elif /usr/local/sbin/pkg-static info -e php72; then
38
	EXTENSIONSDIR="/usr/local/lib/php/20170718/"
39
fi
40

    
41
# Grab amount of memory that is detected
42
if [ -f /var/log/dmesg.boot ]; then
43
	AVAILMEM=$(/bin/cat /var/log/dmesg.boot |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}')
44
else
45
	AVAILMEM=$(/sbin/dmesg -a |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}')
46
fi
47

    
48
# Use hw.physmem if $AVAILMEM is zero
49
if [ -z "$AVAILMEM" ]; then
50
	AVAILMEM=$(/sbin/sysctl -qn hw.physmem | /usr/bin/awk '{print $1/1048576}' | /usr/bin/awk -F '.' '{print $1}')
51
fi
52

    
53
# Get amount of ram installed on this system
54
REALMEM=$(/sbin/sysctl -qn hw.realmem | /usr/bin/awk '{print $1/1048576}' | /usr/bin/awk -F '.' '{print $1}')
55
if [ -z "$REALMEM" -o "$REALMEM" == "0" ]; then
56
	REALMEM=$AVAILMEM
57
fi
58
export REALMEM
59
export LOWMEM
60

    
61
if [  ${REALMEM} -lt $MIN_REALMEM_FOR_OPCACHE ]; then
62
	LOWMEM="TRUE"
63
	echo ">>> Under $MIN_REALMEM_FOR_OPCACHE megabytes of ram detected.  Not enabling opcache"
64
	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
65
else
66

    
67
	# Calculate opcache memory size according
68
	# to detected memory values
69
	if [ "$AVAILMEM" -gt "135" ]; then
70
		OPCACHEMEMSIZE="10"
71
	fi
72
	if [ "$AVAILMEM" -gt "256" ]; then
73
		OPCACHEMEMSIZE="20"
74
	fi
75
	if [ "$AVAILMEM" -gt "384" ]; then
76
		OPCACHEMEMSIZE="25"
77
	fi
78
	if [ "$AVAILMEM" -gt "512" ]; then
79
		OPCACHEMEMSIZE="30"
80
	fi
81
	if [ "$AVAILMEM" -gt "784" ]; then
82
		OPCACHEMEMSIZE="50"
83
	fi
84
fi
85

    
86
PCRE_JIT=1
87

    
88
# Set upload directory
89
UPLOADTMPDIR="/tmp"
90

    
91
# Define php modules.  Do not add .so, it will
92
# be done automatically by the script below.
93
PHPMODULES="standard"
94
# Config read/write
95
PHPMODULES="$PHPMODULES xml libxml dom"
96
PHPMODULES="$PHPMODULES SimpleXML xmlreader xmlwriter"
97
# Downloading via HTTP/FTP (pkg mgr, etc)
98
PHPMODULES="$PHPMODULES curl date"
99
# Internationalization
100
PHPMODULES="$PHPMODULES gettext"
101
# User manager
102
PHPMODULES="$PHPMODULES ldap openssl pcntl"
103
PHPMODULES="$PHPMODULES mcrypt"
104
# Regexs, PERL style!
105
PHPMODULES="$PHPMODULES pcre"
106
# The mighty posix!
107
PHPMODULES="$PHPMODULES posix"
108
PHPMODULES="$PHPMODULES readline"
109
# Login sessions
110
PHPMODULES="$PHPMODULES session"
111
# Firewall rules edit
112
PHPMODULES="$PHPMODULES ctype"
113
# firewall_rules_edit.php
114
PHPMODULES="$PHPMODULES mbstring"
115
# Synchronization primitives
116
PHPMODULES="$PHPMODULES shmop"
117
# Page compression
118
PHPMODULES="$PHPMODULES zlib"
119
# SQLlite & Database
120
PHPMODULES="$PHPMODULES spl"
121
PHPMODULES="$PHPMODULES PDO"
122
PHPMODULES="$PHPMODULES sqlite3"
123
# RADIUS
124
PHPMODULES="$PHPMODULES radius"
125
# pfSense extensions
126
PHPMODULES="$PHPMODULES pfSense"
127
# json
128
PHPMODULES="$PHPMODULES json"
129
# bcmath
130
PHPMODULES="$PHPMODULES bcmath"
131
# filter
132
PHPMODULES="$PHPMODULES filter"
133
# rrd
134
PHPMODULES="$PHPMODULES rrd"
135

    
136
PHP_ZEND_MODULES=""
137

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

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

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

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

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

    
174
if echo "${VERSION}" | grep -q RELEASE; then
175
	# On release versions, reduce error reporting and don't print function arguments.
176
	error_reporting="error_reporting = E_ERROR | E_PARSE"
177
	exception_ignore_args="zend.exception_ignore_args=1"
178
else
179
	# On development versions, increase error reporting and print function arguments.
180
	error_reporting="error_reporting = E_ALL ^ (E_WARNING | E_NOTICE | E_DEPRECATED)"
181
	exception_ignore_args="zend.exception_ignore_args=0"
182
fi
183

    
184
# Get a loaded module list in the stock php
185
# Populate a dummy php.ini to avoid
186
# the file being clobbered and the firewall
187
# not being able to boot back up.
188
/bin/cat >/usr/local/etc/php.ini <<EOF
189
; File generated from /etc/rc.php_ini_setup
190
output_buffering = "0"
191
expose_php = Off
192
implicit_flush = true
193
magic_quotes_gpc = Off
194
max_execution_time = 900
195
request_terminate_timeout = 900
196
max_input_time = 1800
197
max_input_vars = 5000
198
register_argc_argv = On
199
register_long_arrays = Off
200
variables_order = "GPCS"
201
file_uploads = On
202
upload_tmp_dir = ${UPLOADTMPDIR}
203
upload_max_filesize = 200M
204
post_max_size = 200M
205
html_errors = Off
206
zlib.output_compression = Off
207
zlib.output_compression_level = 1
208
include_path = ".:/etc/inc:/usr/local/pfSense/include:/usr/local/pfSense/include/www:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg:/usr/local/www/classes:/usr/local/www/classes/Form:/usr/local/share/pear:/usr/local/share/openssl_x509_crl/"
209
display_startup_errors=off
210
; No need to display errors directly, they are handled in etc/inc/config.lib.inc by pfSense_clear_globals()
211
display_errors=off
212
log_errors=on
213
error_log=/tmp/PHP_errors.log
214
extension_dir=${EXTENSIONSDIR}
215
date.timezone="${TIMEZONE}"
216
session.hash_bits_per_character = 5
217
session.hash_function = 1
218
${error_reporting}
219
${exception_ignore_args}
220

    
221
[Pcre]
222
pcre.jit=${PCRE_JIT}
223

    
224
; Extensions
225

    
226
EOF
227

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

    
246
# Zend modules
247
for EXT in $PHP_ZEND_MODULES; do
248
	# Ensure extension exists before adding.
249
	if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
250
		echo "zend_extension=${EXT}.so" >> /usr/local/etc/php.ini
251
	fi
252
done
253

    
254
if [ "$LOWMEM" != "TRUE" ]; then
255

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

    
258
; opcache Settings
259
opcache.enabled="1"
260
opcache.enable_cli="0"
261
opcache.memory_consumption="${OPCACHEMEMSIZE}"
262

    
263
EOF
264
else
265
	/bin/cat >>/usr/local/etc/php.ini <<EOF
266
; opcache Settings
267
opcache.enabled="0"
268
EOF
269
fi
270

    
271
# Memory limits 128M to calculated max
272
PHP_MEMORY_LIMIT="$(read_xml_tag.sh number system/php_memory_limit)"
273

    
274
# Set local variable for available memory to match the PHP code which always pulls from sysctl
275
MEM=$(/bin/expr $(/sbin/sysctl -q hw.physmem | cut -d':' -f2) / 1048576)
276

    
277
# Calculate MAX memory in the same fashion as get_php_max_memory() in /etc/inc/util.inc
278
let PHP_MAX_LIMIT=${MEM}-512
279

    
280
if [ "${PHP_MAX_LIMIT}" -le "0" ]; then
281
	let PHP_MAX_LIMIT=${MEM}-128
282

    
283
	if [ "${PHP_MAX_LIMIT}" -lt "128" ]; then
284
		PHP_MAX_LIMIT=128
285
	fi
286
fi
287

    
288
# If outside of limits, revert to default in same fashion as get_php_default_memory() in /etc/inc/util.inc
289
if ! { [ -n "${PHP_MEMORY_LIMIT}" ] && [ "${PHP_MEMORY_LIMIT}" -ge "128" ] && [ "${PHP_MEMORY_LIMIT}" -le "${PHP_MAX_LIMIT}" ]; }; then
290
	if [ "$(uname -m)" == "amd64" ]; then
291
		PHP_MEMORY_LIMIT=512
292
	else
293
		PHP_MEMORY_LIMIT=128
294
	fi
295

    
296
	if [ ${PHP_MEMORY_LIMIT} -ge "${MEM}" ]; then
297
		let PHP_MEMORY_LIMIT=${MEM}/2
298
		if [ "${PHP_MEMORY_LIMIT}" -lt "128" ]; then
299
			PHP_MEMORY_LIMIT=128
300
		fi
301
	fi
302
fi
303

    
304
/bin/cat >>/usr/local/etc/php.ini <<EOF
305
memory_limit="${PHP_MEMORY_LIMIT}M"
306
EOF
307

    
308
PHPFPMMAX=3
309
PHPFPMIDLE=30
310
PHPFPMSTART=1
311
PHPFPMSPARE=2
312
PHPFPMREQ=500
313
if [ $REALMEM -lt 250 ]; then
314
	PHPFPMMAX=2
315
       PHPFPMIDLE=5
316
       PHPFPMSTART=1
317
       PHPFPMSPARE=1
318
       PHPFPMREQ=500
319
elif [ ${REALMEM} -gt 1000 ]; then
320
       PHPFPMMAX=8
321
       PHPFPMIDLE=3600
322
       PHPFPMSTART=2
323
       PHPFPMSPARE=7
324
       PHPFPMREQ=5000
325
fi
326

    
327
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
328

    
329
[global]
330
pid = run/php-fpm.pid
331
error_log=syslog
332
syslog.facility = daemon
333
syslog.ident = system
334
log_level = error
335
daemonize = yes
336
events.mechanism = kqueue
337
process.max = ${PHPFPMMAX}
338

    
339
[nginx]
340
user = root
341
group = wheel
342
;mode = 0600
343

    
344
listen = /var/run/php-fpm.socket
345
listen.owner = root
346
listen.group = wheel
347
listen.mode = 0600
348

    
349
security.limit_extensions =
350

    
351
; Pass environment variables
352
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
353
env[LOGNAME] = root
354

    
355
EOF
356

    
357
if [ $REALMEM -lt 350 ]; then
358
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
359

    
360
pm = ondemand
361
pm.process_idle_timeout = $PHPFPMIDLE
362
pm.max_children = $PHPFPMMAX
363
pm.max_requests = $PHPFPMREQ
364
EOF
365

    
366
elif [ $REALMEM -gt 1000 ]; then
367
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
368

    
369
pm = dynamic
370
pm.process_idle_timeout = $PHPFPMIDLE
371
pm.max_children = $PHPFPMMAX
372
pm.start_servers = $PHPFPMSTART
373
pm.max_requests = $PHPFPMREQ
374
pm.min_spare_servers=1
375
pm.max_spare_servers= $PHPFPMSPARE
376

    
377
EOF
378
else
379

    
380
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
381

    
382
pm = static
383
pm.max_children = $PHPFPMMAX
384
pm.max_requests = $PHPFPMREQ
385
EOF
386

    
387
fi
388

    
389
# Add status url for php-fpm this will only be made available from localhost through nginx 'allow 127.0.0.1'
390
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
391
pm.status_path = /status
392

    
393
EOF
394

    
395
# Remove old log file if it exists.
396
if [ -f /var/run/php_modules_load_errors.txt ]; then
397
	/bin/rm /var/run/php_modules_load_errors.txt
398
fi
399

    
400
for EXT in $PHPMODULES; do
401
	PHPMODULESLC="$PHPMODULESLC $(echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]')"
402
done
403

    
404
# Check loaded modules and remove anything that did not load correctly
405
LOADED_MODULES=$(/usr/local/bin/php-cgi -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\[")
406
for EXT in $PHPMODULESLC; do
407
	SHOULDREMOVE="true"
408
	for LM in $LOADED_MODULES; do
409
		if [ "$EXT" = "$LM" ]; then
410
			SHOULDREMOVE="false"
411
			break
412
		fi
413
	done
414
	# Handle low memory situations
415
	if [ "$LOWMEM" = "TRUE" ]; then
416
		if [ "$EXT" = "opcache" ]; then
417
			SHOULDREMOVE="true"
418
		fi
419
		if [ "$EXT" = "xcache" ]; then
420
			SHOULDREMOVE="true"
421
		fi
422
	fi
423
	if [ "$SHOULDREMOVE" = "true" ]; then
424
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
425
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
426
			/bin/cat /usr/local/etc/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
427
			/bin/rm -f /usr/local/etc/php.ini
428
			/bin/mv /tmp/php.ini /usr/local/etc/php.ini
429
		fi
430
	fi
431
done
(66-66/85)