PHP 7.2 Entry Level Tutorial


What you will learn:

PHP is a Server Side Scripting language.
Here you will learn how to install PHP onto Ubuntu 18.04, and configure it for use with Apache2.4.

  1. Install PHP from CLI using apt


Step 1

Install PHP 7.2 using apt from the CLI

The main perk is security of the code. Code is not served to your surfer. The client can't view the source of your script. *Provided you have configured your systems properly, which is what you are here to do.

Open up your terminal using CTRL+ALT+T

Elevate your user to root:sudo su Followed by your passphrase.

Update your package list:apt update
Upgrade if necessary:apt dist-upgrade -y

Now that we are sure our system is up to date, we will attempt to install PHP. If you are following our tutorials, then you are running Ubuntu 18.04.02, which will mean we are installing PHP 7.2.


The command we will issue to install it, will put 3 different folders into our /etc/php/7.2 folder on our filesystem. cli, mods-available, and apache2. These directories contain configuration files for PHP to behave differently depending on the circumstances that they were run. Each contains a copy of specific php.ini files.

Go ahead and issue: apt install php php-cgi libapache2-mod-php php-common php-pear php-mbstring -y


Step 2

Enable CGI

Issue the command: a2enconf php7.2-cgi

This will enable the default configuration for PHP to be served through as a Common Gateway Interface. There are multiple ways to run php, one as a CGI Daemon, on or FPM which boasts being the faster of the two. We are going to go with CGI option.

After enabling php's configuration using apaches built in tool, we are going to alter a couple preferences, and restart the server.

Editing the php config file for apache

By navigating to /etc/php/7.2/ will show you a few folders depending on your chosen install option. You want to target the apache2 folder, and the php.ini inside.

Did you see?

*If you changed directories one by one, you may have noticed the folder structure indicates that PHP has its own config file under cli as well. This means running php scripts from the command line, may perform unexpectedly, unless you configure that php.ini file as well.

Issue the command: sudo nano /etc/php/7.2/apache2/php.ini

Inside the php.ini config file, we want to change the localisation options first. This was, simple scripts work correctly, out of the box.

Line 939 should be your timezone, we will set ours to read 'date.timezone = "America/New_York"

Notice

*7.2 would be the correct version if you are following our walkthroughs, you can swap out version numbers if you've stopped by and just wanted this snippet of info.

Restart your apache2 instance by issuing the command: systemctl restart apache2

Congrats!
You have installed PHP as a Common Gateway Interface. If you would like to improve performance, consider running PHP-FPM. You can see how to do that below, throught the next few steps.

Step Three

Enable FastCGI Process Manager

Lets install the package

As superuser (sudo su)
##As the super user, you will need to apt install another package.
root@wrench:/home/alpha# apt install php-fpm -y

After the installation has completed, we will need to add the FPM directive to a <VirtualHost> structure we've previously set up. In our case that should be /etc/apache2/sites-available/default.conf

Open the config file to set up your virtualhost
##Go ahead and open up /etc/apache2/sites-available/default.conf.
root@wrench:/home/alpha# nano /etc/apache2/sites-available/default.conf
# add into your existing <VirtualHost> - </VirtualHost>

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
    </FilesMatch>
</VirtualHost>

CTRL+X followed by y to save your changes.

Use apaches built-in scripts to enable the PHP-FPM Modification.
Let's wrap this next step up all in one line, you know, save some time.
##Issue this command to enable FastCGI
root@wrench:/home/alpha# time(a2enmod proxy_fcgi setenvif && a2enconf php7.2-fpm) && systemctl restart apache2


Congrats!
You've completed the PHP 7.2 walkthrough. You installed and set up PHP as a CGI, then enabled the FastCGI Module. You a whiz.