Project

General

Profile

Download (9.17 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 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
EXTENSIONSDIR="/usr/local/lib/php/20131226/"
27

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

    
35
if [ -z "$AVAILMEM" ]; then
36
	MEM=`/sbin/sysctl hw.physmem | cut -d':' -f2`
37
	AVAILMEM=`/bin/expr $MEM / 1048576`
38
fi
39

    
40

    
41
# Get amount of ram installed on this system
42
REALMEM=`/sbin/sysctl hw.realmem | /usr/bin/awk '{print $2/1048576}' | /usr/bin/awk -F '.' '{print $1}'`
43
export REALMEM
44
export LOWMEM
45

    
46
if [  ${REALMEM} -lt $MIN_REALMEM_FOR_OPCACHE ]; then
47
	LOWMEM="TRUE"
48
	echo ">>> Under $MIN_REALMEM_FOR_OPCACHE megabytes of ram detected.  Not enabling opcache"
49
	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
50
else
51

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

    
71
# Set upload directory
72
UPLOADTMPDIR="/tmp"
73

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

    
124
PHP_ZEND_MODULES=""
125

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

    
140
# Clear the .ini file to make sure we are clean
141
if [ -f /usr/local/etc/php.ini ]; then
142
	/bin/rm /usr/local/etc/php.ini
143
fi
144
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/grep -v "\["`
145

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

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

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

    
162
if echo "${VERSION}" | grep -q RELEASE; then
163
	error_reporting="error_reporting = E_ERROR | E_PARSE"
164
else
165
	error_reporting=""
166
fi
167

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

    
203
; Extensions
204

    
205
EOF
206

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

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

    
233
if [ "$LOWMEM" != "TRUE" ]; then
234

    
235
	/bin/cat >>/usr/local/etc/php.ini <<EOF
236

    
237
; opcache Settings
238
opcache.enabled="1"
239
opcache.enable_cli="0"
240
opcache.memory_consumption="${OPCACHEMEMSIZE}"
241

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

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

    
252
[suhosin]
253
suhosin.get.max_array_index_length = 256
254
suhosin.get.max_vars = 5000
255
suhosin.get.max_value_length = 500000
256
suhosin.post.max_array_index_length = 256
257
suhosin.post.max_vars = 5000
258
suhosin.post.max_value_length = 500000
259
suhosin.request.max_array_index_length = 256
260
suhosin.request.max_vars = 5000
261
suhosin.request.max_value_length = 500000
262
suhosin.memory_limit = 805306368
263

    
264
EOF
265

    
266

    
267
PHPFPMMAX=3
268
if [ $REALMEM -lt 250 ]; then
269
	PHPFPMMAX=2
270
elif [ ${REALMEM} -gt 1000 ]; then
271
	PHPFPMMAX=4
272
fi
273

    
274
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
275

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

    
286
[nginx]
287
user = root
288
group = wheel
289
;mode = 0600
290

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

    
296
security.limit_extensions =
297

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

    
302
EOF
303

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

    
307
pm = ondemand
308
pm.process_idle_timeout = 5
309
pm.max_children = $PHPFPMMAX
310
pm.max_requests = 500
311

    
312
EOF
313

    
314
elif [ $REALMEM -gt 1000 ]; then
315
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
316

    
317
pm = dynamic
318
pm.process_idle_timeout = 5
319
pm.max_children = $PHPFPMMAX
320
pm.start_servers = 1
321
pm.max_requests = 500
322
pm.min_spare_servers=1
323
pm.max_spare_servers=1
324

    
325
EOF
326
else
327

    
328
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
329

    
330
pm = static
331
pm.max_children = $PHPFPMMAX
332
pm.max_requests = 500
333

    
334
EOF
335

    
336
fi
337

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

    
343
for EXT in $PHPMODULES; do
344
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
345
done
346

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