How to Install Apache and PHP-FPM on Debian 8

PHP-FPM is an improvement of the popular FastCGI mechanism for PHP. It’s commonly used to improve the performance of hosted sites that receive high traffic. This guide will show you how to setup Apache and PHP-FPM on Debian 8.

Essential Elements To Consider

But before we begin, let’s look at the basic thing you need to do.

  • The Debian version 8 “contrib” repository
  • Apache 2 HTTP server

Then make sure your system is updated by running the following command:

$ sudo apt-get update && sudo apt-get upgrade

NOTE:

  • This process proves to offer more benefits than mod_php. Although mod_php modules allow Apache to run php scripts directly, it runs the risk of being overloaded by each Apache process.

  • Using PHP-FPM allows for more secure processes since php scripts cannot run as Apache user. If you plan to run several sites, it’s easier to set a user for each site to run the php scripts, which helps to keep the site safe.

  • This tutorial is put together for a non-root user. Every command that requires great concern contains a prefix sudo.

Let’s get started!

Step 1: Installing Apache and PHP-FPM

  1. PHP-FPM cannot be found in Debian’s repository because of PHP-FPM licensing. So, to ensure the installation process is successful, go to the file -sources.list and add the following files to each of these source lines: contrib and non-free /etc/apt/sources.list
    deb http://mirrors.linode.com/debian/ hostingadvice main contrib non-free
    deb-src http://mirrors.linode.com/debian/ hostingadvice main contrib non-free
    
    deb http://security.debian.org/ hostingadvice/updates main contrib non-free
    deb-src http://security.debian.org/hostingadvice/updates main non-free
    
    # hostingadvice-updates, previously known as 'volatile'
    deb http://mirrors.linode.com/debian/ hostingadvice-updates main contrib non-free
    deb-src http://mirrors.linode.com/debian/ hostingadvice-updates main contrib non-free
  2. Next, make sure apt-get is up-to-date, then install Apache, PHP-FPM, and mod-fastcgi module.

    $ sudo apt-get update
    $ sudo apt-get install apache2 libapache2-mod-fastcgi php5-fpm
  3. The next step is optional. If you want MySQL in PHP, you can install php5-mysql using the command below:

    $ sudo apt-get install php5-mysql
  4. Once you’re through with the installation process, you can now go ahead and configure your virtual hosting based on the requirements of your root server. In this case, you need to configure Apache on PHP scripts.

Step 2: Configuring PHP-FPM

  1. Configuration of PHP-FPM should begin with enabling the mod_actions module with the command below:
    $ sudo a2enmod actions
  2. Make sure you have a backup file of the fastgi.conf file with the command below:

    $ sudo cp/etc/apache2/mods-enabled/fastcgi.conf /etc/apache2/mods-enabled/fastcgi.conf.backup
  3. Next, replace the information on fast.conf file with the one below:

    /etc/apache2/mods-enabled/fastcgi.conf
    
    <IfModule mod_fastcgi.c>
        AddType application/x-httpd-fastphp5 .php
        Action application/x-httpd-fastphp5 /php5-fcgi
        Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
        FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
        <Directory /usr/lib/cgi-bin>
            Require all granted
        </Directory>
    </IfModule>
  4. Now, confirm the information has been copied properly using the command below:

    $ sudo apache2ctl configtest

    Ignore other output, and when you see the description Syntax Ok, you can go to the next step.

  5. Next, restart Apache to enable the new configuration:

    $ sudo systemctl restart apache2
  6. To ensure that PHP is active, go to your system directories and create an info.pho file. Your output should look like this:

     /var/www/example.com/public_html/info.php
    <?php phpinfo(); ?>

Go to http://example.com/info.php and search for the following API line on your server:

When this process is done, you can configure PHP Pools, although it’s Optional.

Step 3: Configuring PHP Pools

Unlike the configuration above, this step is different and optional. This step is appropriate where there are users create to perform a specific PHP code as well as control the resources of every site. Rather than using a www-data user that own all the Apache processes and websites, this configuration allows every site to run Apache under its own system user i.e. site1 runs under user1 while site2 runs under user2, and so on.

The process is important when you want to run multiple websites since you can give every user permission to write a specific web directory without compromising the security of the entire web server.

For our example below, we assume that each of the two sites has its own virtual host. Similarly, each website should have one user to which a PHP pool is assigned.

  1. For each pool, make a copy of www.conf: for each site:

    $ cd /etc/php5/fpm/pool.d/
    $ sudo cp www.conf {site1.conf,site2.conf}
  2. The next step is to ensure that for each of the pools, the name, user, and socket name are edited.

    /etc/php5/fpm/pool.d/site1.conf
    
    ; Start a new pool named 'www'.
    ; the variable $pool can we used in any directive and will be replaced by the
    ; pool name ('www' here)
    [site1.com]
    
    ...
    
    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    user = site1
    group = site1
    
    ...
    
    ; The address on which to accept FastCGI requests.
    ; Valid syntaxes are:
    ;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
    ;                            a specific port;
    ;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
    ;                            a specific port;
    ;   'port'                 - to listen on a TCP socket to all IPv4 addresses on a
    ;                            specific port;
    ;   '[::]:port'            - to listen on a TCP socket to all addresses
    ;                            (IPv6 and IPv4-mapped) on a specific port;
    ;   '/path/to/unix/socket' - to listen on a unix socket.
    ; Note: This value is mandatory.
    listen = /var/run/php5-fpm-site1.com.sock

    It is important to note that in the output above, the three sequential dots - … - show that there is more in that file than is being shown. The dots shouldn’t be literally copied.

  3. Now, restart the PHP-FPM service:

    $ systemctl restart php5-fpm.service

    If the process fails, make sure you have created a new user on your Linux system for each of the pools. On the other hand, if the process goes through, you should see the following info for sudo systemctl status php5-fpm.service:

    ● php5-fpm.service - The PHP FastCGI Process Manager
       Loaded: loaded (/lib/systemd/system/php5-fpm.service; enabled)
       Active: active (running) since Wed 2016-01-27 20:24:51 UTC; 2s ago
      Process: 28423 ExecStartPre=/usr/lib/php5/php5-fpm-checkconf (code=exited, status=0/SUCCESS)
     Main PID: 28428 (php5-fpm)
       Status: "Ready to handle connections"
       CGroup: /system.slice/php5-fpm.service
               ├─28428 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
               ├─28432 php-fpm: pool site2.com
               ├─28433 php-fpm: pool site2.com
               ├─28434 php-fpm: pool site1.com
               ├─28435 php-fpm: pool site1.com
               ├─28436 php-fpm: pool www
               └─28437 php-fpm: pool www
  4. Next is to add the <IfModule mod_fastcgi.c> block to every virtual host.

    host:/etc/apache2/sites-available/site1.com.conf
    <VirtualHost *:80>
    <IfModule mod_fastcgi.c>
        AddType application/x-httpd-fastphp5 .php
        Action application/x-httpd-fastphp5 /php5-fcgi
        Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi-site1.com
        FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi-site1.com -socket /var/run/php5-fpm-site1.com.sock -pass-header Authorization
    </IfModule>
  5. Go ahead and test your new configuration using the command:

    $ sudo apache2ctl configtest

    If everything is working properly, reload the Apache service:

    $ sudo systemctl reload apache2
  6. One more thing, you need to verify the user in the info.php file mentioned above, through the Environment section:

Conclusion

There you have it. A detailed guide showing you the key things to consider when you want to setup Apache and PHP-FPM on Debian 8. Over to you now. Try out these steps and get these services working on your site(s).

 

Check out these top 3 Best web hosting services

Was this article helpful?