Red Hat/CentOS安装Nginx PHP5 FastCGI Web服务器

时间:2020-01-09 10:43:24  来源:igfitidea点击:

如何在Red Hat/RHEL/Fedora/CentOS Linux下安装和配置ngnix FastCGI php5 HTTP/Web服务器?

Nginx(引擎x)是由Igor Sysoev编写的HTTP(S)服务器,反向代理和IMAP/POP3代理服务器。
它以其高性能,稳定性,丰富的功能集,简单的配置和低资源消耗而著称。

步骤1:启用EPEL REPO

ngnix不包含在基本系统中。
打开EPEL repo以安装nginx稳定版本:

# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/$(uname -m)/epel-release-5-3.noarch.rpm

步骤2:安装ngnix

在shell提示符下执行以下命令:

# yum install nginx

输出示例:

Loaded plugins: downloadonly, fastestmirror, priorities, protectbase
Loading mirror speeds from cached hostfile
* epel: archive.linux.duke.edu
* base: ftp.linux.ncsu.edu
* updates: centos.mirror.nac.net
* addons: mirror.cs.vt.edu
* extras: centos.mirror.nac.net
0 packages excluded due to repository protections
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:0.6.34-1.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================
Package                             Arch                                 Version                                    Repository                          Size
==============================================================================================================================================================
Installing:
nginx                               x86_64                               0.6.34-1.el5                               epel                               319 k

Transaction Summary
==============================================================================================================================================================
Install      1 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)         

Total size: 319 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : nginx                                             [1/1] 

Installed: nginx.x86_64 0:0.6.34-1.el5
Complete!

nginx配置文件

  • 默认配置文件:/etc/nginx/nginx.conf
  • 默认的SSL配置文件:/etc/nginx/conf.d/ssl.conf
  • 默认虚拟主机配置文件:/etc/nginx/conf.d/virtual.conf
  • 默认文档根目录:/usr/share/nginx/html

将PHP配置为FastCGI

输入以下内容以将php5与其他模块一起安装:

# yum install php-pear-Net-Socket php-pear php-common php-gd php-devel php php-mbstring php-pear-Mail php-cli php-imap php-snmp php-pdo php-xml php-pear-Auth-SASL php-ldap php-pear-Net-SMTP php-mysql

安装用于生成FastCGI进程的spawn-fcgi简单程序

执行以下命令:

# yum install spawn-fcgi

接下来,下载spawn-fcgi init.d shell脚本:

# wget http://bash.theitroad.local/dl/419.sh.zip
# unzip 419.sh.zip
# mv 419.sh /etc/init.d/php_cgi
# chmod +x /etc/init.d/php_cgi

启动php应用服务器,执行:

# /etc/init.d/php_cgi start
# netstat -tulpn | grep :9000

输出示例:

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      14294/php-cgi

默认情况下,php服务器在127.0.0.1:9000端口上侦听。
最后,如下更新/etc/nginx/nginx.conf:

# vi /etc/nginx/nginx.conf

修改/追加如下:

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

保存并关闭文件。
重新启动nginx:

# service nginx restart

如下创建/usr/share/nginx/html/test.php:

<?php
     phpinfo();
?>