Home  >  Article  >  Backend Development  >  Raspberry Pi php installation mysql

Raspberry Pi php installation mysql

王林
王林Original
2023-05-07 09:26:07823browse

The Raspberry Pi is a small computer with both development and entertainment functions. Due to its small size, low price, and low power consumption, it is widely used in maker, education, home entertainment and other fields. Developing applications on the Raspberry Pi involves learning and using many technology stacks. This article mainly introduces how to install PHP extension on Raspberry Pi, connect to MySQL database, and provide support for application development.

1. Install the LAMP environment on the Raspberry Pi

Before installing the PHP extension on the Raspberry Pi, you need to install the LAMP environment (i.e. Linux Apache MySQL PHP). Here we use Raspberry Pi OS as the operating system.

  1. Install Apache

Enter the following command in the terminal to install Apache:

sudo apt-get update
sudo apt-get install apache2 -y

After the installation is complete, enter the IP of the Raspberry Pi in the browser Address, you can see the following page:

Raspberry Pi php installation mysql

  1. Install MySQL

Enter the following command in the terminal to install MySQL:

sudo apt-get install mysql-server -y

After the installation is completed, enter the following command in the terminal to start the MySQL service:

sudo systemctl start mysql.service

You can use the following command to verify whether MySQL is successfully installed:

sudo mysql -u root -p

After entering the password, if the connection is successful to the MySQL database, the following information will be displayed:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 137
Server version: 5.7.26-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
  1. Install PHP

Enter the following command in the terminal to install PHP:

sudo apt-get install php libapache2-mod-php -y

Installation completed After that, restart the Apache service:

sudo systemctl restart apache2.service

2. Install the MySQL extension on the Raspberry Pi

Install the MySQL extension in the LAMP environment so that the application can connect to the MySQL database and read and write data Enter operations. Two commonly used installation methods are introduced below.

  1. Install via apt-get

Enter the following command in the terminal to install:

sudo apt-get install php-mysql -y

After the installation is complete, restart the Apache service:

sudo systemctl restart apache2.service
  1. Installation through source code

First you need to install the libmysqlclient-dev library, enter the following command to install:

sudo apt-get install libmysqlclient-dev -y

Download the PHP source code package, unzip:

wget https://www.php.net/distributions/php-7.4.27.tar.xz
tar xf php-7.4.27.tar.xz

Enter the decompressed directory and configure the compilation options:

cd php-7.4.27
./configure --with-mysqli/mysqlnd --with-pdo-mysql/mysqlnd

Compile:

make
sudo make install

Add the following configuration to the php.ini file:

extension=mysqli

Save the configuration Afterwards, restart the Apache service:

sudo systemctl restart apache2.service

3. Test the MySQL connection

On the Raspberry Pi, you can use PHP code to test whether the MySQL connection is successful. Create a test.php file in the /var/www/html directory and enter the following code:

<?php $servername = "localhost";
$username = "root";
$password = "password";

// 创建连接
$conn = mysqli_connect($servername, $username, $password);

// 检测连接是否成功
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Among them, $servername is the address of the database, $username is the username, and $password is the password.

Access the test.php file. If "Connected successfully" is displayed, it means the connection to MySQL is successful.

4. Summary

Through the introduction of this article, we learned how to install the LAMP environment and MySQL extension on the Raspberry Pi, and how to test the MySQL connection. In application development, MySQL database is a frequently used storage method. Mastering the method of connecting to MySQL is crucial for Raspberry Pi application development.

The above is the detailed content of Raspberry Pi php installation mysql. 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