【问题标题】:Setting up Redis on Webfaction在 Webfaction 上设置 Redis
【发布时间】:2013-09-08 11:30:21
【问题描述】:

Webfaction 共享主机帐户上设置Redis 数据库需要哪些步骤?

【问题讨论】:

    标签: installation redis webfaction


    【解决方案1】:

    简介

    由于 Webfaction 服务器的特殊环境限制,安装说明并不像他们想象的那么简单。尽管如此,最终您将拥有一个功能齐全的 Redis 服务器,即使在重新启动后也能保持正常运行。大约半年前,我通过以下程序亲自安装了 Redis,从那以后它一直运行完美。不过稍微提醒一下,半年的时间并不长,尤其是服务器还没有被大量使用。

    说明由五个部分组成:安装、测试、启动服务器、管理服务器和保持服务器运行。

    安装

    登录到您的 Webfaction shell

    ssh foouser@foouser.webfactional.com
    

    Redis download site下载最新的Redis。

    > 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 位环境,at least on Webfaction's CentOS 5 machines。位命令是uname -m。如果 Linux 是 32 位,则结果将是 i686,如果是 64 位,则结果将是 x86_64。有关详细信息,请参阅此answer

    > uname -m
    i686
    

    如果您的服务器是 64 位 (x86_64),那么只需简单地制作即可。

    > make
    

    但是如果您的服务器是 32 位 (i686),那么您必须做一些额外的事情。有一个命令make 32bit,但它会产生错误。编辑安装脚本中的一行以使make 32bit 工作。

    > 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。

    > 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

    测试(可选)

    正如安装的输出所暗示的,最好通过运行测试来确保一切都按预期工作。

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

    不幸的是,测试需要安装 tlc8.6.0,至少在机器 web223 上这不是默认设置。因此,您必须首先从源代码安装它。请参阅Tcl/Tk installation notescompiling 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 的环境限制也会失败。我建议你跳过这个。

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

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

    测试结束后,您就可以继续了。

    启动服务器

    首先,通过Webfaction Control Panel(自定义应用程序(监听端口))创建一个自定义应用程序。例如,将其命名为 fooredis。请注意,如果 Redis 仅在本地(即来自同一主机)使用,则您不必为应用创建域或网站。

    其次,记下为应用程序提供的套接字端口号。让示例为 23015

    将之前编译的可执行文件复制到应用程序的目录。您可以选择复制全部或仅复制您需要的部分。

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

    同时复制示例配置文件。你很快就会修改它。

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

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

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

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

    > 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

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

    > 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)
    >
    

    如果你想停止服务器。

    > 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 时,请记住使用制表符而不是空格。

    > 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 不会创建一个新进程,如果已经有一个正在运行。

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

    执行规则:

    > make start
    

    保持服务器运行

    您现在有一个以守护程序模式运行的 Redis 实例,它允许您在不停止 shell 的情况下退出 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 在服务器机器重新启动后立即启动,并且在第一个规则生效之前很久。

    可以使用一些更精致的方法,例如forever。有关该主题的更多信息,另请参阅Webfaction Community thread

    结论

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

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

    有什么想法、cmets 或更正吗?

    【讨论】:

    • 为什么不用redis socket连接代替tcp?更好的性能和更容易的设置。剩下的,太棒了!
    • 这在 2018 年 1 月仍然有效,但您需要更新最新版本的 redis。我的服务器也已经安装了 Tcl。
    【解决方案2】:

    总结 Akseli 的出色回答:

    assume your user is named "magic_r_user"
    
    cd ~
    wget "http://download.redis.io/releases/redis-3.0.0.tar.gz"
    tar -xzf redis-3.0.0.tar.gz
    mv redis-3.0.0 redis
    cd redis
    make
    make test
    create a custom app "listening on port" through the Webfaction management website
        assume we named it magic_r_app
        assume it was assigned port 18932
    cp ~/redis/redis.conf ~/webapps/magic_r_app/
    vi ~/webapps/magic_r_app/redis.conf
        daemonize yes   
        pidfile ~/webapps/magic_r_app/redis.pid
        port 18932
    test it
        ~/redis/src/redis-server ~/webapps/magic_r_app/redis.conf
        ~/redis/src/redis-cli -p 18932
        ctrl-d
        cat ~/webapps/magic_r_app/redis.pid | xargs kill
    crontab -e
        */1 * * * * /home/magic_r_user/redis/src/redis-server /home/magic_r_user/webapps/magic_r_app/redis.conf &>> /home/magic_r_user/logs/user/cron.log
    don't forget to set a password!
    

    【讨论】:

      【解决方案3】:

      仅供参考,如果您正在安装 redis 2.8.8+,您可能会收到错误,编译时未定义对 __sync_add_and_fetch_4 的引用。有关信息,请参阅http://www.eschrade.com/page/undefined-reference-to-__sync_add_and_fetch_4/

      我已经粘贴了下面该页面的相关部分,以防该页面离线。本质上,您需要导出 CFLAGS 变量并重新启动构建过程。

      [root@devvm1 redis-2.6.7]# export CFLAGS=-march=i686
      [root@devvm1 redis-2.6.7]# make distclean
      [root@devvm1 redis-2.6.7]# make
      

      【讨论】:

        猜你喜欢
        • 2013-09-12
        • 1970-01-01
        • 1970-01-01
        • 2012-03-06
        • 1970-01-01
        • 2018-10-08
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        相关资源
        最近更新 更多