Tenken blog

Linux源码编译安装PHP

安装说明

1、系统环境:centos 6.x
2、 php版本:php5.6.25
3、 安装路径:/usr/local/
4、 本次安装是以php-fpm方式编译安装

安装PHP环境相关的依赖包

1
2
3
4
5
yum install -y php-mcrypt libmcrypt libmcrypt-devel mhash mhash-devel
yum install -y libevent libevent-devel
yum install -y libxml2 libxml2-devel bzip2 bzip2-devel libcurl-devel
yum install -y libjpeg-devel libpng-devel freetype-devel
yum install -y zlib-devel bison bison-devel gd php-gd php-common

编译安装php-5.6.25

php源码包下载地址:http://php.net/downloads.php

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
#下载源码包至本地目录/usr/local/
#解压源码包
tar -xvf php-5.6.25.tar.gz
cd php-5.6.25
#配置
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-opcache \
--enable-fpm \
--enable-mysqlnd \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--enable-mbstring \
--enable-ftp \
--enable-xml \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-curl \
--with-zlib \
--enable-zip \
--with-bz2 \
--with-gd \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir
##apache方式安装的参数--with-apxs2=/usr/local/apache/bin/apxs
#编译安装
make && make install

#如果看到出现以下提示就代表成功了
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/php/man/man1/
Installing PHP FPM binary: /usr/local/php/sbin/
Installing PHP FPM config: /usr/local/php/etc/
Installing PHP FPM man page: /usr/local/php/php/man/man8/
Installing PHP FPM status page: /usr/local/php/php/php/fpm/
Installing PHP CGI binary: /usr/local/php/bin/
Installing PHP CGI man page: /usr/local/php/php/man/man1/
Installing build environment: /usr/local/php/lib/php/build/
Installing header files: /usr/local/php/include/php/
Installing helper programs: /usr/local/php/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/php/lib/php/
[PEAR] Archive_Tar - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.3.0
[PEAR] PEAR - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/local/php-5.6.25/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/

配置PHP

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
#目录:/usr/local/php-5.6.25/
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
#设置开机启动
chkconfig --add php-fpm
chkconfig php-fpm on
#启动php-fpm
service php-fpm start
#查看php-fpm状态
service php-fpm status
#配置php.ini文件
vi /usr/local/php/etc/php.ini
#找到disable_functions =
#禁止PHP危险的函数
#修改为:
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 =
#修改为:date.timezone = PRC #设置时区

#找到:expose_php = On
#修改为:expose_php = Off #禁止显示php版本的信息

#找到:short_open_tag = Off
#修改为:short_open_tag = ON #支持php短标签,这个看需求是否开启,有些开源系统可能用到段标签需要开启

#找到opcache.enable=0
#修改为opcache.enable=1 #php支持opcode缓存

#找到:;opcache.enable_cli=1 #php支持opcode缓存
#修改为:opcache.enable_cli=0

#在最后一行添加:zend_extension=opcache.so #开启opcode缓存功能
#保存退出
:wq!

配置nginx支持PHP

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
#编辑nginx.conf配置文件,做如下修改
vi /usr/local/nginx/conf/nginx.conf

#首行user去掉注释,修改Nginx运行组为nginx nginx;必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
user nginx nginx;

#添加index.php
location / {
root html;
index index.php index.html index.htm;
}

#取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为
$document_root$fastcgi_script_name,或者使用绝对路径
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
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;
}

#保存退出
:wq!


#编辑php-fpm.conf文件
vi /usr/local/php/etc/php-fpm.conf

#设置php-fpm运行账号为nginx
user = nginx
#设置php-fpm运行组为nginx
group = nginx
#取消前面的分号
pid = run/php-fpm.pid
#保存退出
:wq!

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#重启nginx
service nginx restart
#重启php-fpm
service php-fpm restart
#进入nginx默认目录
cd /usr/local/nginx/html
#创建index.php文件
vi index.php
<?php
phpinfo();
?>
#保存退出
:wq!
#设置html目录所有者
chown -R nginx:nginx /usr/local/nginx/html
#浏览器访问:ip地址

php-fpm命令

1
2
3
4
5
6
7
8
#启动
service php-fpm start
#重启
service php-fpm restart
#停止
service php-fpm stop
#查看状态
service php-fpm status

末尾

到此,php源码编译安装已经成功了,关于nginx.conf的详细配置,php-fpm.conf的配置下一篇会说明。

请我吃辣条吧~~