- Categories
- Linux Guides
- How to Install and Configure PHP 7.2 on Ubuntu 18.04
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
Create your sudo user
Ubuntu ships with sudo
installed, so the first step will be to simply add a new user:
adduser
You will be asked to set information about this user:
Enter the new value, or press ENTER for the default
Full Name []: Test User
Room Number []: 01
Work Phone []: 5555555
Home Phone []: 5555555
Other []:
You can fill these fields in, or press ENTER to leave them default. After this press Y and then ENTER to verify that the information is correct.
Next, add the new user to the sudo
group:
usermod -aG sudo
You can now log out, and log back in as your new user. To test that the user was added correctly, use the following command once you have logged back in as the new user:
ls -la /root
You will receive the following notice:
ls: cannot open directory '/root': Permission denied
When you append the previous command with sudo
, you will be asked for your password and receive a list of the /root
directory:
sudo ls -la /root
You can now move onto updating Ubuntu.
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
To install and start Nginx:
sudo apt-get install nginx -y
sudo systemctl start nginx.service
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.
Want to contribute?
Table of Contents
1
- Requirements:
- Step 1: Connect to your server via SSH and update your server OS packages
- Step 2: Install a Web Server
- Step 3: Install Ondřej Surý’s PPA
- Step 4: Install PHP 7.2
- Step 5: Search and install specific PHP 7.2 extensions
- Step 7: Install most commonly used PHP extensions
- Step 8: Check the PHP installation
- Disable PHP 7.0
- Enable PHP 7.2
Requirements:
- For the purposes of this tutorial, we will use an Ubuntu VPS. Our Ubuntu 16.04 VPS already comes preinstalled with a fully working LAMP stack. However, we will still go through all the necessary steps and show you how to install and configure the LAMP stack yourself, in case you are doing this on a clean server.
- Full SSH root access or a user with sudo privileges is also required.
Step 1: Connect to your server via SSH and update your server OS packages
Before we begin, let’s connect to your VPS via SSH as user root and update your Ubuntu OS packages to the latest available version.
To connect to your server via SSH as user root, use the following command:
ssh root@IP_ADDRESS -p PORT_NUMBER
Replace “IP_ADDRESS” and “PORT_NUMBER” with your actual server IP address and SSH port number.
Once logged in, make sure that your server OS packages are up-to-date by running the following commands:
apt-get update apt-get upgrade
If this is your first time logging into your brand new Ubuntu VPS, you can also check our First Steps After Getting An Ubuntu VPS tutorial for some additional basic initial server setup and security tips.
Step 2: Install a Web Server
In this section, we will show you how to install a web server on your VPS. We can install Apache, or nginx as a web server. For the purpose of this tutorial, we will install the Apache web server. Apache is a fast and secure web server and one of the most popular and widely used web server in the world.
To install the Apache web server, run the following command on your server:
apt-get install apache2
After the installation is complete, you should start Apache:
systemctl start apache2
Also, you can enable Apache to start automatically on server boot:
systemctl enable apache2
To check the status of the Apache web server and make sure it is up and running, you can use the following command:
systemctl status apache2
To verify that Apache is running, you can also open your web browser and enter your server IP address, [e.g. //your_server_ip_address]. If Apache is successfully installed, you should see the Apache default welcome page.
Step 3: Install Ondřej Surý’s PPA
PHP 7.2 can be installed using Ondřej Surý’s PPA, so install the software-properties-common and python-software-properties packages:
sudo apt-get install software-properties-common python-software-properties
Then, add the ondrej PPA and update your sources:
sudo add-apt-repository -y ppa:ondrej/php sudo apt-get update
Step 4: Install PHP 7.2
Install PHP 7.2 using the following command:
sudo apt-get install php7.2 php7.2-cli php7.2-common
Step 5: Search and install specific PHP 7.2 extensions
If you want to install a specific PHP 7.2 extension, you can search if it is available using the following command:
sudo apt search php7.2
Step 7: Install most commonly used PHP extensions
To install the most commonly used PHP extensions you can use the following command:
sudo apt-get install php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-intl php7.2-mysql php7.2-xml php7.2-zip
Step 8: Check the PHP installation
Use the following command to check the PHP version installed on your server:
php -v
You should receive the following output:
PHP 7.2.9-1+ubuntu16.04.1+deb.sury.org+1 [cli] [built: Aug 19 2018 07:16:12] [ NTS ] Copyright [c] 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright [c] 1998-2018 Zend Technologies with Zend OPcache v7.2.9-1+ubuntu16.04.1+deb.sury.org+1, Copyright [c] 1999-2018, by Zend Technologies
Now, PHP 7.2 has been installed on your Ubuntu server, and if we want to change the default PHP version to PHP 7.2 that is used by the web server, we need to disable the old PHP 7.0 version and enable the newly installed one.
Disable PHP 7.0
a2dismod php7.0
Enable PHP 7.2
a2enmod php7.2
Restart the Apache web server for the changes to take effect:
systemctl restart apache2
To test your installation and check that Apache, PHP and PHP extensions are working properly, create a new php info file:
vi /var/www/html/phpinfo.php
Add the following content to it:
Open the ‘phpinfo.php’ file using a web browser:
///phpinfo.php or ///phpinfo.php
and view the current information about PHP on your
server.
PS. If you liked this post, on how to install PHP 7.2 on an Ubuntu 16.04 server, please share it with your friends on the social networks using the buttons below or simply leave a comment in the comments section. Thanks.