天天看點

Linux-安裝-PHP

20191114 作者:陳文小超

準備工作

  1. 安裝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

  1. 下載下傳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
           
  1. 編譯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 不能正常使用
           
  1. 添加PHP指令到環境變量
vim  /etc/profile

# 在檔案末尾加入
PATH=$PATH:/usr/local/php/bin
export PATH

# 要使改動立即生效執行
source /etc/profile

# 檢視PHP 版本
php -v

           
這時候,nginx還是不支援php的,它需要php-fpm來處理兩者的依賴關系,是以我們要胚子php-fpm
  1. 配置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
           
  1. 啟動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的開機自啟動

  1. 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
           
  1. 寫一個PHP檔案進行檢驗
<?php
echo phpinfo();
           

安裝結束

參考文獻

  • LiNUX安裝PHP7.X
  • 啟動php-fpm 報錯ERROR: pool www cannot get uid for user ‘nginx’
  • CentOS7.X中設定nginx和php-fpm的開機自啟動