Table of Contents
1、php安装及错误排查
2、安装memcache及错误处理
Home Backend Development PHP Tutorial linux安装php与memcache过程记录

linux安装php与memcache过程记录

Jun 23, 2016 pm 01:51 PM
memcache php Install Record process

安装环境是Ubuntu13.10版本i386,已经安装好nginx服务器和mysql服务器,现在的任务是需要安装php和memcache。直接干货,多余的不说。

1、php安装及错误排查

由于php安装前需要安装很多库文件,因此需要耐心得按照一定的顺序来安装。我按照如下顺序安装:

  zlib -> freetype -> libxml2 -> libpng -> libgd2 -> libmcrypt

其中的也有依赖关系的是,安装gd2需要用到zlib、freetype、libpng(libjpeg)等,因此需要先安装这些之后再安装gd2。这里的安装按照通用目录配置到/usr/local/xxx目录下,一般不会出现问题,可能在安装freetype时会出现问题:

Copy after login
注意,安装freetype时有可能提示如下错误: either set the LIBPNG_CFLAGS and LIBPNG_LDFLAGS environment variables 这是提示找不到libpng的安装位置,所以需要将libpng的bin目录配置到环境变量中:解决方法:编辑:vi /etc/profileLIB_PNG=/usr/local/libpng/binPATH=$LIB_PNG:$PATH  export PATH 生效:.  /etc/profile
Copy after login
这里我想记录一下的是我安装GD2的时候没有报错,安装成功了。但是后来安装php的时候,在配置中配置好GD2的安装目录后,出现错误,而且configure是没问题,是在安装的时候出错的,要知道安装php的时候configure本来就要很久,然后没错,make的时候也要很久,这样等了很久很久之后突然说编译出错,真的是有点崩溃。错误如下:

/usr/local/src/php-5.5.6/ext/gd/gd.c:57:22: error: X11/xpm.h: No such file or directorymake: *** [ext/gd/gd.lo] Error 1
Copy after login
说明:这是提示没有安装libXpm库,是在安装gd2库的时候没有配置开启。

这样我去查之前配置安装gd2的时候提示,确实发现对libXpm库不支持,如下:

** Configuration summary for libgd 2.1.0:   Support for Zlib:                 yes   Support for PNG library:          yes   Support for JPEG library:         no   Support for VPX library:          no   Support for TIFF library:         no   Support for Freetype 2.x library: yes   Support for Fontconfig library:   no   Support for Xpm library:          no   Support for pthreads:             yes
Copy after login

因此,解决办法就是去下载libXpm库安装:

sudo apt-get install libXpm-dev
Copy after login
这样安装后本以为可以了,就去重新安装gd2库,在配置gd2的时候多加一条 --with-xpm=/usr/lib。

但是配置时会提示找不到xpm库,这里我上网找到了解决办法,在/usr/lib下面建立软连接文件到真实的xpm库文件,如下:

ln -s /usr/lib/ (x86_64 or i386 ) /libXpm.a  /usr/lib/libXpm.a ln -s /usr/lib/ (x86_64 or i386 ) /libXpm.so  /usr/lib/libXpm.so ln -s /usr/lib/ (x86_64 or i386 ) /libXpm.so.4  /usr/lib/libXpm.so.4 ln -s /usr/lib/ (x86_64 or i386 ) /libXpm.so.4.11  /usr/lib/libXpm.so.4.11
Copy after login

操作完成之后,就可以重新安装gd2库了,配置完成成功:


会发现 "Support for Xpm library : yes"这一项,说明配置成功,然后make && make install即可完成安装gd2库。

接着安装所有库文件完成后,可以配置php安装了,由于之前,配置成功,编译出错,因此这次配置的时候需要加入 --with-xpm-dir=/usr/lib/这一项才能编译成功。完整配置如下:

 ./configure \    --prefix=/usr/local/php5.5 \      --with-libxml-dir=/usr/local/libxml2/ \    --with-png-dir=/usr/local/libpng/ \    --with-freetype-dir=/usr/local/freetype/ \    --with-gd=/usr/local/gd2/ \    --with-zlib-dir=/usr/local/zlib/ \    --with-mcrypt=/usr/local/libmcrypt/ \    --with-xpm-dir=/usr/lib/ \    --with-mysql \    --with-mysqli \    --enable-pdo \    --with-pdo-mysql \     --with-iconv \   --enable-soap \    --enable-mbstring=all \    --enable-sockets \  --enable-fpm
Copy after login
这样configure和make、make install之后,就可以完成php的安装。然后使用

sudo /usr/loca/php5.5/sbin/php-fpm
Copy after login
开启php-fpm服务,可以将这条命令加入到/etc/rc.local文件中作为自启动服务。

在nginx的配置文件中添加对php的支持即可使用php开发了。

2、安装memcache及错误处理

memcache分为服务器端和php扩展,服务器端使用memcached表示,完成所有数据的存储删除等各种管理,php扩展是php使用memcache的一个扩展库,用memcache表示,用于调用和访问系统的memcache服务,直接在php中使用new Memcache即可。

(1)首先需要安装服务器,这个直接使用ubuntu的默认安装即可:

sudo apt-get install memcached
Copy after login

安装完Memcache服务端以后,使用如下命令启动服务:

memcached -d -m 128 -p 11111 -u root
Copy after login

memcached服务的启动参数说明:
-p TCP监听的端口,默认是11211
-l 连接的IP地址, 默认是本机
-d 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u  运行身份 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M  内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-C 禁止使用CAS
-P 设置PID保存的文件名,仅仅和 -d 选项一起调用
-f   块大小增长因子,默认是1.25-n 最小分配空间,key+value+flags默认是48
-t 使用的线程数目,默认是4
-b  设置备份日志队列的大小,默认是1024
-R  设置每个事件的最大请求数目,默认是20
-h 显示帮助

可以从下图查看memcached服务器是否开启:


可以看到11211端口以处于LISTEN状态,说明memcached服务器已经开启。

(2)安装memcache客户端

这个安装其实就是在php已经安装完成的基础上,给php安装新的扩展,就类似为一个没有配置安装socket的php在不卸载php的前提下重新安装一个socket的扩展的原理类似。

首先可以使用php自带的pecl安装程序:

/usr/local/php5.5/bin/pecl install memcache
Copy after login
其次也可以用如下命令下载源码包后解压安装:

wget http://pecl.php.net/get/memcache-2.2.6.tgz
Copy after login

但是我遇到的问题是,不论使用上述哪个方法,最终的结果都是要使用phpize工具为memcache生成配置文件,而这个需要autoconf这个库,从之前安装php的配置中可以看出,我并有安装autoconf库,因此我使用上述两种方法安装都会报同样的错:


同样,当我使用源码包安装的时候,进入到memcache源码包后,需要使用/usr/local/php5.5/bin/phpize工具来生成configure文件,会与上图报同样的错误。从此处也可以说明,上述两种安装方式都是殊途同归,原理都一样。

为此,我需要去安装autoconf库,但是在安装这个库的时候,同样会报错:


这里可以明显看出,就是M4版本太低,需要更新。通过查阅资料知晓,autoconf使用M4宏处理器处理configure.in文件,从而可以生成configure文件用于安装配置。

然后,就是去下载M4源码进行安装了,这里又有一个坑,我安装提示的去下载推荐的 M41.4.16版本的源码包,配置成功,但是编译的时候会出现类似下面的问题:

./stdio.h:477:1: error: 'gets' undeclared here (not in a function)_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");^
Copy after login
这个问题我在网上查找,发现不仅仅是安装M4会出现,很多人安装其他的库的时候也会出现类似问题,也找到有人更改 _GL_WARN_ON_USE进行hack,也有人说进行打补丁(这个问题的具体原因可能比较复杂,也没弄清)。但是我觉得我的ubuntu是13.10是比较新的版本,应该会有修复吧!于是我就重新下载了一个M4的最新版本:

wget  http://ftp.gnu.org/gnu/m4/m4-latest.tar.gztar -zxf m4-latest.tar.gz;mv m4-1.4.17  /usr/local/m4cd /usr/local/m4./configuremakemake insatll
Copy after login

按照上面步骤安装的时候,奇迹出现了,果然可以编译安装完成,没有任何问题。此处得出的结论是,作为软件使用者和开发者我们一定要适合保持软件的更新,尽量使用最新的稳定的版本,这样可以避免遇到很多奇怪的问题。

安装时把解压包放到/usr/local/m4下,然后直接configure不指定prefix才可以安装autoconf,否则也会同样的找不到M4的错误。

安装autoconf如下:

export  PATH=/usr/local/m4:$PATHcd autoconf./configure --prefix=/usr/local/autoconfmakemake install
Copy after login
按照上述命令安装就完成了autoconf的安装。

然后接着去安装memcache,此时,使用export将/usr/local/autoconf/bin导入到PATH并不起作用,因此我尝试使用了如下的命令:

sudo ln -s /usr/local/autoconf/bin/autoconf autoconfsudo ln -s /usr/local/autoconf/bin/autoheader autoheadersudo ln -s /usr/local/autoconf/bin/autom4te autom4tesudo ln -s /usr/local/autoconf/bin/autoreconf autoreconfsudo ln -s /usr/local/autoconf/bin/autoscan autoscansudo ln -s /usr/local/autoconf/bin/autoupdate autoupdatesudo ln -s /usr/local/autoconf/bin/ifnames ifnames
Copy after login

做了这些符号链接之后,我重新进入memcache源码文件使用phpize工具就没问题了,成功生成了configure文件:


安装过程如下:

./configure  \ >--with-php-config=/usr/local/php5.5/bin/php-config \ >--enable-memcache=/usr/bin/memcachedmakemake install
Copy after login
安装完成如下图所示,最后出现的installed  shared extensions是在php的配置文件中进行配置memcache时候需要使用到的,需要保存下来。



配置php.ini文件,在/usr/local/php5.5/lib/php.ini,找到extension_dir这一行:

;extension_dir = "./" 改为extension_dir = "安装memcache完成后的目录路径"extension = "memcache.so" ;添加此行用于添加memcache扩展
Copy after login

使用如下的php代码进行测试:

<?php //Test memcache	$mem = new Memcache;	$mem->connect('127.0.0.1', 11211);		echo '<pre class="brush:php;toolbar:false">';	var_dump($mem);	var_dump(class_exists("Memcache"));?>
Copy after login

结果如图:




通过上述的种种艰辛,不仅是完成了安装的任务,也让我进一步熟悉了linux下安装软件的各种值得注意的地方。安装gd2库的时候需要依赖Xpm库,这个问题其实是在安装好了gd2后安装php的时候出现的,然后又重新安装gd2,特别是安装Xpm后,使用了符号链接之后,就可以安装带有Xpm的gd2库了。这个方法是一个启示!在后面安装autoconf完成后,使用PATH变量失效时是当时突然想起符号链接这个方法,因此就自己尝试性使用并解决了问题。还有就是安装M4的时候,版本问题可能牵涉出很多底层的问题,这个我也没弄清楚,当时也是尝试性的使用最新版本安装就可以解决了。最后安装memcache的时候就比较顺利了。同时也发现网上有很多安装php的时候就使用配置选项--with-autoconf选项直接一步安装了autoconf,这样就可以直接使用phpize工具了,但是我这里没有安装,虽然让我费了周折,但是最后单独安装了autoconf之后,使用符号链接指定安装的autoconf路径,然后phpize就可以从PATH中的/usr/bin这个路径下找到,因此这也说明了安装php是指定配置选项应该就是指定了php对这个依赖项需要调用时的一个寻找路径。晚饭时间到了,折腾了一天,看到一切都可以使用的时候还是感觉蛮不错的!希望有前辈可以解答我前面提到的一些疑惑!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles