HCRM博客

如何在CentOS 7上成功安装PHP 7?

简单安装(通过yum)

1、安装epelrelease源

   sudo rpm ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epelrelease75.noarch.rpm

2、安装webtatic源并更新yum

如何在CentOS 7上成功安装PHP 7?-图1
(图片来源网络,侵权删除)
   sudo rpm Uvh https://mirror.webtatic.com/yum/el7/latest.rpm
   sudo yum update

3、安装PHP 7.0

   sudo yum install php70w

编译安装(高级用户)

1、下载PHP源码

   wget http://cn2.php.net/get/php7.1.1.tar.gz

2、解压源码包

   tar zxvf php7.1.1.tar.gz
   cd php7.1.1

3、安装依赖包

   sudo yum install 
       libxml2 libxml2devel 
       openssl openssldevel 
       bzip2 bzip2devel \
       libjpeg libjpegdevel 
       libpng libpngdevel \
       freetype freetypedevel 
       gmp gmpdevel \
       libmcrypt libmcryptdevel \
       libXslt libXsltdevel \
       libcurl libcurldevel \
       readline xlibs libxslt \
       gd mhash re2c libwebp \
       oniguruma onigdevel \
       ldap ldapdevel \
       mcrypt mhash \
       mbstring libmbfl \
       opcache pdo pdo_mysql pdo_pgsql pdo_sqlite session shmop soap sockets sysvmsg sysvsem sysvshm wddx xmlrpc zip \
       mysqlnd pdo_mysql pdo_sqlite readline xsl zlib \
       pear curl dom ftp filter gd hash iconv fileinfo json ldap libxml mbstring mcrypt mysqli odbc opcache pcntl pcre Phar posix session simplexml soap sockets tokenizer xml xmlreader xmlwriter zip \
       enableinlineoptimization enabledebug withconfigfilepath=/etc withconfigfilescandir=/etc/php.d enablefpm withfpmuser=nginx withfpmgroup=nginx enablesharedmaxmem withthreadsafety withzlibdir=/usr/local/php/lib enablemaintainerzts withbz2 withcurl withenchant withfilepathvalidation withfilter withftp withgd withgettext withgmp withintl withimap withinterbase withldap withlibxmlsource withopenssl withpcreregex withpdo withpdodblib=mysql,sqlite withpdomysql=/usr/local/php/mysql withpdosqlite=/usr/local/php.squashfs withpear withperl withpgsql withpi3web withpopt withreadline withrecode withredis withsnmp withsoap withsockets withsodium withsqlite3 withssl withtidy withtokenizer withxmlreader withxmlwriter withxsl withzip enablezipinline enablembstringall enablembregex withmysqli withmysqlildflags=/usr/local/php/mysql/libmysqlclient.so withpdomysql=/usr/local/php/mysql withpdomysql=/usr/local/php/mysql/mysql_config.h withpdomysql=/usr/local/php/mysql/libmysqlclient.so enablepdosqlite=/usr/local/php/sqlite3.ext withsqlite3=/usr/local/php/sqlite3.ext enablesqlite3intl withsqlite3session withsqlite3compression withsqlite3x withsqlite3xintl withsqlite3xsession withsqlite3xcompression withsqlite3x2 withsqlite3x2intl withsqlite3x2session withsqlite3x2compression with epel all the way down to the bottom of the list and then hit Enter to continue.

4、编译和安装

   make && make install

5、配置环境变量

如何在CentOS 7上成功安装PHP 7?-图2
(图片来源网络,侵权删除)
   vi /etc/profile

在末尾追加:

   PATH=$PATH:/usr/local/php/bin
   export PATH

使更改立即生效:

   source /etc/profile

6、启动PHPFPM服务

   sudo systemctl start phpfpm
   sudo systemctl enable phpfpm

常见问题解答(FAQs)

问题1:如何检查PHP是否成功安装?

答:可以通过命令行输入php v来查看PHP版本信息,如果输出了PHP的版本号,则表示安装成功。

问题2:如何卸载PHP?

如何在CentOS 7上成功安装PHP 7?-图3
(图片来源网络,侵权删除)

答:如果是通过yum安装的,可以使用如下命令卸载:

sudo yum remove php70w

如果是编译安装的,需要手动删除相关文件和目录。

问题3:如何在Nginx中配置PHP支持?

答:在Nginx的配置文件中(通常位于/etc/nginx/nginx.conf),添加或修改以下内容

server {
    listen 80;
    server_name your_domain.com;
    root /var/www/html;
    index index.php index.html index.htm;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

然后重启Nginx服务:

sudo systemctl restart nginx

通过以上步骤,您可以在CentOS 7上顺利安装PHP 7,无论是通过简单的yum安装还是通过编译安装,都能满足不同用户的需求。

分享:
扫描分享到社交APP
上一篇
下一篇