在Linux上编译安装PHP5

实验环境

  • CentOS/Ubuntu系统;
  • 系统架构:64位
  • PHP版本:5.6.30

相关网址

安装指导

Ubuntu系统

  • 安装编译套件:
1
$ apt install -y build-essential libtool
  • 安装编译依赖包:
1
2
3
$ apt install -y pkg-config libxml2 libxml2-dev bzip2 libbz2-dev \
libcurl3 libcurl4-openssl-dev libjpeg-dev libpng12-dev libreadline-dev \
libfreetype6-dev libmcrypt4 libmcrypt-dev libssl-dev

CentOS系统

  • 安装编译套件:
1
$ yum install -y gcc gcc-c++ make automake autoconf bzip2
  • 请添加CentOS系统的Epel源;

  • 安装编译依赖包:

1
2
3
4
5
6
$ yum install -y libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel \
bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel \
e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel \
readline readline-devel

CentOS/Ubuntu系统

  • 解压源码包:
1
$ tar -zxf php-5.6.30.tar.gz
  • 切换目录:
1
$ cd php-5.6.30

Apache服务

  • 预编译(Apache):
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
# 设置Apache的安装路径
$ export Apache_PATH=/usr/local/apache

$ ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-apxs2=${Apache_PATH}/bin/apxs \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-bz2 \
--with-zlib \
--with-curl \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-readline \
--with-gettext \
--with-pcre-regex \
--enable-inline-optimization \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-mbregex \
--enable-sockets \
--enable-exif \
--enable-mysqlnd \
--enable-json \
--enable-zip \
--enable-fpm \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--disable-debug \
--disable-rpath \
--disable-ipv6
  • 配置Apache服务:
1
$ vim /etc/httpd/httpd.conf
1
2
3
4
5
6
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<IfModule dir_module>
DirectoryIndex index.html index.htm index.php
</IfModule>

Nginx服务

  • 预编译(Nginx):
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
$ ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-bz2 \
--with-zlib \
--with-curl \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-readline \
--with-gettext \
--with-pcre-regex \
--enable-inline-optimization \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-mbregex \
--enable-sockets \
--enable-exif \
--enable-mysqlnd \
--enable-json \
--enable-zip \
--enable-fpm \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--disable-debug \
--disable-rpath \
--disable-ipv6

Apache/Nginx服务

  • 编译并安装(多进程编译):
1
$ make -j 4 && make install
  • 设置环境变量:
1
2
$ echo 'export PATH="/usr/local/php/bin:${PATH}"' >> /etc/profile
$ source /etc/profile
  • 获取PHP的版本信息:
1
$ php -v

配置PHP服务

  • 拷贝配置文件:
1
$ cp php.ini-production /usr/local/php/etc/php.ini
  • 拷贝配置文件(Nginx):
1
$ cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  • 配置PHP服务:
1
$ vim /usr/local/php/etc/php.ini
1
2
3
4
cgi.fix_pathinfo = 1
date.timezone = Asia/Shanghai
post_max_size = 8M
upload_max_filesize = 8M
  • 配置fpm服务(Nginx):
1
$ vim /usr/local/php/etc/php-fpm.conf
1
2
3
4
5
6
7
8
[global]
pid = run/php-fpm.pid

[www]
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
  • 拷贝启动脚本:
1
2
3
$ mkdir /var/run/php-fpm/
$ cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
$ chmod 755 /etc/init.d/php-fpm
  • 启动fpm服务(Nginx):
1
$ service php-fpm start

CentOS系统

  • 设置fpm服务随机自启(CentOS-6):
1
2
$ chkconfig --add php-fpm
$ chkconfig php-fpm on

Ubuntu系统

1
$ vim /etc/rc.local
1
/etc/init.d/php-fpm start

测试操作

  • 切换至网页根目录(Apache):
1
$ cd /var/www/html
  • 切换至网页根目录(Nginx):
1
2
$ mkdir -p /data/www
$ cd /data/www
  • 创建探针文件:
1
$ vim phpinfo.php
1
2
3
4
5
6
<?php
# 测试能否连接MySQL
echo mysql_connect('localhost', 'root', 'PASSWORD') ? '连接成功' : '连接失败';
# PHP探针
phpinfo();
?>
  • 重启Apache服务:
1
$ service httpd restart
  • 重启Nginx服务:
1
2
$ service nginx restart
$ nginx -s reload
  • 测试方式
    • 使用浏览器访问:http://<IP>:<PORT>
    • 使用命令测试:php phpinfo.php

有你就有世界,感谢有你,昕!
0%