CentOS 7 編譯安裝PHP7

編譯安裝

系統版本CentOS V7.6

PHP V7.3.3

#yum安裝PHP編譯時的依賴庫

shell>yum install libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel net-snmp net-snmp-devel openssl openssl-devel libcurl libcurl-devel

#創建PHP用戶和用戶組(php用戶沒有登錄權限)

shell>groupadd -r php && useradd -r -g php -s /bin/false -d /usr/local/php -M php

#下載PHP主程序

shell>wget -c -P /opt/tmp/ http://ee1.php.net/distributions/php-7.3.3.tar.gz

#進入目錄並解壓縮

shell>cd /opt/tmp

shell>tar zxvf php-7.3.3.tar.gz

shell>cd php-7.3.3

#配置PHP並編譯安裝

shell>./configure \

--prefix=/usr/local/php \

--enable-bcmath \

--enable-mbstring \

--enable-sockets \

--enable-ctype \

--enable-opcache \

--enable-fpm \

--enable-mysqlnd \

--with-gd \

--with-png-dir \

--with-jpeg-dir \

--with-freetype-dir \

--with-libxml-dir \

--with-gettext \

--with-snmp \

--with-openssl-dir \

--with-curl \

--with-fpm-user=nginx \

--with-fpm-group=nginx \

--with-mysqli=mysqlnd \

--with-pdo-mysql=mysqlnd \

--with-mysql-sock=/var/lib/mysql/mysql.sock \

--without-pear \

--disable-phar

shell>make -j 4

shell>make install

#下載pear.phar並安裝

shell>wget  http://pear.php.net/go-pear.phar

shell>/usr/local/php/bin/php go-pear.phar

#查看編譯成功後的PHP安裝目錄(需要確保至少存在mysqli.so、pdo_mysql.so這兩個動態庫文件)

shell>ls -lrt /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731

#設置PHP7的配置文件跟腳本php.ini、php-fpm.conf、www.conf、php-fpm

shell>cp -rv php.ini-production /usr/local/php/etc/php.ini

shell>cp -rv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

shell>cp -rv /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

shell>cp -R ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

#添加PHP7的環境變量

shell>echo -e '\nexport PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH\n' >> /etc/profile && source /etc/profile

#設置PHP日誌目錄和php-fpm的運行進程ID文件(php-fpm.sock)目錄

shell>mkdir -p /var/log/php-fpm/ && mkdir -p /var/run/php-fpm && cd /var/run/ && chown -R nginx:nginx php-fpm

#修改session的目錄配置

shell>mkdir -p /var/lib/php/session && chown -R nginx:nginx /var/lib/php

#配置開機自啓動,增加到主機sysV服務

shell>chmod +x /etc/init.d/php-fpm

shell>chkconfig --add php-fpm

shell>chkconfig php-fpm on

#測試PHP的配置文件是否正確合法

shell>php-fpm -t

#啓動php服務

shell>service php-fpm start

#查看PHP-FPM進程

shell>ps -aux|grep php-fpm

#查看PHP7版本信息

shell>php -v

#PHP與nginx整合

進入到nginx配置文件中修改

在localtion中增加index.php

location / {

            root   html;

            index  index.html index.htmi index.php;

        }

在location~\.php$去掉全部註釋並更改$document_root

 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;

        }

保存後退出

在網站根目錄下編寫PHP測試文件

vim index.php

<?php

echo phpinfo();

?>

保存退出並重啓nginx服務器

注意!!!測試完畢後刪除該文件


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章