search
HomeBackend DevelopmentPHP Tutoriallinux安装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
这里我想记录一下的是我安装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
说明:这是提示没有安装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

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

sudo apt-get install libXpm-dev
这样安装后本以为可以了,就去重新安装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

操作完成之后,就可以重新安装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
这样configure和make、make install之后,就可以完成php的安装。然后使用

sudo /usr/loca/php5.5/sbin/php-fpm
开启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

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

memcached -d -m 128 -p 11111 -u root

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
其次也可以用如下命令下载源码包后解压安装:

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");^
这个问题我在网上查找,发现不仅仅是安装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

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

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

安装autoconf如下:

export  PATH=/usr/local/m4:$PATHcd autoconf./configure --prefix=/usr/local/autoconfmakemake install
按照上述命令安装就完成了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

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


安装过程如下:

./configure  \ >--with-php-config=/usr/local/php5.5/bin/php-config \ >--enable-memcache=/usr/bin/memcachedmakemake install
安装完成如下图所示,最后出现的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扩展

使用如下的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对这个依赖项需要调用时的一个寻找路径。晚饭时间到了,折腾了一天,看到一切都可以使用的时候还是感觉蛮不错的!希望有前辈可以解答我前面提到的一些疑惑!

Statement
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
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool