Monday, October 14, 2024
HomeTrending newsInstall and configure REDIS on Ubuntu and PHP7.x

Install and configure REDIS on Ubuntu and PHP7.x

Install and optimize REDIS on Ubuntu and PHP7.x

Index

  • Introduction
  • Install Redis
  • Configure Redis
  • Check the connection to the Redis server
  • Monitor Redis
  • Clear the cache
  • Redis + WordPress
  • Control dashboard for Redis
  • Remove Redis
  • Nginx FastCGI-Cache vs Redis Object Cache
  • Results and performance

Introduction

Are you a webmaster and want to make your dynamic site faster? Then read on. In previous guides I talked about applications able to make the web server faster, such as OPCACHE and Memcached , today we see Redis. Redis basically does the same thing as Memcached, but it’s newer and offers a few more features, plus the official site is well-maintained and full of information.

Redis is an open source (BSD licensed) repository of in-memory data structures used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperlogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of persistence on disk, and offers high availability through Redis Sentinel and automatic partitioning with Redis Cluster.

Redis decreases the page load time of dynamic database-driven websites by caching the required data and reducing the load on database servers. Redis is basically a database query cache and is known for its flexibility, performance, and broad language support. The name Redis comes from REmote DIctionary Server.

In this guide, you will learn how to install and configure Redis on an Ubuntu 16.04 server with PHP 7 and Nginx (with Apache it’s the same thing). You need access to the non-root machine with sudo privileges to perform the necessary administrative functions. When you’re ready to get started, log into your Ubuntu server with your sudo user.

Install Redis

Installing Redis is very simple and takes a maximum of 5 minutes. Update the apt-get package index files and also update existing packages to newer versions using the following commands:

Redis packages are available in the default apt repository. To install Redis on an Ubuntu VPS run the command from the terminal:

If you need to use Redis with PHP, you also need to install the Redis PHP extension on your Ubuntu system. Run this command:

Now Redis can communicate with PHP, your web server has all the necessary elements to make the loading of dynamic pages faster. The next step is to enable Redis on system startup. Restart the Redis service when done.

Configure Redis

Redis can be started without a configuration file using a built-in default configuration. But to make further changes to the parameters you can use its configuration file which you find in:/

Edit the Redis configuration file in a text editor to make changes.

Update the following values ​​in the Redis configuration file according to your needs. You can increase the maximum memory limit as available on your server.

The maxmemory-policy allkeys-lru configuration tells Redis to remove any keys that use the LRU algorithm when the maximum memory of 256mb is reached. Save the configuration file and restart the Redis service:

Check the connection to the Redis server

Use the redis-cli command to access the CLI and verify the connection to the Redis server with the ping command .

Here are some other useful redis-cli commands. For more information, consult the official documentation .

Monitor Redis

MONITOR is a debug command that shows the flow of each command processed by the Redis server. It can help you understand what is happening to the database. This command can be used both via redis-cli and via telnet. The ability to see all requests processed by the server is useful in detecting bugs in an application both when using Redis as a database and as a distributed cache system.

The redis-cli command enables the CLI (Command Line Interface), using the scan 0 command you can see the keys saved in the Redis database.

Clear the cache

Delete all keys from all Redis databases:

Delete all keys of the currently selected Redis database:

Delete all keys from the specified Redis database:

Redis + WordPress

If you use WordPress you need to add the following line at the beginning of the wp-config.php configuration file, right after opening <? Php.

Finally, at the first line of the Salt Keys section of the file, add this command:

As the value of WP_CACHE_KEY_SALT (yoursite.com in the example above) enter what you want. It must be a unique key for each WordPress installation on that server. This way you are sure that Redis handles it correctly. I recommend that you use a different key for each domain.

Are you lazy? If you don’t want to use the shell to clear the cache you can use a WordPress plugin, such as Redis Object Cache . The plugin provides info on the connection to the server and a button to clear the Redis cache. With this simple plugin you can also start a diagnostic function to detect any problems.

Manage Redis from WordPress

Control dashboard for Redis

On GitHub there are a couple of dashboards to upload to the server that allow you to manage and monitor Redis, the best known are phpRedisAdmin and phpredmin .

Remove Redis

To remove Redis the first thing to do is to stop the service and remove the automatic start:

Now you can remove the two installed packages:

If there is a Redis user, you can delete it:

Restart the server to finish uninstalling Redis:

Nginx FastCGI-Cache vs Redis Object Cache

The main difference between these two systems is in what is cached. Using Redis with WordPress will cache common database query results and speed up the creation of web pages. The FastCGI cache, on the other hand, caches the entire page after it has been generated. Neither is necessarily better than the other as it is more a matter of what works best for your particular use. Either way, you need to use only one or the other, not both.

The FastCGI cache is useful when you are in a single server environment and do not need to share the cache with other servers in the cluster. It may be slightly faster than Redis, depending on your configuration, but it is only beneficial to the server that is serving the requests and is not accessible beyond that server.

On the other hand, Redis can be configured to work with NGINX, configured as an NGINX module and used by NGINX, and is accessible from more than a single server. Therefore, if you move to a cluster and need to share cached data across multiple servers, connecting to Redis from the servers in the cluster solves the problem.

In addition, Redis can be installed on a completely separate server (i.e. a cache-only server) which allows you to separate the cache server from your web server. You can also extend Redis to a cluster pool and fragment the data across multiple servers and still be able to access them from the primary without having to connect to each individually.

Bottom line, if you only have one server, for simplicity, I would use FastCGI Cache unless you really need Redis. If you plan to set up a server cluster, Redis is a very good option.

Results and performance

The web server that runs this blog uses both OPAQUE and Redis. The results are satisfactory. The average page download time reported by Google Search Console is less than 0.3 seconds, but often drops below 0.2 seconds.

I tempi indicati da Google Analytics per documento caricato sono in media 2 secondi, sia da desktop (colonna sinistra) che da mobile (colonna a destra).

Screaming Frog riesce a scansionare circa 1.000 pagine del sito in 12 secondi.

Niente male.

RELATED ARTICLES

Most Popular

Recent Comments