Project

General

Profile

Download (9.63 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

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