Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Magento 2 Cookbook

You're reading from   Magento 2 Cookbook Exploring Magento 2 in the form of recipes

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher
ISBN-13 9781785887062
Length 342 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Toc

Table of Contents (10) Chapters Close

Preface 1. Installing Magento 2 on Apache and NGINX 2. Magento 2 System Tools FREE CHAPTER 3. Enabling Performance in Magento 2 4. Creating Catalogs and Categories 5. Managing Your Store 6. Creating a Magento 2 Theme 7. Creating Magento 2 Extensions – the Basics 8. Creating Magento 2 Extensions – Advanced Index

Installing PHP-FPM

Throughout the following recipes, we will install the latest PHP 7.x version on Apache and NGINX. The current PHP-FastCGI Process Manager (PHP-FPM) will be installed through a third-party package. This recipe will show you how to do it.

Tip

PHP 7 is the latest version of the PHP stack. It's also known as PHP Next Generation (PHP NG) and PHP's answer to HHVM. Currently, it's two to three times faster than PHP 5.x.

The whole code base is rewritten and uses less memory. Lots of PHP functions are backward-compatible with the PHP 5.x code base, so upgrading to PHP 7 should be easy.

Getting ready

For this recipe, you will need a preinstalled Apache or NGINX setup. No other prerequisites are required.

How to do it...

For the purpose of this recipe, let's assume that we need to create an PHP-FPM hosting environment. The following steps will guide you through this:

  1. First, we will start installing PHP-FPM on Apache; later, we will do the same with NGINX.
  2. Next, we will install PHP 7 on Apache 2 using a third-party package. Currently, Ubuntu doesn't include the latest PHP 7 yet. The third-party vendor that we will use is https://launchpad.net/~ondrej/+archive/ubuntu/php.

    Run the following command:

    echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu wily main" | sudo tee -a /etc/apt/sources.list.d/php.list
    echo "deb-src http://ppa.launchpad.net/ondrej/php/ubuntu wily main" | sudo tee -a /etc/apt/sources.list.d/php.list
    
  3. As we have already used the authorized Apache package from the same vendor, we are okay on the signed key. There is no need to install the key anymore.
  4. Now, we will update our Ubuntu system using the latest PHP 7 packages:
    apt-get update && apt-get -y install php7.0 php7.0-fpm php7.0-dev php7.0-mhash php7.0-mcrypt php7.0-curl php7.0-cli php7.0-mysql php7.0-gd php7.0-intl php7.0-xsl
    
  5. Once every PHP 7 package is installed, we can check whether everything is in order by running the following command on the shell:
    php -v
    

    The output of this command is as follows:

    root@mage2cookbook:/etc/php# php -v
    PHP 7.0.0 (cli) ( NTS )
    Copyright (c) 1997-2015 The PHP Group
    Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
    
  6. Now, we will configure PHP-PFM with Apache. Out of the box, Apache 2 comes with a module named mod_proxy, which we will use to connect Apache to PHP-FPM. We will be using PHP-FPM in TCP mode instead of the Unix socket. The main reason is that it's easy to configure and we can use it later with HHVM. The default port 9000 will be our connector. To enable mod_proxy, run the following command on the shell:
    a2enmod proxy_fcgi
    
  7. Before we can connect Apache to PHP-FPM, we need to give PHP-FPM instructions to listen to the correct internal port. Run the following command from the shell:
    sed -i "s/listen =.*/listen = 127.0.0.1:9000/" /etc/php/7.0/fpm/pool.d/www.conf
    

    You can do this manually as well; edit the www.conf file with your favorite editor in your PHP-FPM pool directory. Search for listen = /var/run/php/php7.0-fpm.sock and change this to listen = 127.0.0.1:9000.

  8. Restart your PHP-FPM process to use the altered changes. Run the following command from the shell:
    service php7.0-fpm restart
    
  9. Next, we need to configure Apache using the currently set-up internal proxy_fcgi module. The default Apache Virtual Host configuration file is located at /etc/apache2/sites-enabled/000-default.conf. Edit this file and add the following code just before the closing </VirtualHost> tag on one line without any breaks:
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1

    The following code is for the new 000-default.conf:

    <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    <Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    allow from all
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1
    </VirtualHost>

    You can change DocumentRoot /var/www/html to any given preferred directory. However, for the rest of the recipes, we will stay with the default.

  10. After saving the Apache configuration, we need to restart the web server. Run the following command from the shell:
    service apache2 restart
    
  11. Now, we need to check whether Apache and PHP-FPM are working fine. Go to the cd /var/www/html directory and create the following file including content:
    echo "<?php phpinfo(); ?>" > phpinfo.php
    
  12. If you have followed steps 1 to 11, you will be able to see if PHP-FPM works with your web server. Go to your favorite browser and search using your yourdomain.com/phpinfo.php.

    You should now see a phpinfo page like the following screenshot. Check on the second line for the Server API; this should be FPM/FastCGI.

    How to do it...

    If everything is working fine, we have completed the Apache PHP-FPM setup. Now let's do the same for the NGINX and PHP-FPM setup.

    As we have already installed the PHP-FPM packages in steps 1 to 5, we now need to combine them with NGINX.

    Before we continue, it is important to use either a clean DigitalOcean Droplet with NGINX running or you can disable the Apache server using the following command form the shell:

    service apache2 stop && service nginx start
    
  13. Next, we need to configure NGINX. The default NGINX configuration file is located at /etc/nginx/conf.d/default.conf. Remove the old configuration in this file and add the following code:
    server {
      listen       80;
      server_name  yourdomain.com;
    
      root   /var/www/html;
      index  index.php index.html;
    
      access_log /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log;
    
      location ~ \.php$ {
    
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
      }
    
      location ~ /\.ht {
        deny  all;
      }
    }
  14. Save the new configuration using your favorite editor and restart your NGINX and PHP-PFM server using the following command on the shell:
    service nginx restart && service php7.0-fpm restart
    

    A nice trick to test if your configuration is correct is as follows:

    nginx –t
    

If you have followed steps 13 to 14, you will be able to see if PHP-FPM works with your web server. Go to your favorite browser and search using your yourdomain.com/phpinfo.php.

You should now see a phpinfo page similar to the one on the previous page during the Apache PHP-FPM setup. Check on the second line for the Server API; this should be FPM/FastCGI.

Tip

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  • Log in or register to our website using your e-mail address and password.
  • Hover the mouse pointer on the SUPPORT tab at the top.
  • Click on Code Downloads & Errata.
  • Enter the name of the book in the Search box.
  • Select the book for which you're looking to download the code files.
  • Choose from the drop-down menu where you purchased this book from.
  • Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

How it works…

Let's recap and find out what we did throughout this recipe. In steps 1 through 4, we first install PHP-FPM from a third-party repository. The main reason that we use this repository is that it is easy to use and well-supported. Current Ubuntu versions don't have the latest PHP 7 available yet. If you are comfortable installing PHP from source, you can do so.

All PHP modules listed in step 4 are mandatory for Magento 2 to work properly. In step 6, we explain why we use PHP-FPM running on port 9000. Setting up a TCP port is much easier when using multiple PHP backends, switching from Apache to NGINX, or PHP-FPM versus HHVM.

In step 7, we add an Apache module that acts as a proxy, so we are able to connect PHP-FPM to Apache.

In step 8, we changed the PHP-FPM pool to switch from Unix sockets to TCP. If you prefer sockets, you can do so.

In step 10, we add the ProxyPassMatch rule to our Apache configuration file, which will serve all incoming PHP requests to the PHP-FPM server. After saving and restarting the Apache server, we are able to test if everything works.

In step 13, we configure NGINX to work with PHP-PFM. Creating fastcgi_pass does the trick to connect to port 9000.

There's more…

If you want to check whether PHP-FPM is running fine, use one of the following commands:

service php7.0-fpm status
netstat -anp | grep php-fpm

To check which server is running, check the headers by running the following command:

curl -I http://mage2cookbook.com/phpinfo.php

The output of this command will be as follows:

root@mage2cookbook:~# curl -I http://mage2cookbook.com/phpinfo.php
HTTP/1.1 200 OK
Server: nginx/1.9.6

Once we switch back to Apache we will see the following:

service nginx stop && service apache2 start
root@mage2cookbook:~# curl -I http://mage2cookbook.com/phpinfo.php
HTTP/1.1 200 OK
Server: Apache/2.4.17 (Ubuntu)

Tip

Use phpinfo.php wisely on a production environment. Sharing this information on a production website is not advised and could expose your security risks.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime