Project

General

Profile

Download (7.22 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=`cat /etc/platform`
30
EXTENSIONSDIR="/usr/local/lib/php/20060613/"
31

    
32
# Grab amount of memory that is detected
33
if [ -f /var/log/dmesg.boot ]; then
34
	AVAILMEM=`/bin/cat /var/log/dmesg.boot |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
35
else 
36
	AVAILMEM=`/sbin/dmesg -a |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
37
fi
38

    
39
if [ -z "$AVAILMEM" ]; then
40
	MEM=`sysctl hw.physmem | cut -d':' -f2`
41
	AVAILMEM=`expr $MEM / 1048576`
42
fi
43

    
44
# Calculate APC SHM size according 
45
# to detected memory values
46
if [ "$AVAILMEM" -lt "65" ]; then
47
	APCSHMEMSIZE="1"
48
fi
49
if [ "$AVAILMEM" -lt "96" ]; then
50
	APCSHMEMSIZE="5"
51
fi
52
if [ "$AVAILMEM" -lt "128" ]; then
53
	APCSHMEMSIZE="10"
54
fi
55
if [ "$AVAILMEM" -gt "128" ]; then
56
	APCSHMEMSIZE="15"
57
fi
58
if [ "$AVAILMEM" -gt "256" ]; then
59
	APCSHMEMSIZE="20"
60
fi
61
if [ "$AVAILMEM" -gt "384" ]; then
62
	APCSHMEMSIZE="25"
63
fi
64
if [ "$AVAILMEM" -gt "512" ]; then
65
	APCSHMEMSIZE="30"
66
fi
67
if [ "$AVAILMEM" -gt "784" ]; then
68
	APCSHMEMSIZE="35"
69
fi
70

    
71
# Set upload directory
72
if [ "$PLATFORM" = "embedded" -o "$PLATFORM" = "nanobsd" ]; then
73
	UPLOADTMPDIR="/root"
74
else 
75
	UPLOADTMPDIR="/tmp"
76
fi
77

    
78
# Define php modules.  Do not add .so, it will  
79
# be done automatically by the script below.
80
PHPMODULES="apc \
81
# Downloading via HTTP/FTP (pkg mgr, etc)
82
	curl \
83
	date \
84
# Internationalization 
85
	gettext \
86
# User manager
87
	ldap \
88
	openssl \
89
	pcntl \
90
# Regexs, PERL style!
91
	pcre \
92
# The mighty posix!
93
	posix \
94
	readline \
95
# Login sessions
96
	session \
97
	standard \
98
# Extra sanity seatbelts
99
	suhosin \
100
	pfSense \
101
# Firewall rules edit
102
	ctype \
103
# Config read/write
104
	xml \
105
	xmlreader \
106
	xmlwriter \
107
	libxml \
108
# user manager
109
	mhash \
110
# firewall_rules_edit.php
111
	mbstring \
112
	shmop \
113
# Page compression
114
	zlib \
115
# SQLlite
116
	sqlite \
117
	pdo \
118
# ZeroMQ
119
	zmq"
120

    
121
# Modules previously included.
122
# can be turned on by touching
123
# /etc/php_dynamodules/$modulename
124
#	sysvmsg \
125
#	sysvsem \
126
#	sysvshm \
127
#	bcmath \
128
#	tokenizer \
129
#	uploadprogress \
130
#	sockets \
131
#	Reflection \
132
#	mysql \
133
#	bz2	\
134
#	json \
135

    
136
# Get a loaded module list in the stock php
137
if [ -f /usr/local/etc/php.ini ]; then
138
	rm /usr/local/etc/php.ini
139
fi
140
if [ -f /usr/local/lib/php.ini ]; then
141
	rm /usr/local/lib/php.ini
142
fi
143
LOADED_MODULES=`php -m | grep -v "\["`
144

    
145
# Populate a dummy php.ini to avoid
146
# the file being clobbered and the firewall
147
# not being able to boot back up.
148
cat >/usr/local/lib/php.ini <<EOF
149
; File generated from /etc/rc.php_ini_setup
150
output_buffering = "0"
151
expose_php = Off
152
implicit_flush = true
153
magic_quotes_gpc = Off
154
max_execution_time = 99999999
155
max_input_time = 99999999
156
set_time_limit = 0
157
register_argc_argv = On
158
file_uploads = On
159
upload_tmp_dir = ${UPLOADTMPDIR}
160
upload_max_filesize = 100M
161
post_max_size = 100M
162
html_errors = Off
163
zlib.output_compression = Off
164
zlib.output_compression_level = 1
165
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg"
166
extension_dir=${EXTENSIONSDIR}
167

    
168
; Extensions
169

    
170
EOF
171

    
172
# Ensure directory exists
173
if [ ! -d /etc/php_dynamodules ]; then
174
	mkdir /etc/php_dynamodules
175
fi
176

    
177
# Read in dynamodules
178
if [ -d /etc/php_dynamodules ]; then
179
	DYNA_MODULES=`ls /etc/php_dynamodules/`
180
	PHPMODULES="$PHPMODULES $DYNA_MODULES"
181
fi
182

    
183
# Loop through and generate modules to load.
184
# Take into account modules built into php.
185
for EXT in $PHPMODULES; do
186
	SHOULDADD="true"
187
	# Check to see if module is compiled into php statically
188
	for LM in $LOADED_MODULES; do
189
		if [ "$EXT" = "$LM" ]; then
190
			SHOULDADD="false"
191
		fi
192
	done
193
	if [ "$SHOULDADD" = "true" ]; then
194
		# Ensure extension exists before adding.
195
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
196
			echo "extension=${EXT}.so" >> /usr/local/lib/php.ini
197
		fi
198
	fi
199
done
200

    
201
# Get amount of ram installed on this system
202
RAM=`sysctl hw.realmem | awk '{print $2/1000000}' | awk -F '.' '{print $1}'`
203
export RAM
204
export LOWMEM
205
if [ "$RAM" -lt "97" ]; then
206
	LOWMEM="TRUE"
207
	cat >>/usr/local/lib/php.ini <<EOF
208

    
209
[suhosin]
210
suhosin.get.max_array_depth = 5000
211
suhosin.get.max_array_index_length = 256
212
suhosin.get.max_vars = 5000
213
suhosin.post.max_array_depth = 5000
214
suhosin.post.max_array_index_length = 256
215
suhosin.post.max_vars = 5000
216
suhosin.request.max_array_depth = 5000
217
suhosin.request.max_array_index_length = 256
218
suhosin.request.max_vars = 5000
219

    
220
EOF
221

    
222
fi
223
if [  $RAM -gt 96 ]; then
224

    
225
	cat >>/usr/local/lib/php.ini <<EOF
226

    
227
; APC Settings
228
apc.enabled="1"
229
apc.enable_cli="1"
230
apc.shm_size="${APCSHMEMSIZE}"
231

    
232
[suhosin]
233
suhosin.get.max_array_depth = 5000
234
suhosin.get.max_array_index_length = 256
235
suhosin.get.max_vars = 5000
236
suhosin.post.max_array_depth = 5000
237
suhosin.post.max_array_index_length = 256
238
suhosin.post.max_vars = 5000
239
suhosin.request.max_array_depth = 5000
240
suhosin.request.max_array_index_length = 256
241
suhosin.request.max_vars = 5000
242

    
243
EOF
244

    
245
else 
246

    
247
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC."
248
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC." | logger -p daemon.info -i -t rc.php_ini_setup
249

    
250
fi
251

    
252
# Copy php.ini file to etc/ too (cli)
253
cp /usr/local/lib/php.ini /usr/local/etc/php.ini
254

    
255
# Remove old log file if it exists.
256
if [ -f /var/run/php_modules_load_errors.txt ]; then
257
	rm /var/run/php_modules_load_errors.txt
258
fi 
259

    
260
# Check loaded modules and remove anything that did not load correctly
261
LOADED_MODULES=`php -m 2>/dev/null | grep -v "\["`
262
for EXT in $PHPMODULES; do
263
	SHOULDREMOVE="true"
264
	for LM in $LOADED_MODULES; do
265
		if [ "$EXT" = "$LM" ]; then
266
			SHOULDREMOVE="false"
267
		fi		
268
	done
269
	# Handle low memory situations
270
	if [ "$LOWMEM" = "TRUE" ]; then
271
		if [ "$EXT" = "apc" ]; then
272
			SHOULDREMOVE="true"
273
		fi
274
		if [ "$EXT" = "xcache" ]; then
275
			SHOULDREMOVE="true"
276
		fi
277
	fi
278
	if [ "$SHOULDREMOVE" = "true" ]; then
279
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
280
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
281
			cat /usr/local/lib/php.ini | grep -v $EXT > /tmp/php.ini
282
			mv /tmp/php.ini /usr/local/lib/php.ini
283
		fi
284
	fi
285
done
286

    
287
# Copy php.ini file to etc/ too (cli)
288
cp /usr/local/lib/php.ini /usr/local/etc/php.ini
289

    
290

    
291

    
292

    
(75-75/97)