Project

General

Profile

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

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

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

    
49

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

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

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

    
80
# Set upload directory
81
UPLOADTMPDIR="/tmp"
82

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

    
128
PHP_ZEND_MODULES=""
129

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

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

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

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

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

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

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

    
207
; Extensions
208

    
209
EOF
210

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

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

    
237
if [ "$LOWMEM" != "TRUE" ]; then
238

    
239
	/bin/cat >>/usr/local/etc/php.ini <<EOF
240

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

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

    
254
# Memory limits 128M to AVAILABLE MEMORY - 1024M
255
PHP_MEMORY_LIMIT="$(read_xml_tag.sh number system/php_memory_limit)"
256
let PHP_MAX_LIMIT=${AVAILMEM}-1024 # Reserve 1GiB
257

    
258
if [ -n "${PHP_MEMORY_LIMIT}" ] && [ "${PHP_MEMORY_LIMIT}" -ge "128" ] && [ "${PHP_MEMORY_LIMIT}" -le "${PHP_MAX_LIMIT"}" ]; then
259
	/bin/cat >>/usr/local/etc/php.ini <<EOF
260
memory_limit="${PHP_MEMORY_LIMIT}"M
261
EOF
262
fi
263

    
264
PHPFPMMAX=3
265
PHPFPMIDLE=30
266
PHPFPMSTART=1
267
PHPFPMSPARE=2
268
PHPFPMREQ=500
269
if [ $REALMEM -lt 250 ]; then
270
	PHPFPMMAX=2
271
       PHPFPMIDLE=5
272
       PHPFPMSTART=1
273
       PHPFPMSPARE=1
274
       PHPFPMREQ=500
275
elif [ ${REALMEM} -gt 1000 ]; then
276
       PHPFPMMAX=8
277
       PHPFPMIDLE=3600
278
       PHPFPMSTART=2
279
       PHPFPMSPARE=7
280
       PHPFPMREQ=5000
281
fi
282

    
283
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
284

    
285
[global]
286
pid = run/php-fpm.pid
287
error_log=syslog
288
syslog.facility = daemon
289
syslog.ident = system
290
log_level = error
291
daemonize = yes
292
events.mechanism = kqueue
293
process.max = ${PHPFPMMAX}
294

    
295
[nginx]
296
user = root
297
group = wheel
298
;mode = 0600
299

    
300
listen = /var/run/php-fpm.socket
301
listen.owner = root
302
listen.group = wheel
303
listen.mode = 0600
304

    
305
security.limit_extensions =
306

    
307
; Pass environment variables
308
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
309
env[LOGNAME] = root
310

    
311
EOF
312

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

    
316
pm = ondemand
317
pm.process_idle_timeout = $PHPFPMIDLE
318
pm.max_children = $PHPFPMMAX
319
pm.max_requests = $PHPFPMREQ
320
EOF
321

    
322
elif [ $REALMEM -gt 1000 ]; then
323
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
324

    
325
pm = dynamic
326
pm.process_idle_timeout = $PHPFPMIDLE
327
pm.max_children = $PHPFPMMAX
328
pm.start_servers = $PHPFPMSTART
329
pm.max_requests = $PHPFPMREQ
330
pm.min_spare_servers=1
331
pm.max_spare_servers= $PHPFPMSPARE
332

    
333
EOF
334
else
335

    
336
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
337

    
338
pm = static
339
pm.max_children = $PHPFPMMAX
340
pm.max_requests = $PHPFPMREQ
341
EOF
342

    
343
fi
344

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

    
349
EOF
350

    
351
# Remove old log file if it exists.
352
if [ -f /var/run/php_modules_load_errors.txt ]; then
353
	/bin/rm /var/run/php_modules_load_errors.txt
354
fi
355

    
356
for EXT in $PHPMODULES; do
357
	PHPMODULESLC="$PHPMODULESLC $(echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]')"
358
done
359

    
360
# Check loaded modules and remove anything that did not load correctly
361
LOADED_MODULES=$(/usr/local/bin/php-cgi -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\[")
362
for EXT in $PHPMODULESLC; do
363
	SHOULDREMOVE="true"
364
	for LM in $LOADED_MODULES; do
365
		if [ "$EXT" = "$LM" ]; then
366
			SHOULDREMOVE="false"
367
			break
368
		fi
369
	done
370
	# Handle low memory situations
371
	if [ "$LOWMEM" = "TRUE" ]; then
372
		if [ "$EXT" = "opcache" ]; then
373
			SHOULDREMOVE="true"
374
		fi
375
		if [ "$EXT" = "xcache" ]; then
376
			SHOULDREMOVE="true"
377
		fi
378
	fi
379
	if [ "$SHOULDREMOVE" = "true" ]; then
380
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
381
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
382
			/bin/cat /usr/local/etc/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
383
			/bin/rm -f /usr/local/etc/php.ini
384
			/bin/mv /tmp/php.ini /usr/local/etc/php.ini
385
		fi
386
	fi
387
done
(66-66/85)