前言
nginx本身不能處理PHP,它只是個web服務器,當接收到請求后,如果是php請求,則發給php解釋器處理,並把結果返回給客戶端。
nginx一般是把請求發fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結果並返回被nginx
這里介紹如何使nginx支持PHP
什么是php-fpm
PHP-FPM是一個PHP FastCGI管理器,是只用於PHP的,可以在 http://php-fpm.org/download下載得到.
PHP-FPM其實是PHP源代碼的一個補丁,旨在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP源代碼中,在編譯安裝PHP后才可以使用。
新版PHP已經集成php-fpm了,不再是第三方的包了,推薦使用。
PHP-FPM提供了更好的PHP進程管理方式,可以有效控制內存和進程、可以平滑重載PHP配置,比spawn-fcgi具有更多優點,所以被PHP官方收錄了。
在./configure的時候帶 –enable-fpm參數即可開啟PHP-FPM,其它參數都是配置php的
安裝php
安裝前的准備
yum -y install gcc automake autoconf libtool make
yum -y install gcc gcc-c++ glibc
yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel sqlite-devel sqlite
//以上已經安裝了可以不用安裝
yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel sqlite-devel sqlite
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar -xvf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4/
./autogen.sh
./configure --prefix=/usr --libdir=/lib64
make && make install
安裝php-fpm
php中文鏡像地址:http://php.p2hp.com/downloads.php
cd /home
wget https://mirrors.sohu.com/php/php-7.4.16.tar.gz
tar -zxvf php-7.4.16.tar.gz
cd php-7.4.16
./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt --enable-mbstring --disable-pdo --with-curl --disable-debug --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli --with-gd --with-jpeg-dir
make all install
安裝完成之后,需要生成php-fpm配置文件
cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
cd /usr/local/php/etc/php-fpm.d
cp www.conf.default www.conf
配置nginx.conf
使nginx將php動態請求發送到PHP解釋器處理
其中server段增加如下配置,否則會出現No input file specified.錯誤
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文件
在/usr/local/nginx/html下創建index.php文件,輸入如下內容
<?php
echo phpinfo();
啟動php-fpm
/usr/local/php/sbin/php-fpm