Category Archives: Apache

Install PHP 7.0 in Mac OS X with MacPorts

I’ve used this process to install PHP 7.0 with MacPorts along side with PHP 5.6.

First make sure Mac OS X included Apache server is stopped:

$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Install PHP7 and some of its modules (full list here):

$ sudo port install php70
$ sudo port install php70-apache2handler php70-cgi php70-gd php70-curl php70-intl php70-iconv php70-gettext php70-mbstring php70-imap php70-mcrypt php70-xmlrpc php70-mysql php70-openssl php70-sockets php70-zip php70-tidy php70-opcache php70-xsl php70-sqlite

Create the base php.ini file from the development example:

$ sudo cp /opt/local/etc/php70/php.ini-development /opt/local/etc/php70/php.ini

Find current timezone, add a backslash in between and put it in php.ini:

$ TIMEZONE=`sudo systemsetup -gettimezone | awk '{ print $3 }'`
$ TIMEZONE=$(printf "%s\n" "$TIMEZONE" | sed 's/[][\.*^$/]/\\&/g')
$ sudo sed -i.bak "s/;date.timezone =/date.timezone = \"${TIMEZONE}\"/g" /opt/local/etc/php70/php.ini

Update MySQL sockets if you are using it:

$ sudo sed -i.bak "s#pdo_mysql\.default_socket.*#pdo_mysql\.default_socket=`/opt/local/bin/mysql_config --socket`#" /opt/local/etc/php70/php.ini
$ sudo sed -i.bak "s#mysqli\.default_socket.*#mysqli\.default_socket=`/opt/local/bin/mysql_config --socket`#" /opt/local/etc/php70/php.ini

Now with this command you’ll see the installed PHP versions:

$ sudo port select --list php
Available versions for php:
	none
	php56 (active)
	php70

Select php70 as the active PHP version:

$ sudo port select php php70
Selecting 'php70' for 'php' succeeded. 'php70' is now active.

Enable PHP 7 in Apache:

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n php7 mod_php70.so

Restart Apache:

$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart

You can check in the console the php version:

$ php -v
PHP 7.0.0 (cli) (built: Dec 12 2015 11:18:35) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

If the current version is not 7.0 make sure that /opt/local/bin is defined before /usr/bin in the PATH variable:

$ echo $PATH
/opt/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

Also check that /opt/local/bin/php is pointing to /opt/local/bin/php70:

$ ls -l /opt/local/bin/php
lrwxr-xr-x  1 root  admin    20B Dec  5 23:25 /opt/local/bin/php -> /opt/local/bin/php70

If running phpinfo(); doesn’t show the 7.0 version check that only the module for 7.0 is loaded in /opt/local/apache2/conf/httpd.conf:

#LoadModule php5_module        modules/mod_php56.so
LoadModule php7_module        modules/mod_php70.so

php7

Increase the maximum file size upload in PHP

Sometimes you need to increase the default 2 megabyte maximum upload file size.

php_upload_max_filesize

The maximum size you can upload to a server using PHP is defined by 2 values:

  • Upload max file size: upload_max_filesize
  • Post max size: post_max_size

That’s why you have to make sure both options in php.ini have the correct values:

upload_max_filesize = 32M
post_max_size = 32M

Enable hosting files outside /var/www in Apache2

I’m using Ubuntu and Mac OS X in my computer. I wanted both to share a partition to put all the things that could be duplicated in both systems: DropBox, downloads, music, etc. The websites I’m developing would be also there but I was getting 403 errors all the time. Anything outside /var/www won’t work. Finally I found out that there is a Directory configuration in /etc/apache2/apache2.conf that denies access to anything outside /var/www and /usr/share:

<Directory />
       Options FollowSymLinks
       AllowOverride None
       Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

To fix this you can disable the first Directory block or even better add a new one with Require all granted in it for your speceific path:

<Directory /home/shared/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

apache_logo

Change between multiple versions of PHP with Apache in Mac Ports

If you have for example 2 different versions of PHP in the same machine you can switch between them by just using APache eXtenSion tool:

For example to enable PHP 5.4:

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n php5 mod_php54.so

To enable PHP 5.6:

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n php5 mod_php56.so

To enable PHP 7.0:

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n php7 mod_php70.so

apache_logo

Can’t establish connection to MySQL after PHP upgrade

When you upgrade PHP there are chances that the new version is reading a different php.ini file than the previous version. You have to see which php.ini it’s reading (for example with phpinfo()) and then check that it has correctly configured the pdo_mysql.default_socket, mysql.default_socket and mysqli.default_socket options. They are probably empty:

pdo_mysql.default_socket =
mysql.default_socket =
mysqli.default_socket =

And they should have something like this:

pdo_mysql.default_socket = /opt/local/var/run/mysql55/mysqld.sock
mysql.default_socket = /opt/local/var/run/mysql55/mysqld.sock
mysqli.default_socket = /opt/local/var/run/mysql55/mysqld.sock

mysql