20191114 作者:陈文小超
准备工作
- 安装php依赖包
wget https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt
make
make install
# 依赖包
yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
yum install gcc libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
安装PHP
- 下载php包并解压
# 下载地址(https://www.php.net/downloads.php)
cd /usr/local/src
# 在windows上前往php官网下载php-7.2.0包,然后使用rz指令将下载好的php安装包上传到centos7上,解压:
tar -zxvf php-7.2.24.tar.gz
- 编译PHP
报错参考链接 安装上述步骤进行,一般不会出错
–with-config-file-path此配置是php.ini要存放的目录切记
cd /usr/local/src/php-7.2.24
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/conf --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-libxml-dir --with-xmlrpc --with-openssl --with-mcrypt --with-mhash --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache
# 大约需15-20分钟
make
# 进行检验,也可以不执行,大约需要8-10分钟
make test
make install
- make报错 virtual memory exhausted: Cannot allocate memory make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
解决方法参考链接
./configure 执行这一步是添加--disable-fileinfo # 不要采用此方法,会导致后续问题 composer 不能正常使用
- 添加PHP命令到环境变量
vim /etc/profile
# 在文件末尾加入
PATH=$PATH:/usr/local/php/bin
export PATH
# 要使改动立即生效执行
source /etc/profile
# 查看PHP 版本
php -v
这时候,nginx还是不支持php的,它需要php-fpm来处理两者的依赖关系,所以我们要胚子php-fpm
- 配置PHP-fpm
# 创建conf
mkdir /usr/local/php/conf
# php.ini-production此文件就在/usr/local/src/php-7.2.24下面
cp php.ini-production /usr/local/php/conf/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# 注册系统服务并设置开机自启
cp /usr/local/src/php-7.2.24/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
- 启动php-fpm
/etc/init.d/php-fpm start
# 报错
ERROR: [pool www] cannot get uid for user 'nginx'
[14-Nov-2019 03:47:41] ERROR: FPM initialization failed
# 解决方法
useradd nginx
groupadd nginx
usermod -G nginx nginx
启动php-fpm报错解决方法
CentOS7.X中设置nginx和php-fpm的开机自启动
- nginx和php-fpm整合–配置nginx支持php
vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
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;
}
# 切记重启Nginx
- 写一个PHP文件进行检验
<?php
echo phpinfo();
安装结束
参考文献