在Webfaction上设置Redis [英] Setting up Redis on Webfaction

查看:109
本文介绍了在Webfaction上设置Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Redis 数据库需要执行哪些步骤? .com/?affiliate = xeli"rel =" noreferrer> Webfaction 共享托管帐户?

What are the steps required to set up Redis database on Webfaction shared hosting account?

推荐答案

简介

由于Webfaction服务器的特殊环境限制,安装说明并不像它们那样简单.尽管如此,最后您将拥有一个功能齐全的Redis服务器,即使重新启动后它也可以保持运行状态.大约半年前,我通过以下过程亲自安装了Redis,此后一直运行良好.提醒一下,半年不是很长的时间,尤其是因为服务器没有被大量使用.

Introduction

Because of the special environment restrictions of Webfaction servers the installation instructions are not as straightforward as they would be. Nevertheless at the end you will have a fully functioning Redis server that stays up even after a reboot. I personally installed Redis by the following procedure about a half a year ago and it has been running flawlessy since. A little word of a warning though, half a year is not a long time, especially because the server have not been under a heavy use.

说明包括五个部分:安装,测试,启动服务器,管理服务器和保持服务器运行.

The instructions consists of five parts: Installation, Testing, Starting the Server, Managing the Server and Keeping the Server Running.

登录到您的Webfaction Shell

Login to your Webfaction shell

ssh foouser@foouser.webfactional.com

Redis下载站点下载最新的Redis.

Download latest Redis from Redis download site.

> mkdir -p ~/src/
> cd ~/src/
> wget http://download.redis.io/releases/redis-2.6.16.tar.gz
> tar -xzf redis-2.6.16.tar.gz
> cd redis-2.6.16/

在制作之前,请先查看您的服务器Linux 32或64位.安装脚本不能很好地处理32位环境,至少在Webfaction的CentOS 5计算机上.位命令为uname -m.如果Linux是32位,则结果为i686,如果是64位,则结果为x86_64.有关详细信息,请参见此 answer .

Before the make, see is your server Linux 32 or 64 bit. The installation script does not handle 32 bit environments well, at least on Webfaction's CentOS 5 machines. The command for bits is uname -m. If Linux is 32 bit the result will be i686, if 64 bit then x86_64. See this answer for details.

> uname -m
i686

如果您的服务器是64位(x86_64),则只需制造.

If your server is 64 bit (x86_64) then just simply make.

> make

但是,如果您的服务器是32位(i686),则您必须做一点额外的工作.有一个命令make 32bit,但它会产生一个错误.在安装脚本中编辑一行以使make 32bit正常工作.

But if your server is 32 bit (i686) then you must do little extra stuff. There is a command make 32bit but it produces an error. Edit a line in the installation script to make make 32bit to work.

> nano ~/src/redis-2.6.16/src/Makefile

从此处更改第214行

$(MAKE) CFLAGS="-m32" LDFLAGS="-m32"

对此

$(MAKE) CFLAGS="-m32 -march=i686" LDFLAGS="-m32 -march=i686"

并保存.然后运行带有32位标志的make.

and save. Then run the make with 32bit flag.

> cd ~/src/redis-2.6.16/  ## Note the dir, no trailing src/
> make 32bit

可执行文件已创建到目录~/src/redis-2.6.16/src/中.可执行文件包括redis-cliredis-serverredis-benchmarkredis-sentinel.

The executables were created into directory ~/src/redis-2.6.16/src/. The executables include redis-cli, redis-server, redis-benchmark and redis-sentinel.

如安装输出所示,最好通过运行测试来确保一切正常.

As the output of the installation suggests, it would be nice to ensure that everything works as expected by running tests.

Hint: To run 'make test' is a good idea ;)

不幸的是,该测试要求安装tlc8.6.0,至少在机器web223上,这不是默认设置.因此,您必须首先从源代码安装它.请参见 Tcl/Tk安装说明

Unfortunately the testing requires tlc8.6.0 to be installed which is not the default at least on the machine web223. So you must install it first, from source. See Tcl/Tk installation notes and compiling notes.

> cd ~/src/
> wget http://prdownloads.sourceforge.net/tcl/tcl8.6.0-src.tar.gz
> tar -xzf tcl8.6.0-src.tar.gz
> cd tcl8.6.0-src/unix/
> ./configure --prefix=$HOME
> make
> make test # Optional, see notes below
> make install

使用make test测试Tcl将花费时间,并且由于WebFaction的环境限制,也将失败.我建议你跳过这个.

Testing Tcl with make test will take time and will also fail due to WebFaction's environment restrictions. I suggest you skip this.

现在,我们已经安装了Tlc,我们可以运行Redis测试.这些测试将花费很长时间,并且会暂时使用大量的内存.

Now that we have Tlc installed we can run Redis tests. The tests will take a long time and also temporarily uses a quite large amount of memory.

> cd ~/src/redis-2.6.16/
> make test

测试之后,您就可以继续.

After the tests you are ready to continue.

首先,通过 Webfaction控制面板(自定义应用程序(在端口上监听))创建自定义应用程序.将其命名为 fooredis .请注意,如果Redis仅在本地使用(即从同一主机使用),则无需为该应用程序创建域或网站.

First, create a custom application via Webfaction Control Panel (Custom app (listening on port)). Name it for example fooredis. Note that you do not have to create a domain or website for the app if Redis is used only locally i.e. from the same host.

第二,记下为该应用程序指定的套接字端口号.假设示例为 23015 .

Second, make a note about the socket port number that was given for the app. Let the example be 23015.

将先前编译的可执行文件复制到应用程序的目录中.您可以选择复制全部或仅复制所需的内容.

Copy the previously compiled executables to the app's directory. You may choose to copy all or only the ones you need.

> cd ~/webapps/fooredis/
> cp ~/src/redis-2.6.16/src/redis-server .
> cp ~/src/redis-2.6.16/src/redis-cli .

还复制样本配置文件.您将很快对其进行修改.

Copy also the sample configuration file. You will soon modify that.

> cp ~/src/redis-2.6.16/redis.conf .

现在Redis已经可以运行了.虽然有几个问题.首先,默认的Redis端口6379可能已在使用中.其次,即使端口是空闲的,是的,您也可以启动服务器,但是在退出外壳程序的同时它将停止运行.对于第一个,必须编辑redis.conf;对于第二个,您需要一个守护程序,该守护程序也可以通过编辑redis.conf来解决.

Now Redis is already runnable. There is couple problems though. First the default Redis port 6379 might be already in use. Second, even if the port was free, yes, you could start the server but it stops running at the same moment you exit the shell. For the first the redis.conf must be edited and for the second, you need a daemon which is also solved by editing redis.conf.

Redis能够在守护程序模式下运行自己.为此,您需要设置一个守护程序存储其进程ID,PID的位置.通常,pidfiles存储在/var/run/中,但是由于环境限制,您必须在主目录中为其选择一个位置.因为稍后在管理服务器"部分中说明了一个原因,所以一个不错的选择是将pidfile与可执行文件放在同一目录下.您不必自己创建文件,Redis会自动为您创建文件.

Redis is able to run itself in the daemon mode. For that you need to set up a place where the daemon stores its process ids, PIDs. Usually pidfiles are stored in /var/run/ but because the environment restrictions you must select a place for them in your home directory. Because a reason explained later in the part Managing the Server, a good choice is to put the pidfile under the same directory as the executables. You do not have to create the file yourself, Redis creates it for you automatically.

现在打开redis.conf进行编辑.

Now open the redis.conf for editing.

> cd ~/webapps/fooredis/
> nano redis.conf

以以下方式更改配置.

  • daemonize no-> daemonize yes
  • pidfile /var/run/redis.pid-> pidfile /home/foouser/webapps/fooredis/redis.pid
  • port 6379-> port 23015
  • daemonize no -> daemonize yes
  • pidfile /var/run/redis.pid -> pidfile /home/foouser/webapps/fooredis/redis.pid
  • port 6379 -> port 23015

现在,最后,启动Redis服务器.指定conf文件,以便Redis侦听正确的端口并作为守护程序运行.

Now finally, start Redis server. Specify the conf-file so Redis listens the right port and runs as a daemon.

> cd ~/webapps/fooredis/
> ./redis-server redis.conf
> 

看到它正在运行.

> cd ~/webapps/fooredis/
> ./redis-cli -p 23015
redis 127.0.0.1:23015> SET myfeeling Phew.
OK
redis 127.0.0.1:23015> GET myfeeling
"Phew."
redis 127.0.0.1:23015> (ctrl-d)
>

如果需要,请停止服务器.

Stop the server if you want to.

> ps -u $USER -o pid,command | grep redis
  718 grep redis
10735 ./redis-server redis.conf
> kill 10735

> cat redis.pid | xargs kill

管理服务器

为了易于使用并作为下一部分的准备工作,请编写一个有助于打开客户端以及启动,重新启动和停止服务器的脚本.一个简单的解决方案是编写一个makefile.编写makefile时,请记住使用制表符而不是空格.

Managing the Server

For the ease of use and as a preparatory work for the next part, make a script that helps to open the client and start, restart and stop the server. An easy solution is to write a makefile. When writing a makefile, remember to use tabs instead of spaces.

> cd ~/webapps/fooredis/
> nano Makefile

# Redis Makefile
client cli:
    ./redis-cli -p 23015

start restart:
    ./redis-server redis.conf

stop:
    cat redis.pid | xargs kill

这些规则是不言自明的.关于第二条规则的特殊之处在于,在守护程序模式下,如果已经有一个正在运行的进程,则调用./redis-server不会创建新进程.

The rules are quite self-explanatory. The special about the second rule is that while in daemon mode, calling the ./redis-server does not create a new process if there is a one running already.

第三条规则蕴含着一些静默的智慧.如果redis.pid没有存储在fooredis目录下,而是存储在/var/run/redis.pid下,那么停止服务器并不是那么容易.如果同时运行多个Redis实例,则尤其如此.

The third rule has some quiet wisdom in it. If redis.pid was not stored under the directory of fooredis but for example to /var/run/redis.pid then it would not be so easy to stop the server. This is especially true if you run multiple Redis instances concurrently.

要执行规则:

> make start

保持服务器运行

您现在拥有一个在守护程序模式下运行的Redis实例,该实例允许您退出Shell而无需停止它.这还不够.如果进程崩溃怎么办?如果服务器计算机重新启动怎么办?要涵盖这些内容,您必须创建两个 cronjobs .

> export EDITOR=nano
> crontab -e

添加以下两行并保存.

*/5 * * * * make -C ~/webapps/fooredis/ -f ~/webapps/fooredis/Makefile start
@reboot make -C ~/webapps/fooredis/ -f ~/webapps/fooredis/Makefile start

第一个确保每五分钟运行一次fooredis.如上所述,如果已经在运行,则不会启动新进程.第二个确保fooredis在服务器计算机重新启动后立即启动,并且在第一个规则生效之前很长时间启动.

The first one ensures each five minutes that fooredis is running. As said above this does not start new process if one is already running. The second one ensures that fooredis is started immediately after the server machine reboot and long before the first rule kicks in.

可以使用更多的专用方法,例如永远.另请参见 Webfaction社区主题,以获取有关该主题的更多信息.

Some more deligate methods for this could be used, for example forever. See also this Webfaction Community thread for more about the topic.

现在您拥有了.许多事情已经完成,但可能还会更多.您将来可能想做的未在此处做的事情包括以下内容.

Now you have it. Lots of things done but maybe more will come. Things you may like to do in the future which were not covered here includes the following.

  • 设置密码,防止其他用户刷新您的数据库. (请参阅redis.conf)
  • 限制内存使用量(请参阅redis.conf)
  • 记录用法和错误(请参阅redis.conf)
  • 偶尔备份一次数据.

有任何想法,评论或更正吗?

Any ideas, comments or corrections?

这篇关于在Webfaction上设置Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆