如何将Memcached安装到运行CentOS的CPanel中

Recently I was trying to configure a VBulletin forum to use Memcached for caching. I was looking for some guidance for easy setup but didn’t find any useful and easy guide. I had installed memcached server on Mac, so I had some idea about it and when I got succeeded, I thought of writing this post to help others also.

最近,我试图将VBulletin论坛配置为使用Memcached进行缓存。 我一直在寻找一些易于设置的指南,但没有找到任何有用且简单的指南。 我已经在Mac上安装了memcached服务器 ,所以对此有了一些想法,当我获得成功时,我想写这篇文章来帮助其他人。

To install and use Memcached caching in any software, we need to do the following three things.

要在任何软件中安装和使用Memcached缓存,我们需要做以下三件事。

  1. Install libevent library that is used by memcached server

    安装Memcached服务器使用的libevent库
  2. Install memcached server and start it

    安装memcached服务器并启动它
  3. Install PHP extension for memcache

    为Memcache安装PHP扩展

Before installing memcached server, just create a directory at any location that you want to use. We will download all the binaries there and use it for our installation purpose.

在安装内存缓存服务器之前,只需在要使用的任何位置创建目录。 我们将在那里下载所有二进制文件,并将其用于安装目的。

在CentOS上安装libevent (Installing libevent on CentOS)

Run below commands to install libevent on CentOS. Use the latest libevent stable version.

运行以下命令以在CentOS上安装libevent。 使用最新的libevent稳定版本。

wget https://github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
tar xvfz libevent-2.0.20-stable.tar.gz
cd libevent-2.0.20-stable
./configure
make
make install

Above commands will install libevent on your CentOS server.

上面的命令将在您的CentOS服务器上安装libevent。

在CentOS上安装Memcached服务器 (Install Memcached server on CentOS)

Run below commands to download latest Memcached server library and install it. Check their website to get the latest version.

运行以下命令以下载最新的Memcached服务器库并进行安装。 检查他们的网站以获取最新版本。

wget https://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
tar xvfz memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure
make
make install

Above steps will install Memcached server, now you can start it will following command.

上面的步骤将安装Memcached服务器,现在您可以按照以下命令启动它。

memcached

疑难解答提示 (Troubleshoot Tips)

If you get following error “error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory” then you need to register libevent by following command.

如果出现以下错误“ 加载共享库时出错:libevent-1.4.so.2:无法打开共享库文件:没有这样的文件或目录 ”,那么您需要通过以下命令注册libevent。

export LD_LIBRARY_PATH= /usr/local/lib

You should keep above export command in your user profile (.bash_profile) so that you don’t need to export it always.

您应该在用户个人资料(.bash_profile)中保留上述export命令,这样就不必始终导出它。

You might get an error message as “[memcached] can’t run as root without the -u switch” if you are using root user. You can’t run Memcached using root user. Use below command to start Memcached server.

如果您使用root用户,则可能会收到一条错误消息,因为“如果没有-u开关,[memcached]不能以root身份运行 ”。 您不能使用root用户运行Memcached。 使用以下命令启动Memcached服务器。

Memcached安全提示 (Memcached Security Tip)

Make sure to use -l option while starting Memcached server, so that only specific IPs can connect to the Memcached instance. If you won’t use this option, anybody who knows the IP and port of memcached can connect it using telnet, which is a security threat.

确保在启动Memcached服务器时使用-l选项,以便只有特定的IP可以连接到Memcached实例。 如果您不使用此选项,那么任何知道memcached的IP和端口的人都可以使用telnet连接它,这是安全威胁。

memcached -d -u nobody -p 11211

Now try to connect to memcached server using telnet and run some commands from memcached telnet commands to make sure its working fine.

现在,尝试使用telnet连接到memcached服务器,并从memcached telnet命令运行一些命令,以确保其工作正常。

Once you are satisfied that memcached server is running fine, go to the next step to install PHP Memcache extension.

当您对memcached服务器运行良好感到满意之后,请转到下一步安装PHP Memcache扩展。

在CentOS上安装PHP Memcached Extension (Install PHP Memcached Extension on CentOS)

Run below commands to install PHP Memcached extension and configure it. Make sure to use the latest stable version.

运行以下命令以安装PHP Memcached扩展并进行配置。 确保使用最新的稳定版本。

wget https://pecl.php.net/get/memcache-2.2.7.tgz
tar xvfz memcache-2.2.7.tgz
cd memcache-2.2.7
phpize
./configure
make
make install

Above commands will install Memcached extension but to use it with PHP, we need to configure it in php.ini file and restart apache web server.

上面的命令将安装Memcached扩展,但是要与PHP一起使用,我们需要在php.ini文件中对其进行配置,然后重新启动apache Web服务器。

vi /usr/local/lib/php.ini

Add below line to the php.ini file.

将以下行添加到php.ini文件。

extension=memcache.so

Check for extension_dir variable, if it’s not set in php.ini (most probably if you are installing any extension for the first time) then add following lines to provide directory location also. Make sure you change directory location as in your server.

检查extension_dir变量(如果未在php.ini中设置)(很可能是首次安装任何扩展程序),然后添加以下几行以提供目录位置。 确保像在服务器中一样更改目录位置。

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613"
extension=memcache.so

Now your PHP configuration is done to load Memcached extension also, run below command to restart apache web server.

现在您PHP配置已完成,还可以加载Memcached扩展,请运行以下命令以重新启动apache Web服务器。

service httpd restart

Run below command to confirm that the Memcache module is loaded.

运行以下命令以确认已加载Memcache模块。

php -m

测试PHP Memcache扩展 (Test PHP Memcache Extension)

To test whether everything is fine or not, just create a PHP file with following content.

要测试一切是否正常,只需创建一个包含以下内容PHP文件。

<html>
<head>
  <title>PHP Memcache Extension Test</title>
</head>
<body>
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
echo "Server's version: {$memcache->getVersion()}";
$tmp = new stdClass;
$tmp->string_attribute = 'JournalDev';
$tmp->string_attribute = 123;
$memcache->set('key', $tmp, false, 10) or die ("Failed to save temporary object at memcache server");
echo "Data from the cache:\n";
print_r($memcache->get('key'));
?> 
</body>
</html>

Now access this PHP file from browser and if you get output like this:

现在,从浏览器访问此PHP文件,如果得到如下输出:

Server's version: 1.4.15Data from the cache: stdClass Object ( [string_attribute] => 123 )

It means that everything is fine. Memcached server is running fine and PHP Memcache module is able to connect and save/retrieve data into caching server.

这意味着一切都很好。 Memcached服务器运行良好,PHP Memcache模块能够连接并保存/检索数据到缓存服务器。

翻译自: https://www.journaldev.com/565/how-to-install-memcached-into-cpanel-running-centos

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值