December 09, 2015

Installing PHP-7 with Memcached

Let's install PHP7 and Nginx on a new Ubuntu 14.04 server, and manually build the (not yet packaged) memcached module for PHP7.

Let's install PHP7 and Nginx on a new Ubuntu 14.04 server, and manually build the (not yet packaged) memcached module for PHP7.> Update: It looks like the php-memcached package was built into ppa:ondrej/php-php7.0 for php7, so the manual build steps is probably not necessary any longer! Install it via the package php-memcached.

Update2: The repository ppa:ondrej/php-php7.0 is being depreciated in favor of ppa:ondrej/php. I haven't had time to re-record the video, but I'll adjust the text of the article here.

First, we'll get the PHP-7 repository:

sudo apt-get update
sudo add-apt-repository ppa:ondrej/php

sudo apt-get update

Then we can install some tools and PHP7:

sudo apt-get install -y tmux curl wget \
    nginx \
    php7.0-fpm \
    php7.0-cli php7.0-curl php7.0-gd \
    php7.0-intl php7.0-mysql

    # php7.0-mcrypt is available, but is already compiled in via ppa:ondrej/php

# Since php-memcached is now available, install that too:

sudo apt-get install -y php-memcached

Then PHP7 is installed!

Nginx

If we want to use it with Nginx, we just need to get the Nginx configuration up to date to use the PHP7.0-FPM's unix socket file path. (It's different from Nginx's default path).

server {
    listen 80 default_server;

    root /usr/share/nginx/html;
    index index.html index.htm index.php;

    server_name localhost;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;

        fastcgi_index index.php;
        include fastcgi_params;
    }
}

Memcached

The php-memcached package should now be available for the latest Memcached library, so the following manual build steps is not necessary - but I'll leave it there just in case you're curious! Install it via sudo apt-get install -y php-memcached after adding the php7 repository.

If php-memcached was not installed, we can build it manually. (However, it's likely available to install via the php7.0-memcached package now).

If you need a newer version of the PHP-Memcached module, we can build it manually. Here's how:

First, we install some basics, including development packages for PHP and Memcached:

sudo apt-get install -y php7.0-dev git pkg-config build-essential libmemcached-dev

Then we can get the php-memcached repository, check out the php7 branch, and build it!

cd ~
git clone https://github.com/php-memcached-dev/php-memcached.git
cd php-memcached
git checkout php7
phpize
./configure --disable-memcached-sasl
make
sudo make install

The memcached.so file gets installed into /usr/lib/php/20151012/.

Then we need to setup PHP (CLI and FPM) to use the memcached module. Edit /etc/php/mods-available/memcached.ini, add:

; configuration for php memcached module
; priority=20
extension=memcached.so

Then enable it by including symlinks to that file in the FPM/CLI conf.d directories:

sudo ln -s /etc/php/mods-available/memcached.ini /etc/php/7.0/fpm/conf.d/20-memcached.ini
sudo ln -s /etc/php/mods-available/memcached.ini /etc/php/7.0/cli/conf.d/20-memcached.ini

# Reload php-fpm to include the new changes
sudo service php7.0-fpm restart

And there we have it, PHP7 is installed, with Memcached support!

All Topics