User Tools

Site Tools


programing:cpp_fastcgi

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

programing:cpp_fastcgi [2025/12/17 01:39] – created mlivolsiprograming:cpp_fastcgi [2025/12/17 14:07] (current) mlivolsi
Line 20: Line 20:
 In regular CGI, a request comes in. One of the web servers forks a new process, which then becomes it’s processing and returns the response. The forking and then mutating into the process (fork-exec is an interesting side conversation) is a time consuming process and doesn’t scale well. In regular CGI, a request comes in. One of the web servers forks a new process, which then becomes it’s processing and returns the response. The forking and then mutating into the process (fork-exec is an interesting side conversation) is a time consuming process and doesn’t scale well.
  
-Added Read: All About fork/exec.+Added Read: [[https://en.wikipedia.org/wiki/Fork%E2%80%93exec#:~:text=When%20a%20process%20forks%2C%20a,0%20to%20the%20child%20process| All About fork/exec.]]
  
 ==== FastCGI ==== ==== FastCGI ====
Line 34: Line 34:
     * apt-get install nginxsudo      * apt-get install nginxsudo 
     * apt-get install curl     * apt-get install curl
 +    * The Gnu C++ compiler
 +
 +
 +==== Configuring the Web Server ====
 +
 +**Create a Directory**
  
-The Gnu C++ compiler 
-Configuring the Web Server 
-Create a Directory 
 First, create a directory for your fastCGI binary. It should be under your DOCUMENT_ROOT directory. First, create a directory for your fastCGI binary. It should be under your DOCUMENT_ROOT directory.
-Ie. For me, my site was located under /var/www/mikelivolsi.com/public_html, so I created a directory: +For me, my site was located under /var/www/mikelivolsi.com/public_html, so I created a directory: 
-mkdir /var/www/mikelivolsi.com/public_html/fcgichown www-data:www-data /var/www/mikelivolsi.com/public_html/fcgi+  mkdir /var/www/mikelivolsi.com/public_html/fcgi 
 +  * chown www-data:www-data /var/www/mikelivolsi.com/public_html/fcgi 
 + 
 +**Configuring NGINX Config**
  
-Configuring NGINX Config 
 On Ubuntu (this should run on releases 18 and up), just add the following to any of your configuration files under /etc/nginx/sites-available, under the server bracket.  On Ubuntu (this should run on releases 18 and up), just add the following to any of your configuration files under /etc/nginx/sites-available, under the server bracket. 
 +
 +<code>
 server {      …       server {      …      
-location /fcgi/ {     gzip off;     fastcgi_pass 127.0.0.1:8000;     include fastcgi_params; }+location /fcgi/ {      
 +    gzip off;      
 +    fastcgi_pass 127.0.0.1:8000;      
 +    include fastcgi_params;  
 + }
 # Other listening stuff # Other listening stuff
 +</code>
 +
 +Restart nginx: <code> sudo systemctl restart nginx</code>
 +
 +**Explanation**
 +
 +  * Location is where you will place your fastCGI scripts.
 +  * The 127.0.0.1:8000 is how the web server will connect to the 
 +  * fastCGI program that will listen on port 8000
 +  * Fastcgi_params come with nginx/ubuntu and have all the goodness to make your fastCGI stuff work
  
-Restart nginx: sudo systemctl restart nginx 
-Explanation 
- Location is where you will place your fastCGI scripts. 
-The 127.0.0.1:8000 is how the web server will connect to the fastCGI program that will listen on port 8000 
-Factcgi_params come with nginx/ubuntu and have all the goodness to make your fastCGI stuff work 
 You are now done with the NGINX webserver. You are now done with the NGINX webserver.
-Getting ready to Test + 
-You’re going to want to test POST and GET form submission. So for the POST, let’s create a basic page that will submit a form. Place this under any place you store your HTML+**Getting ready to Test** 
 + 
 +You’re going to want to test POST and GET form submission. So for the POST, let’s create a basic page that will submit a form. Place this under any place you store your HTML.
  
 <code> <code>
Line 70: Line 88:
 </code> </code>
  
-NOTE: I called this fast.html+**NOTE:** I called this fast.html 
 Hmmm.. it looks like I’m going to call a program called “fast.cgi” under the fcgi directory Hmmm.. it looks like I’m going to call a program called “fast.cgi” under the fcgi directory
-Create Compile Scripts+ 
 +**Create Compile Scripts** 
 Before we get to coding, let’s set up the compile scripts. Before we get to coding, let’s set up the compile scripts.
 The program will create an object file whose purpose is to break up the query string into name/value pairs. This will be helpful later on in your code to find out the values for each field in your HTML form. The program will create an object file whose purpose is to break up the query string into name/value pairs. This will be helpful later on in your code to find out the values for each field in your HTML form.
-g++ common.cpp -c -Wall  -Wno-deprecatedmv common.o ./objects+<code> g++ common.cpp -c -Wall  -Wno-deprecatedmv common.o ./objects</code> 
 + 
 +**The meat and potatoes program.** 
  
-The meat and potatoes program. This will compile the fast.cpp into fast.cgi using the object file above.  If everything is cool, it will spawn the process (here it’s port 8000, but can be anything) and wait for incoming connections from NGINX.+This will compile the fast.cpp into fast.cgi using the object file above.  If everything is cool, it will spawn the process (here it’s port 8000, but can be anything) and wait for incoming connections from NGINX.
  
 <code> <code>
programing/cpp_fastcgi.1765935562.txt.gz · Last modified: by mlivolsi