Custom PHP 5.3.1 with APC and XDEBUG on (Dreamhost) Shared Host

I’ve recently been setting up my new dreamhost for symfony projects deployment and the only thing the default PHP is missing is the support for APC (alternate php cache). So after looking at the dreamhost wiki I cleaned up and added some features to the one of the install scripts. Here it is for your/mine (future) commodity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/sh
 
# update 16.2.2010 
# @author Marco Bernasocchi <[email protected]>
# - Added OPENSSL LIBMCRYPT LIBTOOL and a promt for installing XDEBUG
#   (still uses XDEBUG 2.05, which has basic PHP 5.3.1 support. 2.1.0 is on its way)
# - removed unsupported php configure switches
# - disabled --with-xsl, if you want to use it you'll probably need to install
#   libxslt (http://xmlsoft.org/XSLT/) version 1.1.0 or greater.
# - sets the ini files into the install directory instead than on cgi-bin
# - to add more domains just copy the cgi binary to the new domain
#   cp "$PHP_BIN_DIR/php-cgi" "$NEW_CGI_BIN_DIR/php.cgi" and modifiy the .htaccess
 
#Script for a minimal PHP 5.3.x install with APC
#
#- Prompts for the domain to build for
#- PHP configure line contains only valid PHP5 options
#- Displays colourful status messages
#- Many build messages, which aren't helpful to most people, are now suppressed
#- Procedurised, making it cleaner and easier to follow
#
#The only things you may want to change in here are marked with "@todo"s
#
#Derived form the original PHP 5.3 install script at
#http://wiki.dreamhost.com/Installing_PHP5
#
#@author Dan Bettles <[email protected]>
 
#Exit on error
set -e
 
clear
 
echo -n "Enter the domain for which you want to build PHP and press [ENTER]: "
read DOMAIN
 
echo -n "Enable XDEBUG (y|n): "
read ENABLEXDEBUG
 
#===============================================================================
 
#@todo Update versions, if necessary
M4="m4-1.4.13"
AUTOCONF="autoconf-2.65"
OPENSSL="openssl-0.9.8l"   
CURL="curl-7.20.0"
LIBMCRYPT="libmcrypt-2.5.8"
LIBTOOL="libtool-2.2.6b"
PHP="php-5.3.1"
APC="APC-3.1.3p1"
XDEBUG="xdebug-2.0.5"
 
#@todo Update install paths, if necessary
WEB_ROOT="$HOME/$DOMAIN/web"
CGI_BIN_DIR="$WEB_ROOT/cgi-bin"
HTACCESS="$WEB_ROOT/.htaccess"
 
INSTALL_DIR="$HOME/mycompiles"
BUILD_DIR="$INSTALL_DIR/build"
DOWNLOADS_DIR="$INSTALL_DIR/downloads"
PHP_BASE_DIR="$INSTALL_DIR/$PHP"
PHP_BIN_DIR="$PHP_BASE_DIR/bin"
PHP_EXTENSIONS_DIR="$PHP_BASE_DIR/extensions"
PHP_CONFIG_DIR="$PHP_BASE_DIR/etc/php5/config"
PHP_INI="$PHP_CONFIG_DIR/php.ini"
 
#@todo Alter features, if necessary
PHP_FEATURES="--prefix=$PHP_BASE_DIR 
 --with-config-file-path=$PHP_CONFIG_DIR 
 --with-config-file-scan-dir=$PHP_CONFIG_DIR 
 --bindir=$PHP_BIN_DIR 
 --enable-zip 
 --with-xmlrpc 
 --with-freetype-dir=/usr 
 --with-zlib-dir=/usr 
 --with-jpeg-dir=/usr 
 --with-png-dir=/usr 
 --with-curl=$PHP_BASE_DIR 
 --with-gd 
 --enable-gd-native-ttf 
 --enable-ftp 
 --enable-exif 
 --enable-sockets 
 --enable-wddx 
 --enable-sqlite-utf8 
 --enable-calendar 
 --enable-mbstring 
 --enable-mbregex 
 --enable-bcmath 
 --with-mysql=/usr 
 --with-mysqli 
 --without-pear 
 --with-gettext 
 --with-pdo-mysql 
 --with-openssl=$PHP_BASE_DIR 
 #--with-xsl=$=$PHP_BASE_DIR 
 --with-mcrypt=$PHP_BASE_DIR"
 
#===============================================================================
 
#@param string $1 Message
function echoL1 () {
    echo -e "n33[1;37;44m$133[0;0;0mn"
}
 
#@param string $1 Message
function echoL2 () {
    echo -e "n33[0;37;44m$133[0;0;0mn"
}
 
#@param string $1 URL
#@param string $2 Output directory
function downloadTo () {
    wget -c $1 --directory-prefix=$2
}
 
#@param string $1 TAR filename
#@param string $2 Output directory
function untarTo () {
    cd $2
    tar -xzf $1
    cd -
}
 
#@param string $1 Source directory
#@param string $2 Output directory
#@param string $3 configure arguments
function configureAndMake () {
    cd $1
    COMMAND="./configure --quiet --prefix=$2 $3"
    if [ $1 = "$BUILD_DIR/$OPENSSL" ];then
      #special command for OPEN SSL
      COMMAND="./config --prefix=$2 $3"
    fi
    echo "$COMMAND"
    eval $COMMAND
    make --quiet 
    cd -
}
 
#@param string $1 Source directory
#@param string $2 Output directory
#@param string $3 configure arguments
function makeAndInstall () {
    configureAndMake $1 $2 "$3"
    cd $1
    make install --quiet 
    cd -
}
 
#@param string $1 Directory
function mkdirClean () {
    rm -rf $1
    mkdir -p $1
}
 
#@param string $1 Message
function echoWarning () {
    echo -e "n33[1;37;41m$133[0;0;0mn"
}
 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
export PATH="$PATH:$PHP_BIN_DIR"
 
echoL1 "-> DOWNLOADING..."
 
mkdirClean $BUILD_DIR 
 
echoL2 "--> Downloading $M4..."
downloadTo "http://ftp.gnu.org/gnu/m4/$M4.tar.gz" $DOWNLOADS_DIR
untarTo "$DOWNLOADS_DIR/$M4.tar.gz" $BUILD_DIR 
 
echoL2 "--> Downloading $AUTOCONF..."
downloadTo "http://ftp.gnu.org/gnu/autoconf/$AUTOCONF.tar.gz" $DOWNLOADS_DIR
untarTo "$DOWNLOADS_DIR/$AUTOCONF.tar.gz" $BUILD_DIR 
 
echoL2 "--> Downloading $OPENSSL..."
downloadTo "http://www.openssl.org/source/$OPENSSL.tar.gz" $DOWNLOADS_DIR
untarTo "$DOWNLOADS_DIR/$OPENSSL.tar.gz" $BUILD_DIR 
 
echoL2 "--> Downloading $CURL..."
downloadTo "http://curl.haxx.se/download/$CURL.tar.gz" $DOWNLOADS_DIR
untarTo "$DOWNLOADS_DIR/$CURL.tar.gz" $BUILD_DIR 
 
echoL2 "--> Downloading $LIBMCRYPT..."
downloadTo "http://easynews.dl.sourceforge.net/sourceforge/mcrypt/$LIBMCRYPT.tar.gz" $DOWNLOADS_DIR
untarTo "$DOWNLOADS_DIR/$LIBMCRYPT.tar.gz" $BUILD_DIR 
 
echoL2 "--> Downloading $LIBTOOL..."
downloadTo "http://ftp.gnu.org/gnu/libtool/$LIBTOOL.tar.gz" $DOWNLOADS_DIR
untarTo "$DOWNLOADS_DIR/$LIBTOOL.tar.gz" $BUILD_DIR 
 
echoL2 "--> Downloading $PHP..."
downloadTo "http://www.php.net/get/$PHP.tar.gz/from/this/mirror" $DOWNLOADS_DIR
untarTo "$DOWNLOADS_DIR/$PHP.tar.gz" $BUILD_DIR 
 
echoL2 "--> Downloading $APC..."
downloadTo "http://pecl.php.net/get/$APC.tgz" $DOWNLOADS_DIR
untarTo "$DOWNLOADS_DIR/$APC.tgz" $BUILD_DIR 
 
if [ $ENABLEXDEBUG = "y" ]; then
  echoL2 "--> Downloading $XDEBUG..."
  downloadTo "http://xdebug.org/files/$XDEBUG.tgz" $DOWNLOADS_DIR
  untarTo "$DOWNLOADS_DIR/$XDEBUG.tgz" $BUILD_DIR 
fi
 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
echoL1 "-> BUILDING..."
 
mkdir -p $PHP_BASE_DIR 
 
echoL2 "--> Building $M4..."
makeAndInstall "$BUILD_DIR/$M4" $PHP_BASE_DIR
 
echoL2 "--> Building $AUTOCONF..."
makeAndInstall "$BUILD_DIR/$AUTOCONF" $PHP_BASE_DIR
 
echoL2 "--> Building $OPENSSL..."
makeAndInstall "$BUILD_DIR/$OPENSSL" $PHP_BASE_DIR
 
echoL2 "--> Building $CURL..."
makeAndInstall "$BUILD_DIR/$CURL" $PHP_BASE_DIR "--enable-ipv6 --enable-cookies
 --enable-crypto-auth --with-ssl" 
 
echoL2 "--> Building $LIBMCRYPT..."
makeAndInstall "$BUILD_DIR/$LIBMCRYPT" $PHP_BASE_DIR "--disable-posix-threads"
 
echoL2 "--> Building $LIBTOOL..."
makeAndInstall "$BUILD_DIR/$LIBTOOL" $PHP_BASE_DIR 
 
echoL2 "--> Building $PHP..."
#Fixes compile error
export EXTRA_LIBS="-lresolv"
makeAndInstall "$BUILD_DIR/$PHP" $PHP_BASE_DIR "$PHP_FEATURES" 
 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
echoL1 "-> INSTALLING PHP..."
 
mkdir -p -m 0755 $CGI_BIN_DIR
mkdir -p -m 0755 $PHP_CONFIG_DIR
cp "$PHP_BIN_DIR/php-cgi" "$CGI_BIN_DIR/php.cgi"
cp "$BUILD_DIR/$PHP/php.ini-production" $PHP_INI
 
mkdir -p $PHP_EXTENSIONS_DIR
 
echoL2 "--> Building $APC..."
APC_SOURCE_DIR="$BUILD_DIR/$APC"
cd $APC_SOURCE_DIR
$PHP_BIN_DIR/phpize
configureAndMake $APC_SOURCE_DIR $PHP_BASE_DIR "--enable-apc --enable-apc-mmap
 --with-php-config=$PHP_BIN_DIR/php-config" 
cp modules/apc.so $PHP_EXTENSIONS_DIR
echo "extension=$PHP_EXTENSIONS_DIR/apc.so" > $PHP_CONFIG_DIR/apc.ini
cd -
 
if [ $ENABLEXDEBUG = "y" ]; then
  echoL2 "--> Building $XDEBUG..."
  XDEBUG_SOURCE_DIR="$BUILD_DIR/$XDEBUG"
  cd $XDEBUG_SOURCE_DIR
  $PHP_BIN_DIR/phpize
  configureAndMake $XDEBUG_SOURCE_DIR $PHP_BASE_DIR "--enable-xdebug
   --with-php-config=$PHP_BIN_DIR/php-config" 
  cp modules/xdebug.so $PHP_EXTENSIONS_DIR
  echo "zend_extension=$PHP_EXTENSIONS_DIR/xdebug.so" > $PHP_CONFIG_DIR/xdebug.ini
  cd -
fi
#-------------------------------------------------------------------------------
 
if [ -f $HTACCESS ]; then
    HTACCESS_NEW="$HTACCESS.old"
    cp $HTACCESS $HTACCESS_NEW
    echoWarning "--> Copied $HTACCESS to $HTACCESS_NEW"
fi
 
#The backslash prevents a newline being inserted at the start
HTACCESS_CONTENT="
Options +ExecCGI
AddHandler php-cgi .php
Action php-cgi /cgi-bin/php.cgi
 
#Deny access to the PHP CGI executable and config files
<FilesMatch "^php5?.(cgi|ini)$">
    Order Deny,Allow
    Deny from All
    Allow from env=REDIRECT_STATUS
</FilesMatch>"
 
#Preserve newlines in the content by quoting the variable name
echo "#######ADDED BY installPHP script" >> $HTACCESS
echo "$HTACCESS_CONTENT" >> $HTACCESS
 
echoL2 "--> Created $PHP_INI"
 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
rm -rf $BUILD_DIR
 
echo -n "Delete the downloads directory? (y/n): "
read DELETE_DOWNLOADS_DIR
 
if [ $DELETE_DOWNLOADS_DIR = "y" ]; then
    rm -rf $DOWNLOADS_DIR
fi
 
echoL1 "DONE"
 
exit 0

Tagged with: , , ,
Posted in Web Development
20 Comments » for Custom PHP 5.3.1 with APC and XDEBUG on (Dreamhost) Shared Host
  1. Emmanuele says:

    Hi,
    does this script create a custom php install with APC working by itself or do I need to modify something else to make it work? I’d like to use APC for magento and all the other scripts for installing php and APC on dreamhost failed to install…
    Thanks

    • marco says:

      hi,
      well, you should be able to run it as it is, but you might want to have a look at the 3 “#@todo Update” lines and decide if the settings suits you.

      hope it helps

      Cheers, Marco

  2. Emmanuele says:

    Thanks a lot it works! One last question: Magento doesn’t support php5.3 very well… I’d like to install 5.2. Is it sufficient change PHP=”php-5.3.1″ to PHP=”php-5.2.12″ (for example) at the 1 “#@todo Update” or are there other settings to customize and how?
    I’d really appreciate if you could answer this question, hope it doesn’t bother you too much.
    Thanks a lot.
    Emmanuele

  3. Emmanuele says:

    Script blocks when downloading Curl, there must be something with their site cause i cannot reach it. I’ll try later and I’ll tell you.

  4. Emmanuele says:

    It blocks during Php 5.2.12 installation: this is the error -> INSTALLING PHP...

    cp: cannot stat `/home/milchstore/mycompiles/build/php-5.2.12/php.ini-production': No such file or directory

  5. marco says:

    hi, it means that it cannot find the php.ini-production file in the $BUILDDIR.
    Check line 245 and adapt it to php.ini-recommended or php.ini-dist (depending on your needs)

    cheers
    Marco

  6. dave says:

    Is it just me or is APC not actually functional? I see that it is enabled, however if you look at apc.php there is never any entries other than apc.php itself, and apc start time is the whenever the script ran. I’m sure there is a way around this, but what am I missing?

    • marco says:

      mmm haven’t had a chance to play with apc (and I’m no apc expert either…) since then due to lack of time… i just cleaned up the install script using the settings from the scripts on wiki.dreamhost but looking at a phpinfo it all seams to work… i’ll have a look asap

  7. dave says:

    yea, it works fine, except i believe php as cgi runs as a new instance of php each time, hence no shared memory. (well that’s what I’ve gathered from other research I’ve done)

    Take a look at that apc.php file, the output will let you know whether apc is doing anything for you or not

    Thanks for your efforts btw, the script worked great.

  8. dave says:

    According to this: http://www.brandonturner.net/blog/2009/07/fastcgi_with_php_opcode_cache/#implementation

    One needs to do quite a bit more to get PC properly functioning with PHP /fastcgi. I don’t think those steps are possible as a user account on dreamhost, but if someone knows better, I’m all ears.

  9. lsowen says:

    Thank you for putting this together… It really helped me out!

  10. Daniel says:

    Hi, Im getting the following error, no idea what to do, any advice?

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.

    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

  11. ahaab says:

    i get a lot of errors after the php licence notification. posted everything to pastebin here.
    http://pastebin.com/UBGstUXP
    any ideas?

    thanks in advance!

  12. MacMladen says:

    I use Drupal and some modules would like to have PECL upload progress.

    How could it be added to build script and can you provide a solution for such integration?

  13. GeorgeK says:

    Did anyone use this installation to debug with Xdebug ?

    I use the following configuration in the php.ini
    end_extension_ts-
    xdebug.remote_enable=on
    xdebug.remote_handler=dbgp
    xdebug.remote_host=
    xdebug.remote_port=

    Alas I it doesn’t work and before I spend more time on it I would like to know if this port opening is even allowed?

    Any help will be very welcomed

  14. GeorgeK says:

    My message got scrambled, as stated I using the following configuration in the php.ini

    end_extension_ts=[absolute path to xdebug.so]
    xdebug.remote_enable=on
    xdebug.remote_handler=dbgp
    xdebug.remote_host=[domain name]
    xdebug.remote_port=[port of my choosing]

  15. marco says:

    @daniel http://blog.ayanray.com/2010/04/custom-php-install-on-dreamhost-shared-hosting/ suggests using a UNIX texteditor to avoid this problem, are you working your files on a win machine and then uploading them?

    @MacMladen you can remove the –without-pear switch on line 92 so your php will have pear and you can use PECL

    @GeorgeK I did play around a bit but not much. (short in time….)

  16. Typically I actually do not post on weblogs, although I would like to state that this article seriously obligated me to do so! nice post.

  17. Gary says:

    I got the script to run without errors, but APC is not showing up as installed. Any suggestions? Is there steps not listed here that I was supposed to follow? Thanks!

Leave a Reply