在 CentOS 7 上安装 spawn-fcgi
与 Apache HTTP Server 不同,Nginx 不会产生 FastCGI 进程。
因此,我们需要安装 spawn-fcgi 来处理 Perl 脚本。
[root@nginx-01 ~]# yum install -y spawn-fcgi
现在使用 yum 命令安装 fcgiwrap 包。
[root@nginx-01 ~]# yum install -y fcgiwrap
编辑 spawn-fcgi 配置文件。
[root@nginx-01 ~]# vi /etc/sysconfig/spawn-fcgi
并其中添加以下行。
OPTIONS="-u nginx -g nginx -a 127.0.0.1 -p 9001 -P /var/run/spawn-fcgi.pid -- /usr/sbin/fcgiwrap"
启用并启动 spawn-fcgi 服务。
[root@nginx-01 ~]# systemctl enable --now spawn-fcgi spawn-fcgi.service is not a native service, redirecting to /sbin/chkconfig. Executing /sbin/chkconfig spawn-fcgi on
创建一个目录作为 Nginx 文档根目录。
[root@nginx-01 ~]# mkdir /var/www
在 Nginx 文档根目录中创建一个示例 Perl 脚本。
[root@nginx-01 ~]# vi /usr/share/nginx/html/index.cgi
并添加以下代码行。
#!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html><body><h1>Hello World!"; print "</h1></body></html>\n";
调整 index.cgi 的文件权限。
[root@nginx-01 ~]# chmod +x /usr/share/nginx/html/index.cgi
添加我们的 Nginx 服务器配置文件,如下所示。
[root@nginx-01 ~]# vi /etc/nginx/default.d/default.conf
并其中添加以下指令。
index index.cgi; location ~* \.(pl|cgi)$ { gzip off; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9001; fastcgi_index index.cgi; }
重启nginx服务。
[root@nginx-01 ~]# systemctl restart nginx
设置 SELinux 布尔值,以便 Nginx 可以与 spawn-fcgi 通信。
[root@nginx-01 ~]# setsebool -P httpd_can_network_connect on
在客户端浏览器中打开 URL http://nginx-01.onitroad.com/。
我们的Perl脚本已成功执行。我们已经在CentOS 7服务器上使用Nginx成功配置了Perl FastCGI。
Nginx Web 服务器不支持 Perl 语言。
因此,我们必须使用 Perl-FastCGI 进程管理器在我们的 Nginx Web 服务器中添加 Perl 语言支持。
在本文中,我们将在 CentOS 7 上配置 Nginx Web 服务器,并使用 spawn-fcgi 包添加 Perl-FastCGI 支持。
在 CentOS 7 上安装 Nginx
Nginx 在标准 yum 存储库中不可用,因此,我们正在安装 EPEL(Extra Packages for Enterprise Linux- 企业 Linux 的另外包)yum 存储库。
[root@nginx-01 ~]# yum install -y epel-release
为 EPEL yum 存储库构建缓存。
[root@nginx-01 ~]# yum makecache fast
使用 yum 命令安装 Nginx Web 服务器。
[root@nginx-01 ~]# yum install -y nginx
启用并启动 nginx 服务。
[root@nginx-01 ~]# systemctl enable --now nginx.service Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
在 Linux 防火墙中允许 HTTP 服务。
[root@nginx-01 ~]# firewall-cmd --permanent --add-service=http success [root@nginx-01 ~]# firewall-cmd --reload success