5.nginx学习笔记:nginx+php

左手的ㄟ右手 2022-07-15 02:18 261阅读 0赞

nginx学习笔记:nginx+php


nginx连接php可以使nginx支持更多的应用。接下来我们搭建nginx和php
之间的架构。


安装前准备

(1)php的安装需要多种组件,使用yum安装:

  1. [root@rsync ~]# yum install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel freetype-devel -y

(2)在官方网站下载php5.5的最新版本php5.5.38:

http://php.net/downloads.php

(3)确保nginx已经源码编译安装,笔者将nginx安装在/usr/local/nginx目录


安装步骤

(1)解压php源码包:

  1. [root@rsync php]# tar xvf php-5.5.38.tar.gz

(2)进入安装目录进行配置选择:

  1. [root@rsync php]# cd php-5.5.38
  2. [root@rsync php-5.5.38]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local/ --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --enable-dom --enable-xml --enable-fpm --with-libdir=lib64

这里选择了安装目录为/usr/local/php,–with-和–enable-都是php支持的各种模块。读者可以通过输入 “./configure –help”选择自己需要的模块进行配置。如果上述操作报错,应该是缺少相关库的安装,使用yum命令安装即可。

(3)配置完后进行编译安装:

  1. [root@rsync php-5.5.38]# make && make install

(4)编译完成后再/usr/local/php目录下将会得到php的各种组件,拷贝配置文件:

  1. //这个是php的配置文件模板
  2. [root@rsync php-5.5.38]# cp php.ini-production /usr/local/php/etc/php.ini
  3. //这个是php-fpm的配置文件
  4. [root@rsync php-5.5.38]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

(5)将php-fpm的启动脚本拷贝到/etc/init.d/目录中:

  1. [root@rsync fpm]# cp /php/php-5.5.38/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

(6)启动php-fpm服务并且查看端口:

  1. [root@rsync fpm]# /etc/init.d/php-fpm start
  2. [root@rsync fpm]# netstat -anutlp | grep 9000
  3. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2700/php-fpm

看到9000端口,说明php-fpm已经开启!


nginx中添加php-fpm

这需要我们修改nginx的配置文件nginx.conf:

  1. [root@rsync conf]# vim /usr/local/nginx/conf/nginx.conf

在默认server的location中添加php的设置:

  1. location ~ \.php$ { root html; #默认路径 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #默认首页 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #include fastcgi_params; include fastcgi.conf; #使用fastcgi.conf作为配置文件 }

上述配置中的fastcgi_params也是fastcgi.conf的配置文件,但是fastcgi.conf更新一些,他比fastcgi_params多了如下内容:

  1. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #脚本文件请求的路径

在nginx的默认发布目录/usr/local/nginx/html下添加新的php文件:

  1. [root@rsync html]# cat index.php
  2. <?php phpinfo(); ?>

启动nginx服务并查看端口:

  1. [root@rsync conf]# /usr/local/nginx/sbin/nginx
  2. [root@rsync conf]# netstat -anutlp |grep 80
  3. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2776/nginx

使用浏览器访问默认该节点的nginx服务:
这里写图片描述

访问成功说明nginx已经支持了php应用。


发表评论

表情:
评论列表 (有 0 条评论,261人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Nginx学习笔记

    公司产品出现瓶颈? 我们公司项目刚刚上线的时候,并发量小,用户使用的少,所以在低并发的情况下,一个jar包启动应用就够了,然后内部tomcat返回内容给用户。 ![6

    相关 nginx学习笔记

    目录 1.1. 要求的安装环境 1.2. 安装步骤 1.3. 启动nginx 五、通过域名区分虚拟主机 1.1.1. 什么是域名 9.1. 什么是负载均衡高可用

    相关 Nginx学习笔记

    下载nginx后文件放置 ![解压安装后生成这样一个文件,我将打包后的项目文件(生成的dist文件)放在了html文件夹中,nginx默认的是80端口,所以点击文件中的n

    相关 Nginx学习笔记

    一、基本概念 正向代理: 在客户端(浏览器)里配置代理服务器,通过代理服务器进行互联网的访问就叫正向代理 反向代理: 我们只需要将请求发送到反向代理服务器,由反向代