Project

General

Profile

Download (10.8 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-2023 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 php82; then
28
	EXTENSIONSDIR="/usr/local/lib/php/20220829/"
29
elif /usr/local/sbin/pkg-static info -e php81; then
30
	EXTENSIONSDIR="/usr/local/lib/php/20210902/"
31
elif /usr/local/sbin/pkg-static info -e php74; then
32
	EXTENSIONSDIR="/usr/local/lib/php/20190902/"
33
elif /usr/local/sbin/pkg-static info -e php73; then
34
	EXTENSIONSDIR="/usr/local/lib/php/20180731/"
35
elif /usr/local/sbin/pkg-static info -e php72; then
36
	EXTENSIONSDIR="/usr/local/lib/php/20170718/"
37
fi
38

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

    
46
if [ -z "$AVAILMEM" ]; then
47
	MEM=$(/sbin/sysctl -q hw.physmem | cut -d':' -f2)
48
	AVAILMEM=$(/bin/expr $MEM / 1048576)
49
fi
50

    
51

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

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

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

    
82
# Set upload directory
83
UPLOADTMPDIR="/tmp"
84

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

    
130
PHP_ZEND_MODULES=""
131

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

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

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

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

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

    
168
if echo "${VERSION}" | grep -q RELEASE; then
169
	error_reporting="error_reporting = E_ERROR | E_PARSE"
170
else
171
	error_reporting="error_reporting = E_ALL ^ (E_WARNING | E_NOTICE | E_DEPRECATED)"
172
fi
173

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

    
209
; Extensions
210

    
211
EOF
212

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

    
231
# Zend modules
232
for EXT in $PHP_ZEND_MODULES; do
233
	# Ensure extension exists before adding.
234
	if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
235
		echo "zend_extension=${EXT}.so" >> /usr/local/etc/php.ini
236
	fi
237
done
238

    
239
if [ "$LOWMEM" != "TRUE" ]; then
240

    
241
	/bin/cat >>/usr/local/etc/php.ini <<EOF
242

    
243
; opcache Settings
244
opcache.enabled="1"
245
opcache.enable_cli="0"
246
opcache.memory_consumption="${OPCACHEMEMSIZE}"
247

    
248
EOF
249
else
250
	/bin/cat >>/usr/local/etc/php.ini <<EOF
251
; opcache Settings
252
opcache.enabled="0"
253
EOF
254
fi
255

    
256
# Memory limits 128M to calculated max
257
PHP_MEMORY_LIMIT="$(read_xml_tag.sh number system/php_memory_limit)"
258

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

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

    
265
if [ "${PHP_MAX_LIMIT}" -le "0" ]; then
266
	let PHP_MAX_LIMIT=${MEM}-128
267

    
268
	if [ "${PHP_MAX_LIMIT}" -lt "128" ]; then
269
		PHP_MAX_LIMIT=128
270
	fi
271
fi
272

    
273
# If outside of limits, revert to default in same fashion as get_php_default_memory() in /etc/inc/util.inc
274
if ! { [ -n "${PHP_MEMORY_LIMIT}" ] && [ "${PHP_MEMORY_LIMIT}" -ge "128" ] && [ "${PHP_MEMORY_LIMIT}" -le "${PHP_MAX_LIMIT}" ]; }; then
275
	if [ "$(uname -m)" == "amd64" ]; then
276
		PHP_MEMORY_LIMIT=512
277
	else
278
		PHP_MEMORY_LIMIT=128
279
	fi
280

    
281
	if [ ${PHP_MEMORY_LIMIT} -ge "${MEM}" ]; then
282
		let PHP_MEMORY_LIMIT=${MEM}/2
283
		if [ "${PHP_MEMORY_LIMIT}" -lt "128" ]; then
284
			PHP_MEMORY_LIMIT=128
285
		fi
286
	fi
287
fi
288

    
289
/bin/cat >>/usr/local/etc/php.ini <<EOF
290
memory_limit="${PHP_MEMORY_LIMIT}M"
291
EOF
292

    
293
PHPFPMMAX=3
294
PHPFPMIDLE=30
295
PHPFPMSTART=1
296
PHPFPMSPARE=2
297
PHPFPMREQ=500
298
if [ $REALMEM -lt 250 ]; then
299
	PHPFPMMAX=2
300
       PHPFPMIDLE=5
301
       PHPFPMSTART=1
302
       PHPFPMSPARE=1
303
       PHPFPMREQ=500
304
elif [ ${REALMEM} -gt 1000 ]; then
305
       PHPFPMMAX=8
306
       PHPFPMIDLE=3600
307
       PHPFPMSTART=2
308
       PHPFPMSPARE=7
309
       PHPFPMREQ=5000
310
fi
311

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

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

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

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

    
334
security.limit_extensions =
335

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

    
340
EOF
341

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

    
345
pm = ondemand
346
pm.process_idle_timeout = $PHPFPMIDLE
347
pm.max_children = $PHPFPMMAX
348
pm.max_requests = $PHPFPMREQ
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 = $PHPFPMIDLE
356
pm.max_children = $PHPFPMMAX
357
pm.start_servers = $PHPFPMSTART
358
pm.max_requests = $PHPFPMREQ
359
pm.min_spare_servers=1
360
pm.max_spare_servers= $PHPFPMSPARE
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 = $PHPFPMREQ
370
EOF
371

    
372
fi
373

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

    
378
EOF
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
(66-66/85)