Project

General

Profile

Download (11.3 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
if [ -z "$AVAILMEM" ]; then
49
	MEM=$(/sbin/sysctl -q hw.physmem | cut -d':' -f2)
50
	AVAILMEM=$(/bin/expr $MEM / 1048576)
51
fi
52

    
53

    
54
# Get amount of ram installed on this system
55
REALMEM=$(/sbin/sysctl -q hw.realmem | /usr/bin/awk '{print $2/1048576}' | /usr/bin/awk -F '.' '{print $1}')
56
export REALMEM
57
export LOWMEM
58

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

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

    
84
# Set upload directory
85
UPLOADTMPDIR="/tmp"
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 mcrypt"
100
# Regexs, PERL style!
101
PHPMODULES="$PHPMODULES pcre"
102
# The mighty posix!
103
PHPMODULES="$PHPMODULES posix"
104
PHPMODULES="$PHPMODULES readline"
105
# Login sessions
106
PHPMODULES="$PHPMODULES session"
107
# Firewall rules edit
108
PHPMODULES="$PHPMODULES ctype"
109
# firewall_rules_edit.php
110
PHPMODULES="$PHPMODULES mbstring"
111
# Synchronization primitives
112
PHPMODULES="$PHPMODULES shmop"
113
# Page compression
114
PHPMODULES="$PHPMODULES zlib"
115
# SQLlite & Database
116
PHPMODULES="$PHPMODULES spl"
117
PHPMODULES="$PHPMODULES PDO"
118
PHPMODULES="$PHPMODULES sqlite3"
119
# RADIUS
120
PHPMODULES="$PHPMODULES radius"
121
# pfSense extensions
122
PHPMODULES="$PHPMODULES pfSense"
123
# json
124
PHPMODULES="$PHPMODULES json"
125
# bcmath
126
PHPMODULES="$PHPMODULES bcmath"
127
# filter
128
PHPMODULES="$PHPMODULES filter"
129
# rrd
130
PHPMODULES="$PHPMODULES rrd"
131

    
132
PHP_ZEND_MODULES=""
133

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

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

    
154
unset TIMEZONE
155
# Fetch the timezone from /var/db/zoneinfo if present
156
if [ -f /var/db/zoneinfo ]; then
157
	TIMEZONE=$(cat /var/db/zoneinfo)
158
fi
159

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

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

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

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

    
217
; Extensions
218

    
219
EOF
220

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

    
239
# Zend modules
240
for EXT in $PHP_ZEND_MODULES; do
241
	# Ensure extension exists before adding.
242
	if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
243
		echo "zend_extension=${EXT}.so" >> /usr/local/etc/php.ini
244
	fi
245
done
246

    
247
if [ "$LOWMEM" != "TRUE" ]; then
248

    
249
	/bin/cat >>/usr/local/etc/php.ini <<EOF
250

    
251
; opcache Settings
252
opcache.enabled="1"
253
opcache.enable_cli="0"
254
opcache.memory_consumption="${OPCACHEMEMSIZE}"
255

    
256
EOF
257
else
258
	/bin/cat >>/usr/local/etc/php.ini <<EOF
259
; opcache Settings
260
opcache.enabled="0"
261
EOF
262
fi
263

    
264
# Memory limits 128M to calculated max
265
PHP_MEMORY_LIMIT="$(read_xml_tag.sh number system/php_memory_limit)"
266

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

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

    
273
if [ "${PHP_MAX_LIMIT}" -le "0" ]; then
274
	let PHP_MAX_LIMIT=${MEM}-128
275

    
276
	if [ "${PHP_MAX_LIMIT}" -lt "128" ]; then
277
		PHP_MAX_LIMIT=128
278
	fi
279
fi
280

    
281
# If outside of limits, revert to default in same fashion as get_php_default_memory() in /etc/inc/util.inc
282
if ! { [ -n "${PHP_MEMORY_LIMIT}" ] && [ "${PHP_MEMORY_LIMIT}" -ge "128" ] && [ "${PHP_MEMORY_LIMIT}" -le "${PHP_MAX_LIMIT}" ]; }; then
283
	if [ "$(uname -m)" == "amd64" ]; then
284
		PHP_MEMORY_LIMIT=512
285
	else
286
		PHP_MEMORY_LIMIT=128
287
	fi
288

    
289
	if [ ${PHP_MEMORY_LIMIT} -ge "${MEM}" ]; then
290
		let PHP_MEMORY_LIMIT=${MEM}/2
291
		if [ "${PHP_MEMORY_LIMIT}" -lt "128" ]; then
292
			PHP_MEMORY_LIMIT=128
293
		fi
294
	fi
295
fi
296

    
297
/bin/cat >>/usr/local/etc/php.ini <<EOF
298
memory_limit="${PHP_MEMORY_LIMIT}M"
299
EOF
300

    
301
PHPFPMMAX=3
302
PHPFPMIDLE=30
303
PHPFPMSTART=1
304
PHPFPMSPARE=2
305
PHPFPMREQ=500
306
if [ $REALMEM -lt 250 ]; then
307
	PHPFPMMAX=2
308
       PHPFPMIDLE=5
309
       PHPFPMSTART=1
310
       PHPFPMSPARE=1
311
       PHPFPMREQ=500
312
elif [ ${REALMEM} -gt 1000 ]; then
313
       PHPFPMMAX=8
314
       PHPFPMIDLE=3600
315
       PHPFPMSTART=2
316
       PHPFPMSPARE=7
317
       PHPFPMREQ=5000
318
fi
319

    
320
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
321

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

    
332
[nginx]
333
user = root
334
group = wheel
335
;mode = 0600
336

    
337
listen = /var/run/php-fpm.socket
338
listen.owner = root
339
listen.group = wheel
340
listen.mode = 0600
341

    
342
security.limit_extensions =
343

    
344
; Pass environment variables
345
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
346
env[LOGNAME] = root
347

    
348
EOF
349

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

    
353
pm = ondemand
354
pm.process_idle_timeout = $PHPFPMIDLE
355
pm.max_children = $PHPFPMMAX
356
pm.max_requests = $PHPFPMREQ
357
EOF
358

    
359
elif [ $REALMEM -gt 1000 ]; then
360
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
361

    
362
pm = dynamic
363
pm.process_idle_timeout = $PHPFPMIDLE
364
pm.max_children = $PHPFPMMAX
365
pm.start_servers = $PHPFPMSTART
366
pm.max_requests = $PHPFPMREQ
367
pm.min_spare_servers=1
368
pm.max_spare_servers= $PHPFPMSPARE
369

    
370
EOF
371
else
372

    
373
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
374

    
375
pm = static
376
pm.max_children = $PHPFPMMAX
377
pm.max_requests = $PHPFPMREQ
378
EOF
379

    
380
fi
381

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

    
386
EOF
387

    
388
# Remove old log file if it exists.
389
if [ -f /var/run/php_modules_load_errors.txt ]; then
390
	/bin/rm /var/run/php_modules_load_errors.txt
391
fi
392

    
393
for EXT in $PHPMODULES; do
394
	PHPMODULESLC="$PHPMODULESLC $(echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]')"
395
done
396

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