Project

General

Profile

Download (9.69 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
#	rc.php_ini_setup
4
#	Copyright (C) 2010 Scott Ullrich <sullrich@gmail.com>
5
#	All rights reserved.
6
#
7
#	Redistribution and use in source and binary forms, with or without
8
#	modification, are permitted provided that the following conditions are met:
9
#
10
#	1. Redistributions of source code must retain the above copyright notice,
11
#	   this list of conditions and the following disclaimer.
12
#
13
#	2. Redistributions in binary form must reproduce the above copyright
14
#	   notice, this list of conditions and the following disclaimer in the
15
#	   documentation and/or other materials provided with the distribution.
16
#
17
#	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
#	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19
#	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
#	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
#	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
#	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
#	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
#	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
#	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
#	POSSIBILITY OF SUCH DAMAGE.
27

    
28
# Set our operating platform
29
PLATFORM=`/bin/cat /etc/platform`
30
MIN_REALMEM_FOR_OPCACHE=512
31

    
32
EXTENSIONSDIR="/usr/local/lib/php/20131226/"
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 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 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
if [ "$PLATFORM" = "nanobsd" ]; then
79
	UPLOADTMPDIR=$(/usr/local/sbin/read_global_var upload_path "/root")
80
else
81
	UPLOADTMPDIR="/tmp"
82
fi
83

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

    
134
PHP_ZEND_MODULES=""
135

    
136
# Modules previously included.
137
# can be turned on by touching
138
# /etc/php_dynamodules/$modulename
139
#	sysvmsg \
140
#	sysvsem \
141
#	sysvshm \
142
#	bcmath \
143
#	tokenizer \
144
#	uploadprogress \
145
#	sockets \
146
#	Reflection \
147
#	mysql \
148
#	bz2	\
149

    
150
# Clear the .ini file to make sure we are clean
151
if [ -f /usr/local/etc/php.ini ]; then
152
	/bin/rm /usr/local/etc/php.ini
153
fi
154
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/grep -v "\["`
155

    
156
unset TIMEZONE
157
# Fetch the timezone from /var/db/zoneinfo if present
158
if [ -f /var/db/zoneinfo ]; then
159
	TIMEZONE=$(cat /var/db/zoneinfo)
160
fi
161

    
162
if [ -z "${TIMEZONE}" ]; then
163
	# Second option is from config.xml
164
	TIMEZONE=$(/usr/local/sbin/read_xml_tag.sh string system/timezone)
165
fi
166

    
167
if [ -z "${TIMEZONE}" ]; then
168
	# Last option, use default value from $g or Etc/UTC
169
	TIMEZONE=$(/usr/local/sbin/read_global_var default_timezone "Etc/UTC")
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
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"
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

    
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 = 512435456
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
		fi
357
	done
358
	# Handle low memory situations
359
	if [ "$LOWMEM" = "TRUE" ]; then
360
		if [ "$EXT" = "opcache" ]; then
361
			SHOULDREMOVE="true"
362
		fi
363
		if [ "$EXT" = "xcache" ]; then
364
			SHOULDREMOVE="true"
365
		fi
366
	fi
367
	if [ "$SHOULDREMOVE" = "true" ]; then
368
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
369
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
370
			/bin/cat /usr/local/etc/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
371
			/bin/rm -f /usr/local/etc/php.ini
372
			/bin/mv /tmp/php.ini /usr/local/etc/php.ini
373
		fi
374
	fi
375
done
(74-74/95)