Home  >  Article  >  Backend Development  >  How to install PHP and MySQL database on Linux

How to install PHP and MySQL database on Linux

PHPz
PHPzOriginal
2023-03-23 16:35:221619browse

Linux is an open source operating system that supports multiple programming languages, among which PHP is the most popular language and MySQL is the most popular relational database management system. Installing PHP and MySQL on Linux is a very simple process.

Below we will introduce you how to install PHP and MySQL database on Linux.

  1. Installing PHP

To install PHP on Linux, you need to use the following command:

sudo apt-get update
sudo apt-get install php

This command will download and install PHP. Once the download and installation is complete, you can verify that the installation was successful:

php -v
  1. Installing MySQL

Installing MySQL is very simple. You can use the following command:

sudo apt-get install mysql-server

This command will install the MySQL service and prompt you to set a password for MySQL. Be sure to remember this password as you will need it in later steps.

  1. Connecting to MySQL

On Linux, you can use the command line interface to connect to MySQL. Just use the following command:

mysql -u 用户名 -p

You will then be prompted for your MySQL password. Enter the password you assigned MySQL when you installed it.

  1. Create Database

To create a new MySQL database, follow these steps:

a. In MySQL Enter the following command on the command line:

CREATE DATABASE dbname;

Please note that dbname is the name of the database you will create. You can use any name as the database name.

b. Then you need to create a user for the database. Use the following command:

CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';

In this command, 'user' is the username you will create and 'password' is the password for this user. Note that this command creates a user that can only connect to the MySQL server from localhost.

c. Authorize users to access the database. Use the following command:

GRANT ALL PRIVILEGES ON dbname.* TO 'user'@'localhost';

Please note that 'dbname' is the name of the database you created previously and 'user' is the username you created previously.

d. Refresh permissions. Use the following command:

FLUSH PRIVILEGES;
  1. Connect to MySQL using PHP

Now that you have created the database and granted access to the user, next are the steps to connect PHP with MySQL.

a. First, you need to install PHP and MySQL related extensions. Use the following command:

sudo apt-get install php-mysql

b. Open the php.ini file and enable the mysql extension. Use the following command:

sudo nano /etc/php/7.4/apache2/php.ini

Find the following line in the file:

;extension=pdo_mysql

Uncomment and change it to:

extension=pdo_mysql

c. Save the changes and restart the Apache service. Use the following command:

sudo systemctl restart apache2

Now, PHP will connect with the MySQL database. You can use the following code in your PHP script to connect to a MySQL database:

<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "dbname";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>

So far, we have covered how to install PHP and MySQL database on Linux and how to connect them. Now you are free to explore these technologies and use them to create interactive web applications.

The above is the detailed content of How to install PHP and MySQL database on Linux. 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