📜 ⬆️ ⬇️

Installing Redis + Redis PHP + phpRedisAdmin on the production server in 15 minutes

Everyone has long known that the best binary files are those that were compiled on this particular computer. And, when the question comes to performance, then compiling components on this computer will give its advantage in speed and stability.

This article will discuss how to prepare Redis, phpredis (C module for php) and phpRedisAdmin to work on the combat server.

In order to collect everything fully, we need to have on the server:

')
After we make sure that all dependencies are satisfied, we begin to assemble all the components.
We will need:


We collect Redis


In order to collect Radishes, we will need the source itself. You can get them from github. therefore
https://github.com/antirez/redis/zipball/2.4.4 download the right tag immediately.

$ wget https://github.com/antirez/redis/zipball/2.4.4 $ unzip 2.4.4 $ cd antirez-redis-04bba69 


Now we have all the source code for Redis 2.4.4, it remains to compile them.
Everything is very simple
 $ make $ make test 


The first team we collected Redis, the second it was checked for performance.
Make test will take a long time (about 2 minutes). After the test, the following line should return "\ o / All tests passed without errors!"

Redis collected, it remains to clear the garbage and carry radishes in the right directory.
Let's take all the compiled Redis to the / usr / bin / redis folder. And in order to run the Redis-server, in / usr / bin we will create the shell file redis-server.

Bringing Redis files to / usr / bin / redis


We still stay in the antirez-redis-04bba69 folder, where the sources are
 $ sudo mkdir /usr/bin/redis $ sudo cp src/redis-benchmark /usr/bin/redis $ sudo cp src/redis-check-aof /usr/bin/redis $ sudo cp src/redis-check-dump /usr/bin/redis $ sudo cp src/redis-cli /usr/bin/redis $ sudo cp src/redis-server /usr/bin/redis $ cd .. $ rm -rf antirez-redis-04bba69 

We copied all the files from Redis and deleted the source folder, we no longer need it.
Be careful with the last command! sudo is not needed for it, a folder is already in our domain.

Now we need to create a file to run redis-server.

 $ sudo vim /usr/bin/redis-server 


Instead of vim, you can use any other editor. It does not matter.
Insert into the file:
cd / usr / bin / redis && ./redis-server redis.conf
Save, close.

For this file you need to give the right to execute.
 $ sudo chmod 755 /usr/bin/redis-server 


It remains to take redis.conf
 $ cd /usr/bin/redis/ $ sudo wget https://raw.github.com/antirez/redis/unstable/redis.conf $ sudo vim redis.conf 


Redis.conf is loaded, it remains to configure it.
Replace in configuration file

daemonize no on daemonize yes # Let it work as a daemon
timeout 0 at timeout 30 # In the case of bugs, a client that does not disconnect will hang in Redis memory forever
loglevel notice on loglevel warning # We have a combat server, a minimum of debug messages
Line 166 needs to be uncommented and set a password for example requirepass foobared
Line 350 comment, it is not needed.

Save, close.

Run!


If everything was done correctly, then we start the server.
 $ redis-server $ telnet localhost 6379 


In theory, you should show an invitation from Redis
Trying 127.0.1.1 ...
Connected to localhost.
Escape character is '^]'.

If you received it, then everything is fine, Redis works.

Testing server


Now we communicate with Redis on the socket.
Log in:
 AUTH mypassword 


Set the test value
 SET testkey testvalue KEYS * #    GET testkey #    testvalue QUIT 


If everything went well, it means that the server is working, and we are finishing with it. Set the startup command at system startup and that's it.

Redis + PHP5. We compile the module for PHP


Here, too, everything is very simple, so you can just follow the commands.
 $ cd ~ $ wget https://github.com/nicolasff/phpredis/zipball/2.1.3 $ unzip 2.1.3 $ cd nicolasff-phpredis-43bc590 

Then I think the comments are not needed.

Now we collect the redis.so extension.
 $ phpize $ ./configure CFLAGS="-O3" $ make clean all 

Now the redis.so file has appeared in the modules folder, we need it.

 $ sudo cp modules/redis.so /usr/lib/php5/<date> $ cd .. $ rm -rf nicolasff-phpredis-43bc590 

<date> change to a folder name that looks like this: 20090626.

Further give php information about Redis.so
so
 $ sudo vim /etc/php5/apache2/conf.d/redis.ini 

and enter extension = redis.so into it
Optionally, replace apache2 with cli, cgi, and so on, depending on how you have php installed and how you want to use it with Redis.

Now restart apache2 and in the test php file we write:
 $redis = new Redis(); $redis->connect('localhost:6379'); 

If the error that the Redis class does not exist, has not crashed, then everything is fine.

We will carry out benchmark test directly in php.
 try { $redis = new Redis(); $redis->connect('localhost:6379'); } catch(RedisException $e) { exit('Connect error'); } $benchmark = microtime(true); for($i=0;$i < 80000; $i++) $redis->set('key','value'); echo microtime(true) - $benchmark; 


I got the information that 80,000 requests were processed in 2.6 seconds.

We are done with php on this. Next phpRedisAdmin.

Install phpRedisAdmin



Installing phpRedisAdmin is absolutely not necessary, but it does not hurt to visualize data.

Downloading git admin panel itself.
 $ cd /var/www $ git clone git://github.com/ErikDubbelboer/phpRedisAdmin.git redisadmin $ cd redisadmin $ chmod 755 -R /var/www/redisadmin 


We create the redisadmin folder in / var / www. We add the rights to the files, because initially I have permission denied on redisadmin.

Do not forget to set up a web server so that you can open the site from the network.
But using redis admin is not safe on production, so you need to close it with a password for all prying eyes.

 $ vim config.inc.php 

You need to uncomment line 11 and set a password to the server.

Next you need to uncomment the block from 36 to 46 line, assigning the admin password, which will be requested at the entrance to phpRedisAdmin

PROFIT!
In 15 minutes we were able to raise the Redis server, configure it to work with PHP and raise phpRedisAdmin.

Source: https://habr.com/ru/post/134974/


All Articles