Red Hat / CentOS 安装 nginx PHP5 FastCGI Web 服务器
如何在 Red Hat / RHEL / Fedora / CentOS Linux 下安装和配置 ngnix FastCGI php5 HTTP / web 服务器?
Nginx (engine 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
安装 spawn-fcgi 简单程序来生成 FastCGI 进程
键入以下命令:
# yum install spawn-fcgi
接下来,下载 spawn-fcgi init.d shell 脚本:
启动 php 应用服务器,输入:
示例输出:
# wget http://bash.example.com/dl/419.sh.zip
# unzip 419.sh.zip
# mv 419.sh /etc/init.d/php_cgi
# chmod +x /etc/init.d/php_cgi
# /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(); ?>