How to install and configure PHP on Linux

PHPz
Release: 2023-07-06 13:15:25
Original
1535 people have browsed it

How to install and configure PHP on Linux

PHP is an open source server-side scripting language widely used in web development. Installing and configuring PHP on Linux can provide us with a powerful development environment. This article explains how to install and configure PHP on Linux and provides some sample code.

  1. Installing PHP
    Installing PHP on Linux is very simple. We can install PHP through the package manager.

On Ubuntu or Debian systems, use the following command:

$ sudo apt-get install php

On CentOS or RHEL system, use the following command:

$ sudo yum install php

  1. Configure PHP
    After installing PHP, we need to perform some configurations on PHP.

First, we need to edit the PHP configuration file php.ini. Run the following command in the terminal to edit:

$ sudo vim /etc/php/php.ini

You can make some common configurations according to actual needs. For example, we can modify the time zone settings.

Find the following line:

;date.timezone =

Change it to:

date.timezone = Asia/Shanghai

Save and exit the editor.

  1. Configuring Apache (optional)
    If we want to use Apache as a web server on Linux, we also need to do some additional configuration.

First, we need to enable the PHP module. Run the following command in the terminal:

$ sudo a2enmod php

Then, restart the Apache server:

$ sudo service apache2 restart

  1. Sample code
    Through the following sample code, we can see how to use PHP to output "Hello, World!" on a web page.

First, create a file named "index.php" and run the following command in the terminal:

$ touch index.php

Then, edit The file:

$ vim index.php

Paste the following code into the file:

<!DOCTYPE html>
<html>
<body>

<?php
echo "Hello, World!";
?>

</body>
</html>
Copy after login

Save and exit the editor.

Now, we can view the results through the browser. Enter "http://localhost/index.php" in the browser, and you can see "Hello, World!" output on the page.

Summary
This article explains how to install and configure PHP on Linux and provides some sample code. With these simple steps, we can set up a powerful PHP development environment on Linux for web development and other application development. Installing and configuring PHP is a basic skill and an essential step for developers who want to do web development on Linux.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template