Book Image

Learning PHP 7 High Performance

Book Image

Learning PHP 7 High Performance

Overview of this book

PHP is a great language for building web applications. It is essentially a server-side scripting language that is also used for general-purpose programming. PHP 7 is the latest version, providing major backward-compatibility breaks and focusing on high performance and speed. This fast-paced introduction to PHP 7 will improve your productivity and coding skills. The concepts covered will allow you, as a PHP programmer, to improve the performance standards of your applications. We will introduce you to the new features in PHP 7 and then will run through the concepts of object-oriented programming (OOP) in PHP 7. Next, we will shed some light on how to improve your PHP 7 applications' performance and database performance. Through this book, you will be able to improve the performance of your programs using the various benchmarking tools discussed. At the end, the book discusses some best practices in PHP programming to help you improve the quality of your code.
Table of Contents (17 chapters)
Learning PHP 7 High Performance
Credits
About the Author
Acknowledgement
About the Reviewer
www.PacktPub.com
Preface
Index

Setting up Debian or Ubuntu


Ubuntu is derived from Debian, so the process is the same for both Ubuntu and Debian. We will use Debian 8 Jessie and Ubuntu 14.04 Server LTS. The same process can be applied to desktop versions for both.

First, add the repositories for both Debian and Ubuntu.

Debian

As of the time we're writing this book, Debian does not provide an official repository for PHP 7. So, for Debian, we will use dotdeb repositories to install NGINX and PHP 7. Perform the following steps:

  1. Open the /etc/apt/sources.list file and add the following two lines at the end of the file:

    deb http://packages.dotdeb.org jessie all
    deb-src http://packages.dotdeb.org jessie all
  2. Now, execute the following commands in the terminal:

    wget https://www.dotdeb.org/dotdeb.gpg
    sudo apt-key add dotdeb.gpg
    sudo apt-get update
    

The first two commands will add dotdeb repo to Debian and the last command will refresh the cache for sources.

Ubuntu

As of the time of writing this book, Ubuntu also does not provide PHP 7 in their official repos, so we will use a third-party repo for the PHP 7 installation. Perform the following steps:

  1. Run the following commands in the terminal:

    sudo add-apt-repository ppa:ondrej/php
    sudo apt-get update
    
  2. Now, the repositories are added. Let's install NGINX and PHP 7.

    Note

    The rest of the process is mostly the same for both Debian and Ubuntu, so we wont list them separately, as we did for the adding repositories section.

  3. To install NGINX, run the following command in the terminal (Debian and Ubuntu):

    sudo apt-get install nginx
    
  4. After the installation is successful, it can be verified by entering the hostname and IP of the Debian or Ubuntu server. If we see something similar to the following screenshot, then our installation is successful:

    The following is a list of three useful NGINX commands:

    • service nginx start: This starts the NGINX server

    • service nginx restart: This restarts the NGINX server

    • service nginx stop: This stops the NGINX server

  5. Now, it's time to install PHP 7 by issuing the following command:

    sudo apt-get install php7.0 php7.0-fpm php7.0-mysql php7.0-mcrypt php7.0-cli
    

    This will install PHP 7 along with the other modules mentioned. Also, we installed PHP Cli for the command-line purpose. To verify whether PHP 7 is properly installed, issue the following command in the terminal:

    php –v
    
  6. If it displays the PHP version along with some other details, as shown in the following screenshot, then PHP is properly installed:

  7. Now, we need to configure NGINX to work with PHP 7. First, copy the NGINX default config file /etc/nginx/sites-available/default to /etc/nginx/sites-available/www.packt.com.conf using the following command in the terminal:

    cd /etc/nginx/sites-available
    sudo cp default www.packt.com.conf
    sudo ln –s /etc/nginx /sites-available/www.packt.com.conf /etc/ nginx/sites-enabled/www.packt.com.conf
    

    First, we copied the default configuration file, created another virtual host configuration file, www.packt.com.conf, and then created a symbolic link file to this virtual host file in the sites-enabled folder.

    Note

    It is good practice to create a configuration file for each virtual host by the same name as of the domain so that it can easily be recognized by any other person.

  8. Now, open the /etc/nginx/sites-available/www.packt.com.conf file and add or edit the highlighted code, as shown here:

    server {
      server_name your_ip:80;
      root /var/www/html;
      index index.php index.html index.htm;
      location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
          fastcgi_index index.php;
          include fastcgi_params;
      }
    }

    The preceding configuration is not a complete configuration file. We copied only those configuration options that are important and that we may want to change.

    In the preceding code, our webroot path is /var/www/html, where our PHP files and other application files will be placed. In the index config option, add index.php so that if no file is provided in the URL, NGINX can look for and parse index.php.

    We added a location block for PHP that includes a fastcgi_pass option, which has a path to the PHP7 FPM socket. Here, our PHP runs on a Unix socket, which is faster than that of TCP/IP.

  9. After making these changes, restart NGINX. Now, to test whether PHP and NGINX are properly configured, create an info.php file at the root of the webroot folder and place the following code in it:

    <?php
      phpinfo();
     ?>
  10. Now, in the browser, type server_ip/info.php, and if you see a PHP configuration page, then congratulations! PHP and NGINX are both properly configured.

    Note

    If PHP and NGINX run on the same system, then PHP listens to the loopback IP at port 9000. The port can be changed to any other port. In case, we want to run PHP on the TCP/IP port, then in fastcgi_pass, we will enter 127.0.0.1:9000.

Now, let's install Percona Server. Percona Server is a fork of MySQL and is optimized for high performance. We will read more about Percona Server in Chapter 3, Increasing PHP 7 Application Performance. Now, let's install Percona Server on Debian/Ubuntu via the following steps:

  1. First, let's add the Percona Server repository to our system by running the following command in the terminal:

    sudo wget https://repo.percona.com/apt/percona-release_0.1-3.$(lsb_release -sc)_all.deb
    sudo dpkg -i percona-release_0.1-3.$(lsb_release -sc)_all.deb
    

    The first command will download the repo packages from the Percona repo. The second command will install the downloaded packages and will create a percona-release.list file at /etc/apt/sources.list.d/percona-release.list.

  2. Now, install Percona Server by executing the following command in the terminal:

    sudo apt-get update
    
  3. Now, issue the following command to install Percona Server:

    sudo apt-get install percona-server-5.5
    

    The installation process will start. It will take a while to download it.

    Note

    For the purpose of this book, we will install Percona Server 5.5. Percona Server 5.6 is also available, which can be installed without any issues.

    During the installation, the password for the root user will be asked, as shown in the following screenshot:

    It is optional but recommended to enter the password. After entering the password, re-enter the password on the next screen. The installation process will continue.

  4. After the installation is complete, the Percona Server installation can be verified by using the following command:

    mysql –-version
    

    It will display the version of Percona Server. As mentioned before, Percona Server is a fork of MySQL, so all the same MySQL commands, queries, and settings can be used.