Upgrade PHP on Linux from source

In this short receipt I’ll show how to uninstall current version of PHP and compile from source + install a new version of PHP.
I am upgrading from php7.1.1 to php 7.1.4 You will find that it works very similar to any version, including 5.x.x versions.

It is strongly recommended to keep the bug fixes version (the V) of PHP up to date. (x.x.V)


Step 1 – remove existing PHP

  • Stop apache (remove server from load balancer if in production, under a LB).
  • open a terminal
  • sudo su    [you need admin rights on the machine]
  • Find the installation path of your Apache, in this article we assume /usr/local/apache2   If your apache folder is someware else, you will need to find it, use that folder anyware we refer to Apache folders going forward.
  • rm /usr/local/apache2/modules/libphp* #Any PHP module you have in that folder
    rm -rf /usr/local/lib/php*
    rm -rf /usr/local/php*
    rm -rf /usr/local/bin/php*
    rm -rf /bin/phar*
    rm -rf /usr/bin/phar*
    rm -rf /usr/local/bin/phar*
    rm -rf /usr/local/apache2/conf/php.ini
    rm -rf /etc/php.ini
    rm -rf /usr/bin/php*
    rm -fr /bin/php*
    rm -fr /etc/php.d
    rm -fr /usr/lib64/php*
    rm -fr /usr/local/bin/pear*
    rm -fr /usr/local/src/php* #(Assuming this is where you download the PHP source code from previous installations)

  • If you upgrade from PHP5 to PHP7, you need to delete the configuration for php5 module libphp5 in conf/httpd.conf  

Step 2 – Get the source

  • Get Source for PHP 7.1.4 from http://php.net/downloads.php#v7.1.4 
  • mkdir /usr/local/src/php
  • copy and untar (tar zxvf FILENAME) the file you downloaded into /usr/local/src/php
  • cd /usr/local/src/php/php-7.1.4

Step 3 – Compile

  • The following is the compilation command I use. You might need less (or more) extensions. Use your own judgment and Google Fu 
    ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-zlib --with-zlib-dir=/usr/local --with-gd --with-xsl=/usr/local --enable-gd-native-ttf --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --with-iconv --with-curl=/usr/local --with-gettext --with-config-file-path=/usr/local/apache2/conf --enable-mbstring --enable-pdo --with-pdo-mysql --with-openssl --with-libxml-dir=/usr/local --with-pspell --enable-zip --with-mysqli --enable-mysqlnd --with-libdir=lib64 --enable-sockets --enable-opcache --enable-phar
    NOTICE! in the above the setting –with-apxs2=/usr/local/apache2/bin/apxs Make sure it really is pointing to the Apache installation folder you use.
  • make
    make test #Look for errors! there will always be some. See if any of the errors are on functions you plan to use, otherwise, do not worry about it.
    make install #This will also put the Apache module in place. Do verify visually the file was created in the Apache module folder.

  • Read the log of the make install to find the location of the phpizeMake ln -s (soft link) to phpize, php binary and phar (and any other PHP utility you need) from you /bin folder

    ln -s /usr/local/bin/php /bin/php
    ln -s /usr/local/bin/phpize /bin/phpize
    ln -s /usr/local/bin/phar.phar /bin/phar

Step 4 – Test the installation

  • Confirm /usr/local/apache2/conf/httpd.conf has: LoadModule php7_module modules/libphp7.so
  • do php -i | grep extension_dir to know your extension dir. This is where you will check if extensions you install manually got installed in the right folder.
  • In the php src folder /usr/local/src/php/php-7.1.4 there will be several php.ini files. Choose the one you want and copy it to /usr/local/apache2/conf/php.ini.
    If you choose the production version cp php.ini-production /usr/local/apache2/conf/php.ini
  • Create a soft link for your php.ini under /etc
    ln -s /usr/local/apache2/conf/php.ini php.ini

  • Test the cli, do php -i | grep php.ini to see the right file is used. Create a simple php file with phpinfo(); and run it via cli. See you get output.
  • Start the apache, test it reads php files properly.
  • Not working, Post a question here.



QUESTIONS? FEEDBACK? All are welcome, post as a comment.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close