源码安装方式 centos7 nginx php7.3
   1 分钟阅读    邵晨峰

源码安装方式 centos7 nginx php7.3

1.源码安装nginx

这里列出了全部步骤,如果要了解详情,可以查看 源码安装nginx到指定目录

yum install -y libaio ncurses gcc gcc-c++ cmake ncurses-devel wget
yum install -y pcre-devel zlib-devel
wget http://nginx.org/download/nginx-1.16.1.tar.gz # 下载源码包
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/opt/nginx1.16 # 这里指定安装目录
make && make install

2.下载php源码包

前往 https://www.php.net/distributions 下载最新的php源码包

wget https://www.php.net/distributions/php-7.3.13.tar.xz

3.解压、编译、安装

yum install -y libxml2-devel openssl-devel libcurl-devel libjpeg-turbo-devel libpng-devel freetype-devel libzip-devel
tar xvf php-7.3.13.tar.xz
cd php-7.3.13
./configure --prefix=/opt/php7.3 --with-config-file-path=/opt/php7.3/usr --enable-mbstring --with-openssl --enable-ftp --with-gd --with-jpeg-dir=/opt/php7.3/usr --with-png-dir=/opt/php7.3/usr --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pear --enable-sockets --with-freetype-dir=/opt/php7.3/usr --with-zlib --with-libxml-dir=/opt/php7.3/usr --with-xmlrpc --enable-zip --enable-fpm --enable-xml --enable-sockets --with-gd --with-zlib --with-iconv --enable-zip --with-freetype-dir=/opt/php7.3/usr/lib --enable-soap --enable-pcntl --enable-cli --with-curl --enable-bcmath --with-gettext
make && make install

这里使用 --prefix=/opt/php7.3 指定安装在 /opt/php7.3 ,可以自定义安装位置

4.复制php配置文件并启动php-fpm

cp 源码包目录/php.ini-development /opt/php7.3/php.ini
cd /opt/php7.3/etc
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
../sbin/php-fpm -t
../sbin/php-fpm # 启动php-fpm

5.编辑nginx配置文件,使访问php文件的请求发送到php-fpm

vi /opt/nginx1.16/conf/nginx.conf

在server中增加

location ~ \.php$ {
    #root           html;
    fastcgi_pass   127.0.0.1:9000; # 或 unix:/var/run/php5-fpm.sock; 具体看php-fpm.d/www.conf中的配置
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

启动nginx /opt/nginx1.16/sbin/nginx

关闭nginx /opt/nginx1.16/sbin/nginx -s stop

测试

echo "<?php phpinfo(); ?>" > /opt/nginx1.16/html/test.php

浏览器访问 127.0.0.1/test.php 看到php信息成功