centos編譯安裝php7

來源:互聯網
上載者:User

標籤:mbr   複製   profile   tty   key   server   git   use   bcmath   

環境說明
  1. VMware 12 中搭建的CentOS 7 x64 4核 2G記憶體
  2. 環境中已經安裝了http://blog.csdn.net/u014595668/article/details/50166277 中編譯安裝的MySQL5.7.9
工具下載

編譯環境:libmcrypt2.5.7下載、mhash-0.9.9.9下載、mcrypt-2.6.8下載 
PHP安裝包:php 7下載 
用xftp把所有的工具上傳到/root/目錄中

建立php使用者和使用者組

首先建立一個名為php且沒有登入許可權的使用者和一個名為php的使用者組

#######建立php使用者和php組[root@localhost ~]# groupadd -r php && useradd -r -g php -s /bin/false -d /usr/local/php7 -M php
  • 1
  • 2
  • 1
  • 2
編譯安裝php 7所需的依賴

編譯安裝 libmcrypt, mhash, mcrypt 二進位源碼包 
這裡之所以編譯安裝libmcrypt,原因是yum安裝的貌似會報錯

######編譯安裝libmcrypt-2.5.7[root@localhost ~]# tar zxvf libmcrypt-2.5.7.tar.gz[root@localhost ~]# cd libmcrypt-2.5.7[root@localhost libmcrypt-2.5.7]# ./configure --prefix=/usr/local/related/libmcrypt[root@localhost libmcrypt-2.5.7]# make && make install[root@localhost libmcrypt-2.5.7]# cd ~[root@localhost ~]# rm -rf libmcrypt-2.5.7*
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
######編譯安裝mhash-0.9.9.9[root@localhost ~]# tar zxf mhash-0.9.9.9.tar.gz [root@localhost ~]# cd mhash-0.9.9.9[root@localhost mhash-0.9.9.9]# ./configure --prefix=/usr/local/related/mhash[root@localhost mhash-0.9.9.9]# make && make install[root@localhost mhash-0.9.9.9]# cd ~[root@localhost ~]# rm -rf mhash-0.9.9.9*
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
######編譯安裝mcrypt-2.6.8[root@localhost ~]# tar zxf mcrypt-2.6.8.tar.gz && cd mcrypt-2.6.8[root@localhost mcrypt-2.6.8]# export LD_LIBRARY_PATH=/usr/local/related/libmcrypt/lib:/usr/local/related/mhash/lib[root@localhost mcrypt-2.6.8]# export LDFLAGS="-L/usr/local/related/mhash/lib -I/usr/local/related/mhash/include/"[root@localhost mcrypt-2.6.8]# export CFLAGS="-I/usr/local/related/mhash/include/"[root@localhost mcrypt-2.6.8]# ./configure --prefix=/usr/local/related/mcrypt --with-libmcrypt-prefix=/usr/local/related/libmcrypt[root@localhost mcrypt-2.6.8]# make && make install[root@localhost mcrypt-2.6.8]# cd ~[root@localhost ~]# rm -rf mcrypt-2.6.8*
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
######其他依賴yum安裝[root@localhost ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel
  • 1
  • 2
  • 1
  • 2
PHP 7編譯參數的配置

注意,操作時一定要先把下面反斜線“\”後面添加的注釋文字去掉!!!

######組建組態檔案[[email protected] ~]# tar -zxf php-7.0.0.tar.gz && cd php-7.0.0[[email protected] php-7.0.0]# ./buildconf --forceForcing buildconfRemoving configure cachesbuildconf: checking installation...buildconf: autoconf version 2.69 (ok)rebuilding configurerebuilding main/php_config.h.in######開始配置[[email protected] php-7.0.0]# ./configure --prefix=/usr/local/php7 \                              [PHP7安裝的根目錄]--exec-prefix=/usr/local/php7 --bindir=/usr/local/php7/bin --sbindir=/usr/local/php7/sbin --includedir=/usr/local/php7/include --libdir=/usr/local/php7/lib/php --mandir=/usr/local/php7/php/man --with-config-file-path=/usr/local/php7/etc \ [PHP7的配置目錄]--with-mysql-sock=/var/run/mysql/mysql.sock \ [PHP7的Unix socket通訊檔案]--with-mcrypt=/usr/include --with-mhash --with-openssl --with-mysql=shared,mysqlnd \ [PHP7依賴mysql庫] --with-mysqli=shared,mysqlnd \ [PHP7依賴mysql庫]--with-pdo-mysql=shared,mysqlnd \ [PHP7依賴mysql庫]--with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session \ [允許php會話session]--with-curl \ [允許curl擴充]--with-jpeg-dir --with-freetype-dir --enable-opcache \ [使用opcache緩衝]--enable-fpm --enable-fastcgi --with-fpm-user=nginx \ [php-fpm的使用者]--with-fpm-group=nginx \ [php-fpm的使用者組]--without-gdbm --with-mcrypt=/usr/local/related/libmcrypt \ [指定libmcrypt位置]--disable-fileinfo
  • 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
  • 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

執行上面的配置命令的運行結果如下: 

開始編譯安裝PHP 7
[[email protected] php-7.0.0]# make clean && make && make install
  • 1
  • 1

PHP 7編譯安裝成功 

選做步驟:執行make test命令進行測試

這是一個非必需的操作步驟,執行make test命令

查看編譯成功後的PHP7安裝目錄

由於需要和MySQL進行通訊,所以需要特別查看PHP7安裝後的lib擴充庫目錄(/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/)。需要確保至少存在mysqli.so、pdo_mysql.so這兩個動態庫檔案,如所示。

[root@localhost php-7.0.0]# ls -lrt /usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/
  • 1
  • 1

開始設定PHP7的設定檔php.ini、php-fpm.conf、www.conf和php-fpm指令碼

可以用編譯後的設定檔複製到PHP7的配置目錄(/usr/local/php7/etc/),推薦使用 github中的配置。這個配置來自《PHP7中php.ini、php-fpm和www.conf的配置》

#######方法一:直接使用編譯後未經最佳化處理的配置[root@localhost php-7.0.0]# cp php.ini-production /usr/local/php7/etc/php.ini[root@localhost php-7.0.0]# cp /root/php-7.0.0/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm[root@localhost php-7.0.0]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf[root@localhost php-7.0.0]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf#######方法二:使用https://github.com/lizer2014/mylnmp/tree/master/PHP文中的配置 [root@localhost php-7.0.0]# mv ~/php.ini /usr/local/php7/etc/php.ini && mv ~/php-fpm /etc/init.d/php-fpm[root@localhost php-7.0.0]# mv ~/php-fpm.conf /usr/local/php7/etc/php-fpm.conf && mv ~/www.conf /usr/local/php7/etc/php-fpm.d/www.conf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

注意:需要修改php.ini配置中的參數,extension_dir改為自己的

extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/"
  • 1
  • 1

其中在/etc/init.d/php-fpm中在start函數的守護進程前必須有make /var/run/php-fpm命令,否則重啟伺服器會導致啟動失敗,nginx中配置也有這個問題

添加php的環境變數
[root@localhost php-7.0.0]# echo -e ‘\nexport PATH=/usr/local/php7/bin:/usr/local/php7/sbin:$PATH\n‘ >> /etc/profile && source /etc/profile
  • 1
  • 1
設定PHP日誌目錄和php-fpm進程檔案(php-fpm.sock)目錄

其中,設定php-fpm進程目錄的使用者和使用者組為nginx,並建立php會話session目錄

#######設定PHP日誌目錄和php-fpm的運行進程ID檔案(php-fpm.sock)目錄[root@localhost php-7.0.0]# groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx[root@localhost php-7.0.0]# mkdir -p /var/log/php-fpm/ && mkdir -p /var/run/php-fpm && cd /var/run/ && chown -R nginx:nginx php-fpm#######修改session的目錄配置[root@localhost run]# mkdir -p /var/lib/php/session[root@localhost run]# chown -R nginx:nginx /var/lib/php
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
設定PHP開機啟動以及測試組態檔案是否正確
######配置開機自啟動,增加到主機sysV服務[[email protected] run]# chmod +x /etc/init.d/php-fpm[[email protected] run]# chkconfig --add php-fpm[[email protected] run]# chkconfig php-fpm on######測試PHP的設定檔是否正確合法[[email protected] run]# php-fpm -t[05-Dec-2015 17:33:03] NOTICE: configuration file /usr/local/php7/etc/php-fpm.conf test is successful
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
啟動php服務

在完成上面的操作後,就可以正式使用php服務了。啟動php進程服務的命令如下:

[[email protected] init.d]# service php-fpm startStarting php-fpm  done
  • 1
  • 2
  • 1
  • 2

然後可以通過命令ps -aux|grep php查看是否成功(圖中的php-fpm進程數和進程使用者nginx都是由www.conf中pm.start_servers和user的值分別決定的):

查看PHP7版本資訊

最後,可以通過命令php -v查看當前PHP版本資訊,圖中可以看到當前PHP7還使用了Zend OPcache緩衝,是因為在php.ini檔案中添加了zend_extension=opcache.so配置。

centos編譯安裝php7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.