PHP and related packages are the most commonly used components when deploying a web server. In this article, we will learn how to setup PHP 7.2 on Ubuntu 18.04 LTS.
Prerequisites
- An up-to-date Ubuntu 18.04 server instance.
- A sudo user.
Update Ubuntu 18.04
First, update the list of packages:
sudo apt-get update -y
Next, install the updates:
sudo apt-get upgrade -y
Install a webserver
You can use Apache or Nginx as your webserver.
To install and start Apache:
sudo apt-get install apache2 -y
sudo systemctl start apache2.service
Install PHP 7.2
PHP 7.2 is included in the default Ubuntu repository for 18.04. You can list each of the available PHP 7.2 packages with the following command:
apt-cache pkgnames | grep php7.2
Next, install the packages that your application requires:
sudo apt-get install php -y
sudo apt-get install php-{bcmath,bz2,intl,gd,mbstring,mysql,zip,fpm} -y
Finally, restart your webserver to allow PHP to run.
For Apache, use the following:
systemctl restart apache2.service
Alternatively, use the following for Nginx:
systemctl restart nginx.service
Confirm the PHP version:
php -v
The output will resemble the following:
PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
The main config file of PHP 7.2 will be saved as /etc/php/7.2/fpm/php.ini
. You can use the vi
text editor to modify relevant settings in that file:
sudo vi /etc/php/7.2/fpm/php.ini
Note: Remember to restart Apache or Nginx if you make any changes to that file or any other PHP config files.
You have successfully set up PHP 7.2 on Ubuntu 18.04 to work with either Nginx or Apache. You are now ready to customize your configurations and deploy your apps.
How to Install PHP 7.2 with Nginx on Ubuntu 18.04
Update Ubuntu to Latest Version
Entering the following command into a terminal window to ensure you are using the latest software:
apt-get update && apt-get upgrade
Install PHP 7.2 with Nginx
1. To install PHP for Nginx, enter the following command:
sudo apt-get install php-fpm
The system will reach out to download and install the package and its dependencies.
2. Once installation finishes, restart the service by entering:
sudo systemctl restart nginx
This will apply any changes.
3. If you are using Nginx, you must be familiar with configuring server blocks.
First, add the following code to your server block file for Nginx to make use of PHP:
server {
# . . . existing configuration
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
4. Save the file and exit. Then, restart Nginx on Ubuntu with:
sudo systemctl restart nginx