一、为什么要编译安装
为什么服务器软件需要编译安装?一个流传很广的说法是编译安装性能更好,其实这是个谣言,服务器CPU事实已经被Intel垄断了,就那么几种型号,编来编去生成的机器码是一样的。Intel宣传自己的编译工具Intel C++ Compiler 比GCC编译出来性能要提升10%-20%,这就是一广告,生产环境很少用人用它,何况它还要收费
性能真不是问题,比如 strip 命令可以大大减小可执行文件的size,但是我装过几千遍软件,都没有见有人在安装脚本里面使用。
软件需要编译安装的真实理由有如下3点:
-
软件在编译期间需要配置:比如说nginx,需要在编译的时候指定包含哪些module,php,apache 也是一样。同样的是数据库,mysql通过编译安装,因为要定制存储引擎(是否支持innodb .. ),而sqlite却绝少有人编译,都是直接下载二进制文件来用。
-
软件需要统一安装路径:每个team都会自己的安装目录约定,有些喜欢装在 /opt/下面,有些喜欢装在 /usr/local/ ,编译安装可以方便的指定这些路径(configure --prefix=xxx )
-
需要最新的版本:软件仓库的版本一般都比较低,这个理由其实不充分,生产环境倾向保守,不追求最新版本,但是对于某些软件来说,必须安装特定的版本,这时候就只能进行编译安装。
二、Apache环境准备及安装
我使用的搭建环境是Centos6.6 x86-64 mini版的系统;最小版安装有很多软件没有提供,我们可以通过yum软件库进行安装,到现在LAMP服务器的“L”(linux)我们已经准备好了,那么下一步我们是需要下载apache的httpd软件,下载地址是httpd.apache.org,现在apache维护的httpd有2.0,、2.2、2.4几个分支版本,每个版本的功能官网写的很清楚,我这里不再赘述,我所选择的是httpd的最新版本2.4.10,下面我们进行httpd的编译过程讲解;
因为httpd可以在各种操作系统,各种硬件架构上运行,是因为httpd运行在apr(Apache Portable Runtime)虚拟机上面,针对不同平台和操作系统开发apr,所有首先在我们按照httpd之前应该先安装apr,我们不使用软件包自带的apr软件,好像是版本有点低,httpd2.4.10需要的apr版本最低是1.4,我们去官网下载最新版本的apr和apr-util,下载地址是apr.apache.org;
1
2
3
4
5
6
|
wget http: //apache .fayea.com //httpd/httpd-2 .4.10. tar .bz2
wget http: //mirrors .cnnic.cn /apache/apr/apr-1 .5.1. tar .bz2
wget http: //mirrors .cnnic.cn /apache/apr/apr-util-1 .5.4. tar .bz2
tar -xf httpd-2.4.10. tar .bz2
tar -xf apr-1.5.1. tar .bz2
tar -xf apr-util-1.5.4. tar .bz2
|
安装开发库
进行软件的编译
1
2
3
4
5
6
7
8
9
|
cd apr-1.5.1
. /configure --prefix= /usr/local/apr
make
make install
cd apr-util-1.5.4
. /configure --prefix= /usr/local/apr-util --with-apr= /usr/local/apr
make
make install
|
因为httpd需要重写功能,所有需要安装pcre库
1
|
yum install -y pcre-devel
|
然而我在编译的时候出现checking for OpenSSL version >= 0.9.8a... FAILED,是因为我们的mini版本没有安装openssl的开发库,所有要先安装开发库
1
|
yum install openssl-devel.x86_64 -y
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
cd httpd-2.4.10
. /configure \
--prefix= /usr/local/apache \
--sysconfdir= /etc/httpd \
-- enable -so \
-- enable -rewirte \
-- enable -ssl \
-- enable -cgi \
-- enable -cgid \
-- enable -modules=most \
-- enable -mods-shared=most \
-- enable -mpms-shared=all \
--with-apr= /usr/local/apr \
--with-apr-util= /usr/local/apr-util
make
make install
|
关闭SELinux
1
2
|
echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile .d /httpd .sh
|
把httpd的man文件加入系统里面,首先安装man工具
1
2
|
yum install man -y
vim /etc/man .config
|
添加如下内容
1
|
MANPATH /usr/local/apache/man
|
给httpd添加一个SysV风格的脚步放到/etc/init.d/目录下面,到此为止httpd安装完成了;
#cat /etc/rc.d/init.d/httpd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#!/bin/bash
. /etc/rc .d /init .d /functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
HTTPD_LANG=${HTTPD_LANG- "C" }
INITLOG_ARGS= ""
apachectl= /usr/local/apache/bin/apachectl
httpd=${HTTPD- /usr/local/apache/bin/httpd }
prog=httpd
pidfile=${PIDFILE- /var/run/httpd .pid}
lockfile=${LOCKFILE- /var/lock/subsys/httpd }
RETVAL=0
start() {
echo -n $ "Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $ "Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $ "Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >& /dev/null ; then
RETVAL=$?
echo $ "not reloading due to configuration syntax error"
failure $ "not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $ "Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
|
而后为此脚本赋予执行权限:
1
|
chmod +x /etc/rc .d /init .d /httpd
|
加入服务列表:
1
2
|
chkconfig --add httpd
chkconfig httpd on
|
三、Mysql的安装:
Mysql的安装可以rpm安装,可以使用通用二进制包安装,也可以使用源码编译安装,鉴于源码编译太耗费时间,我们这使用通用二进制格式包安装,软件包可以在搜狐镜像网站下载到,我们现在使用的版本是mysql-5.5.20-linux2.6-x86_64.tar.gz。
1
2
3
4
5
6
7
8
9
10
11
|
wget
tar xf mysql-5.5.20-linux2.6-x86_64. tar .gz -C /usr/local
cd /usr/local
ln -s mysql-5.5.20-linux2.6-x86_64 mysql
groupadd -r mysql
useradd -g mysql -r -s /sbin/nologin mysql
cd mysql
chown -R mysql:mysql *
|
创建一个逻辑卷给Mysql数据存放位置。
1
2
3
4
5
6
|
pvcreate /dev/sda5
vgcreate myvg /dev/sda5
lvcreate -n mysql -L 5G myvg
mkfs.ext4 /dev/myvg/mysql
vim /etc/fstab
mkdir /data
|
添加开机自动挂载。
1
2
|
/dev/myvg/mysql /data ext4 defaults 0 0
|
1
2
3
4
5
6
7
8
9
|
mount -a
mkdir /data/mysql
chown mysql.mysql /data/mysql
chmod o-x /data/mysql
cd /usr/local/mysql
. /scripts/mysql_install_db --user=mysql --datadir= /data/mysql/
chown -R root *
|
添加二进制程序环境变量
1
|
echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile .d /mysql .sh
|
添加帮助文件
添加如下内容
1
|
MANPATH /usr/local/mysql/man
|
把mysql的库文件加入到系统库文件目录搜索里面
1
|
echo "/usr/local/mysql/lib" > /etc/ld .so.conf.d /mysql .conf
|
添加头文件
1
|
ln -s /usr/local/mysql/include /usr/include/mysql
|
添加启动脚本
1
2
|
cp support-files /mysql .server /etc/init .d /mysqld
chkconfig --add mysqld
|
添加配置文件
1
|
cp support-files /my-large .cnf /etc/my .cnf
|
在配置文件里面添加
现在可以启动Mysql服务了,可以为mysql添加密码
1
|
mysqladmin -uroot password '123456'
|
四、PHP的安装
下面我们进行最后一项php的编译安装,我们选择的php版本是5.4.36。
1
|
wget http: //cn2 .php.net /distributions/php-5 .4.36. tar .bz2
|
如果想让编译的php支持mcrypt扩展,此处还需要下如下两个rpm包并安装之:
libmcrypt-2.5.8-9.el6.rpm
libmcrypt-devel-2.5.8-9.el6.rpm
因为这两个软件包自带的软件库没有,所有我们更新增加yum源
1
2
|
rpm -ivh http: //download4 .fedora.redhat.com /pub/epel/6/x86_64/epel-release-6-8 .noarch.rpm
yum install -y libmcrypt libmcrypt-devel gd-devel
|
说明:
这些没有自带的包我们也可以到网站去查询下载地址,比如:http://rpmfind.net;在里面我们可以查询到支持各种硬件平台的rpm软件包。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
cd php-5.4.36
. /configure \
--prefix= /usr/local/php \
--with-mysql= /usr/local/mysql \
--with-openssl \
--with-mysqli= /usr/local/mysql/bin/mysql_config \
-- enable -mbstring \
--with-freetype- dir \
--with-jpeg- dir \
--with-png- dir \
--with-zlib \
--with-libxml- dir = /usr \
-- enable -xml \
-- enable -sockets \
--with-apxs2= /usr/local/apache/bin/apxs \
--with-mcrypt \
--with-config- file -path= /etc \
--with-config- file -scan- dir = /etc/php .d \
--with-bz2 \
-- enable -maintainer-zts
|
说明:
1、这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
2、--with-apxs2=/usr/local/apache/bin/apxs 这里是让php以httpd的模块模式运行,如果使用FastCGI模式运行的话需改为 --enable-fpm,如果httpd是rpm安装的话,使用apxs需要安装http-devle软件包
3、如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
4、此时如果编译的东西不适合您的需要,您可以添加需要编译的模块参数,也可以就此这样安装,后面我们可以动态安装需要的模块,安装方法和我后面为php安装xcache的方法是一样的。
注意:
我在编译中出现了一个错误configure: error: xml2-config not found. Please check your libxml2 installation.
在这里我们首先应该提交安装libxml2,然后再进行编译;
1
|
yum install libxml2 libxml2-devel -y
|
紧接着又出现错误,出现这么多错误的原因是因为我们安装的linux是mini版,好多软件并没有预装的,安装之前我们也不知道哪些软件存在,哪些软件不存在,只能出现一个问题解决一个问你
错误:configure: error: Please reinstall the BZip2 distribution
1
|
yum install bzip2 -devel.x86_64 -y
|
后面一切顺利,直接到编译配置结束,
为php提供好配置文件,在php源文件目录下面
1
|
cp php.ini-production /etc/php .ini
|
编辑apache配置文件httpd.conf,以apache支持php,而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。
1
2
3
4
5
6
7
|
vim /etc/httpd/httpd .conf
1、添加如下二行
AddType application /x-httpd-php .php
AddType application /x-httpd-php-source .phps
2、定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
|
具体apache的配置我们这里不在陈述,可以更改中心主机的路径,配置虚拟主机,加载需要的模块等等;
到现在为止LAMP环境是搭建好了,下面我们进行测试,看看我们的php是否可以连接数据库;在index.php里面填写上如下内容:
1
2
3
4
5
6
7
8
|
<?php
$conn =mysql_connect( 'localhost' , 'root' , '123456' );
if ( $conn )
echo "Success....." ;
else
echo "Failure....." ;
phpinfo();
?>
|
当数据库开启的时候显示Success....,数据库关闭的时候显示Failure...。
五、安装加速器XCache
XCache 是一个开源的 php的opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接使用缓冲区已编译的代码从而提高速度. 通常能够提高您的页面生成速率 2 到5 倍, 降低服务器负载.
1
2
3
4
|
wget http: //xcache .lighttpd.net /pub/Releases/3 .2.0 /xcache-3 .2.0. tar .gz
tar xf xcache-3.2.0. tar .gz
cd xcache-3.2.0
/usr/local/php/bin/phpize
|
然后却出现了错误:
1
2
|
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
|
原因是因为我还是有软件包没有安装
1
2
3
4
|
yum install autoconf -y
. /configure -- enable -xcache --with-php-config= /usr/local/php/bin/php-config
make
make install
|
为xcache提供配置文件
1
2
|
mkdir /etc/php .d
cp xcache.ini /etc/php .d/
|
到此为止xcache安装完成
第二部分、配置apache-2.4.10与fpm方式的php-5.4.36
一、apache、MySQL的安装与前一部分相同;请根据其进行安装;
二、编译安装php-5.4.36
1、解决依赖关系:
如果想让编译的php支持mcrypt扩展,此处还需要下载如下两个rpm包并安装之:
libmcrypt-2.5.7-5.el5.i386.rpm
libmcrypt-devel-2.5.7-5.el5.i386.rpm
mhash-0.9.9-1.el5.centos.i386.rpm
mhash-devel-0.9.9-1.el5.centos.i386.rpm
2、编译安装php-5.4.36
首先下载源码包至本地目录,
# tar xf php-5.4.36.tar.bz2
# cd php-5.4.36
# ./configure \
--prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-openssl \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--enable-sockets \
--enable-fpm \
--with-mcrypt \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--with-bz2
# make
# make intall
为php提供配置文件:
# cp php.ini-production /etc/php.ini
3、配置php-fpm
为php-fpm提供Sysv init脚本,并将其添加至服务列表:
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on
为php-fpm提供配置文件:
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:
# vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php/var/run/php-fpm.pid
接下来就可以启动php-fpm了:
# service php-fpm start
使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
# ps aux | grep php-fpm
默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验正其是否已经监听在相应的套接字。
# netstat -tnlp | grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 689/php-fpm
三、配置httpd-2.4.10
1、启用httpd的相关模块
在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
2、配置虚拟主机支持使用fcgi
在相应的虚拟主机中添加类似如下两行。
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
例如:
<VirtualHost *:80>
DocumentRoot "/www/html"
ServerName hebrxz.com
ServerAlias www.hebrxz.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/html/$1
<Directory "/www/html">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
ProxyRequests Off:关闭正向代理
ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。
3、编辑apache配置文件httpd.conf,让apache能识别php格式的页面,并支持php格式的主页
# vim /etc/httpd/httpd.conf
1、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
2、定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
补充:Apache httpd 2.4以前的版本中,要么把PHP作为Apache的模块运行,要么添加一个第三方模块支持PHP-FPM实现。
本文转自 wzlinux 51CTO博客,原文链接:http://blog.51cto.com/wzlinux/1605480,如需转载请自行联系原作者