Home >Backend Development >PHP Problem >How to install and configure a PHP7 environment on Ubuntu

How to install and configure a PHP7 environment on Ubuntu

PHPz
PHPzOriginal
2023-04-21 09:14:021428browse

Ubuntu is a widely used operating system based on the Linux distribution. Developers and users like Ubuntu because it is relatively fast and easy to use. In this article, we will explain how to install and configure a PHP7 environment on Ubuntu.

Step 1: Update the system

Before installation, we should ensure that the operating system is up to date. Open a terminal and enter the following command:

sudo apt update
sudo apt upgrade

The above two commands will update the system packages and install any available updates. It may take several minutes to complete these tasks.

Step 2: Install the Apache web server

PHP is a scripting language that runs on the web server. Therefore, before installing PHP, we must first install the web server.

Ubuntu uses the Apache web server, let us install it through the following command:

sudo apt install apache2

During the installation process, you will be asked to confirm the installation (select Yes), and then wait for some time until the installation is completed .

Step 3: Install PHP7 and related modules

Next, there are a few more steps required to install PHP and enable the modules. In Ubuntu 18.04 and above, we can install PHP using the following command:

sudo apt install php libapache2-mod-php php-mysql

In Ubuntu 16.04, we can install PHP using the following command:

sudo apt install php7.0 libapache2-mod-php7.0 php7.0-mysql

After the installation is complete, run the following Command to restart the Apache web server:

sudo systemctl restart apache2

Step 4: Check whether the PHP installation is successful

After the installation is completed, we can test whether it is working properly by checking whether the PHP version is correct. Enter the following command in the terminal:

php -v

This command will display the installed PHP version as follows:

PHP 7.4.3 (cli) (built: Feb 24 2020 17:23:43) ( NTS )

Step 5: Test whether PHP is working properly in Apache

To verify that the Apache web server is correctly configured to interpret PHP scripts, you need to create a file containing phpinfo(). The phpinfo() function will display detailed information about the PHP environment.

Let’s create a file called test.php. Enter the following command in the terminal:

sudo nano /var/www/html/test.php

In the open file, enter the following:

<?php
phpinfo();
?>

Save and close the file. Now, access the test.php file through your browser (for example: http://your_ip_address/test.php).

Detailed information about PHP, including installed versions, server settings, etc. are available in this page.

Conclusion

Through the guidance of this article, you have successfully installed and configured the PHP7 environment on Ubuntu. Now you can start developing PHP-based web applications.

The above is the detailed content of How to install and configure a PHP7 environment on Ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn