Thursday, June 25, 2020

Redis installation

Uninstall Redis from ubuntu

A-a. if you use apt-get:


If you use apt-get to install redis then use sudo apt-get purge --auto-remove redis-server

Here --auto-remove option removes the Redis server package and its dependencies.

And the purge option removes the configuration and other Redis related data files.

This successfully uninstalls the Redis from the Ubuntu server.

After uninstalling the package, in the Ubuntu system check if the package is present or not. For this, we use the command, sudo apt-cache policy redis-server


this will show redis-server: Installed: (none)

A-b. If you use make file  - Compiled redis manually:


# Stop a all instance.
sudo service redis_version stop 
sudo service redis_6379
or systemctl stop redis
# Remove the files in sudo rm /usr/local/bin/redis-* # Remove Redis Configuration.
sudo rm -r /etc/redis/
sudo rm -r /var/redis/
rm /etc/redis/redis.conf
# Remove  existing Redis log files.
sudo rm /var/log/redis_*
# Remove  existing Redis data directory.
sudo rm -r /var/lib/redis/ # Remove existing Redis server init scripts
sudo rm /etc/init.d/redis_* # Remove existing Redis PID files (Only if exists) sudo rm /var/run/redis_*​ # Restart your Ubuntu server must. sudo reboot now


Install these two preinstall packages first:
sudo apt update -y
apt-cache policy redis-server
sudo apt install build-essential tcl pkg-config -y
mkdir redis
cd redis # download redis
wget http://download.redis.io/releases/redis-6.0.9.tar.gz
tar xzf redis-6.0.5.tar.gz
cd redis-6.0.9

# Proceed with the make command
make
cd src && make test
Install the program system wide - install the binaries onto the system - Make a global Alias: 
cd src && sudo make install



Start on booting:
sudo systemctl enable redis-server.service
Shutdown:
redis-cli shutdown
Start the server:
redis-server
Clint terminal:
redis-cli
set name foo
get name




But for production environments, you should use a configuration file. In order to start Redis with a configuration file, use the full path of the configuration file as first argument, like in the following example: redis-server /etc/redis.conf

Installing Redis more properly


  • Create a directory in which to store your Redis config files and your data:

    sudo mkdir /etc/redis
    sudo mkdir /var/redis
    
  • Copy the init script that you'll find in the Redis distribution under the utils directory into /etc/init.d. We suggest calling it with the name of the port where you are running this instance of Redis. For example:

    sudo cp /home/mg/redis/redis-6.0.5/utils/redis_init_script /etc/init.d/redis_6379
    
    sudo cp /home/mg/redis/redis-6.0.5/utils/redis_init_script /etc/init.d/redis_6379
    
  • Edit the init script.

    sudo vi /etc/init.d/redis_6379 

Make sure to modify REDISPORT accordingly to the port you are using. Both the pid file path and the configuration file name depend on the port number.


  • Copy the template configuration file you'll find in the root directory of the Redis distribution into /etc/redis/ using the port number as name, for instance:

    sudo cp redis.conf /etc/redis/6379.conf
    
  • Create a directory inside /var/redis that will work as data and working directory for this Redis instance:

    sudo mkdir /var/redis/6379
    
  • Edit the configuration file, making sure to perform the following changes:

    • Set daemonize to yes (by default it is set to no).
    • Set the pidfile to /var/run/redis_6379.pid (modify the port if needed).
    • Change the port accordingly. In our example it is not needed as the default port is already 6379.
    • Set your preferred loglevel.
    • Set the logfile to /var/log/redis_6379.log
    • Set the dir to /var/redis/6379 (very important step!)

  • Finally, add the new Redis init script to all the default runlevels using the following command:

    sudo update-rc.d redis_6379 defaults
    

You are done! Now you can try running your instance with:

sudo /etc/init.d/redis_6379 start

sudo /etc/init.d/redis_6379 stop


sudo systemctl restart redis_6379.service
sudo systemctl status redis_6379.service

sudo systemctl disable redis
(Not sure) sudo systemctl enable redis_6379.service

Make sure that everything is working as expected:

  • Try pinging your instance with redis-cli.
  • Do a test save with redis-cli save and check that the dump file is correctly stored into /var/redis/6379/ (you should find a file called dump.rdb).
  • Check that your Redis instance is correctly logging in the log file cat /var/log/redis_6379.log.
  • If it's a new machine where you can try it without problems make sure that after a reboot everything is still working.

Note: In the above instructions we skipped many Redis configuration parameters that you would like to change, for instance in order to use AOF persistence instead of RDB persistence, or to setup replication, and so forth. Make sure to read the example redis.conf file (that is heavily commented) and the other documentation you can find in this web site for more information.



Configure Redis for Magento

Configure Redis



# Redis default caching
bin/magento setup:config:set \
--cache-backend=redis \
--cache-backend-redis-server=127.0.0.1 \
--cache-backend-redis-port=6379 \
--cache-backend-redis-db=0

# Redis page caching
bin/magento setup:config:set \
--page-cache=redis \
--page-cache-redis-server=127.0.0.1 \
--page-cache-redis-port=6379 \
--page-cache-redis-db=1


# Redis for session storage
bin/magento setup:config:set --session-save=redis \
--session-save-redis-host=127.0.0.1 \
--session-save-redis-port=6379 \
--session-save-redis-log-level=4 \
--session-save-redis-db=2



No comments:

Post a Comment