Project

General

Profile

Download (9.82 KB) Statistics
| Branch: | Tag: | Revision:
1 40e46009 Scott Ullrich
#!/bin/sh
2 de96a790 Scott Ullrich
#
3
#	rc.php_ini_setup
4 1e86e897 Scott Ullrich
#	Copyright (C) 2010 Scott Ullrich <sullrich@gmail.com>
5 de96a790 Scott Ullrich
#	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 40e46009 Scott Ullrich
28
# Set our operating platform
29 51d0f816 Ermal
PLATFORM=`/bin/cat /etc/platform`
30 5436d37a Renato Botelho
MIN_REALMEM_FOR_OPCACHE=512
31 c44417f8 Scott Ullrich
32 d759f499 Renato Botelho
EXTENSIONSDIR="/usr/local/lib/php/20131226/"
33 79454450 Scott Ullrich
34 0d60f50a Seth Mos
# Grab amount of memory that is detected
35 47eee8fa Scott Ullrich
if [ -f /var/log/dmesg.boot ]; then
36 839cad07 smos
	AVAILMEM=`/bin/cat /var/log/dmesg.boot |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
37 e173dd74 Phil Davis
else
38 839cad07 smos
	AVAILMEM=`/sbin/dmesg -a |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
39 47eee8fa Scott Ullrich
fi
40 c44417f8 Scott Ullrich
41 b713d790 Scott Ullrich
if [ -z "$AVAILMEM" ]; then
42 51d0f816 Ermal
	MEM=`/sbin/sysctl hw.physmem | cut -d':' -f2`
43
	AVAILMEM=`/bin/expr $MEM / 1048576`
44 b713d790 Scott Ullrich
fi
45
46 73fa0178 Individual IT Services
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 5436d37a Renato Botelho
if [  ${REALMEM} -lt $MIN_REALMEM_FOR_OPCACHE ]; then
53 73fa0178 Individual IT Services
	LOWMEM="TRUE"
54 5436d37a Renato Botelho
	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 73fa0178 Individual IT Services
else
57
58 5436d37a Renato Botelho
	# Calculate opcache memory size according
59 73fa0178 Individual IT Services
	# to detected memory values
60
	if [ "$AVAILMEM" -gt "135" ]; then
61 5436d37a Renato Botelho
		OPCACHEMEMSIZE="10"
62 73fa0178 Individual IT Services
	fi
63
	if [ "$AVAILMEM" -gt "256" ]; then
64 5436d37a Renato Botelho
		OPCACHEMEMSIZE="20"
65 73fa0178 Individual IT Services
	fi
66
	if [ "$AVAILMEM" -gt "384" ]; then
67 5436d37a Renato Botelho
		OPCACHEMEMSIZE="25"
68 73fa0178 Individual IT Services
	fi
69
	if [ "$AVAILMEM" -gt "512" ]; then
70 5436d37a Renato Botelho
		OPCACHEMEMSIZE="30"
71 73fa0178 Individual IT Services
	fi
72
	if [ "$AVAILMEM" -gt "784" ]; then
73 5436d37a Renato Botelho
		OPCACHEMEMSIZE="50"
74 73fa0178 Individual IT Services
	fi
75 2b11ff4d Scott Ullrich
fi
76 40e46009 Scott Ullrich
77 1c225a98 jim-p
/usr/bin/sort -u /usr/local/etc/php/extensions.ini > /tmp/extensions.ini
78
/bin/mv /tmp/extensions.ini /usr/local/etc/php/extensions.ini
79
80 31c96a14 Scott Ullrich
# Set upload directory
81 e61f548f Ermal
if [ "$PLATFORM" = "nanobsd" ]; then
82 e4121dde Renato Botelho
	UPLOADTMPDIR=$(/usr/local/sbin/read_global_var upload_path "/root")
83 e173dd74 Phil Davis
else
84 31c96a14 Scott Ullrich
	UPLOADTMPDIR="/tmp"
85
fi
86
87 e173dd74 Phil Davis
# Define php modules.  Do not add .so, it will
88 de96a790 Scott Ullrich
# be done automatically by the script below.
89 a4fc6ec7 Ermal
PHPMODULES="standard"
90 c25197ba smos
# Config read/write
91
PHPMODULES="$PHPMODULES xml libxml dom"
92 e115bd22 Renato Botelho
PHPMODULES="$PHPMODULES SimpleXML xmlreader xmlwriter"
93 be1db1d3 Scott Ullrich
# Downloading via HTTP/FTP (pkg mgr, etc)
94 5556f3a1 Ermal
PHPMODULES="$PHPMODULES curl date"
95 e173dd74 Phil Davis
# Internationalization
96 5556f3a1 Ermal
PHPMODULES="$PHPMODULES gettext"
97 be1db1d3 Scott Ullrich
# User manager
98 5556f3a1 Ermal
PHPMODULES="$PHPMODULES ldap openssl pcntl"
99 d9867431 jim-p
PHPMODULES="$PHPMODULES hash"
100 b3765f4c Roberto Nunnari
PHPMODULES="$PHPMODULES mcrypt"
101 be1db1d3 Scott Ullrich
# Regexs, PERL style!
102 5556f3a1 Ermal
PHPMODULES="$PHPMODULES pcre"
103 be1db1d3 Scott Ullrich
# The mighty posix!
104 5556f3a1 Ermal
PHPMODULES="$PHPMODULES posix"
105
PHPMODULES="$PHPMODULES readline"
106 be1db1d3 Scott Ullrich
# Login sessions
107 5556f3a1 Ermal
PHPMODULES="$PHPMODULES session"
108 be1db1d3 Scott Ullrich
# Extra sanity seatbelts
109 c25197ba smos
PHPMODULES="$PHPMODULES suhosin"
110 fca48a65 Scott Ullrich
# Firewall rules edit
111 5556f3a1 Ermal
PHPMODULES="$PHPMODULES ctype"
112 16058a05 Scott Ullrich
# firewall_rules_edit.php
113 5556f3a1 Ermal
PHPMODULES="$PHPMODULES mbstring"
114
# Synchronization primitives
115
PHPMODULES="$PHPMODULES shmop"
116 be1db1d3 Scott Ullrich
# Page compression
117 5556f3a1 Ermal
PHPMODULES="$PHPMODULES zlib"
118
# SQLlite & Database
119
PHPMODULES="$PHPMODULES spl"
120 e115bd22 Renato Botelho
PHPMODULES="$PHPMODULES PDO"
121 6f657dfd Renato Botelho
PHPMODULES="$PHPMODULES sqlite3"
122 04747c75 Warren Baker
# RADIUS
123
PHPMODULES="$PHPMODULES radius"
124 e929c925 Scott Ullrich
# ZeroMQ
125 5556f3a1 Ermal
PHPMODULES="$PHPMODULES zmq"
126 c88ff708 Scott Ullrich
# SSH2
127
PHPMODULES="$PHPMODULES ssh2"
128 5556f3a1 Ermal
# pfSense extensions
129
PHPMODULES="$PHPMODULES pfSense"
130 a1f77238 Darren Embry
# json
131
PHPMODULES="$PHPMODULES json"
132 c1993935 jim-p
# bcmath
133
PHPMODULES="$PHPMODULES bcmath"
134 c28da0a7 Matt Smith
# filter
135
PHPMODULES="$PHPMODULES filter"
136 e83dca8c Scott Ullrich
137 aee36a29 Renato Botelho
PHP_ZEND_MODULES=""
138 8ef700da Scott Ullrich
139 80d887d1 Scott Ullrich
# Modules previously included.
140 fcdf9492 Scott Ullrich
# can be turned on by touching
141 80d887d1 Scott Ullrich
# /etc/php_dynamodules/$modulename
142
#	sysvmsg \
143
#	sysvsem \
144
#	sysvshm \
145
#	bcmath \
146
#	tokenizer \
147
#	uploadprogress \
148
#	sockets \
149
#	Reflection \
150
#	mysql \
151
#	bz2	\
152
153 5556f3a1 Ermal
# Clear the .ini file to make sure we are clean
154 69b27c16 Scott Ullrich
if [ -f /usr/local/etc/php.ini ]; then
155 51d0f816 Ermal
	/bin/rm /usr/local/etc/php.ini
156 69b27c16 Scott Ullrich
fi
157 cb7d18d5 Renato Botelho
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/grep -v "\["`
158 e83dca8c Scott Ullrich
159 a5c36eb2 Renato Botelho
unset TIMEZONE
160 339b8893 Renato Botelho
# Fetch the timezone from /var/db/zoneinfo if present
161
if [ -f /var/db/zoneinfo ]; then
162
	TIMEZONE=$(cat /var/db/zoneinfo)
163 a5c36eb2 Renato Botelho
fi
164
165
if [ -z "${TIMEZONE}" ]; then
166
	# Second option is from config.xml
167
	TIMEZONE=$(/usr/local/sbin/read_xml_tag.sh string system/timezone)
168
fi
169
170
if [ -z "${TIMEZONE}" ]; then
171 e4121dde Renato Botelho
	# Last option, use default value from $g or Etc/UTC
172
	TIMEZONE=$(/usr/local/sbin/read_global_var default_timezone "Etc/UTC")
173 a5c36eb2 Renato Botelho
fi
174
175 5556f3a1 Ermal
# Get a loaded module list in the stock php
176 2ed3203c Scott Ullrich
# Populate a dummy php.ini to avoid
177
# the file being clobbered and the firewall
178
# not being able to boot back up.
179 3646fbcb Renato Botelho
/bin/cat >/usr/local/etc/php.ini <<EOF
180 4b29393a Scott Ullrich
; File generated from /etc/rc.php_ini_setup
181 40e46009 Scott Ullrich
output_buffering = "0"
182
expose_php = Off
183
implicit_flush = true
184
magic_quotes_gpc = Off
185 9d0be827 smos
max_execution_time = 900
186 7e824233 smos
max_input_time = 1800
187 e56374a8 NewEraCracker
max_input_vars = 5000
188 40e46009 Scott Ullrich
register_argc_argv = On
189 aa205c3b Ermal
register_long_arrays = Off
190 362ec35d Ermal
variables_order = "GPCS"
191 40e46009 Scott Ullrich
file_uploads = On
192 31c96a14 Scott Ullrich
upload_tmp_dir = ${UPLOADTMPDIR}
193 96325dba Warren Baker
upload_max_filesize = 200M
194
post_max_size = 200M
195 40e46009 Scott Ullrich
html_errors = Off
196 f4015bd7 Scott Ullrich
zlib.output_compression = Off
197 40e46009 Scott Ullrich
zlib.output_compression_level = 1
198 93b1f6fb Stephen Beaver
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg:/usr/local/www/classes:/usr/local/www/classes/Form"
199 8919256f Scott Ullrich
display_startup_errors=on
200
display_errors=on
201 baa2dba2 Scott Ullrich
log_errors=on
202
error_log=/tmp/PHP_errors.log
203 0804f515 Scott Ullrich
extension_dir=${EXTENSIONSDIR}
204 9d0be827 smos
date.timezone="${TIMEZONE}"
205 3e582e4f NewEraCracker
session.hash_bits_per_character = 5
206
session.hash_function = 1
207 40e46009 Scott Ullrich
208 4b29393a Scott Ullrich
; Extensions
209 a8e61346 Ermal
210 40e46009 Scott Ullrich
EOF
211
212 e83dca8c Scott Ullrich
# Loop through and generate modules to load.
213
# Take into account modules built into php.
214
for EXT in $PHPMODULES; do
215
	SHOULDADD="true"
216 de96a790 Scott Ullrich
	# Check to see if module is compiled into php statically
217 e83dca8c Scott Ullrich
	for LM in $LOADED_MODULES; do
218
		if [ "$EXT" = "$LM" ]; then
219
			SHOULDADD="false"
220
		fi
221
	done
222
	if [ "$SHOULDADD" = "true" ]; then
223 de96a790 Scott Ullrich
		# Ensure extension exists before adding.
224 69b27c16 Scott Ullrich
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
225 3646fbcb Renato Botelho
			echo "extension=${EXT}.so" >> /usr/local/etc/php.ini
226 0804f515 Scott Ullrich
		fi
227 e83dca8c Scott Ullrich
	fi
228
done
229
230 8ef700da Scott Ullrich
# Zend modules
231
for EXT in $PHP_ZEND_MODULES; do
232
	# Ensure extension exists before adding.
233 aee36a29 Renato Botelho
	if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
234 3646fbcb Renato Botelho
		echo "zend_extension=${EXT}.so" >> /usr/local/etc/php.ini
235 8ef700da Scott Ullrich
	fi
236
done
237
238 73fa0178 Individual IT Services
if [ "$LOWMEM" != "TRUE" ]; then
239 40e46009 Scott Ullrich
240 3646fbcb Renato Botelho
	/bin/cat >>/usr/local/etc/php.ini <<EOF
241 e83dca8c Scott Ullrich
242 5436d37a Renato Botelho
; opcache Settings
243
opcache.enabled="1"
244
opcache.enable_cli="0"
245
opcache.memory_consumption="${OPCACHEMEMSIZE}"
246 40e46009 Scott Ullrich
247 a5c53d26 Renato Botelho
EOF
248
else
249 3646fbcb Renato Botelho
	/bin/cat >>/usr/local/etc/php.ini <<EOF
250 a5c53d26 Renato Botelho
; opcache Settings
251
opcache.enabled="0"
252 5556f3a1 Ermal
EOF
253
fi
254
255 3646fbcb Renato Botelho
	/bin/cat >>/usr/local/etc/php.ini <<EOF
256 5556f3a1 Ermal
257 a8e61346 Ermal
[suhosin]
258
suhosin.get.max_array_index_length = 256
259
suhosin.get.max_vars = 5000
260 4f1bace5 Ermal
suhosin.get.max_value_length = 500000
261 a8e61346 Ermal
suhosin.post.max_array_index_length = 256
262
suhosin.post.max_vars = 5000
263 4f1bace5 Ermal
suhosin.post.max_value_length = 500000
264 a8e61346 Ermal
suhosin.request.max_array_index_length = 256
265
suhosin.request.max_vars = 5000
266 4f1bace5 Ermal
suhosin.request.max_value_length = 500000
267 b9bc333b Scott Ullrich
suhosin.memory_limit = 512435456
268 a8e61346 Ermal
269 40e46009 Scott Ullrich
EOF
270
271
272 6d7ee1ab Ermal
PHPFPMMAX=3
273 7b03748b Ermal
if [ $REALMEM -lt 250 ]; then
274 6d7ee1ab Ermal
	PHPFPMMAX=2
275 4aea91d8 Ermal
elif [ ${REALMEM} -gt 1000 ]; then
276
	PHPFPMMAX=4
277
fi
278
279
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
280
281
[global]
282
pid = run/php-fpm.pid
283
error_log=syslog
284
syslog.facility = daemon
285 9e0fb701 Ermal
syslog.ident = system
286 3ffae79b Ermal
log_level = error
287 4aea91d8 Ermal
daemonize = yes
288
events.mechanism = kqueue
289
process.max = ${PHPFPMMAX}
290
291 40880eec Renato Botelho
[nginx]
292 4aea91d8 Ermal
user = root
293
group = wheel
294
;mode = 0600
295
296
listen = /var/run/php-fpm.socket
297
listen.owner = root
298
listen.group = wheel
299
listen.mode = 0600
300
301
security.limit_extensions =
302
303 5293c5c7 Warren Baker
; Pass environment variables
304
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
305 75b8eb83 Renato Botelho
env[LOGNAME] = root
306 5293c5c7 Warren Baker
307 4aea91d8 Ermal
EOF
308
309 5be2085a Ermal
if [ $REALMEM -lt 350 ]; then
310 4aea91d8 Ermal
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
311
312
pm = ondemand
313
pm.process_idle_timeout = 5
314 6d7ee1ab Ermal
pm.max_children = $PHPFPMMAX
315 4aea91d8 Ermal
pm.max_requests = 500
316
317
EOF
318
319
elif [ $REALMEM -gt 1000 ]; then
320
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
321
322
pm = dynamic
323
pm.process_idle_timeout = 5
324 6d7ee1ab Ermal
pm.max_children = $PHPFPMMAX
325 4aea91d8 Ermal
pm.start_servers = 1
326
pm.max_requests = 500
327
pm.min_spare_servers=1
328
pm.max_spare_servers=1
329
330
EOF
331
else
332
333
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
334
335
pm = static
336 6d7ee1ab Ermal
pm.max_children = $PHPFPMMAX
337 4aea91d8 Ermal
pm.max_requests = 500
338
339
EOF
340
341
fi
342
343 e1fda0c0 Scott Ullrich
# Remove old log file if it exists.
344
if [ -f /var/run/php_modules_load_errors.txt ]; then
345 51d0f816 Ermal
	/bin/rm /var/run/php_modules_load_errors.txt
346 e173dd74 Phil Davis
fi
347 e1fda0c0 Scott Ullrich
348 7030262c Scott Ullrich
for EXT in $PHPMODULES; do
349 51d0f816 Ermal
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
350 7030262c Scott Ullrich
done
351
352 e1fda0c0 Scott Ullrich
# Check loaded modules and remove anything that did not load correctly
353 cb7d18d5 Renato Botelho
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\["`
354 7030262c Scott Ullrich
for EXT in $PHPMODULESLC; do
355 e1fda0c0 Scott Ullrich
	SHOULDREMOVE="true"
356
	for LM in $LOADED_MODULES; do
357
		if [ "$EXT" = "$LM" ]; then
358
			SHOULDREMOVE="false"
359 e173dd74 Phil Davis
		fi
360 e1fda0c0 Scott Ullrich
	done
361 1e86e897 Scott Ullrich
	# Handle low memory situations
362 8ee1dc80 Scott Ullrich
	if [ "$LOWMEM" = "TRUE" ]; then
363 5436d37a Renato Botelho
		if [ "$EXT" = "opcache" ]; then
364 8ee1dc80 Scott Ullrich
			SHOULDREMOVE="true"
365
		fi
366
		if [ "$EXT" = "xcache" ]; then
367
			SHOULDREMOVE="true"
368
		fi
369
	fi
370 e1fda0c0 Scott Ullrich
	if [ "$SHOULDREMOVE" = "true" ]; then
371
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
372
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
373 3646fbcb Renato Botelho
			/bin/cat /usr/local/etc/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
374
			/bin/rm -f /usr/local/etc/php.ini
375
			/bin/mv /tmp/php.ini /usr/local/etc/php.ini
376 e1fda0c0 Scott Ullrich
		fi
377
	fi
378
done