Project

General

Profile

Download (10 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 c0570a67 Renato Botelho
VERSION=`/bin/cat /etc/version`
31 5436d37a Renato Botelho
MIN_REALMEM_FOR_OPCACHE=512
32 c44417f8 Scott Ullrich
33 d759f499 Renato Botelho
EXTENSIONSDIR="/usr/local/lib/php/20131226/"
34 79454450 Scott Ullrich
35 0d60f50a Seth Mos
# Grab amount of memory that is detected
36 47eee8fa Scott Ullrich
if [ -f /var/log/dmesg.boot ]; then
37 839cad07 smos
	AVAILMEM=`/bin/cat /var/log/dmesg.boot |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
38 e173dd74 Phil Davis
else
39 839cad07 smos
	AVAILMEM=`/sbin/dmesg -a |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
40 47eee8fa Scott Ullrich
fi
41 c44417f8 Scott Ullrich
42 b713d790 Scott Ullrich
if [ -z "$AVAILMEM" ]; then
43 51d0f816 Ermal
	MEM=`/sbin/sysctl hw.physmem | cut -d':' -f2`
44
	AVAILMEM=`/bin/expr $MEM / 1048576`
45 b713d790 Scott Ullrich
fi
46
47 73fa0178 Individual IT Services
48
# Get amount of ram installed on this system
49
REALMEM=`/sbin/sysctl hw.realmem | /usr/bin/awk '{print $2/1048576}' | /usr/bin/awk -F '.' '{print $1}'`
50
export REALMEM
51
export LOWMEM
52
53 5436d37a Renato Botelho
if [  ${REALMEM} -lt $MIN_REALMEM_FOR_OPCACHE ]; then
54 73fa0178 Individual IT Services
	LOWMEM="TRUE"
55 5436d37a Renato Botelho
	echo ">>> Under $MIN_REALMEM_FOR_OPCACHE megabytes of ram detected.  Not enabling opcache"
56
	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
57 73fa0178 Individual IT Services
else
58
59 5436d37a Renato Botelho
	# Calculate opcache memory size according
60 73fa0178 Individual IT Services
	# to detected memory values
61
	if [ "$AVAILMEM" -gt "135" ]; then
62 5436d37a Renato Botelho
		OPCACHEMEMSIZE="10"
63 73fa0178 Individual IT Services
	fi
64
	if [ "$AVAILMEM" -gt "256" ]; then
65 5436d37a Renato Botelho
		OPCACHEMEMSIZE="20"
66 73fa0178 Individual IT Services
	fi
67
	if [ "$AVAILMEM" -gt "384" ]; then
68 5436d37a Renato Botelho
		OPCACHEMEMSIZE="25"
69 73fa0178 Individual IT Services
	fi
70
	if [ "$AVAILMEM" -gt "512" ]; then
71 5436d37a Renato Botelho
		OPCACHEMEMSIZE="30"
72 73fa0178 Individual IT Services
	fi
73
	if [ "$AVAILMEM" -gt "784" ]; then
74 5436d37a Renato Botelho
		OPCACHEMEMSIZE="50"
75 73fa0178 Individual IT Services
	fi
76 2b11ff4d Scott Ullrich
fi
77 40e46009 Scott Ullrich
78 332ea4dd Renato Botelho
/usr/bin/sort -u -o /usr/local/etc/php/extensions.ini /usr/local/etc/php/extensions.ini
79 1c225a98 jim-p
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 475f0b6d Renato Botelho
# rrd
137
PHPMODULES="$PHPMODULES rrd"
138 e83dca8c Scott Ullrich
139 aee36a29 Renato Botelho
PHP_ZEND_MODULES=""
140 8ef700da Scott Ullrich
141 80d887d1 Scott Ullrich
# Modules previously included.
142 fcdf9492 Scott Ullrich
# can be turned on by touching
143 80d887d1 Scott Ullrich
# /etc/php_dynamodules/$modulename
144
#	sysvmsg \
145
#	sysvsem \
146
#	sysvshm \
147
#	bcmath \
148
#	tokenizer \
149
#	uploadprogress \
150
#	sockets \
151
#	Reflection \
152
#	mysql \
153
#	bz2	\
154
155 5556f3a1 Ermal
# Clear the .ini file to make sure we are clean
156 69b27c16 Scott Ullrich
if [ -f /usr/local/etc/php.ini ]; then
157 51d0f816 Ermal
	/bin/rm /usr/local/etc/php.ini
158 69b27c16 Scott Ullrich
fi
159 cb7d18d5 Renato Botelho
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/grep -v "\["`
160 e83dca8c Scott Ullrich
161 a5c36eb2 Renato Botelho
unset TIMEZONE
162 339b8893 Renato Botelho
# Fetch the timezone from /var/db/zoneinfo if present
163
if [ -f /var/db/zoneinfo ]; then
164
	TIMEZONE=$(cat /var/db/zoneinfo)
165 a5c36eb2 Renato Botelho
fi
166
167
if [ -z "${TIMEZONE}" ]; then
168
	# Second option is from config.xml
169
	TIMEZONE=$(/usr/local/sbin/read_xml_tag.sh string system/timezone)
170
fi
171
172
if [ -z "${TIMEZONE}" ]; then
173 e4121dde Renato Botelho
	# Last option, use default value from $g or Etc/UTC
174
	TIMEZONE=$(/usr/local/sbin/read_global_var default_timezone "Etc/UTC")
175 a5c36eb2 Renato Botelho
fi
176
177 c0570a67 Renato Botelho
if echo "${VERSION}" | grep -q RELEASE; then
178
	error_reporting="error_reporting = E_ERROR | E_PARSE"
179
else
180
	error_reporting=""
181
fi
182
183 5556f3a1 Ermal
# Get a loaded module list in the stock php
184 2ed3203c Scott Ullrich
# Populate a dummy php.ini to avoid
185
# the file being clobbered and the firewall
186
# not being able to boot back up.
187 3646fbcb Renato Botelho
/bin/cat >/usr/local/etc/php.ini <<EOF
188 4b29393a Scott Ullrich
; File generated from /etc/rc.php_ini_setup
189 40e46009 Scott Ullrich
output_buffering = "0"
190
expose_php = Off
191
implicit_flush = true
192
magic_quotes_gpc = Off
193 9d0be827 smos
max_execution_time = 900
194 7e824233 smos
max_input_time = 1800
195 e56374a8 NewEraCracker
max_input_vars = 5000
196 40e46009 Scott Ullrich
register_argc_argv = On
197 aa205c3b Ermal
register_long_arrays = Off
198 362ec35d Ermal
variables_order = "GPCS"
199 40e46009 Scott Ullrich
file_uploads = On
200 31c96a14 Scott Ullrich
upload_tmp_dir = ${UPLOADTMPDIR}
201 96325dba Warren Baker
upload_max_filesize = 200M
202
post_max_size = 200M
203 40e46009 Scott Ullrich
html_errors = Off
204 f4015bd7 Scott Ullrich
zlib.output_compression = Off
205 40e46009 Scott Ullrich
zlib.output_compression_level = 1
206 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"
207 8919256f Scott Ullrich
display_startup_errors=on
208
display_errors=on
209 baa2dba2 Scott Ullrich
log_errors=on
210
error_log=/tmp/PHP_errors.log
211 0804f515 Scott Ullrich
extension_dir=${EXTENSIONSDIR}
212 9d0be827 smos
date.timezone="${TIMEZONE}"
213 3e582e4f NewEraCracker
session.hash_bits_per_character = 5
214
session.hash_function = 1
215 c0570a67 Renato Botelho
${error_reporting}
216 40e46009 Scott Ullrich
217 4b29393a Scott Ullrich
; Extensions
218 a8e61346 Ermal
219 40e46009 Scott Ullrich
EOF
220
221 e83dca8c Scott Ullrich
# Loop through and generate modules to load.
222
# Take into account modules built into php.
223
for EXT in $PHPMODULES; do
224
	SHOULDADD="true"
225 de96a790 Scott Ullrich
	# Check to see if module is compiled into php statically
226 e83dca8c Scott Ullrich
	for LM in $LOADED_MODULES; do
227
		if [ "$EXT" = "$LM" ]; then
228
			SHOULDADD="false"
229
		fi
230
	done
231
	if [ "$SHOULDADD" = "true" ]; then
232 de96a790 Scott Ullrich
		# Ensure extension exists before adding.
233 69b27c16 Scott Ullrich
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
234 3646fbcb Renato Botelho
			echo "extension=${EXT}.so" >> /usr/local/etc/php.ini
235 0804f515 Scott Ullrich
		fi
236 e83dca8c Scott Ullrich
	fi
237
done
238
239 8ef700da Scott Ullrich
# Zend modules
240
for EXT in $PHP_ZEND_MODULES; do
241
	# Ensure extension exists before adding.
242 aee36a29 Renato Botelho
	if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
243 3646fbcb Renato Botelho
		echo "zend_extension=${EXT}.so" >> /usr/local/etc/php.ini
244 8ef700da Scott Ullrich
	fi
245
done
246
247 73fa0178 Individual IT Services
if [ "$LOWMEM" != "TRUE" ]; then
248 40e46009 Scott Ullrich
249 3646fbcb Renato Botelho
	/bin/cat >>/usr/local/etc/php.ini <<EOF
250 e83dca8c Scott Ullrich
251 5436d37a Renato Botelho
; opcache Settings
252
opcache.enabled="1"
253
opcache.enable_cli="0"
254
opcache.memory_consumption="${OPCACHEMEMSIZE}"
255 40e46009 Scott Ullrich
256 a5c53d26 Renato Botelho
EOF
257
else
258 3646fbcb Renato Botelho
	/bin/cat >>/usr/local/etc/php.ini <<EOF
259 a5c53d26 Renato Botelho
; opcache Settings
260
opcache.enabled="0"
261 5556f3a1 Ermal
EOF
262
fi
263
264 3646fbcb Renato Botelho
	/bin/cat >>/usr/local/etc/php.ini <<EOF
265 5556f3a1 Ermal
266 a8e61346 Ermal
[suhosin]
267
suhosin.get.max_array_index_length = 256
268
suhosin.get.max_vars = 5000
269 4f1bace5 Ermal
suhosin.get.max_value_length = 500000
270 a8e61346 Ermal
suhosin.post.max_array_index_length = 256
271
suhosin.post.max_vars = 5000
272 4f1bace5 Ermal
suhosin.post.max_value_length = 500000
273 a8e61346 Ermal
suhosin.request.max_array_index_length = 256
274
suhosin.request.max_vars = 5000
275 4f1bace5 Ermal
suhosin.request.max_value_length = 500000
276 b9bc333b Scott Ullrich
suhosin.memory_limit = 512435456
277 a8e61346 Ermal
278 40e46009 Scott Ullrich
EOF
279
280
281 6d7ee1ab Ermal
PHPFPMMAX=3
282 7b03748b Ermal
if [ $REALMEM -lt 250 ]; then
283 6d7ee1ab Ermal
	PHPFPMMAX=2
284 4aea91d8 Ermal
elif [ ${REALMEM} -gt 1000 ]; then
285
	PHPFPMMAX=4
286
fi
287
288
/bin/cat > /usr/local/lib/php-fpm.conf <<EOF
289
290
[global]
291
pid = run/php-fpm.pid
292
error_log=syslog
293
syslog.facility = daemon
294 9e0fb701 Ermal
syslog.ident = system
295 3ffae79b Ermal
log_level = error
296 4aea91d8 Ermal
daemonize = yes
297
events.mechanism = kqueue
298
process.max = ${PHPFPMMAX}
299
300 40880eec Renato Botelho
[nginx]
301 4aea91d8 Ermal
user = root
302
group = wheel
303
;mode = 0600
304
305
listen = /var/run/php-fpm.socket
306
listen.owner = root
307
listen.group = wheel
308
listen.mode = 0600
309
310
security.limit_extensions =
311
312 5293c5c7 Warren Baker
; Pass environment variables
313
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
314 75b8eb83 Renato Botelho
env[LOGNAME] = root
315 5293c5c7 Warren Baker
316 4aea91d8 Ermal
EOF
317
318 5be2085a Ermal
if [ $REALMEM -lt 350 ]; then
319 4aea91d8 Ermal
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
320
321
pm = ondemand
322
pm.process_idle_timeout = 5
323 6d7ee1ab Ermal
pm.max_children = $PHPFPMMAX
324 4aea91d8 Ermal
pm.max_requests = 500
325
326
EOF
327
328
elif [ $REALMEM -gt 1000 ]; then
329
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
330
331
pm = dynamic
332
pm.process_idle_timeout = 5
333 6d7ee1ab Ermal
pm.max_children = $PHPFPMMAX
334 4aea91d8 Ermal
pm.start_servers = 1
335
pm.max_requests = 500
336
pm.min_spare_servers=1
337
pm.max_spare_servers=1
338
339
EOF
340
else
341
342
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
343
344
pm = static
345 6d7ee1ab Ermal
pm.max_children = $PHPFPMMAX
346 4aea91d8 Ermal
pm.max_requests = 500
347
348
EOF
349
350
fi
351
352 e1fda0c0 Scott Ullrich
# Remove old log file if it exists.
353
if [ -f /var/run/php_modules_load_errors.txt ]; then
354 51d0f816 Ermal
	/bin/rm /var/run/php_modules_load_errors.txt
355 e173dd74 Phil Davis
fi
356 e1fda0c0 Scott Ullrich
357 7030262c Scott Ullrich
for EXT in $PHPMODULES; do
358 51d0f816 Ermal
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
359 7030262c Scott Ullrich
done
360
361 e1fda0c0 Scott Ullrich
# Check loaded modules and remove anything that did not load correctly
362 cb7d18d5 Renato Botelho
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\["`
363 7030262c Scott Ullrich
for EXT in $PHPMODULESLC; do
364 e1fda0c0 Scott Ullrich
	SHOULDREMOVE="true"
365
	for LM in $LOADED_MODULES; do
366
		if [ "$EXT" = "$LM" ]; then
367
			SHOULDREMOVE="false"
368 14dd1631 Renato Botelho
			break
369 e173dd74 Phil Davis
		fi
370 e1fda0c0 Scott Ullrich
	done
371 1e86e897 Scott Ullrich
	# Handle low memory situations
372 8ee1dc80 Scott Ullrich
	if [ "$LOWMEM" = "TRUE" ]; then
373 5436d37a Renato Botelho
		if [ "$EXT" = "opcache" ]; then
374 8ee1dc80 Scott Ullrich
			SHOULDREMOVE="true"
375
		fi
376
		if [ "$EXT" = "xcache" ]; then
377
			SHOULDREMOVE="true"
378
		fi
379
	fi
380 e1fda0c0 Scott Ullrich
	if [ "$SHOULDREMOVE" = "true" ]; then
381
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
382
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
383 3646fbcb Renato Botelho
			/bin/cat /usr/local/etc/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
384
			/bin/rm -f /usr/local/etc/php.ini
385
			/bin/mv /tmp/php.ini /usr/local/etc/php.ini
386 e1fda0c0 Scott Ullrich
		fi
387
	fi
388
done