linux安装php与memcache过程记录
安装环境是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时会出现问题:
注意,安装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
/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
这样我去查之前配置安装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
因此,解决办法就是去下载libXpm库安装:
sudo apt-get install libXpm-dev
但是配置时会提示找不到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
操作完成之后,就可以重新安装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
sudo /usr/loca/php5.5/sbin/php-fpm
在nginx的配置文件中添加对php的支持即可使用php开发了。
2、安装memcache及错误处理
memcache分为服务器端和php扩展,服务器端使用memcached表示,完成所有数据的存储删除等各种管理,php扩展是php使用memcache的一个扩展库,用memcache表示,用于调用和访问系统的memcache服务,直接在php中使用new Memcache即可。
(1)首先需要安装服务器,这个直接使用ubuntu的默认安装即可:
sudo apt-get install memcached
安装完Memcache服务端以后,使用如下命令启动服务:
memcached -d -m 128 -p 11111 -u root
memcached服务的启动参数说明:
-p
-l
-d 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u
-m
-M 内存耗尽时返回错误,而不是删除项
-c
-C 禁止使用CAS
-P
-f
-t
-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
wget http://pecl.php.net/get/memcache-2.2.6.tgz
但是我遇到的问题是,不论使用上述哪个方法,最终的结果都是要使用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");^
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
按照上面步骤安装的时候,奇迹出现了,果然可以编译安装完成,没有任何问题。此处得出的结论是,作为软件使用者和开发者我们一定要适合保持软件的更新,尽量使用最新的稳定的版本,这样可以避免遇到很多奇怪的问题。
安装时把解压包放到/usr/local/m4下,然后直接configure不指定prefix才可以安装autoconf,否则也会同样的找不到M4的错误。
安装autoconf如下:
export PATH=/usr/local/m4:$PATHcd autoconf./configure --prefix=/usr/local/autoconfmakemake install
然后接着去安装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
做了这些符号链接之后,我重新进入memcache源码文件使用phpize工具就没问题了,成功生成了configure文件:
./configure \ >--with-php-config=/usr/local/php5.5/bin/php-config \ >--enable-memcache=/usr/bin/memcachedmakemake install
配置php.ini文件,在/usr/local/php5.5/lib/php.ini,找到extension_dir这一行:
;extension_dir = "./" 改为extension_dir = "安装memcache完成后的目录路径"extension = "memcache.so" ;添加此行用于添加memcache扩展
使用如下的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"));?>
结果如图:
通过上述的种种艰辛,不仅是完成了安装的任务,也让我进一步熟悉了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对这个依赖项需要调用时的一个寻找路径。晚饭时间到了,折腾了一天,看到一切都可以使用的时候还是感觉蛮不错的!希望有前辈可以解答我前面提到的一些疑惑!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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,

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

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