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.
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.
For this recipe, you will need a preinstalled Apache or NGINX setup. No other prerequisites are required.
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:
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
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
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
a2enmod proxy_fcgi
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
.
service php7.0-fpm restart
/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.
service apache2 restart
cd /var/www/html
directory and create the following file including content:echo "<?php phpinfo(); ?>" > phpinfo.php
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.
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
/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;
}
}
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.
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:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
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.
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)
3.142.135.249