How To Install PHP 7.4 and 8.0 On Ubuntu 18.04 or 20.04

Introduction

PHP stands for Hypertext Preprocessor, and it’s a script-based server-side programming language.

PHP is often used to automate server tasks. It handles tasks like dynamic content, database requests, and processing and displaying data.

Read our tutorial on how to install PHP 7.4 and 8.0 on Ubuntu 18.04 or 20.04 including integration with Apache and Nginx.

How to install PHP on Ubuntu

Prerequisites

Installing PHP on Ubuntu 18.04 and 20.04

Like many developer tools, PHP has several different release versions. At the time of writing, PHP 7.4. and 8.0 are the currently supported versions of the software.

How to Install PHP with Apache on Ubuntu

If you are running an Apache web server, you want to install PHP with the Apache module. Below you will find installations for PHP 7.4 and PHP 8.0.

1. Ensure you are using the latest Ubuntu updates by entering the following command into a terminal window:

sudo apt update && sudo apt upgrade

2. Install software-properties-common to help you manage distributions and independent software sources:

sudo apt install software-properties-common

3. Next, add the ondrej/php PPA which provides all the latest releases of PHP for Ubuntu 18.04:

sudo add-apt-repository ppa:ondrej/php

4. Update the repository to include the new packages:

sudo apt update

5. Now you can install PHP.

  • To install PHP 7.4, run the command:
sudo apt -y install php7.4
  • To install PHP 8.0, run:
sudo apt -y install php8.0

Note: The command for installing PHP 8.0 should automatically install the PHP module for Apache 2. If it fails to do so, run the following command instead: sudo apt -y install php8.0 libapache2-mod-php8.0.

6. Verify the installation with:

php -v

The output should display the PHP version you installed. If you installed version 7.4, the output appears as in the image below.

Output displaying PHP 7.4 has been successfully installed on Ubuntu.

If you installed PHP 8.0, the output shows:

Output displaying PHP 8.0 has been successfully installed on Ubuntu.

How to Install PHP with Nginx on Ubuntu

If you are using an Nginx server instead of Apache, follow the steps below to install PHP 7.4 or 8.0 to work with Nginx.

  1. Enter the following command into a terminal window to ensure you are using the latest software:
sudo apt update && sudo apt upgrade

. 2. To install PHP for Nginx, use one of the following commands:

  • For PHP 7.4:
sudo apt install php7.4-fpm
  • For PHP 8.0:
sudo apt install php8.0-fpm

The system will reach out to download and install the package and its dependencies.

Download and install the package and its dependencies

2. Once the installation finishes, restart the service to apply the changes by entering:

sudo systemctl restart nginx

3. Next, you need to enable PHP support by editing the server block. Open the server block with the command:

sudo nano /etc/nginx/sites-available/default

4. 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/php8.0-fpm.sock;

}

}

4. Save the file and exit.

5. Finally, restart Nginx on Ubuntu and reload PHP:

sudo systemctl restart nginx
sudo systemctl reload php[version_number]-fpm

Installing PHP Modules on Ubuntu

To install additional PHP modules, use the following syntax:

sudo apt install php[version_numeber]-[package_name]

You can also install multiple modules at once. For example, to install modules mysql, zip, json, common, and bcmath on PHP 7.4, you would run:

sudo apt install php7.4-{mysql,zip,json,common,bcmath}

To list all loaded PHP modules run the command:

php -m

The output lists all compiled PHP modules, as in the example below.

List compiled PHP modules.

Conclusion

After reading this article, you should have successfully installed PHP 7.4 or 8.0 with Apache or Nginx on your Ubuntu 18.04 and 20.04 system.

One important thing to remember is that PHP works in tandem with an existing server, so you need to install it specifically to your server software. Alternatively, you can always install a stable version from the default Ubuntu software repositories.

原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/222825.html

(0)
上一篇 2022年1月6日
下一篇 2022年1月6日

相关推荐

发表回复

登录后才能评论