一聚教程网:一个值得你收藏的教程网站

热门教程

CentOS 6.6下编译安装Tengine 2+PHP 5.5.+MariaDB 5.5步骤

时间:2022-06-30 21:17:25 编辑:袖梨 来源:一聚教程网


前言
本文主要大致介绍CentOS 6.6 下编译安装Tengine、PHP、MariaDB。面向有Linux基础且爱好钻研的朋友。技艺不精,疏漏再所难免,还望指正。
测试环境简介:
系统: CentOS 6.6 X86_64,最小化安装
IP: 192.168.1.10
Tengine: 2.1.0(基于Nginx1.6.2)
MariaDB: 5.5.41
PHP: 5.5.22
1、准备工作
1.1、系统硬件准备
尽管Linux能最大化发挥硬件资源,但RHEL/CentOS随着版本增加对最低硬件的配置也越来越高[1]。RHEL7/CentOS最低要求内存最小 1GB,建议每个逻辑 CPU 1 GB。但如果系统为CentOS7,硬件内存小于1G,比如512M,而且没有配置交换分区,建议先配置SWAP,以防内存不足导致数据库启动失败或进程异常退出。配置添加SWAP方法参考《Linux下添加swap分区》。
1.2、系统软件准备
如果是最小化安装,比如VPS、云主机一般就是最小化安装。需要配置系统至方便易用。
安装EPEL第三方软件库并更新系统至最新:
1
yum -y install epel* && yum -y update
安装必要实用基础软件:
1
yum -y install bash-completion vim wget git screen net-tools bind-utils nmap tree htop iftop nload jwhois
安装开发工具:
1
[root@topseek ~]# yum -y install autoconf automake make kernel kernel-headers gcc gcc-c++ gtk+-devel libstdc++-devel bzip2 bzip2* keyutils keyutils-libs-devel krb5-devel cpp curl curl-devel fontconfig fontconfig-devel
安装依赖软件:
1
[root@topseek ~]# yum install -y apr* bison cloog-ppl compat* gd gd-devel gettext gettext-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libxml2 libxml2-devel libXpm* libtiff libtiff* mpfr ncurses* php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* zlib-devel freetype freetype* freetype-devel openssl openssl-devel patch pcre-devel perl glibc libselinux-devel libtool* libgomp
1.3、所需软件及各软件简介
安装需要用到的软件有nginx、mysql、php、pcre、openssl、zlib、cmake、mcrypt、yasm、t1lib、libgd、libvpx、tiff、libpng、libjpeg、freetype。这些软件的简介如下:
(1)Tengine
官方介绍:“Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。”
Nginx
Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:新浪、网易、腾讯等。
官方下载地址:http://nginx.org/en/download.html
(2)PHP
PHP,这里略去十万字…
官方下载网址:http://cn2.php.net/downloads.php
最新版下载地址:http://cn2.php.net/distributions/php-5.5.22.tar.bz2
(3)MariaDB
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。在存储引擎方面,使用XtraDB(英语:XtraDB)来代替MySQL的InnoDB。现在MariaDB官方主推最新版MariaDB 10,其对应MySQL 5.6,但它在文件结构、一些功能上已经和MySQL 5.6大为不同,查看官方博文《MariaDB vs. MySQL》、《MariaDB 10.0 and MySQL 5.6》。如果用于生产环境,建议使用稳定版5.5。
官方下载地址:https://downloads.mariadb.org/;MariaDB 5.5;下载页面:https://mariadb.com/kb/en/mariadb/what-is-mariadb-55/
(4)PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。PCRE被广泛使用在许多开源软件之中,最著名的莫过于Apache HTTP服务器和PHP脚本语言。
官方网址:http://www.pcre.org/
(5)OpenSSL
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。该程序出现安全漏洞时比较危险,建议从官方网站下载最新版。
官方下载网址:http://www.openssl.org/source/
(6)zlib
zlib是提供数据压缩用的函式库,使用DEFLATE算法.zlib是一种事实上的业界标准,以至于在标准文档中,zlib和DEFLATE常常互换使用。数以千计的应用程序直接或间接依靠zlib压缩函式库,包括: Linux核心:使用zlib以实作网络协定的压缩、档案系统的压缩以及开机时解压缩自身的核心;libpng,用于PNG图形格式的一个实现,对bitmap数据规定了DEFLATE作为流压缩方法。Apache:使用zlib实作http 1.1;OpenSSH、OpenSSL:以zlib达到最佳化加密网络传输。
官方下载网址:http://zlib.net/
(7)CMake
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。CMake 可以编译源代码、制作程式库、产生适配器(wrapper)、还可以用任意的顺序建构执行档。CMake 支持 in-place 建构(二进档和源代码在同一个目录树中)和 out-of-place 建构(二进档在别的目录里),因此可以很容易从同一个源代码目录树中建构出多个二进档。CMake 也支持静态与动态程式库的建构。这里用来编译MySQL或MariaDB
官方网址:http://www.cmake.org/
(8)mcrypt
mcrypt 是 php 里面重要的加密支持扩展库,Mcrypt库支持20多种加密算法和8种加密模式,具体可以通过函数mcrypt_list_algorithms()和mcrypt_list_modes()来显示。
下载地址:http://mcrypt.sourceforge.net/
(9)yasm
In computing, Yasm is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. Yasm is a full rewrite of Netwide Assembler (NASM). Yasm can generally be used interchangeably with NASM and supports the x86 and x86-64 architectures. It is licensed under a revision of the BSD licenses. As of 2011 it was developed by Peter Johnson and Michael Urman.
下载地址:http://yasm.tortall.net/Download.html
(10)libgd
GD 是一个开源的图像处理库,支持 PNG/JPEG/GIF以及其他格式的图像。GD一般被用来创建图表、图形、缩略图以及其他图像相关的处理操作。
下载地址:http://libgd.bitbucket.org/
(11)libpng
libpng 软件包包含 libpng 库.这些库被其他程式用于读写png文件. PNG 库是用来创立和操作PNG 格式的图像文件.PNG 格式是设计来替代GIF,他对于更小范围的TIFF(标记图象文件格式)来说, 有了非常多的进步和拓展并且减少了关于专利权的麻烦.
下载地址:http://www.libpng.org/pub/png/libpng.html
(12)libvpx
libvpx 是一个开源的VP8编码解码器。VP8:高质量的视频编码。
(13)FreeType
FreeType库是一个完全免费(开源)的、高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件,包括TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF等。FreeType 2被设计为一种占用空间小的、高效的、高度可定制的、并且可以产生可移植的高品质输出(符号图像)。可以被用在诸如图像库、展出服务器、字体转换工具、图像文字产生工具等多种其它产品上。
官方网址:http://www.freetype.org/
下载页面网址:http://www.freetype.org/download.html
(14)LibJPEG
LibJPEG 是一个广泛使用的 JPEG 图像压缩库,采用 C 语言开发。
官方网址:http://www.ijg.org/
1.4、下载所需软件
下载前设置指定DNS,防止下载某些软件中提示域名无法解析而导致下载失败:


[root@topseek ~]# vim /etc/resolv.conf
nameserver 8.8.8.8
nameserver 114.114.114.114
各软件一键下载:


cd /usr/local/src/ &&
wget -ct 5 http://tengine.taobao.org/download/tengine-2.1.0.tar.gz &&
wget -ct 5 http://wiki.nginx.org/images/5/51/Nginx-accesskey-2.0.3.tar.gz &&
wget -ct 5 http://mirrors.ustc.edu.cn/mariadb/mariadb-5.5.41/source/mariadb-5.5.41.tar.gz &&
wget -ct 5 http://cn2.php.net/distributions/php-5.5.22.tar.bz2 &&
wget -ct 5 http://www.openssl.org/source/openssl-1.0.2.tar.gz &&
wget -ct 5 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz &&
wget -ct 5 http://zlib.net/zlib-1.2.8.tar.gz &&
wget -ct 5 http://www.cmake.org/files/v3.1/cmake-3.1.2.tar.gz &&
wget -ct 5 http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz &&
wget -ct 5 http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz &&
wget -ct 5 ftp://sunsite.unc.edu/pub/Linux/libs/graphics/t1lib-5.1.2.tar.gz &&
wget -ct 5 https://bitbucket.org/libgd/gd-libgd/downloads/libgd-2.1.1.tar.bz2 &&
wget -ct 5 http://anduin.linuxfromscratch.org/sources/other/libvpx-v1.3.0.tar.xz &&
wget -ct 5 http://download.osgeo.org/libtiff/tiff-4.0.3.tar.gz &&
wget -ct 5 http://jaist.dl.sourceforge.net/project/libpng/libpng16/1.6.16/libpng-1.6.16.tar.gz &&
wget -ct 5 http://download.savannah.gnu.org/releases/freetype/freetype-2.5.3.tar.bz2 &&
wget -ct 5 http://www.ijg.org/files/jpegsrc.v9a.tar.gz &&
git clone https://github.com/aperezdc/ngx-fancyindex.git ngx-fancyindex &&
echo "下载完毕!"
注意:使用-c断点续传,-t参数表示重试次数。
1.5、配置防火墙
运行80、443、3306端口通过:
可以使用文字图形界面配置:


[root@localhost ~]# setup
或者修改/etc/sysconfig/iptables,如下:

 

修改后记得重启服务:


[root@localhost ~]# service iptables restart
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:清除防火墙规则:                                 [确定]
iptables:正在卸载模块:                                   [确定]
iptables:应用防火墙规则: 
                               [确定]
2、安装MariaDB

安装CMake:


cd /usr/local/src &&
tar -zxvf cmake-3.1.2.tar.gz &&
cd cmake-3.1.2 &&
./configure &&
make && make install

添加mysql运行组及用户、创建数据库数据文件文件夹并授予合适权限:
Trip:/bin/false是最严格的禁止login选项,一切服务都不能用,而/sbin/nologin只是不允许系统login,可以使用其他ftp等服务。如果想要用false在禁止login的同时允许ftp,则必须在/etc/shells里增加一行/bin/false。为了系统安全,这里建议使用/bin/false。


groupadd mysql && useradd -g mysql mysql -s /bin/false &&
mkdir -p /data/mysql &&
chown -R mysql:mysql /data/mysql

编译、安装MariaDB:


mkdir -p /opt/mysql &&
cd /usr/local/src &&
tar -zxvf mariadb-5.5.41.tar.gz &&
cd mariadb-5.5.41 &&
cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc &&
make && make install

由于系统自带了MySQL配置文件,备份或删除它都可以:


mv /etc/my.cnf /etc/my.cnf.bak

初始化数据库,配置启动脚本:


cd /opt/mysql &&
./scripts/mysql_install_db --user=mysql --basedir=/opt/mysql --datadir=/data/mysql &&
ln -s /opt/mysql/my.cnf /etc/my.cnf &&
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld &&
chmod 755 /etc/init.d/mysqld &&
chkconfig mysqld on

配置MariaDB启动脚本:


#vim /etc/rc.d/init.d/mysqld
改动如下:
basedir=/opt/mysql #MySQL程序安装路径
datadir=/data/mysql #MySQl数据库存放目录
##重启MySQL
#service mysqld start

把MariaDB加入系统环境变量:


echo 'export PATH=$PATH:/opt/mysql/bin'>>/etc/profile && source /etc/profile

把MariaDB的库文件链接到系统默认的位置,在编译PHP等软件时可以不用指定其库文件地址:


ln -s /opt/mysql/lib/mysql /usr/lib/mysql &&
ln -s /opt/mysql/include/mysql /usr/include/mysql &&
mkdir /var/lib/mysql &&
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

交互式设置数据库:


[root@localhost mysql]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none):
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
 
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
 
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n] Y
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n] Y
 ... Success!
 
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] Y
 ... Success!
 
Cleaning up...
 
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
 
Thanks for using MariaDB!

3、安装Tengine

创建Tengine(nginx)的用户和用户组:


groupadd www && useradd -g www www -s /bin/false

安装PCRE:


cd /usr/local/src &&
mkdir /usr/local/pcre &&
tar -zxvf pcre-8.36.tar.gz &&
cd pcre-8.36 &&
./configure --prefix=/usr/local/pcre &&
make && make install

安装OpenSSL:


cd /usr/local/src &&
mkdir /usr/local/openssl &&
tar -zxvf openssl-1.0.2.tar.gz &&
cd openssl-1.0.2 &&
./config --prefix=/usr/local/openssl &&
make && make install &&
echo 'export PATH=$PATH:/usr/local/openssl/bin'>>/etc/profile &&
source /etc/profile

安装zlib:


cd /usr/local/src &&
mkdir /usr/local/zlib &&
tar zxvf zlib-1.2.8.tar.gz &&
cd zlib-1.2.8 &&
./configure --prefix=/usr/local/zlib &&
make && make install

安装第三方模块ngx_http_accesskey_module以实现Nginx防盗链


cd /usr/local/src &&
tar -vxzf Nginx-accesskey-2.0.3.tar.gz &&
cd nginx-accesskey-2.0.3/

修改config文件,替换其中 的”$HTTP_ACCESSKEY_MODULE”为”ngx_http_accesskey_module”.
我们可以在官方文档查看更多信息(http://nginx.org/en/docs/)。下面安装Tengine:


cd /usr/local/src &&
tar -zxvf tengine-2.1.0.tar.gz &&
cd tengine-2.1.0 &&
./configure
--prefix=/opt/webserver/tengine
--conf-path=/opt/etc/tengine/conf/tengine.conf
--pid-path=/data/logs/tengine/tengine.pid
--error-log-path=/data/logs/tengine/error.log
--without-http_memcached_module
--user=www
--group=www
--with-poll_module
--with-syslog
--with-http_image_filter_module
--with-http_stub_status_module
--with-http_ssl_module
--with-http_gzip_static_module
--with-openssl=/usr/local/src/openssl-1.0.2
--with-zlib=/usr/local/src/zlib-1.2.8
--with-pcre=/usr/local/src/pcre-8.36
--with-http_stub_status_module
--with-http_sub_module
--add-module=/usr/local/src/nginx-accesskey-2.0.3
--add-module=/usr/local/src/ngx-fancyindex/
--with-http_realip_module
--with-http_sysguard_module &&
make && make install
补充:–with-http_sub_module用于替换,非常有用(见Wiki)。–with-http_stub_status_module开启Nginx状态信息,非核心模块,可不加入。ngx-fancyindex是一个能做出类似apache漂亮索引的模块,可以修改起源代码下的template.h模板文件做出更漂亮的索引页面。–with-http_sysguard_module是一个过载保护模块;开启syslog日志功能需要在编译是添加参数–with-syslog,否则syslog不会生效。
到此,软件编译并安装完毕。
Tengine官方文档:http://tengine.taobao.org/documentation_cn.html
更多官方文档:nginx documentation
配置控制脚本或者从这里下载,其实这个服务脚本跟Nginx是一样的:

 

从本网站下载:

cd /etc/rc.d/init.d/ && wget https://idoseek.com/dl/nx/tengine
或手动写入:
vim /etc/rc.d/init.d/tengine

写入如下内容:


#!/bin/sh
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: tengine
# config: /opt/webserver/tengine/conf/tengine.conf
# pidfile: /opt/webserver/tengine/logs/tengine.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/opt/webserver/tengine/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/opt/etc/tengine/conf/tengine.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
 # make required directories
 user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`
 if [ -z "`grep $user /etc/passwd`" ]; then
 useradd -M -s /bin/nologin $user
 fi
 options=`$nginx -V 2>&1 | grep 'configure arguments:'`
 for opt in $options; do
 if [ `echo $opt | grep '.*-temp-path'` ]; then
 value=`echo $opt | cut -d "=" -f 2`
 if [ ! -d "$value" ]; then
 # echo "creating" $value
 mkdir -p $value && chown -R $user $value
 fi
 fi
 done
}
 
start() {
 [ -x $nginx ] || exit 5
 [ -f $NGINX_CONF_FILE ] || exit 6
 make_dirs
 echo -n $"Starting $prog: "
 daemon $nginx -c $NGINX_CONF_FILE
 retval=$?
 echo
 [ $retval -eq 0 ] && touch $lockfile
 return $retval
}
 
stop() {
 echo -n $"Stopping $prog: "
 killproc $prog -QUIT
 retval=$?
 echo
 [ $retval -eq 0 ] && rm -f $lockfile
 return $retval
}
 
restart() {
 #configtest || return $?
 stop
 sleep 1
 start
}
 
reload() {
 #configtest || return $?
 echo -n $"Reloading $prog: "
 killproc $nginx -HUP
 RETVAL=$?
 echo
}
 
force_reload() {
 restart
}
 
configtest() {
 $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
 status $prog
}
 
rh_status_q() {
 rh_status >/dev/null 2>&1
}
 
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
 
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

设置启动脚本、启动Nginx:


chmod 775 /etc/rc.d/init.d/tengine &&
chkconfig tengine on &&
/etc/rc.d/init.d/tengine start

把Nginx加入系统变量:


echo 'export PATH=$PATH:/opt/webserver/tengine/sbin'>>/etc/profile && source /etc/profile

测试,输出如下内容说明安装Tengine已经成功:


[root@localhost ~]# curl -I http://localhost
HTTP/1.1 200 OK
Server: Tengine/2.1.0
Date: Sat, 07 Feb 2015 13:53:43 GMT
Content-Type: text/html
Content-Length: 555
Last-Modified: Sat, 07 Feb 2015 13:32:26 GMT
Connection: keep-alive
ETag: "54d613ea-22b"
Accept-Ranges: bytes
附注:Tengine命令行参数
‘-m’ 选项
显示所有编译的模块,Tengine支持动态模块,static表示静态编译,shared表示动态编译(后面接的是动态模块的版本)。
‘-d’ 选项
输出配置文件的内容,包括’include’的内容。
‘-l’ 选项

显示所有支持的指令。

4、安装PHP

4.1、编译相关软件安装PHP

安装yasm:


cd /usr/local/src &&
tar -zxvf yasm-1.2.0.tar.gz &&
cd yasm-1.2.0 &&
./configure &&
make &&
make install

安装mcrypt:


cd /usr/local/src &&
tar -zxvf libmcrypt-2.5.8.tar.gz &&
cd libmcrypt-2.5.8 &&
./configure &&
make &&
make install

安装libvpx:


cd /usr/local/src &&
xz -d libvpx-v1.3.0.tar.xz &&
tar xvf libvpx-v1.3.0.tar &&
cd libvpx-v1.3.0 &&
./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 &&
make &&
make install

安装tiff:


cd /usr/local/src &&
tar -zxvf tiff-4.0.3.tar.gz &&
cd tiff-4.0.3 &&
./configure --prefix=/usr/local/tiff --enable-shared &&
make &&
make install

安装libpng:


cd /usr/local/src &&
tar -zxvf libpng-1.6.16.tar.gz &&
cd libpng-1.6.16 &&
./configure --prefix=/usr/local/libpng --enable-shared --with-x=no &&
make &&
make install

安装freetype:


cd /usr/local/src &&
tar -jxvf freetype-2.5.3.tar.bz2 &&
cd freetype-2.5.3 &&
./configure --prefix=/usr/local/freetype --enable-shared --with-x=no &&
make &&
make install

安装jpeg:


cd /usr/local/src &&
tar -zxvf jpegsrc.v9a.tar.gz &&
cd jpeg-9a &&
./configure --prefix=/usr/local/jpeg --enable-shared &&
make &&
make install

安装libgd:


cd /usr/local/src &&
tar -jxvf libgd-2.1.1.tar.bz2 &&
cd libgd-2.1.1 &&
./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx &&
make && make install

安装t1lib(FUUUU):


cd /usr/local/src &&
tar -zxvf t1lib-5.1.2.tar.gz &&
cd t1lib-5.1.2 &&
./configure --prefix=/usr/local/t1lib --enable-shared --with-x=no&&
make without_doc &&
make install

安装php:

注意:如果系统是64位,安装前请执行以下两条命令:


ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so &&
cp -frp /usr/lib64/libXpm.so* /usr/lib/
编译并安装PHP:


cd /usr/local/src &&
tar -jvxf php-5.5.22.tar.bz2 &&
cd php-5.5.22 &&
export LD_LIBRARY_PATH=/usr/local/libgd/lib &&
 ./configure
--prefix=/opt/php
--with-fpm-user=www
--with-fpm-group=www
--sysconfdir=/opt/etc/php/5.5
--with-config-file-path=/opt/etc/php/5.5
--with-config-file-scan-dir=/opt/etc/php/5.5/conf.d
--with-mysql=/opt/mysql
--with-mysqli=/opt/mysql/bin/mysql_config
--with-mysql-sock=/tmp/mysql.sock
--with-pdo-mysql=/opt/mysql
--with-gd
--with-png-dir=/usr/local/libpng
--with-jpeg-dir=/usr/local/jpeg
--with-freetype-dir=/usr/local/freetype
--with-xpm-dir=/usr/
--with-vpx-dir=/usr/local/libvpx/
--with-zlib-dir=/usr/local/zlib
--with-t1lib=/usr/local/t1lib
--with-openssl
--with-iconv
--with-bz2
--with-xmlrpc
--with-mcrypt
--with-curl
--with-gettext
--without-pear
--enable-fpm
--enable-opcache
--enable-libxml
--enable-xml
--enable-bcmath
--enable-shmop
--enable-sysvsem
--enable-inline-optimization
--enable-mbregex
--enable-mbstring
--enable-ftp
--enable-gd-native-ttf
--enable-pcntl
--enable-sockets
--enable-zip
--enable-soap
--enable-session
--enable-exif
--enable-ctype
--enable-calendar &&
make && make install
编译配置说明:
–enable-opcache:PHP5.5内置了Zend Opcache,即Optimizer+,是 Zend 开发的闭源但可以免费使用的 PHP 优化加速组件。现在,Zend 科技公司将 Optimizer+ 在 PHP License 下开源成为 Zend Opcache。Zend OPcache 通过 opcode 缓存和优化提供更快的 PHP 执行过程。它将预编译的脚本文件存储在共享内存中供以后使用,从而避免了从磁盘读取代码并进行编译的时间消耗。同时,它还应用了一些代码优化模式,使得代码执行更快。

更多编译信息请查看


./configure --help

编译、安装完成后结尾输出如下一段比较重要的信息:


Build complete.
Don't forget to run 'make test'.
 
Installing shared extensions:     /opt/php/lib/php/extensions/no-debug-non-zts-20121212/
Installing PHP CLI binary:        /opt/php/bin/
Installing PHP CLI man page:      /opt/php/php/man/man1/
Installing PHP FPM binary:        /opt/php/sbin/
Installing PHP FPM config:        /opt/etc/php/5.5/
Installing PHP FPM man page:      /opt/php/php/man/man8/
Installing PHP FPM status page:      /opt/php/php/php/fpm/
Installing PHP CGI binary:        /opt/php/bin/
Installing PHP CGI man page:      /opt/php/php/man/man1/
Installing build environment:     /opt/php/lib/php/build/
Installing header files:          /opt/php/include/php/
Installing helper programs:       /opt/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /opt/php/php/man/man1/
  page: phpize.1
  page: php-config.1
/usr/local/src/php-5.5.21/build/shtool install -c ext/phar/phar.phar /opt/php/bin
ln -s -f /opt/php/bin/phar.phar /opt/php/bin/phar
Installing PDO headers:          /opt/php/include/php/ext/pdo/
安装结束。

4.2、配置PHP运行环境

复制php配置文件到安装目录:


cp php.ini-production /opt/etc/php/5.5/php.ini

备份系统自带配置文件:


mv /etc/php.ini /etc/php.ini.bak

添加软链接到 /etc目录:


ln -s /opt/etc/php/5.5/php.ini /etc/php.ini

拷贝模板文件为php-fpm配置文件:


cp /opt/etc/php/5.5/php-fpm.conf.default /opt/etc/php/5.5/php-fpm.conf

添加软连接到 /etc目录:


ln -s /opt/etc/php/5.5/php-fpm.conf /etc/php-fpm.conf

以上几步写法合并如下:


cp php.ini-production /opt/etc/php/5.5/php.ini &&
mv /etc/php.ini /etc/php.ini.bak &&
ln -s /opt/etc/php/5.5/php.ini /etc/php.ini &&
cp /opt/etc/php/5.5/php-fpm.conf.default  /opt/etc/php/5.5/php-fpm.conf &&
ln -s /opt/etc/php/5.5/php-fpm.conf /etc/php-fpm.conf

修改PHP配置文件:


# vim /opt/etc/php/5.5/php.ini
;找到"disable_functions =" (禁用掉某些比较“危险”函数,大概在305行),改为
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
 
;找到";date.timezone ="(大概在913行),修改为
date.timezone = Asia/Shanghai
 
;找到"expose_php = On"(禁止显示php版本的信息,大概在366行),修改为
 
expose_php = Off
 
;找到"short_open_tag = Off"(支持php短标签,大概在202行),修改为
 
short_open_tag = On
 
;找到";opcache.enable=0"(支持opcode缓存,大概在1838行),修改为
 
opcache.enable=1
 
;找到";opcache.enable_cli=0"(支持opcode缓存,大概在1841行),修改为
 
opcache.enable_cli=0
;并在下面加入'zend_extension = "opcache.so"'一行, 开启opcode缓存功能
zend_extension = "opcache.so"
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

4.3、配置php-fpm


#vim /opt/etc/php/5.5/php-fpm.conf
大概在分别25、147行,修改内容如下:
 
;取消pid前面的分号
pid = run/php-fpm.pid
;...
;设置php-fpm运行账号为www
user = www
;设置php-fpm运行组为www
group = www

拷贝php-fpm脚本到启动目录,给予执行权限, 设置开机启动:


cp /usr/local/src/php-5.5.21/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm &&
chmod +x /etc/rc.d/init.d/php-fpm &&
chkconfig php-fpm on
把php加入系统变量:
1
echo 'export PATH=$PATH:/opt/php/bin'>>/etc/profile && source /etc/profile
 
4.4、配置Tengine支持php

修改/etc/tengine.conf 配置文件,需做如下修改


user www www;
 
index index.php index.html index.htm;
 
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 
#
#取消FastCGI server部分location的注释,注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

5、测试

上述配置修改完毕,重启tengine,并启动php-fpm:


service tengine restart &&
service php-fpm start

创建phpinfo文件,设置适当的目录权限。


cd /opt/webserver/tengine/html/  &&
echo ''>index.php &&
chown -R  www:www /opt/webserver/tengine/html/
浏览器查看,如果PHP文档配置没有错误,将输出如下内容:
QQ20150207-2

配置汇总
根目录: /opt/webserver/tengine/html/
Nginx配置文件: /opt/etc/tengine/conf/nginx.conf
PHP配置文件: /opt/etc/php/php.ini
PHP默认拓展文件夹:/usr/lib64/php/modules/
PHP拓展可放置于:/opt/php/lib/php/extensions/no-debug-non-zts-20121212
MySQL配置文件: /etc/my.cnf
MySQL程序安装路径: basedir=/opt/mysql
MySQl数据库存放目录: datadir=/data/mysql
 
问题汇总

(1)NOTICE: PHP message: PHP Warning:  Module ‘exif’ already loaded in Unknown on line 0
重复加载模块了,可以使用以下命令查询(参考):
 
#php -i | grep .ini$
Loaded Configuration File => /usr/local/etc/php.ini
Additional .ini files parsed => /usr/local/etc/php/extensions.ini
user_ini.filename => .user.ini => .user.ini
(2)[22-Oct-2014 09:07:25] NOTICE: PHP message: PHP Warning:  PHP Startup: Unable to load dynamic library ‘/usr/lib64/php/modules/imagick.so’ – /usr/lib64/php/modules/imagick.so: cannot open shared object file: No such file or directory in Unknown on line 0
很明显,没有正确加载拓展模块文件,请确认好php.ini的extensions_dir。
(3)PHP Warning:  PHP Startup: mhash: Unable to initialize module
PHP版本升级后模块没有重新编译,需要重新编译。
(4)版本升级时注意哪些问题?
建议把php-fpm服务关掉,否则容易出现使用旧模块,一些新模块无法替换。建议备份后删除旧模块重新编译。

热门栏目