Project

General

Profile

Download (9.72 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 31c96a14 Scott Ullrich
# Set upload directory
78 e61f548f Ermal
if [ "$PLATFORM" = "nanobsd" ]; then
79 e4121dde Renato Botelho
	UPLOADTMPDIR=$(/usr/local/sbin/read_global_var upload_path "/root")
80 e173dd74 Phil Davis
else
81 31c96a14 Scott Ullrich
	UPLOADTMPDIR="/tmp"
82
fi
83
84 e173dd74 Phil Davis
# Define php modules.  Do not add .so, it will
85 de96a790 Scott Ullrich
# be done automatically by the script below.
86 a4fc6ec7 Ermal
PHPMODULES="standard"
87 c25197ba smos
# Config read/write
88
PHPMODULES="$PHPMODULES xml libxml dom"
89 e115bd22 Renato Botelho
PHPMODULES="$PHPMODULES SimpleXML xmlreader xmlwriter"
90 be1db1d3 Scott Ullrich
# Downloading via HTTP/FTP (pkg mgr, etc)
91 5556f3a1 Ermal
PHPMODULES="$PHPMODULES curl date"
92 e173dd74 Phil Davis
# Internationalization
93 5556f3a1 Ermal
PHPMODULES="$PHPMODULES gettext"
94 be1db1d3 Scott Ullrich
# User manager
95 5556f3a1 Ermal
PHPMODULES="$PHPMODULES ldap openssl pcntl"
96 d9867431 jim-p
PHPMODULES="$PHPMODULES hash"
97 b3765f4c Roberto Nunnari
PHPMODULES="$PHPMODULES mcrypt"
98 be1db1d3 Scott Ullrich
# Regexs, PERL style!
99 5556f3a1 Ermal
PHPMODULES="$PHPMODULES pcre"
100 be1db1d3 Scott Ullrich
# The mighty posix!
101 5556f3a1 Ermal
PHPMODULES="$PHPMODULES posix"
102
PHPMODULES="$PHPMODULES readline"
103 be1db1d3 Scott Ullrich
# Login sessions
104 5556f3a1 Ermal
PHPMODULES="$PHPMODULES session"
105 be1db1d3 Scott Ullrich
# Extra sanity seatbelts
106 c25197ba smos
PHPMODULES="$PHPMODULES suhosin"
107 fca48a65 Scott Ullrich
# Firewall rules edit
108 5556f3a1 Ermal
PHPMODULES="$PHPMODULES ctype"
109 16058a05 Scott Ullrich
# firewall_rules_edit.php
110 5556f3a1 Ermal
PHPMODULES="$PHPMODULES mbstring"
111
# Synchronization primitives
112
PHPMODULES="$PHPMODULES shmop"
113 be1db1d3 Scott Ullrich
# Page compression
114 5556f3a1 Ermal
PHPMODULES="$PHPMODULES zlib"
115
# SQLlite & Database
116
PHPMODULES="$PHPMODULES spl"
117 e115bd22 Renato Botelho
PHPMODULES="$PHPMODULES PDO"
118 6f657dfd Renato Botelho
PHPMODULES="$PHPMODULES sqlite3"
119 04747c75 Warren Baker
# RADIUS
120
PHPMODULES="$PHPMODULES radius"
121 e929c925 Scott Ullrich
# ZeroMQ
122 5556f3a1 Ermal
PHPMODULES="$PHPMODULES zmq"
123 c88ff708 Scott Ullrich
# SSH2
124
PHPMODULES="$PHPMODULES ssh2"
125 5556f3a1 Ermal
# pfSense extensions
126
PHPMODULES="$PHPMODULES pfSense"
127 a1f77238 Darren Embry
# json
128
PHPMODULES="$PHPMODULES json"
129 c1993935 jim-p
# bcmath
130
PHPMODULES="$PHPMODULES bcmath"
131 c28da0a7 Matt Smith
# filter
132
PHPMODULES="$PHPMODULES filter"
133 e83dca8c Scott Ullrich
134 aee36a29 Renato Botelho
PHP_ZEND_MODULES=""
135 8ef700da Scott Ullrich
136 80d887d1 Scott Ullrich
# Modules previously included.
137 fcdf9492 Scott Ullrich
# can be turned on by touching
138 80d887d1 Scott Ullrich
# /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 5556f3a1 Ermal
# Clear the .ini file to make sure we are clean
151 69b27c16 Scott Ullrich
if [ -f /usr/local/etc/php.ini ]; then
152 51d0f816 Ermal
	/bin/rm /usr/local/etc/php.ini
153 69b27c16 Scott Ullrich
fi
154 cb7d18d5 Renato Botelho
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/grep -v "\["`
155 e83dca8c Scott Ullrich
156 a5c36eb2 Renato Botelho
unset TIMEZONE
157 339b8893 Renato Botelho
# Fetch the timezone from /var/db/zoneinfo if present
158
if [ -f /var/db/zoneinfo ]; then
159
	TIMEZONE=$(cat /var/db/zoneinfo)
160 a5c36eb2 Renato Botelho
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 e4121dde Renato Botelho
	# Last option, use default value from $g or Etc/UTC
169
	TIMEZONE=$(/usr/local/sbin/read_global_var default_timezone "Etc/UTC")
170 a5c36eb2 Renato Botelho
fi
171
172 5556f3a1 Ermal
# Get a loaded module list in the stock php
173 2ed3203c Scott Ullrich
# Populate a dummy php.ini to avoid
174
# the file being clobbered and the firewall
175
# not being able to boot back up.
176 3646fbcb Renato Botelho
/bin/cat >/usr/local/etc/php.ini <<EOF
177 4b29393a Scott Ullrich
; File generated from /etc/rc.php_ini_setup
178 40e46009 Scott Ullrich
output_buffering = "0"
179
expose_php = Off
180
implicit_flush = true
181
magic_quotes_gpc = Off
182 9d0be827 smos
max_execution_time = 900
183 7e824233 smos
max_input_time = 1800
184 40e46009 Scott Ullrich
register_argc_argv = On
185 aa205c3b Ermal
register_long_arrays = Off
186 362ec35d Ermal
variables_order = "GPCS"
187 40e46009 Scott Ullrich
file_uploads = On
188 31c96a14 Scott Ullrich
upload_tmp_dir = ${UPLOADTMPDIR}
189 96325dba Warren Baker
upload_max_filesize = 200M
190
post_max_size = 200M
191 40e46009 Scott Ullrich
html_errors = Off
192 f4015bd7 Scott Ullrich
zlib.output_compression = Off
193 40e46009 Scott Ullrich
zlib.output_compression_level = 1
194 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"
195 8919256f Scott Ullrich
display_startup_errors=on
196
display_errors=on
197 baa2dba2 Scott Ullrich
log_errors=on
198
error_log=/tmp/PHP_errors.log
199 0804f515 Scott Ullrich
extension_dir=${EXTENSIONSDIR}
200 9d0be827 smos
date.timezone="${TIMEZONE}"
201 40e46009 Scott Ullrich
202 4b29393a Scott Ullrich
; Extensions
203 a8e61346 Ermal
204 40e46009 Scott Ullrich
EOF
205
206 e83dca8c Scott Ullrich
# Loop through and generate modules to load.
207
# Take into account modules built into php.
208
for EXT in $PHPMODULES; do
209
	SHOULDADD="true"
210 de96a790 Scott Ullrich
	# Check to see if module is compiled into php statically
211 e83dca8c Scott Ullrich
	for LM in $LOADED_MODULES; do
212
		if [ "$EXT" = "$LM" ]; then
213
			SHOULDADD="false"
214
		fi
215
	done
216
	if [ "$SHOULDADD" = "true" ]; then
217 de96a790 Scott Ullrich
		# Ensure extension exists before adding.
218 69b27c16 Scott Ullrich
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
219 3646fbcb Renato Botelho
			echo "extension=${EXT}.so" >> /usr/local/etc/php.ini
220 0804f515 Scott Ullrich
		fi
221 e83dca8c Scott Ullrich
	fi
222
done
223
224 8ef700da Scott Ullrich
# Zend modules
225
for EXT in $PHP_ZEND_MODULES; do
226
	# Ensure extension exists before adding.
227 aee36a29 Renato Botelho
	if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
228 3646fbcb Renato Botelho
		echo "zend_extension=${EXT}.so" >> /usr/local/etc/php.ini
229 8ef700da Scott Ullrich
	fi
230
done
231
232 73fa0178 Individual IT Services
if [ "$LOWMEM" != "TRUE" ]; then
233 40e46009 Scott Ullrich
234 3646fbcb Renato Botelho
	/bin/cat >>/usr/local/etc/php.ini <<EOF
235 e83dca8c Scott Ullrich
236 5436d37a Renato Botelho
; opcache Settings
237
opcache.enabled="1"
238
opcache.enable_cli="0"
239
opcache.memory_consumption="${OPCACHEMEMSIZE}"
240 40e46009 Scott Ullrich
241 a5c53d26 Renato Botelho
EOF
242
else
243 3646fbcb Renato Botelho
	/bin/cat >>/usr/local/etc/php.ini <<EOF
244 a5c53d26 Renato Botelho
; opcache Settings
245
opcache.enabled="0"
246 5556f3a1 Ermal
EOF
247
fi
248
249 3646fbcb Renato Botelho
	/bin/cat >>/usr/local/etc/php.ini <<EOF
250 5556f3a1 Ermal
251 a8e61346 Ermal
[suhosin]
252
suhosin.get.max_array_depth = 5000
253
suhosin.get.max_array_index_length = 256
254
suhosin.get.max_vars = 5000
255 4f1bace5 Ermal
suhosin.get.max_value_length = 500000
256 a8e61346 Ermal
suhosin.post.max_array_depth = 5000
257
suhosin.post.max_array_index_length = 256
258
suhosin.post.max_vars = 5000
259 4f1bace5 Ermal
suhosin.post.max_value_length = 500000
260 a8e61346 Ermal
suhosin.request.max_array_depth = 5000
261
suhosin.request.max_array_index_length = 256
262
suhosin.request.max_vars = 5000
263 4f1bace5 Ermal
suhosin.request.max_value_length = 500000
264 b9bc333b Scott Ullrich
suhosin.memory_limit = 512435456
265 a8e61346 Ermal
266 40e46009 Scott Ullrich
EOF
267
268
269 6d7ee1ab Ermal
PHPFPMMAX=3
270 7b03748b Ermal
if [ $REALMEM -lt 250 ]; then
271 6d7ee1ab Ermal
	PHPFPMMAX=2
272 4aea91d8 Ermal
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 9e0fb701 Ermal
syslog.ident = system
283 3ffae79b Ermal
log_level = error
284 4aea91d8 Ermal
daemonize = yes
285
events.mechanism = kqueue
286
process.max = ${PHPFPMMAX}
287
288 40880eec Renato Botelho
[nginx]
289 4aea91d8 Ermal
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 5293c5c7 Warren Baker
; Pass environment variables
301
env[PATH] = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
302 75b8eb83 Renato Botelho
env[LOGNAME] = root
303 5293c5c7 Warren Baker
304 4aea91d8 Ermal
EOF
305
306 5be2085a Ermal
if [ $REALMEM -lt 350 ]; then
307 4aea91d8 Ermal
	/bin/cat >> /usr/local/lib/php-fpm.conf <<EOF
308
309
pm = ondemand
310
pm.process_idle_timeout = 5
311 6d7ee1ab Ermal
pm.max_children = $PHPFPMMAX
312 4aea91d8 Ermal
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 6d7ee1ab Ermal
pm.max_children = $PHPFPMMAX
322 4aea91d8 Ermal
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 6d7ee1ab Ermal
pm.max_children = $PHPFPMMAX
334 4aea91d8 Ermal
pm.max_requests = 500
335
336
EOF
337
338
fi
339
340 e1fda0c0 Scott Ullrich
# Remove old log file if it exists.
341
if [ -f /var/run/php_modules_load_errors.txt ]; then
342 51d0f816 Ermal
	/bin/rm /var/run/php_modules_load_errors.txt
343 e173dd74 Phil Davis
fi
344 e1fda0c0 Scott Ullrich
345 7030262c Scott Ullrich
for EXT in $PHPMODULES; do
346 51d0f816 Ermal
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
347 7030262c Scott Ullrich
done
348
349 e1fda0c0 Scott Ullrich
# Check loaded modules and remove anything that did not load correctly
350 cb7d18d5 Renato Botelho
LOADED_MODULES=`/usr/local/bin/php-cgi -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\["`
351 7030262c Scott Ullrich
for EXT in $PHPMODULESLC; do
352 e1fda0c0 Scott Ullrich
	SHOULDREMOVE="true"
353
	for LM in $LOADED_MODULES; do
354
		if [ "$EXT" = "$LM" ]; then
355
			SHOULDREMOVE="false"
356 e173dd74 Phil Davis
		fi
357 e1fda0c0 Scott Ullrich
	done
358 1e86e897 Scott Ullrich
	# Handle low memory situations
359 8ee1dc80 Scott Ullrich
	if [ "$LOWMEM" = "TRUE" ]; then
360 5436d37a Renato Botelho
		if [ "$EXT" = "opcache" ]; then
361 8ee1dc80 Scott Ullrich
			SHOULDREMOVE="true"
362
		fi
363
		if [ "$EXT" = "xcache" ]; then
364
			SHOULDREMOVE="true"
365
		fi
366
	fi
367 e1fda0c0 Scott Ullrich
	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 3646fbcb Renato Botelho
			/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 e1fda0c0 Scott Ullrich
		fi
374
	fi
375
done