• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

centos7下部署nginx与php

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

相信读者在看这篇文章之前已经fastcgi,php-fpm有所了解。大概来讲php语言需要fastcgi程序,即php解释器解释,而php解释器需要php-fpm管理器进行调度。
以下对CGI、FastCGI、php-fpm之间关系进行通俗解释(来源于知乎用户Journey Lin):

讲Fastcgi之前需要先讲CGI,CGI是为了保证web server传递过来的数据是标准格式的,它是一个协议,方便CGI程序的编写者。Fastcgi是CGI的更高级的一种方式,是用来提高CGI程序性能的。web server(如nginx)只是内容的分发者。比如,如果请求/index.html,那么web server会去文件系统中找到这个文件,发送给浏览器,这里分发的是静态资源。如果现在请求的是/index.php,根据配置文件,nginx知道这个不是静态文件,需要去找PHP解析器来处理,那么他会把这个请求简单处理后交给PHP解析器。此时CGI便是规定了要传什么数据/以什么格式传输给php解析器的协议。当web server收到/index.php这个请求后,会启动对应的CGI程序,这个程序就是PHP的解析器。接下来PHP解析器会解析php.ini文件,初始化执行环境,然后处理请求,再以CGI规定的格式返回处理后的结果,退出进程。web server再把结果返回给浏览器。那么CGI相较于Fastcgi而言其性能瓶颈在哪呢?CGI针对每个http请求都是fork一个新进程来进行处理,处理过程包括解析php.ini文件,初始化执行环境等,然后这个进程会把处理完的数据返回给web服务器,最后web服务器把内容发送给用户,刚才fork的进程也随之退出。 如果下次用户还请求动态资源,那么web服务器又再次fork一个新进程,周而复始的进行。而Fastcgi则会先fork一个master,解析配置文件,初始化执行环境,然后再fork多个worker。当请求过来时,master会传递给一个worker,然后立即可以接受下一个请求。这样就避免了重复的劳动,效率自然是高。而且当worker不够用时,master可以根据配置预先启动几个worker等着;当然空闲worker太多时,也会停掉一些,这样就提高了性能,也节约了资源。这就是Fastcgi的对进程的管理。大多数Fastcgi实现都会维护一个进程池。注:swoole作为httpserver,实际上也是类似这样的工作方式。
那PHP-FPM又是什么呢?它是一个实现了Fastcgi协议的程序,用来管理Fastcgi起的进程的,即能够调度php-cgi进程的程序。现已在PHP内核中就集成了PHP-FPM,使用--enalbe-fpm这个编译参数即可。另外,修改了php.ini配置文件后,没办法平滑重启,需要重启php-fpm才可。此时新fork的worker会用新的配置,已经存在的worker继续处理完手上的活。

在php5.3.3之前 php-fpm以补丁包的形式存在,而5.3.3 以后将其整合到了php,只消编译安装时带上--enable-fpm 开启该功能。

yum 安装

安装nginx

sudo yum -y install nginx    

启动nginx,在本机浏览器访问nginx页面,检查服务是否启动成功

sudo systemctl start  nginx
访问url:http://ip:80 

安装php与php-fpm

sudo yum -y install php php-fpm

启动php-fpm服务

sudo systemctl start php-fpm

在nginx.conf中添加php的配置

在其http{}的server{}中 root /usr/share/nginx/html行下添加:

sudo  vim /etc/nginx/nginx.conf
    index        index.html index.htm index.php;
    location ~ \.php$ {
       fastcgi_pass   127.0.0.1:9000;           #fastcgi服务端口,将http请求代理到此端口
       fastcgi_index  index.php;                    #fastcgi服务默认页面
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    #设置请求的脚本文件路径
       include        fastcgi_params;        
   }

检查nginx配置,并重新加载配置

nginx -t
sudo systemctl reload  nginx

测试是否能解析php页面

sudo vim /usr/share/nginx/html/info.php
<?php
    phpinfo();
?>

在本机浏览器访问以上页面 http://ip:80/info.php

编译安装

安装前准备

安装一些编译工具和依赖包等

sudo yum -y install gcc automake autoconf libtool make unzip gcc-c++ glibc gd-devel
sudo 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-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

安装nginx

下载官方软件包

wget http://nginx.org/download/nginx-1.4.2.tar.gz

解压

tar -xvf nginx-1.4.2.tar.gz 

创建安装目录

sudo mkdir /usr/local/nginx

编译安装

cd nginx-1.4.2
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx  --conf-path=/usr/local/nginx/conf/nginx.conf --user=nginx --group=nginx
 sudo make && sudo make install

起服务

cd /usr/local/nginx/sbin/
sudo ./nginx

出现 make[1]: Leaving directory `/home/xxx/nginx-1.4.2',不用管。

安装php

下载官方软件包

wget http://cn2.php.net/distributions/php-5.6.39.tar.gz

解压

tar -xvf php-5.6.39.tar.gz 

创建安装目录

sudo mkdir /usr/local/php

编译安装

./configure --prefix=/usr/local/php  \
--with-config-file-path=/usr/local/php/etc \
--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
sudo make all install

进行php-fpm的用户设置

sudo groupadd www
sudo useradd www -g www -s /sbin/nologin
cd /usr/local/php
sudo cp etc/php-fpm.conf.default etc/php-fpm.conf
sudo vi etc/php-fpm.conf   #修改以下两个参数
user = www
group = www

起服务

sudo  /usr/local/php/sbin/php-fpm    

在nginx.conf进行php的为配置

在http{}的server{}里添加

 vim /usr/local/nginx/conf/nginx.conf
    index        index.html index.htm index.php;
    location ~ \.php$ {
             fastcgi_pass   127.0.0.1:9000;           #fastcgi服务端口,将http请求代理到此端口
             fastcgi_index  index.php;                    #fastcgi服务默认页面
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    #设置请求的脚本文件路径
             include        fastcgi_params;
             }

添加php测试页面

vim  /usr/local/nginx/html/index.php
<?php
    phpinfo();
?>    

重启nginx服务

cd /usr/local/nginx/sbin/
sudo ./nginx

测试:在本机上访问该页面

http://ip:80/index.php


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Linux下php调用命令行的小研究发布时间:2022-07-12
下一篇:
jQuery通过Ajax向PHP服务端发送请求并返回JSON数据发布时间:2022-07-12
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap