Project

General

Profile

Download (9.21 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
# SSH2
114
PHPMODULES="$PHPMODULES ssh2"
115
# pfSense extensions
116
PHPMODULES="$PHPMODULES pfSense"
117
# json
118
PHPMODULES="$PHPMODULES json"
119
# bcmath
120
PHPMODULES="$PHPMODULES bcmath"
121
# filter
122
PHPMODULES="$PHPMODULES filter"
123
# rrd
124
PHPMODULES="$PHPMODULES rrd"
125

    
126
PHP_ZEND_MODULES=""
127

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

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

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

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

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

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

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

    
205
; Extensions
206

    
207
EOF
208

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

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

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

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

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

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

    
252
	/bin/cat >>/usr/local/etc/php.ini <<EOF
253

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

    
266
EOF
267

    
268

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

    
276
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
277

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

    
288
[nginx]
289
user = root
290
group = wheel
291
;mode = 0600
292

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

    
298
security.limit_extensions =
299

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

    
304
EOF
305

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

    
309
pm = ondemand
310
pm.process_idle_timeout = 5
311
pm.max_children = $PHPFPMMAX
312
pm.max_requests = 500
313

    
314
EOF
315

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

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

    
327
EOF
328
else
329

    
330
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
331

    
332
pm = static
333
pm.max_children = $PHPFPMMAX
334
pm.max_requests = 500
335

    
336
EOF
337

    
338
fi
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
(61-61/78)