Table of Contents
一、源码包准备
Home Database Mysql Tutorial CentOS 5.4下的Memcache安装步骤(Linux+Nginx+PHP+Memcached)

CentOS 5.4下的Memcache安装步骤(Linux+Nginx+PHP+Memcached)

Jun 02, 2017 pm 06:05 PM
centos memcache

CentOS 5.4下的Memcache安装步骤分享,想要配置Linux+Nginx+PHP+Memcached运行环境的朋友可以参考下

一、源码包准备

服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-v1.4.4 。
下载:http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,

libevent的最新版本是libevent-1.4.13-stable。(如果你的系统已经安装了libevent,可以不用安装)
官网:http://www.monkey.org/~provos/libevent/
下载:http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz

准备Memcached的PHP扩展的源码安装包:
官网:http://pecl.php.net/get/memcache-2.2.5.tgz

Linux指令下载:

代码如下:

wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz 
wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz 
wget http://pecl.php.net/get/memcache-2.2.5.tgz
Copy after login


二、安装与配置
1、先安装libevent

代码如下:

tar zxvf libevent-1.4.13-stable.tar.gz 
cd libevent-1.4.13-stable 
./configure --prefix=/usr 
make 
make install
Copy after login


2、测试libevent是否安装成功

代码如下:

ls -al /usr/lib | grep libevent 
libevent-1.1a.so.1 
libevent-1.1a.so.1.0.2 
libevent-1.4.so.2 
libevent-1.4.so.2.1.3 
libevent.a 
libevent_core-1.4.so.2 
libevent_core-1.4.so.2.1.3 
libevent_core.a 
libevent_core.la 
libevent_core.so 
libevent_extra-1.4.so.2 
libevent_extra-1.4.so.2.1.3 
libevent_extra.a 
libevent_extra.la 
libevent_extra.so 
libevent.la 
libevent.so
Copy after login


版本不同,可能文件列表不同。

3、安装memcached,同时需要安装中指定libevent的安装位置

代码如下:

tar zxvf memcached-1.4.4.tar.gz 
cd memcached-1.4.4 
./configure –with-libevent=/usr 
make && make install
Copy after login


安装完成后会把memcached 自动放到 /usr/local/bin/memcached

4、测试是否成功安装memcached

代码如下:

ls -al /usr/local/bin/mem* 
-rwxr-xr-x 1 root root 201869 12-14 21:44 /usr/local/bin/memcached
Copy after login


5、安装Memcache的PHP扩展

①安装PHP的memcache扩展

代码如下:

tar vxzf memcache-2.2.5.tgz 
cd memcache-2.2.5 
/usr/local/webserver/php/bin/phpize 
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir 
make 
make install
Copy after login


②上述安装完后会有类似这样的提示:

Installing shared extensions: /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/

③把php.ini中的extension_dir = “./”修改为

代码如下:


extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/”


④添加一行来载入memcache扩展:extension=memcache.so

三、memcached的基本设置
1.启动Memcache的服务器端:

代码如下:


memcached -d -m 10 -u root -l 202.207.177.177 -p 11211 -c 256 -P /tmp/memcached.pid


参数说明:

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址202.207.177.177,
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,

2.如果要结束Memcache进程,执行:

代码如下:


kill `cat /tmp/memcached.pid`


也可以启动多个守护进程,不过端口不能重复。

3.检查Memcached是否启动

代码如下:


netstat -ant
tcp 0 0 202.207.177.177:11211 0.0.0.0:* LIST


11211端口已经打开,说明Memcached已正常启动。

4.重启CentOS

代码如下:


reboot


四、Memcache环境测试
运行下面的php文件,如果有输出This is a test!,就表示环境搭建成功。开始你的Memcache的征途吧!

代码如下:

<?php 
$mem = new Memcache; 
$mem->connect("202.207.177.177", 11211); 
$mem->set(&#39;key&#39;, &#39;This is a test!&#39;, 0, 60); 
$val = $mem->get(&#39;key&#39;); 
echo $val; 
?>
Copy after login


著名的PHPCMS同样支持Memcached扩展:

代码如下:

<?php 
//MemCache服务器配置 
//define(&#39;MEMCACHE_HOST&#39;, &#39;localhost&#39;); //MemCache服务器主机 
//define(&#39;MEMCACHE_PORT&#39;, 11211); //MemCache服务器端口 
//define(&#39;MEMCACHE_TIMEOUT&#39;, 1); //S,MemCache服务器连接超时 
class cache 
{ 
var $memcache; 
function __construct() 
{ 
$this->memcache = &new Memcache; 
$this->memcache->pconnect(MEMCACHE_HOST, MEMCACHE_PORT, MEMCACHE_TIMEOUT); 
} 
function cache() 
{ 
$this->__construct(); 
} 
function get($name) 
{ 
return $this->memcache->get($name); 
} 
function set($name, $value, $ttl = 0) 
{ 
return $this->memcache->set($name, $value, 0, $ttl); 
} 
function rm($name) 
{ 
return $this->memcache->delete($name); 
} 
function clear() 
{ 
return $this->memcache->flush(); 
} 
} 
?>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the backup methods for GitLab on CentOS What are the backup methods for GitLab on CentOS Apr 14, 2025 pm 05:33 PM

Backup and Recovery Policy of GitLab under CentOS System In order to ensure data security and recoverability, GitLab on CentOS provides a variety of backup methods. This article will introduce several common backup methods, configuration parameters and recovery processes in detail to help you establish a complete GitLab backup and recovery strategy. 1. Manual backup Use the gitlab-rakegitlab:backup:create command to execute manual backup. This command backs up key information such as GitLab repository, database, users, user groups, keys, and permissions. The default backup file is stored in the /var/opt/gitlab/backups directory. You can modify /etc/gitlab

Centos shutdown command line Centos shutdown command line Apr 14, 2025 pm 09:12 PM

The CentOS shutdown command is shutdown, and the syntax is shutdown [Options] Time [Information]. Options include: -h Stop the system immediately; -P Turn off the power after shutdown; -r restart; -t Waiting time. Times can be specified as immediate (now), minutes ( minutes), or a specific time (hh:mm). Added information can be displayed in system messages.

How to optimize CentOS HDFS configuration How to optimize CentOS HDFS configuration Apr 14, 2025 pm 07:15 PM

Improve HDFS performance on CentOS: A comprehensive optimization guide to optimize HDFS (Hadoop distributed file system) on CentOS requires comprehensive consideration of hardware, system configuration and network settings. This article provides a series of optimization strategies to help you improve HDFS performance. 1. Hardware upgrade and selection resource expansion: Increase the CPU, memory and storage capacity of the server as much as possible. High-performance hardware: adopts high-performance network cards and switches to improve network throughput. 2. System configuration fine-tuning kernel parameter adjustment: Modify /etc/sysctl.conf file to optimize kernel parameters such as TCP connection number, file handle number and memory management. For example, adjust TCP connection status and buffer size

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

What are the common misunderstandings in CentOS HDFS configuration? What are the common misunderstandings in CentOS HDFS configuration? Apr 14, 2025 pm 07:12 PM

Common problems and solutions for Hadoop Distributed File System (HDFS) configuration under CentOS When building a HadoopHDFS cluster on CentOS, some common misconfigurations may lead to performance degradation, data loss and even the cluster cannot start. This article summarizes these common problems and their solutions to help you avoid these pitfalls and ensure the stability and efficient operation of your HDFS cluster. Rack-aware configuration error: Problem: Rack-aware information is not configured correctly, resulting in uneven distribution of data block replicas and increasing network load. Solution: Double check the rack-aware configuration in the hdfs-site.xml file and use hdfsdfsadmin-printTopo

Centos configuration IP address Centos configuration IP address Apr 14, 2025 pm 09:06 PM

Steps to configure IP address in CentOS: View the current network configuration: ip addr Edit the network configuration file: sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 Change IP address: Edit IPADDR= Line changes the subnet mask and gateway (optional): Edit NETMASK= and GATEWAY= Lines Restart the network service: sudo systemctl restart network verification IP address: ip addr

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos stops maintenance 2024 Centos stops maintenance 2024 Apr 14, 2025 pm 08:39 PM

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

See all articles