1????环境准备
?????1.1????安装环境:
1
2
3
4
5
6
|
[root@web01?mysql]
CentOS?release?6.8?(Final)
[root@web01?mysql]
2.6.32-642.el6.x86_64
[root@web01?mysql]
x86_64
|
????1.2????安装php服务的前提是:nginx和mysql服务都启动

1
2
3
|
[root@web01?mysql]
tcp????????0??????0?0.0.0.0:3306????????????????0.0.0.0:*???????????????????LISTEN??????2742 /mysqld ?????????
tcp????????0??????0?0.0.0.0:80??????????????????0.0.0.0:*???????????????????LISTEN??????2931 /nginx
|
????1.3???? 安装PHP基础库
????检查有没有安装这些库:
????rpm -qa freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
????rpm -qa zlib-devel libxm12-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
1
2
3
4
|
[root@web01?mysql]
[root@web01?mysql]
zlib-devel-1.2.3-29.el6.x86_64
[root@web01?mysql]
|
????提示:libjpeg-turbo-devel是早期的libjpeg-devel的新名字,libcurl-devel是早期的curl-devel的新名字。
????每个lib一般都会存在对应的以“*-devel”命名的包安装对应的“-devel”包后,对应的lib包会自动安装好,例如安装gd-devel就会安装gd。
????这些lib库也不是必须安装的,但是目前的企业环境下一般都需要安装。否则,PHP程序运行有问题,例如验证码无法显示等。
????执行下面命令安装相关的LIB软件包:
1
2
|
[root@web01?mysql]
[root@web01?mysql]
|
????检查是否yum安装成功
1
2
3
4
5
6
7
8
9
|
[root@web01?~]
libjpeg-turbo-devel-1.2.1-3.el6_5.x86_64
gd-devel-2.0.35-11.el6.x86_64
freetype-devel-2.3.11-17.el6.x86_64
libxml2-devel-2.7.6-21.el6_8.1.x86_64
libpng-devel-1.2.49-2.el6_7.x86_64
libxslt-devel-1.1.26-2.el6_3.1.x86_64
zlib-devel-1.2.3-29.el6.x86_64
libcurl-devel-7.19.7-53.el6_9.x86_64
|
????发现有一个包没有安装成功:No package libiconv-devel available.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@web01?~]
Loaded?plugins:?fastestmirror,?security
Setting?up?Install?Process
Loading?mirror?speeds?from?cached?hostfile
? *?base:?mirrors.aliyun.com
? *?extras:?mirrors.aliyun.com
? *?updates:?mirrors.aliyun.com
Package?zlib-devel-1.2.3-29.el6.x86_64?already?installed?and?latest?version
Package?libxml2-devel-2.7.6-21.el6_8.1.x86_64?already?installed?and?latest?version
Package?libjpeg-turbo-devel-1.2.1-3.el6_5.x86_64?already?installed?and?latest?version
Package?libjpeg-turbo-devel-1.2.1-3.el6_5.x86_64?already?installed?and?latest?version
No?package?libiconv-devel?available.
Nothing?to? do
|
????从以上结果看出,仅有libiconv-devel这个包没有安装,因为默认的yum源没有此包,后面会编译安装。
????当然也可以一个一个的yum安装或通过源文件手工编译安装,这个方法太麻烦,毕竟效率优先。
安装yum无法安装的libiconv库。
????编译安装libiconv-devel如下所示:执行完如下命令就安装完成了libiconv-devel
1
2
3
4
5
6
7
8
9
|
mkdir ?-p? /home/oldboy/tools
cd ?/home/oldboy/tools
wget?http: //ftp .gnu.org /pub/gnu/libiconv/libiconv-1 .14. tar .gz
tar ?zxf?libiconv-1.14. tar .gz
cd ?libiconv-1.14
. /configure ?--prefix= /usr/local/libiconv
make
make ?install
cd ?../
|
????技巧:可以复制多条命令一次输入执行。
????libiconv相关信息地址为:http://www.gnu.org/software/libiconv/(获得软件包的途径为打开g.cn,输入download libiconv)
????1.4????安装PHP扩展库:安装libmcrypt库
????这是一个使用动态加载模块化的libmcrypt。libmcrypt对于在程序运行时添加、移除算法是有用的。limbcrypt-nm目前不在被官方支持,软件地址:http://mcrypt.hellug.gr/lib/?,编译PHP的过程中libmcrypt库不是必须要安装的包。很多网友这里采用的是很复杂的编译安装libmcrypt的方法,本文选择更简单的yum安装方法。 由于在centos默认的yum源里没有libmcrypt-devel,因此需要事先配置eqel第三方yum源,具体命令如下:
1
|
wget?-O? /etc/yum .repos.d /epel .repo?http: //mirrors .aliyun.com /repo/epel-6 .repo
|
????下面就可以安装PHP相关包libmcrypt对应的包libmcrypt-devel了
1
|
yum?-y? install ?libmcrypt-devel
|
????建议使用更简单的yum方法,抛弃网上的流行编译方法安装。
????1.5????安装PHP扩展库:安装mhash加密扩展库
????1.6????安装PHP扩展库:安装mcrypt加密扩展库
??????????????安装方法:
????最后检查PHP扩展库:1.4 1.5 1.6是否安装成功,如下图已安装成功。
1
2
3
4
|
[root@web01?tools]
mcrypt-2.6.8-10.el6.x86_64
mhash-0.9.9.9-3.el6.x86_64
libmcrypt-devel-2.5.8-9.el6.x86_64
|
2 ????获取PHP软件包
????可以使用wget方式下载PHP软件包,也可以下载到本地电脑,再上传到linux中,这里以本地下载后上传到linux系统为例。
????下面使用rz 命令通过本地上传,当然也可以网上下载,地址为http://cn.php.net
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@web01?tools]
[root@web01?tools]
rz?waiting?to?receive.
? zmodem???°?Ctrl+C?
?? 100%???17356?KB?17356?KB /s ?00:00:01???????0?Errors
[root@web01?tools]
total?204544
drwxr-xr-x?22?root?root??????4096?Aug?24?21:05?libiconv-1.14
-rw-r--r--??1?root?root???4984397?Aug??8??2011?libiconv-1.14. tar .gz
-rw-r--r--??1?root?root?185870973?Aug?23?19:57?mysql-5.5.49-linux2.6-x86_64. tar .gz
drwxr-xr-x??9?1001?1001??????4096?Aug?19?16:11?nginx-1.6.3
-rw-r--r--??1?root?root????805253?Apr??8??2015?nginx-1.6.3. tar .gz
-rw-r--r--??1?root?root??17773092?Aug?24?21:24?php-5.5.32. tar .gz
|
?linx下的下载命令为:
?3????配置PHP软件./configure系列命令
1
2
3
4
|
[root@web01?tools]
[root@web01?tools]
[root@web01?php-5.5.32]
uid=503(www)?gid=503(www)? groups =503(www)
|
????
1
2
3
|
[root@web01?php-5.5.32]
/home/oldboy/tools/php-5 .5.32
[root@web01?php-5.5.32]
|
当前目录输入如下编译内容回车:(注意\后面不能有空格)
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
|
[root@web01?php-5.5.32]
--prefix= /application/php5 .5.32?\
--with-mysql= /application/mysql/ ?\
--with-pdo-mysql=mysqlnd?\
--with-iconv- dir = /usr/local/libiconv ?\
--with-freetype- dir ?\
--with-jpeg- dir ?\
--with-png- dir ?\
--with-zlib?\
--with-libxml- dir = /usr ?\
-- enable -xml?\
--disable-rpath?\
-- enable -bcmath?\
-- enable -shmop?\
-- enable -sysvsem?\
-- enable -inline-optimization?\
--with-curl?\
-- enable -mbregex?\
-- enable -fpm?\
-- enable -mbstring?\
--with-mcrypt?\
--with-gd?\
-- enable -gd-native-ttf?\
--with-openssl?\
--with-mhash?\
-- enable -pcntl?\
-- enable -sockets?\
--with-xmlrpc?\
-- enable -soap?\
-- enable -short-tags?\
-- enable -static?\
--with-xsl?\
--with-fpm-user=www?\
--with-fpm-group=www?\
-- enable - ftp ?\
-- enable -opcache=no
|
相关参数介绍:
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
|
. /configure ?\
--prefix= /application/php5 .5.32?\?
--with-mysql= /application/mysql/ ?\
当然,如果没有mysql软件包,也可以不单独安装,这样的情况可使用--with-mysql=mysqlnd替代--with-mysql= /application/mysql ,
因为php软件里已经自带了连接mysql的客户端工具。
--with-pdo-mysql=mysqlnd?\
--with-iconv- dir = /usr/local/libiconv ?\
--with-freetype- dir ?\
--with-jpeg- dir ?\
--with-png- dir ?\
--with-zlib?\
--with-libxml- dir = /usr ?\
-- enable -xml?\
--disable-rpath?\
-- enable -bcmath?\
-- enable -shmop?\
-- enable -sysvsem?\
-- enable -inline-optimization?\
--with-curl?\
-- enable -mbregex?\
-- enable -fpm?\
-- enable -mbstring?\
--with-mcrypt?\
--with-gd?\
-- enable -gd-native-ttf?\
--with-openssl?\
--with-mhash?\
-- enable -pcntl?\
-- enable -sockets?\
--with-xmlrpc?\
-- enable -soap?\
-- enable -short-tags?\
-- enable -static?\
--with-xsl?\
--with-fpm-user=www?\
--with-fpm-group=www?\
-- enable - ftp ?\
可以通过执行. /configure ?--help命令来详细查看以上各参数的用户
以上配置中的"\"反斜线表示换一行输入
-- enable -opcache=no
其他需要mysql相关包常见的php对应编译参数如下:
-- enable -mysqlnd\
--with-pdo-mysql=mysqlnd\
--with-mysqli=mysqlnd\
|
4????编译PHP
????配置PHP软件./configure系列命令后,就可以编译PHP软件了。操作过程如下:
1
2
3
|
[root@web01?php-5.5.32]
[root@web01?php-5.5.32]
[root@web01?php-5.5.32]
|
编译完成后如果成功了会这样提示:
1
2
3
4
5
6
7
8
9
10
11
|
Generating?phar.php
Generating?phar.phar
PEAR?package?PHP_Archive?not?installed:?generated?phar?will?require?PHP's?phar?extension?be?enabled.
pharcommand.inc
invertedregexiterator.inc
clicommand.inc
directorytreeiterator.inc
directorygraphiterator.inc
phar.inc
Build?complete.
Don 't?forget?to?run?' make ?test '.
|
1
2
|
[root@web01?php-5.5.32]
0
|
5????安装PHP
????编译完成后执行安装命令:
??????安装完成
6?????安装PHP服务完成后,配置PHP
????????做一个link
1
2
|
[root@web01?php-5.5.32]
bin??etc??include??lib??php??sbin??var
|
????????php两个配置文件
1
2
3
|
[root@web01?php-5.5.32]
-rw-r--r--?1?1001?1001?69236?Feb??2??2016?php.ini-development
-rw-r--r--?1?1001?1001?69266?Feb??2??2016?php.ini-production
|
????????复制
????????配置PHP服务(fastcgi模式)配置文件php-fpm.conf
1
2
3
4
5
|
[root@web01?php-5.5.32]
[root@web01?etc]
total?28
-rw-r--r--?1?root?root??1321?Aug?24?23:38?pear.conf
-rw-r--r--?1?root?root?22602?Aug?24?23:37?php-fpm.conf.default
|
1
2
3
|
[root@web01?etc]
[root@web01?etc]
pear.conf??php-fpm.conf??php-fpm.conf.default
|
????启动php进程服务:两种方法判断是否启动lsof -i :9000和ps -ef|grep php-fpm
1
2
3
4
5
6
7
8
9
10
11
|
[root@web01?etc]
[root@web01?etc]
COMMAND??PID?USER???FD???TYPE?DEVICE?SIZE /OFF ?NODE?NAME
php-fpm?3829?root????7u??IPv4?114778??????0t0??TCP?localhost:cslistener?(LISTEN)
php-fpm?3830??www????0u??IPv4?114778??????0t0??TCP?localhost:cslistener?(LISTEN)
php-fpm?3831??www????0u??IPv4?114778??????0t0??TCP?localhost:cslistener?(LISTEN)
[root@web01?etc]
root???????3829??????1??0?23:55??????????00:00:00?php-fpm:?master?process?( /application/php5 .5.32 /etc/php-fpm .conf)
www????????3830???3829??0?23:55??????????00:00:00?php-fpm:?pool?www??????????
www????????3831???3829??0?23:55??????????00:00:00?php-fpm:?pool?www????????
root???????3834???1600??0?23:56?pts /1 ????00:00:00? grep ?php-fpm
|
????????????其中两个子进程如下:
????????????00:00:00 php-fpm: pool www? #子进程
????????????00:00:00 php-fpm: pool www? #子进程
????????????管理进程为:php-fpm: master process
7????整合PHP和nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@web01?extra]
/application/nginx/conf/extra
[root@web01?extra]
[root@web01?extra]
???? server?{
???????? listen???????80;
???????? server_name??blog.etiantian.org;
???????? location?/?{
???????????? root???html /blog ;
???????????? index??index.html?index.htm;
???????? }
???????? location?~?.*\.(php|php5)?$?{
???????????? root?html /blog ;
???????????? fastcgi_pass?127.0.0.1:9000;
???????????? fastcgi_index?index.php;
???????????? include?fastcgi.conf;
???????? }
???? }
|
????检查语法
1
2
3
|
[root@web01?extra]
nginx:?the?configuration? file ?/application/nginx-1 .6.3 //conf/nginx .conf?syntax?is?ok
nginx:?configuration? file ?/application/nginx-1 .6.3 //conf/nginx .conf? test ?is?successful
|
????平滑重启nginx服务
8 ????测试LNMP环境生效的情况
(1)测试PHP解析请求是否OK
1)进入指定的默认站点目录后,编辑index.php,添加:
1
2
3
4
5
6
7
8
|
[root@web01?blog]
/application/nginx/html/blog
[root@web01?blog]
total?4
-rw-r--r--?1?root?root?5?Aug?19?23:17?index.html
[root@web01?blog]
[root@web01?blog]
<?php?phpinfo();??>
|
以上代码为显示PHP配置信息的简单PHP文件代码。
????返回windows界面,ping blog.etiantian.org看是否解析
1
2
3
4
5
6
7
8
9
10
|
C:\Users\Administrator> ping ?blog.etiantian.org
正在?Ping?www.etiantian.org?[10.0.0.8]?具有?32?字节的数据:
来自?10.0.0.8?的回复:?字节=32?时间<1ms?TTL=64
来自?10.0.0.8?的回复:?字节=32?时间<1ms?TTL=64
来自?10.0.0.8?的回复:?字节=32?时间<1ms?TTL=64
来自?10.0.0.8?的回复:?字节=32?时间<1ms?TTL=64
10.0.0.8?的?Ping?统计信息:
???? 数据包:?已发送?=?4,已接收?=?4,丢失?=?0?(0%?丢失),
往返行程的估计时间(以毫秒为单位):
???? 最短?=?0ms,最长?=?0ms,平均?=?0ms
|
????在ie浏览器中输入http://blog.etiantian.org/test_info.php?看是否可以解析PHP动态语言,下图出现说明PHP动态解析成功。

????如果没有出现PHP的界面说明解析不成功。解析不成功返回404错误等一般都是在nginx.conf配置文件里面配置错误导致的。
????(2)????针对于nginx请求访问PHP,然后对PHP连接mysql的情况进行测试
1
2
3
4
5
6
7
8
9
10
11
|
[root@web01?blog]
/application/nginx/html/blog
[root@web01?blog]
<?php
???????? $link_id=mysql_connect( 'localhost' , 'root' , '123456' )?or?mysql_error();
???????? if ($link_id){
???????????????? echo ?"mysql?successful?by?oldboy?!" ;
???????? } else {
???????????????? echo ?mysql_error();
???????? }
?>
|
????其中'root','123456'为本机mysql的用户名和密码
正常的结果如下:

至此,LNMP的组合已经搭建完毕。
本文转自sandshell博客51CTO博客,原文链接http://blog.51cto.com/sandshell/1958843如需转载请自行联系原作者
sandshell