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

Centos7下yum安装配置nginx与php

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

开始安装Nginx和PHP-FPM之前,首先卸载系统中以前安装的Apache和PHP保证安装不会冲突。用root登录输入下面的命令:

[plain] view plain copy
  1. yum remve httpd* php*  
增加额外nginx php最新资源库

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm


    默认情况下,CentOS的官方资源是没有php-fpm的, 但我们可以从Remi的RPM资源中获得,它依赖于EPEL资源。我们可以这样增加两个资源库:

1:安装nginx:

[plain] view plain copy
  1. yum install nginx  

安装完成后可以启动nginx,在浏览器里面访问,查看nginx是否安装成功。端口默认为80。

[plain] view plain copy
  1. systemctl start nginx  
  2. nginx中yum安装的默认网站根目录在/usr/share/nginx/html  
结果如下:

表示已成功安装nginx.

2:安装PHP和PHP-FPM:

[plain] view plain copy
  1. yum install php php-fpm  
[plain] view plain copy
  1. 启动php-fpm  
  2. systemctl start php-fpm  

3:将PHP与mysql模块关联起来:

数据库再变不在安装。
[plain] view plain copy
  1. 可以通过yum install mariadh mariadb-server 安装  

[plain] view plain copy
  1. yum install php-gd php-mysql php-mbstring php-xml php-mcrypt  php-imap php-odbc php-pear php -xmlrpc  

4:配置nginx与php一起工作:


Nginx+FastCGI运行原理
Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket,(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接纳到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运作过程。详细的过程,如下图所示:

打开nginx主配置文件。

vim /etc/nginx/nginx.conf

[plain] view plain copy
  1. 在http模块中添加配置:  
  2.        location / {  
  3.         root   /usr/share/nginx/html;  
  4.            index  index.html index.htm index.php;  
  5.         }  
  6. location ~ \.php$ {  
  7.            root           html;  
  8.            fastcgi_pass   127.0.0.1:9000;  
  9.            fastcgi_index  index.php;  
  10.            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
  11.            include        fastcgi_params;  
  12.        }  

5:测试nginx与php是否正常。

重启nginx服务器,在网站根目录创建一个index.php文件

[plain] view plain copy
  1. # vi /usr/share/nginx/html/info.php    
文件内容如下:

[plain] view plain copy
  1. <?php    
  2. phpinfo();    
  3. ?>    



可以看到我们的php文件可以加载出来了。此时我们的nginx已经与php关联可以共同工作了。即LNMP环境搭建完毕。

开始安装Nginx和PHP-FPM之前,首先卸载系统中以前安装的Apache和PHP保证安装不会冲突。用root登录输入下面的命令:

[plain] view plain copy
  1. yum remve httpd* php*  
增加额外nginx php最新资源库

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm


    默认情况下,CentOS的官方资源是没有php-fpm的, 但我们可以从Remi的RPM资源中获得,它依赖于EPEL资源。我们可以这样增加两个资源库:

1:安装nginx:

[plain] view plain copy
  1. yum install nginx  

安装完成后可以启动nginx,在浏览器里面访问,查看nginx是否安装成功。端口默认为80。

[plain] view plain copy
  1. systemctl start nginx  
  2. nginx中yum安装的默认网站根目录在/usr/share/nginx/html  
结果如下:

表示已成功安装nginx.

2:安装PHP和PHP-FPM:

[plain] view plain copy
  1. yum install php php-fpm  
[plain] view plain copy
  1. 启动php-fpm  
  2. systemctl start php-fpm  

3:将PHP与mysql模块关联起来:

数据库再变不在安装。
[plain] view plain copy
  1. 可以通过yum install mariadh mariadb-server 安装  

[plain] view plain copy
  1. yum install php-gd php-mysql php-mbstring php-xml php-mcrypt  php-imap php-odbc php-pear php -xmlrpc  

4:配置nginx与php一起工作:


Nginx+FastCGI运行原理
Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket,(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接纳到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运作过程。详细的过程,如下图所示:

打开nginx主配置文件。

vim /etc/nginx/nginx.conf

[plain] view plain copy
  1. 在http模块中添加配置:  
  2.        location / {  
  3.         root   /usr/share/nginx/html;  
  4.            index  index.html index.htm index.php;  
  5.         }  
  6. location ~ \.php$ {  
  7.            root           html;  
  8.            fastcgi_pass   127.0.0.1:9000;  
  9.            fastcgi_index  index.php;  
  10.            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
  11.            include        fastcgi_params;  
  12.        }  

5:测试nginx与php是否正常。

重启nginx服务器,在网站根目录创建一个index.php文件

[plain] view plain copy
  1. # vi /usr/share/nginx/html/info.php    
文件内容如下:

[plain] view plain copy
  1. <?php    
  2. phpinfo();    
  3. ?>    



可以看到我们的php文件可以加载出来了。此时我们的nginx已经与php关联可以共同工作了。即LNMP环境搭建完毕。

开始安装Nginx和PHP-FPM之前,首先卸载系统中以前安装的Apache和PHP保证安装不会冲突。用root登录输入下面的命令:

[plain] view plain copy
  1. yum remve httpd* php*  
增加额外nginx php最新资源库

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm


    默认情况下,CentOS的官方资源是没有php


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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