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

查看:34
本文介绍了在 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/

在 make 之前,请查看您的服务器 Linux 是 32 位还是 64 位.安装脚本不能很好地处理 32 位环境,至少在 Webfaction 的 CentOS 5 机器上.位的命令是uname -m.如果 Linux 是 32 位,结果将是 i686,如果是 64 位,则结果是 x86_64.请参阅此答案了解详情.

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 行

Change the line 214 from this

$(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 可能已在使用中.其次,即使端口是空闲的,是的,您可以启动服务器,但它会在您退出 shell 的同时停止运行.对于第一个必须编辑 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 的地方.通常 pidfile 存储在/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
  • 端口 6379 -> 端口 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.

为此可以使用更多委托方法,例如 forever.另请参阅此网络派系社区主题,了解有关该主题的更多信息.

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天全站免登陆