The Ubuntu system is currently one of the most widely used server operating systems and is often used to build applications such as Web servers. Redis is a high-performance open source cache database that is widely used in various web applications, especially in high-concurrency environments of large websites. It is a very common operation to build a PHP running environment and Redis database in Ubuntu, so this article will introduce the steps and process of installing PHP and Redis in the Ubuntu system.
1. Install PHP
First, we need to install PHP in the Ubuntu system. Enter the following command in the command line terminal:
sudo apt-get install php7.2
After the installation is completed, we can verify whether PHP is installed successfully through the following command:
php -v
If the PHP version information is output, it means that PHP has been successfully installed.
2. Install Redis
Next, we need to install the Redis database in the Ubuntu system. Enter the following command in the command line terminal:
sudo apt-add-repository ppa:chris-lea/redis-server && sudo apt-get update && sudo apt-get install redis-server
After the installation is completed, we can verify whether Redis is installed successfully through the following command:
redis-cli ping
If "PONG" is output, it means that Redis has been successfully installed.
3. Install PHP extension
After installing PHP and Redis, we also need to install the Redis extension of PHP to use Redis in PHP. Enter the following command in the command line terminal:
sudo apt-get install php7.2-redis
After installation, we need to enable the Redis extension in the PHP configuration file. Enter the following command in the command line terminal to edit the PHP configuration file:
sudo nano /etc/php/7.2/apache2/php.ini
Find the following line in the configuration file:
;extension=redis.so
Change it to:
extension=redis.so
Save the configuration file and exit.
4. Test PHP and Redis
After the installation is complete, we can create a PHP file for testing. Enter the following command in the command line terminal to create a test.php file:
sudo nano /var/www/html/test.php
Enter the following PHP code in the file:
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server successfully";
echo "Server is running: ".$redis->ping();
?>
Save the file and exit the editor.
Access http://localhost/test.php in the browser. If "Connection to server successfully" and "Server is running: PONG" are output, it means that PHP has successfully connected to the Redis database.
Summary
Through the above steps, we successfully installed PHP and Redis in the Ubuntu system, and enabled the Redis extension of PHP. Through testing, we can ensure that PHP can successfully connect to the Redis database. These steps are very practical for building applications such as web servers.
The above is the detailed content of ubuntu php install redis. For more information, please follow other related articles on the PHP Chinese website!