Share on Social Media

Learn how to configure FastCGI Server on CentOS 7 with this detailed guide. Follow our step-by-step instructions to set up and optimize your FastCGI server efficiently on CentOS 7. #centlinux #linux #nginx

What is FastCGI?

FastCGI is a protocol for interfacing interactive programs with a web server. It is a variation of the earlier Common Gateway Interface (CGI) but offers significant performance improvements. Here are the key points about FastCGI:

  1. Persistent Processes: Unlike CGI, which starts a new process for each request, FastCGI processes persist between requests. This reduces the overhead of creating and destroying processes, improving performance.
  2. Language Agnostic: FastCGI can be used with a variety of programming languages, including Python, Perl, PHP, and Ruby, making it versatile for different web applications.
  3. Improved Performance: By reusing processes, FastCGI reduces the time required to handle each request, leading to faster response times and better handling of high-traffic situations.
  4. Scalability: FastCGI supports distributed computing, allowing the web server to communicate with FastCGI applications running on different machines. This can improve load balancing and scalability.
  5. Separation of Concerns: FastCGI enables a clear separation between the web server and application logic. This modularity can simplify development, deployment, and maintenance.
  6. Security: By isolating the application logic from the web server, FastCGI can enhance security. Each FastCGI process can run with different permissions, reducing the risk of security vulnerabilities.

FastCGI is widely used to enhance the performance of web applications, particularly those requiring dynamic content generation. Its efficiency and flexibility make it a popular choice for many web developers and system administrators.

Recommended Online Training: Learn Bash Shell in Linux for Beginners

745772 0021

Linux Server Specification

We have provisioned a CentOS 7 minimal install virtual machine with following specifications.

  • CPU – 3.4 Ghz (2 Cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – CentOS 7.6
  • Hostname – nginx-01.example.com
  • IP Address – 192.168.116.197 /24

Install Nginx on CentOS 7

Connect with nginx-01.example.com using ssh as root user.

Nginx is not available in standard yum repositories, therefore, we are installing EPEL (Extra Packages for Enterprise Linux) yum repository.

# yum install -y epel-release

Build cache for EPEL yum repository.

# yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                     | 6.9 kB     00:00
 * base: mirrors.ges.net.pk
 * epel: my.fedora.ipserverone.com
 * extras: mirrors.ges.net.pk
 * updates: mirrors.ges.net.pk
base                                                     | 3.6 kB     00:00
epel                                                     | 5.4 kB     00:00
extras                                                   | 3.4 kB     00:00
updates                                                  | 3.4 kB     00:00
(1/3): epel/x86_64/group_gz                                |  88 kB   00:03
(2/3): epel/x86_64/updateinfo                              | 998 kB   00:07
(3/3): epel/x86_64/primary_db                              | 6.8 MB   01:07
Metadata Cache Created

Install Nginx web server using yum command.

# yum install -y nginx

Enable and start nginx service.

# systemctl enable --now nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

Configure Linux Firewall

Allow HTTP service in Linux firewall.

# firewall-cmd --permanent --add-service=http
success
# firewall-cmd --reload
success

Browse URL http://nginx-01.example.com using a client’s browser.

Nginx Default Homepage

Nginx web server has been installed on CentOS 7.

Configure FastCGI Server on CentOS 7

Unlike Apache HTTP Server, Nginx do not spawn FastCGI processes. Therefore, we required to install spawn-fcgi to process Perl scripts.

# yum install -y spawn-fcgi

Now install fcgiwrap package using yum command.

# yum install -y fcgiwrap

Edit spawn-fcgi configuration file.

# vi /etc/sysconfig/spawn-fcgi

and add following line therein.

OPTIONS="-u nginx -g nginx -a 127.0.0.1 -p 9001 -P /var/run/spawn-fcgi.pid -- /usr/sbin/fcgiwrap"

Enable and start spawn-fcgi service.

# systemctl enable --now spawn-fcgi
spawn-fcgi.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig spawn-fcgi on

Create a directory as Nginx document root.

# mkdir /var/www

Create a sample Perl script in Nginx document root directory.

# vi /usr/share/nginx/html/index.cgi

and add following lines of code.

#!/usr/bin/perl 

print "Content-type: text/htmlnn";
print "<html><body><h1>Hello World!";
print "</h1></body></html>n";

Adjust file permissions for index.cgi.

# chmod +x /usr/share/nginx/html/index.cgi

Add our Nginx server configuration file as follows.

# vi /etc/nginx/default.d/default.conf

and add following directive therein.

index index.cgi;

location ~* .(pl|cgi)$ {
  gzip off;
  include /etc/nginx/fastcgi_params;
  fastcgi_pass   127.0.0.1:9001;
  fastcgi_index  index.cgi;
}

Restart nginx service.

# systemctl restart nginx

Set SELinux boolean, so the Nginx can communicate to spawn-fcgi.

# setsebool -P httpd_can_network_connect on

Browse URL http://nginx-01.example.com/ in a client’s browser.

Hello World Web Page
Hello World Web Page

Our Perl script has been executed successfully. We have successfully configure FastCGI Server using Nginx on CentOS 7.

If you are new to Linux and facing difficulty in working at Linux Bash prompt. We recommend that, you should read The Linux Command Line, 2nd Edition: A Complete Introduction by William Shotts.

Final Thoughts

Thank you for following this guide on how to configure a FastCGI Server on CentOS 7. If you need further assistance or prefer a professional to handle the setup, I offer expert services on Fiverr. Visit my Fiverr profile to hire me for a smooth and efficient FastCGI server configuration. Let me help you optimize your server performance and ensure a seamless setup!

Leave a Reply