Install PHP on Ubuntu

Install PHP on Ubuntu

If you haven’t installed iRedMail email server and would like to host websites only without the email server, you need to install PHP on Ubuntu with Nginx and MariaDB manually. The Aforementioned three components come as default with the iRedMail installation. It’s advisable to install PHP before Nginx webserver to pick some configurations from the PHP.

1. Before you install PHP on Ubuntu, update your server by running the below command on PuTTY SSH client or any Linux terminal as below. Always, you need to hit Enter key to proceed.

root@iRedMail-doesnt-exist:~# apt update

Once completed, will see the result as follows, it will take only few seconds to be done.

APT Update Command to Install PHP on Ubuntu

2. Upgrade the packages on your server by running the below command.

root@iRedMail-doesnt-exist:~# apt upgrade

If there are packages to upgrade, you will need to confirm as seen, type: Y (or: y), then hit Enter key.

APT Upgrade Command

It might take few minutes and you may see a progress like below:

Apt Upgrade Progress

Sometimes during the upgrade process apt upgrade command, you get popups like the one below about updating the packages configuration, don’t change anything, just hit Enter key and keep it as default. Do this for any similar popup.

Apt Upgrade Popup

And here is it when the upgrade process is complete.

Apt Upgrade Completed

If your server has no packages to upgrade, you don’t need to do action as seen below:

Apt Upgrade No Packages to Upgrade

3. Install the Software Properties Common package by running the command:

root@iRedMail-doesnt-exist:~# apt install software-properties-common

You should confirm the installation as per the prompt below, type: Y (or y) then hit Enter key. You will see a progress, you need to wait few seconds until it’s complete.

Install Software Properties Common

In case the package already exists by default, you will see something like this and don’t need to do any further action.

Install Software Properties common Already Exists

4. Add the APT repository for the PHP. This step will allow the PHP version to be upgraded automatically when you upgrade the packages on your server (when running: apt upgrade).

root@iRedMail-doesnt-exist:~# add-apt-repository ppa:ondrej/php

Ubuntu prompt will ask you to confirm the installation, press (hit) Enter key.

Add PHP Apt Repository

Few seconds following the confirmation, it will be done as seen below.

Add PHP Apt Repository Done

5. Update your server again by running the below commands:

root@iRedMail-doesnt-exist:~# apt update
root@iRedMail-doesnt-exist:~# apt upgrade

Find Latest PHP Release

6. Now, it’s the time to install the latest stable PHP release which is PHP 8.0, to know that, search on google or any search engine and write: “latest stable PHP release“, here’s the result until the time of writing this article:

Search for Latest Stable PHP Release

Now, run the below command to install the latest stable PHP release:

root@iRedMail-doesnt-exist:~# apt install php8.0-fpm

Notice that we didn’t run the command: apt install php8.0, because it installs the Apache server too and in our tutorial we will use Nginx server as it provides better performance.

Type: Y (or y) to confirm and continue.

Install PHP FPM Confirm

You will see the progress as follows.

Install PHP FPM Progress

It will take few seconds to finish the installation as seen below.

Install PHP FPM Done

7. To make sure that we’ve installed the latest version of PHP, run the below command:

root@iRedMail-doesnt-exist:~# php -v

You can see the latest version (until writing this article) below. Once you upgrade your server by running apt upgrade, this version will be upgraded too.

Check Latest PHP Version Installed

Improve the performance of PHP

Go php.ini file:

root@iRedMail-doesnt-exist:~# nano /etc/php/8.0/fpm/php.ini

And make sure the below variables have the corresponding values.

max_execution_time = 60
memory_limit = 256M
post_max_size = 64M
upload_max_filesize = 64M
max_input_vars = 5000

You must restart php-fpm service for the changes to take effect by running the below command:

root@iRedMail-doesnt-exist:~# service php8.0-fpm restart

Remove the Repository

Once you finish the installation, remove the repository we added by running the command below. Notice that after removing it, when you run apt-upgrade, it will not offer you to upgrade to a higher PHP veresion. So this way you can upgrade the PHP manually whenever you want.

 root@iRedMail-doesnt-exist:~# add-apt-repository ppa:ondrej/php --remove

You need to hit Enter key to proceed.

The next step is to Install Nginx on Ubuntu.