CentOS 6通过YUM安装Nginx和PHP-FPM

分类:CentOS教程 阅读:81305 次

开始安装Nginx和PHP-FPM之前,你必须卸载系统中以前安装的Apache和PHP。用root登录输入下面的命令:

  1. #yumremovehttpd*php*
增加额外资源库

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

  1. #yuminstallyum-priorities-y
  2. #rpm-Uvhhttp://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm
  3. #rpm-Uvhhttp://rpms.famillecollet.com/enterprise/remi-release-6.rpm

输出样例
  1. Retrievinghttp://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
  2. warning:/var/tmp/rpm-tmp.00kiDx:HeaderV3RSA/SHA256Signature,keyID0608b895:NOKEY
  3. Preparing...###########################################[100%]
  4. 1:epel-release###########################################[100%]

安装Nginx

输入下列命令

  1. #yuminstallnginx
输出样例
  1. DependenciesResolved
  2. ================================================================================
  3. PackageArchVersionRepositorySize
  4. ================================================================================
  5. Installing:
  6. nginxx86_640.8.54-1.el6epel358k
  7. Installingfordependencies:
  8. GeoIPx86_641.4.8-1.el6epel620k
  9. fontconfigx86_642.8.0-3.el6base186k
  10. freetypex86_642.3.11-6.el6_1.8updates358k
  11. gdx86_642.0.35-10.el6base142k
  12. libX11x86_641.3-2.el6base582k
  13. libX11-commonnoarch1.3-2.el6base188k
  14. libXaux86_641.0.5-1.el6base22k
  15. libXpmx86_643.5.8-2.el6base59k
  16. libjpegx86_646b-46.el6base134k
  17. libpngx86_642:1.2.46-1.el6_1base180k
  18. libxcbx86_641.5-1.el6base100k
  19. libxsltx86_641.1.26-2.el6base450k
  20. perlx86_644:5.10.1-119.el6_1.1base10M
  21. perl-Module-Pluggablex86_641:3.90-119.el6_1.1base37k
  22. perl-Pod-Escapesx86_641:1.04-119.el6_1.1base30k
  23. perl-Pod-Simplex86_641:3.13-119.el6_1.1base209k
  24. perl-libsx86_644:5.10.1-119.el6_1.1base575k
  25. perl-versionx86_643:0.77-119.el6_1.1base49k
  26. TransactionSummary
  27. ================================================================================
  28. Install19Package(s)
  29. Upgrade0Package(s)
  30. Totaldownloadsize:14M
  31. Installedsize:47M
  32. Isthisok[y/N]:y

如果你想在系统启动时自动运行nginx,输入下列命令:
  1. #chkconfig--level345nginxon

第一次启动nginx,输入下列命令:
  1. #/etc/init.d/nginxstart

输出样例
  1. Startingnginx:[OK]
安装PHP-FPM

输入下列命令:

  1. #yum--enablerepo=remiinstallphpphp-fpm

输出样例
  1. DependenciesResolved
  2. ====================================================================================
  3. PackageArchVersionRepositorySize
  4. ====================================================================================
  5. Installing:
  6. phpx86_645.3.10-2.el6.remiremi2.3M
  7. php-fpmx86_645.3.10-2.el6.remiremi1.1M
  8. Installingfordependencies:
  9. aprx86_641.3.9-3.el6_1.2base123k
  10. apr-utilx86_641.3.9-3.el6_0.1base87k
  11. apr-util-ldapx86_641.3.9-3.el6_0.1base15k
  12. httpdx86_642.2.15-15.el6.centos.1updates813k
  13. httpd-toolsx86_642.2.15-15.el6.centos.1updates70k
  14. libeditx86_642.11-4.20080712cvs.1.el6base74k
  15. mailcapnoarch2.1.31-2.el6base27k
  16. php-clix86_645.3.10-2.el6.remiremi2.2M
  17. TransactionSummary
  18. ====================================================================================
  19. Install10Package(s)
  20. Upgrade0Package(s)
  21. Totaldownloadsize:6.8M
  22. Installedsize:21M
  23. Isthisok[y/N]:y

如果你想在系统启动时自动运行php-fpm,输入下列命令:
  1. #chkconfig--level345php-fpmon

PHP仅安装了核心模块,你很可能需要安装其他的模块,比如MySQL、 XML、 GD等等,你可以输入下列命令:
  1. #yum--enablerepo=remiinstallphp-gdphp-mysqlphp-mbstringphp-xmlphp-mcrypt

第一次启动php-fpm,输入下列命令:
  1. #/etc/init.d/php-fpmrestart

输出样例
  1. Startingphp-fpm:[OK]

配置PHP-FPM和Nginx,让他们一起工作

  1. #vi/etc/nginx/nginx.conf
像下面这样编辑取消注释:
  1. ...
  2. location/{
  3. root/usr/share/nginx/html;
  4. indexindex.htmlindex.htmindex.php;
  5. }
  6. ...
  7. location~\.php${
  8. roothtml;
  9. fastcgi_pass127.0.0.1:9000;
  10. fastcgi_indexindex.php;
  11. fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
  12. includefastcgi_params;
  13. }
  14. ...
重启Nginx会重新读取配置文件,输入
  1. #/etc/init.d/nginxreload
现在在document root目录下建立下列PHP文件
  1. #vi/usr/share/nginx/html/info.php

文件内容如下:
  1. <?php
  2. phpinfo();
  3. ?>

访问http://YOUR-SERVER-IP

CentOS 6通过YUM安装Nginx和PHP-FPM

Nginx虚拟主机设置

设置例子

IP: 192.168.1.113
Domain: domain.local
Hosted at: /home/www/domain.local

输入下列命令新建名叫“www”的用户

  1. #useraddwww
创建必要的目录
  1. #mkdir-p/home/www/domain.local/public_html
  2. #mkdir-p/home/www/domain.local/log
  3. #chown-Rwww.www/home/www/
  4. #chmod755/home/www/

创建虚拟主机配置文件
  1. #cd/etc/nginx/conf.d/
  2. #cpvirtual.confwww.conf
输入下面命令打开www.conf文件
  1. #vi/etc/nginx/conf.d/www.conf
增加以下配置
  1. server{
  2. server_namedomain.local;
  3. root/home/www/domain.local/public_html;
  4. access_log/home/www/domain.local/log/domain.local-access.log;
  5. error_log/home/www/domain.local/log/domain.local-error.log;
  6. location/{
  7. indexindex.htmlindex.htmindex.php;
  8. }
  9. location~\.php${
  10. include/etc/nginx/fastcgi_params;
  11. fastcgi_pass127.0.0.1:9000;
  12. fastcgi_indexindex.php;
  13. fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
  14. }
  15. }
你可以用下列方法检查配置文件是否有语法错误
  1. #/etc/init.d/nginxconfigtest
输出样例
  1. theconfigurationfile/etc/nginx/nginx.confsyntaxisok
  2. configurationfile/etc/nginx/nginx.conftestissuccessful

现在编辑/etc/php-fpm.d/www.conf文件,将运行php-fpm进程的用户改为“www”,输入
  1. #vi/etc/php-fpm.d/www.conf
找到“group of processes”,编辑成下面的样子:
  1. ;Unixuser/groupofprocesses
  2. ;Note:Theuserismandatory.Ifthegroupisnotset,thedefaultuser'sgroup
  3. ;willbeused.
  4. ;RPM:apacheChoosedtobeabletoaccesssomedirashttpd
  5. user=www
  6. ;RPM:Keepagroupallowedtowriteinlogdir.
  7. group=www
最后重启nginx
  1. #/etc/init.d/nginxrestart
  2. #/etc/init.d/php-fpmrestart
原文地址:http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/