Project

General

Profile

Download (9.42 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-2020 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 php73; then
28
	EXTENSIONSDIR="/usr/local/lib/php/20180731/"
29
elif /usr/local/sbin/pkg-static info -e php72; then
30
	EXTENSIONSDIR="/usr/local/lib/php/20170718/"
31
fi
32

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

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

    
45

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

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

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

    
76
# Set upload directory
77
UPLOADTMPDIR="/tmp"
78

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

    
127
PHP_ZEND_MODULES=""
128

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

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

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

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

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

    
165
if echo "${VERSION}" | grep -q RELEASE; then
166
	error_reporting="error_reporting = E_ERROR | E_PARSE"
167
else
168
	error_reporting=""
169
fi
170

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

    
206
; Extensions
207

    
208
EOF
209

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

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

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

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

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

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

    
253
PHPFPMMAX=3
254
PHPFPMIDLE=30
255
PHPFPMSTART=1
256
PHPFPMSPARE=2
257
PHPFPMREQ=500
258
if [ $REALMEM -lt 250 ]; then
259
	PHPFPMMAX=2
260
       PHPFPMIDLE=5
261
       PHPFPMSTART=1
262
       PHPFPMSPARE=1
263
       PHPFPMREQ=500
264
elif [ ${REALMEM} -gt 1000 ]; then
265
       PHPFPMMAX=8
266
       PHPFPMIDLE=3600
267
       PHPFPMSTART=2
268
       PHPFPMSPARE=7
269
       PHPFPMREQ=5000
270
fi
271

    
272
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
273

    
274
[global]
275
pid = run/php-fpm.pid
276
error_log=syslog
277
syslog.facility = daemon
278
syslog.ident = system
279
log_level = error
280
daemonize = yes
281
events.mechanism = kqueue
282
process.max = ${PHPFPMMAX}
283

    
284
[nginx]
285
user = root
286
group = wheel
287
;mode = 0600
288

    
289
listen = /var/run/php-fpm.socket
290
listen.owner = root
291
listen.group = wheel
292
listen.mode = 0600
293

    
294
security.limit_extensions =
295

    
296
; Pass environment variables
297
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
298
env[LOGNAME] = root
299

    
300
EOF
301

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

    
305
pm = ondemand
306
pm.process_idle_timeout = $PHPFPMIDLE
307
pm.max_children = $PHPFPMMAX
308
pm.max_requests = $PHPFPMREQ
309
EOF
310

    
311
elif [ $REALMEM -gt 1000 ]; then
312
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
313

    
314
pm = dynamic
315
pm.process_idle_timeout = $PHPFPMIDLE
316
pm.max_children = $PHPFPMMAX
317
pm.start_servers = $PHPFPMSTART
318
pm.max_requests = $PHPFPMREQ
319
pm.min_spare_servers=1
320
pm.max_spare_servers= $PHPFPMSPARE
321

    
322
EOF
323
else
324

    
325
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
326

    
327
pm = static
328
pm.max_children = $PHPFPMMAX
329
pm.max_requests = $PHPFPMREQ
330
EOF
331

    
332
fi
333

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

    
338
EOF
339

    
340
# Remove old log file if it exists.
341
if [ -f /var/run/php_modules_load_errors.txt ]; then
342
	/bin/rm /var/run/php_modules_load_errors.txt
343
fi
344

    
345
for EXT in $PHPMODULES; do
346
	PHPMODULESLC="$PHPMODULESLC $(echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]')"
347
done
348

    
349
# Check loaded modules and remove anything that did not load correctly
350
LOADED_MODULES=$(/usr/local/bin/php-cgi -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\[")
351
for EXT in $PHPMODULESLC; do
352
	SHOULDREMOVE="true"
353
	for LM in $LOADED_MODULES; do
354
		if [ "$EXT" = "$LM" ]; then
355
			SHOULDREMOVE="false"
356
			break
357
		fi
358
	done
359
	# Handle low memory situations
360
	if [ "$LOWMEM" = "TRUE" ]; then
361
		if [ "$EXT" = "opcache" ]; then
362
			SHOULDREMOVE="true"
363
		fi
364
		if [ "$EXT" = "xcache" ]; then
365
			SHOULDREMOVE="true"
366
		fi
367
	fi
368
	if [ "$SHOULDREMOVE" = "true" ]; then
369
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
370
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
371
			/bin/cat /usr/local/etc/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
372
			/bin/rm -f /usr/local/etc/php.ini
373
			/bin/mv /tmp/php.ini /usr/local/etc/php.ini
374
		fi
375
	fi
376
done
(63-63/81)