PHP

 
 
PHP (= Hypertext Preprocessor) is an HTML-embedded server-side scripting language.

PHP is distributed at no charge for commercial or non-commercial use. For more information read the LICENSE information.

More information about PHP can be found at: http://www.php.net

PHP manuals can be found at:
http://www.php.net/manual/en/
http://devzone.zend.com/manual/

The latest PHP version can be downloaded from: http://www.php.net/downloads.php







Installing PHP5.6.30 on macOS Sierra.



Information
This tutorial describes how to install a different PHP version on the macOS Sierra.
The preinstalled system PHP version remains intact and will not be removed.

The binary php 5.6 package is downloaded from https://php-osx.liip.ch/

A reason why you might want to install this binary package is that more php extensions, such as FreeType, are installed. The preinstalled system PHP version is missing several php extensions.

Operating system used
macOS Sierra

Software prerequisites
None

Procedure
  1. Download and install PHP 5.6:
    curl -s https://php-osx.liip.ch/install.sh | bash -s 5.6

    You will see the following:

    extracted to /usr/local
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/tiffmedian
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/tiffset
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/tiffsplit
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/tsql
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/uconv
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/vacuumdb
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/wrjpgcom
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/xgettext
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/xslt-config
    Extracting usr/local/php5-5.6.30-20170506-093822/bin/xsltproc
    Executing post-install script /tmp/5.6-10.10-frontenddev-post-install
    Create symlink /usr/local/php5/entropy-php.conf
    /etc/apache2/other/+php-osx.conf
    Restarting Apache
    [Sat Jun 03 16:26:55.532811 2017] [so:warn] [pid 27019] AH01574:
    module php5_module is already loaded, skipping
    Syntax OK


  2. Edit .bash_profile and add the following line:
    export PATH=/usr/local/php5/bin:$PATH

  3. Edit /etc/apache2/httpd.conf and add the following line:
    #LoadModule php5_module libexec/apache2/libphp5.so
    LoadModule php5_module /usr/local/php5/libphp5.so


  4. The ini file is executed in the following order:
    • First /usr/local/php5/lib/php.ini is executed (Note: /etc/php.ini is not used)
    • Second /usr/local/php5/php.d/99-liip-developer.ini is executed.
      The settings in 99-liip-developer.ini overrules the settings in php.ini.

    Note:
    To know where you php.ini is located, type: php --ini

  5. ONLY when you have changed the User in /etc/apache2/httpd.conf, see:
    Setup Apache in macOS Sierra with PHP, Server Side Includes and name-based virtual hosting
    for example:

    User robertlie
    Group _www


    then the session data is not stored.
    You must update file /usr/local/php5/lib/php.ini and change the following line:

    session.save_path = "/tmp"

  6. Modify /usr/local/php5/php.d/99-liip-developer.ini
    display_errors = Off
    display_startup_errors = Off

  7. Restart apache
    sudo apachectl restart

  8. To test the PHP installation, create a file phpinfo.php with the following line:

    <?php phpinfo(); ?>

    Save this file into your docroot and access this file in a browser.

    You should see something like this:

    PHPinfo

  9. If your website support SSL (see: Setup Apache in macOS Sierra with PHP, Server Side Includes and name-based virtual hosting you might find the following error message in you Apache error log file (/var/log/apache2/sand-mobilefish-error.log)
    Note: The location of the Apache eror log file is set in /etc/apache2/extra/httpd-ssl.conf

    SSL routines:ssl3_get_server_certificate:certificate verify failed

    This error is caused when using the following functions "file_get_contents" or "get_headers" (there are more functions which generates this error).
    To reproduce the error, create a test.php file containing the following lines. Open this file in your browser (https://www.mobilefish.com/test.php) and check the Apache error log file:

    <?php
    // Enter your image location.
    $url = "https://www.mobilefish.com/
    images/mobilefish_logo.gif";
    //$fileContent = file_get_contents($url, false);
    $headers = get_headers($url, 1);
    ?>


    To solve this error:

    • Solution A. With peer verification disabled.

      Modify the test.php file:

      <?php
      $default_opts = array(
         'ssl' => array(
            'verify_peer'=>false,
            'verify_peer_name'=>false
         )
      );

      $default = stream_context_get_default($default_opts);

      // Enter your image location.
      $url = "https://www.mobilefish.com/
      images/mobilefish_logo.gif";
      //$fileContent = file_get_contents($url, false);
      $headers = get_headers($url, 1);
      echo "Type=".$headers['Content-Type']."<br />"
      ?>


      This will solve the error!

    • Solution B. With peer verification enabled.

      You need the Certificate Authority (CA) certificate file (ca.pem) which issued the SSL certicate. See:
      Setup Apache in macOS Sierra with PHP, Server Side Includes and name-based virtual hosting

      Modify the test.php file:

      <?php
      $default_opts = array(
         'ssl' => array(
            'cafile' => '/etc/apache2/ssl/ca.pem',
            'verify_peer'=>true,
            'verify_peer_name'=>true,
            'allow_self_signed'=>true
         )
      );

      $default = stream_context_get_default($default_opts);

      // Enter your image location.
      $url = "https://www.mobilefish.com/
      images/mobilefish_logo.gif";
      //$fileContent = file_get_contents($url, false);
      $headers = get_headers($url, 1);
      echo "Type=".$headers['Content-Type']."<br />"
      ?>


      This will solve the error!
      It is important to set "allow_self_signed" when you use a self signed certificate.

      More information about "SSL context options":
      http://php.net/manual/en/context.ssl.php
      http://php.net/manual/en/migration56.openssl.php

    • Solution C. With peer verification enabled without using default_opts cafile.

      You need the Certificate Authority (CA) certificate file (/etc/apache2/ssl/ca.pem) which issued the SSL certicate. See:
      Setup Apache in macOS Sierra with PHP, Server Side Includes and name-based virtual hosting

      • Type: cd /usr/local/php5/ssl

      • Append to content of /etc/apache2/ssl/ca.pem at the end of file: /usr/local/php5/ssl/cert.pem
        Make a backup first!

        Note:
        The location /usr/local/php5/ssl/cert.pem is set in file: /usr/local/php5/php.d/40-openssl.ini

        Note:
        To check the CA certificate file location (default_cert_file) used by PHP:

        <?php
        var_dump(openssl_get_cert_locations());
        ?>


      • Modify the test.php file:

        <?php
        $default_opts = array(
           'ssl' => array(
              'verify_peer'=>true,
              'verify_peer_name'=>true,
              'allow_self_signed'=>true
           )
        );

        $default = stream_context_get_default($default_opts);

        // Enter your image location.
        $url = "https://www.mobilefish.com/
        images/mobilefish_logo.gif";
        //$fileContent = file_get_contents($url, false);
        $headers = get_headers($url, 1);
        echo "Type=".$headers['Content-Type']."<br />"
        ?>


        This will solve the error!
        It is important to set "allow_self_signed" when you use a self signed certificate.

        More information about "SSL context options":
        http://php.net/manual/en/context.ssl.php
        http://php.net/manual/en/migration56.openssl.php

  10. If you use the function set_time_limit, the php script keeps running.
    This problem is caused by the Xdebug pecl extension.
    A hacky solution to this problem is disabling xdebug. I have not found another solution!

    Type: cd /usr/local/php5/php.d/
    Type: sudo mv 50-extension-xdebug.ini 50-extension-xdebug.ini_disable