ColoCrossing
Hosting Websites on Bare Minimum VPS/Dedicated Servers

PHP on CentOS 7 (PHP-FPM)

Tags: , Date/Time: October 5, 2015 @ 1:00 pm, by Maarten Kossen

lowendtutorial

Welcome back! This week’s tutorial will focus on installing PHP on CentOS 7 and will continue were I left off last time: with the installation of NGINX. The last tutorial is a prerequisite for this one, so if you haven’t followed that one yet, now is the time to do so.

We’re not going to “just” install PHP, though. We want to do it right. First of all, we’re going to install PHP-FPM so PHP can run as a separate process rather than embedded in the web server process. This has been the best practice for a while now, though it’s a little harder to set up than the “old way”. There’s plenty of advantages to this set-up, but PHP running as a separate process is the biggest one.

Second, CentOS ships an unsupported and outdated version of PHP. PHP 5.4 is the version that comes with CentOS 7 (or actually: the EPEL repository) and that version is no longer being maintained. Therefore we’re going to install PHP 5.6 from a different repository. This will not only give you all the latest features, but also a version that still received security patches.

So, let’s get started!

Preparing your environment

First let’s start by ensuring your system is up-to-date. This will not only update any packages that may have an update pending, but will also update the repository caches ensuring that any packages added meanwhile have been added:

sudo yum update

When is asks you to continue, please type ‘y’ to ensure everything gets updated properly.

Now, in order to get any sort of decent version of PHP (in other words: one that is still receiving security updates), we need to add a third-party repository that has up-to-date packages. There’s a nice French guy called Remi that maintains PHP repositories for a number of PHP versions and has been doing so for years. They’re free to use and well-documented. You can read more about these repositories on his website.

To add his PHP repository, run the following command:

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

This pulls in the package and adds the repository to YUM. It is being added disabled, though, so we have to enable that manually. So, open the repository file:

sudo vi /etc/yum.repos.d/remi.repo

And locate these two blocks of text:

[remi]
name=Remi’s RPM repository for Enterprise Linux 7 – $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/remi/mirror
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php56]
name=Remi’s PHP 5.6 RPM repository for Enterprise Linux 7 – $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php56/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/php56/mirror
# WARNING: If you enable this repository, you must also enable “remi”
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

I’ve cut out the ‘remi-php55’ block as we want PHP 5.6 and not PHP 5.5. Now, in order to enable this repository, you have to change ‘enabled=0’ into ‘enabled=1’. This means that after the change the blocks look like this:

[remi]
name=Remi’s RPM repository for Enterprise Linux 7 – $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/remi/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php56]
name=Remi’s PHP 5.6 RPM repository for Enterprise Linux 7 – $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php56/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/php56/mirror
# WARNING: If you enable this repository, you must also enable “remi”
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Save the file. You are now ready to install PHP-FPM!

Installation

In order to install PHP-FPM (and not the PHP package that pulls in Apache as a dependency), run the following command:

sudo yum install php-fpm

It again asks you if you want to continue. Several other packages have been selected as well as dependencies. Type ‘y’ again to start the installation.

Once the installation is finished, PHP-FPM will have been installed but is not yet running. In order to start it, type:

sudo systemctl start php-fpm

To ensure it also starts automatically with the server, run the following command:

sudo systemctl enable php-fpm

Now we need to ensure that NGINX knows to send PHP-requests to PHP-FPM. For this to work we need to add a configuration block to the default host. For reasons beyond my understanding this was set up a bit “different” on CentOS, so if you’re looking for logic stop trying. In any case, fire up a new file:

sudo vi /etc/nginx/default.d/php-fpm.conf

And add the following content to it:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root           /usr/share/nginx/html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
include        fastcgi_params;
}

This block tells NGINX to pass on any request going to a file ending with .php to 127.0.0.1 port 9000, which is where PHP-FPM runs. NGINX does this using FastCGI. The configuration block includes default configuration we won’t touch for now.

With these contents in the file, save the file, and restart NGINX:

sudo systemctl restart NGINX

You’re now set to test PHP. We’re going to do that by displaying phpinfo on your server. Remember to remove the test file after the tutorial as it displays quite some server and environment information!

Open up a new file:

sudo vi /usr/share/nginx/html/info.php

And add the following contents to it:

<?php phpinfo();

Save the file.

If you now go to your server IP address or hostname plus “/info.php” (example: http://192.0.2.1/info.php) you should see an nice and shiny (or well, purple and ugly) page with information about PHP and your server!


That’s it for this time! Up next is your usual suspect: MySQL!

 

 

14 Comments

  1. acuz:

    TOP 20 VPS hosting??

    October 5, 2015 @ 2:15 pm | Reply
  2. Ivan:

    Thanx! This is a good post!

    October 5, 2015 @ 5:45 pm | Reply
  3. Gzero:

    Can you include the step to swap out the port and use sockets instead? Usually a performance boost for most setups.

    October 5, 2015 @ 5:46 pm | Reply
    • MKV:

      Using unix sockets may make the site/application unstable at higher loads. There are SEVERAL reports about this and sockets should never be used under high loads. The “performance boost” on lower (=stable) loads is pretty questionable as well. I have tried both ports and sockets and one can see a “boost” with vivid imagination, not measure it.

      There are some other advantages using sockets, of course, but performance boost is not one of them.

      October 6, 2015 @ 1:08 pm | Reply
      • Gzero:

        Fair enough.

        October 7, 2015 @ 6:39 pm | Reply
      • I see that this way, PHP is listening on a port. Does that present any sort of security risk?

        October 16, 2015 @ 3:12 am | Reply
        • MKV:

          Well, if almost 100% of installations use a TCP/IP port solution for PHP-FPM the risks would have been noticed by now. Moreover, every sane server installation blocks unneeded outside ports in the firewall so the service is only used from inside the server, anyhow. There have been no reports so far. And what is the alternative? The shaky UNIX socket that crashes connections on high loads?

          There are things one has to take into account in nginx virtual server settings, however, and some domain specific PHP_VALUE settings seem to cause problems. One should not use “auto_prepend_file” settings for virtual domains as they traverse to wrong domains as well, something that caused a big headache for us.

          As for reliability: We have an in-house user/domain/email/etc administration application that we designed and wrote to use UNIX sockets but have reverted to the TCP/IP port one for very good reasons. It is such a shame that the socket solution sucks so badly.

          October 18, 2015 @ 9:21 am | Reply
  4. thank’s fro this tutorial before I use my VPS

    October 6, 2015 @ 1:54 am | Reply
  5. Cloud Vps Good Link–>https://menyohost.com

    October 8, 2015 @ 12:05 am | Reply
  6. guido sanchez:

    is this updated? because they changed the remi repo in december now it’s all messed up

    March 3, 2016 @ 2:00 pm | Reply
  7. sleddog:

    > Second, CentOS ships an unsupported and outdated version of PHP

    False. The version of PHP provided by CentOS is supported by Red Hat for security and major bug fixes. These fixes are backported by Red Hat personnel into RHEL 7, and then are picked up by CentOS 7.

    RHEL and CentOS aren’t called “Enterprise” distros for no reason.

    March 5, 2016 @ 8:17 pm | Reply
  8. I appreciate this post. Well written

    June 28, 2016 @ 10:50 am | Reply
  9. boypeorerbame
    unu9jnzbiz7z13qwej

    June 22, 2017 @ 12:07 pm | Reply
  10. dd tls:

    No package php-fpm available dude

    March 14, 2018 @ 9:17 pm | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Quoting webhostingtalk.com URL seems to get binned consistently here, but I do peek into the spam box frequently to publish those comments.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *