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 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:
    Ubuntu

    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:
    Ubuntu
  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:

    Ubuntu

    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.

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