Project

General

Profile

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

    
22
# Set our operating platform
23
VERSION=`/bin/cat /etc/version`
24
MIN_REALMEM_FOR_OPCACHE=512
25

    
26
if pkg info -e php72; then
27
	PHP_VER=7
28
	EXTENSIONSDIR="/usr/local/lib/php/20170718/"
29
else
30
	PHP_VER=5
31
	EXTENSIONSDIR="/usr/local/lib/php/20131226/"
32
fi
33

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

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

    
46

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

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

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

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

    
80
# Define php modules.  Do not add .so, it will
81
# be done automatically by the script below.
82
PHPMODULES="standard"
83
# Config read/write
84
PHPMODULES="$PHPMODULES xml libxml dom"
85
PHPMODULES="$PHPMODULES SimpleXML xmlreader xmlwriter"
86
# Downloading via HTTP/FTP (pkg mgr, etc)
87
PHPMODULES="$PHPMODULES curl date"
88
# Internationalization
89
PHPMODULES="$PHPMODULES gettext"
90
# User manager
91
PHPMODULES="$PHPMODULES ldap openssl pcntl"
92
PHPMODULES="$PHPMODULES hash"
93
PHPMODULES="$PHPMODULES mcrypt"
94
# Regexs, PERL style!
95
PHPMODULES="$PHPMODULES pcre"
96
# The mighty posix!
97
PHPMODULES="$PHPMODULES posix"
98
PHPMODULES="$PHPMODULES readline"
99
# Login sessions
100
PHPMODULES="$PHPMODULES session"
101
if [ $PHP_VER -eq 5 ]; then
102
	# Extra sanity seatbelts
103
	PHPMODULES="$PHPMODULES suhosin"
104
fi
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
# ZeroMQ
120
PHPMODULES="$PHPMODULES zmq"
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
	error_reporting="error_reporting = E_ERROR | E_PARSE"
172
else
173
	error_reporting=""
174
fi
175

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

    
211
; Extensions
212

    
213
EOF
214

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

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

    
241
if [ "$LOWMEM" != "TRUE" ]; then
242

    
243
	/bin/cat >>/usr/local/etc/php.ini <<EOF
244

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

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

    
258
if [ $PHP_VER -eq 5 ]; then
259
	/bin/cat >>/usr/local/etc/php.ini <<EOF
260

    
261
[suhosin]
262
suhosin.get.max_array_index_length = 256
263
suhosin.get.max_vars = 5000
264
suhosin.get.max_value_length = 500000
265
suhosin.post.max_array_index_length = 256
266
suhosin.post.max_vars = 5000
267
suhosin.post.max_value_length = 500000
268
suhosin.request.max_array_index_length = 256
269
suhosin.request.max_vars = 5000
270
suhosin.request.max_value_length = 500000
271
suhosin.memory_limit = 805306368
272

    
273
EOF
274
fi
275

    
276
PHPFPMMAX=3
277
PHPFPMIDLE=30
278
PHPFPMSTART=1
279
PHPFPMSPARE=2
280
PHPFPMREQ=500
281
if [ $REALMEM -lt 250 ]; then
282
	PHPFPMMAX=2
283
       PHPFPMIDLE=5
284
       PHPFPMSTART=1
285
       PHPFPMSPARE=1
286
       PHPFPMREQ=500
287
elif [ ${REALMEM} -gt 1000 ]; then
288
       PHPFPMMAX=8
289
       PHPFPMIDLE=3600
290
       PHPFPMSTART=2
291
       PHPFPMSPARE=7
292
       PHPFPMREQ=5000
293
fi
294

    
295
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
296

    
297
[global]
298
pid = run/php-fpm.pid
299
error_log=syslog
300
syslog.facility = daemon
301
syslog.ident = system
302
log_level = error
303
daemonize = yes
304
events.mechanism = kqueue
305
process.max = ${PHPFPMMAX}
306

    
307
[nginx]
308
user = root
309
group = wheel
310
;mode = 0600
311

    
312
listen = /var/run/php-fpm.socket
313
listen.owner = root
314
listen.group = wheel
315
listen.mode = 0600
316

    
317
security.limit_extensions =
318

    
319
; Pass environment variables
320
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
321
env[LOGNAME] = root
322

    
323
EOF
324

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

    
328
pm = ondemand
329
pm.process_idle_timeout = $PHPFPMIDLE
330
pm.max_children = $PHPFPMMAX
331
pm.max_requests = $PHPFPMREQ
332
EOF
333

    
334
elif [ $REALMEM -gt 1000 ]; then
335
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
336

    
337
pm = dynamic
338
pm.process_idle_timeout = $PHPFPMIDLE
339
pm.max_children = $PHPFPMMAX
340
pm.start_servers = $PHPFPMSTART
341
pm.max_requests = $PHPFPMREQ
342
pm.min_spare_servers=1
343
pm.max_spare_servers= $PHPFPMSPARE
344

    
345
EOF
346
else
347

    
348
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
349

    
350
pm = static
351
pm.max_children = $PHPFPMMAX
352
pm.max_requests = $PHPFPMREQ
353
EOF
354

    
355
fi
356

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

    
361
EOF
362

    
363
# Remove old log file if it exists.
364
if [ -f /var/run/php_modules_load_errors.txt ]; then
365
	/bin/rm /var/run/php_modules_load_errors.txt
366
fi
367

    
368
for EXT in $PHPMODULES; do
369
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
370
done
371

    
372
# Check loaded modules and remove anything that did not load correctly
373
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\["`
374
for EXT in $PHPMODULESLC; do
375
	SHOULDREMOVE="true"
376
	for LM in $LOADED_MODULES; do
377
		if [ "$EXT" = "$LM" ]; then
378
			SHOULDREMOVE="false"
379
			break
380
		fi
381
	done
382
	# Handle low memory situations
383
	if [ "$LOWMEM" = "TRUE" ]; then
384
		if [ "$EXT" = "opcache" ]; then
385
			SHOULDREMOVE="true"
386
		fi
387
		if [ "$EXT" = "xcache" ]; then
388
			SHOULDREMOVE="true"
389
		fi
390
	fi
391
	if [ "$SHOULDREMOVE" = "true" ]; then
392
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
393
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
394
			/bin/cat /usr/local/etc/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
395
			/bin/rm -f /usr/local/etc/php.ini
396
			/bin/mv /tmp/php.ini /usr/local/etc/php.ini
397
		fi
398
	fi
399
done
(65-65/83)