Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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
Learning PHP 7 High Performance

You're reading from   Learning PHP 7 High Performance Improve the performance of your PHP application to ensure the application users aren't left waiting

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher Packt
ISBN-13 9781785882265
Length 202 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Iltaf (Altaf) Hussain Gul Iltaf (Altaf) Hussain Gul
Author Profile Icon Iltaf (Altaf) Hussain Gul
Iltaf (Altaf) Hussain Gul
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Setting Up the Environment FREE CHAPTER 2. New Features in PHP 7 3. Improving PHP 7 Application Performance 4. Improving Database Performance 5. Debugging and Profiling 6. Stress/Load Testing PHP Applications 7. Best Practices in PHP Programming A. Tools to Make Life Easy B. MVC and Frameworks Index

Setting up CentOS

CentOS is a fork of Red Hat Enterprise Linux (RHEL) and stands for Community Enterprise Operating System. It is a widely used OS on servers specially used by hosting companies to provide shared hosting.

Let's start by configuring CentOS for our development environment. Perform the following steps:

Installing NGINX

  1. First, we need to add NGINX RPM to our CentOS installation because CentOS does not provide any default repository for NGINX. Issue the following command in your terminal:
    sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    

    This will add the NGINX repo to CentOS.

  2. Now, issue the following command to see which versions of NGINX are available to install:
    sudo yum --showduplicates list Nginx
    

    This will show you the latest stable releases. In our case, it displays NGINX 1.8.0 and NGINX 1.8.1.

  3. Now, let's install NGINX using the following command:
    sudo yum install Nginx
    

    This will install NGINX.

  4. On CentOS, NGINX won't start automatically after installation or restarting. So, first, we will enable NGINX to autostart after a system restarts using the following command:
    systemctl enable Nginx.service
    
  5. Now, let's start NGINX by issuing the following command:
    systemctl start Nginx.service
    
  6. Then, open your browser and enter the IP of the CentOS server or host name. If you see the same welcome screen as we saw in the figure earlier in the chapter for Debian, then NGINX is installed successfully.

    To check which version of NGINX is installed, issue the following command in the terminal:

    Nginx –v
    

    On our server, the NGINX version installed is 1.8.1.

    Now, our web server is ready.

Installing PHP 7

  1. The next step is to install PHP 7 FPM and configure both NGINX and PHP 7 to work together. As of the time of writing this book, PHP 7 is not packaged in official CentOS repositories. So, we have two choices to install PHP 7: either we build it from source, or we use third-party repositories. Building from source is a little bit difficult, so let's go the easy way and use third-party repositories.

    Note

    For this book, we will use webtatic repos for the PHP 7 installation as they provide quick updates for the new versions. There are some more repositories, and it is just the reader's choice to use any repository as long as it works.

  2. Now, let's add a webtatic repository to our CentOS repo by issuing the following command:
    rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    
  3. After the repos are added successfully, issue the following command to see which version is available for installation:
    sudo yum –showduplicates list php70w
    

    In our case, PHP 7.0.3 is available to install.

  4. Now, issue the following command to install PHP 7 along with some modules that may be required:
    sudo yum install php70w php70w-common php70w-cli php70w-fpm php70w-mysql php70w-opcache php70w-mcrypt
    
  5. This will install core PHP 7 and some modules available for PHP 7. If any other module is required, it can be installed easily; however, first, search to check whether it is available or not. Issue the following command in the terminal to see all the available modules for PHP 7:
    sudo yum search php70w-
    

    We will see a long list of all the available modules for PHP 7.

  6. Now, let's say that we want to install the PHP 7 gd module; issue the following command:
    sudo yum install php70w-gd
    

    This will install the gd module. Multiple modules can be installed using the same command and separating each module by a space, as we did in the initial installation of PHP.

    Now, to check which version of PHP is installed, issue the following command:

    php –v
    

    In our case, PHP 7.0.3 is installed.

  7. To start, stop, and restart PHP, issue the following commands in the terminal:
    sudo systemctl start php-fpm
    sudo systemctl restart php-fpm
    sudo systemctl stop php-fpm
    
  8. Now, let's configure NGINX to use PHP FPM. Open the default NGINX virtual host file located at /etc/Nginx/conf.d/default.conf using either vi, nano, or any other editor of your choice. Now, make sure that two options are set in the server block, as follows:
    server {
        listen  80;
        server_name  localhost;
        root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
    

    The root option indicates the web document root where our website source code files will be placed. Index indicates the default files that will be loaded along with extensions. If any of these files are found, they will be executed by default, regardless of any file mentioned in the URLs.

  9. The next configuration in NGINX is a location block for PHP. The following is the configuration for PHP:
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
          include fastcgi_params;
        }

    The preceding block is the most important configuration as it enables NGINX to communicate with PHP. The line fastcgi_pass 127.0.0.1:9000 tells NGINX that PHP FPM can be accessed on the 127.0.0.1 loopback IP on port 9000. The rest of the details are the same as those we discussed for Debian and Ubuntu.

  10. Now, to test our installation, we will create a file named info.php with the following contents:
    <?php
      phpinfo();
    ?>

    After saving the file, type http://server_ip/info.php or http://hostname/info.php, and we will get a page with complete information about PHP. If you see this page, congratulations! PHP runs alongside NGINX.

Installing Percona Server

  1. Now, we will install Percona Server on CentOS. The installation process is the same, except that it has a separate repository. To add the Percona Server repo to CentOS, execute the following command in the terminal:
    sudo yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
    

    After the repo installation is completed, a message will be displayed stating the completion of the installation.

  2. Now, to test the repo, issue the following command, and it will list all the available Percona packages:
    sudo yum search percona
    
  3. To install Percona Server 5.5, issue the following command in the terminal:
    sudo yum install Percona-Server-server-55
    

    The installation process will start. The rest of the process is the same as for Debian/Ubuntu.

  4. After the installation is completed, we will see a completion message.
You have been reading a chapter from
Learning PHP 7 High Performance
Published in: Apr 2016
Publisher: Packt
ISBN-13: 9781785882265
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 $19.99/month. Cancel anytime
Banner background image