C++ Server Side Web Development
CGI, the Common Gateway Interface is a simple way to write web applications. It is by no means the fastest and some may say that:
"If you're planning to write your web application in C++, it would be total waste to then interface it as CGI.
My suggestion would be to build it asynchronous using ASIO (Asynchronous I/O). With that you can build blazing fast web service (combine with nginx as a reverse-proxy and statics server for best effects); Combine that with template library like Wt and you're ready to serve tens of thousands request per second from a single server.
Whether this is practical alternative to dynamic language web framework is another issue." vartec 2013
However, it is a simple way of demonstrating server side web development with a number of programming languages, including C++.
Install Apache2
To support websites built with C++ using the CGI requires webserver software capable of (which is all of them) and configured to enable (not all of them) execute code in a cgi directory.
A common webserver choice is Apache2 herein version 2.4 since 2012-02-21 latest 2019-08-14 (2.4.41)
$ sudo apt-get install apache2
Install cURL
cURL (client URL) aka and henceforth curl, is a command line tool for file transfer with a URL syntax. It supports a number of protocols including HTTP, HTTPS, FTP and assuming remote headless server hardware can be used to fetch web pages.
$ sudo apt-get install curl
Usage for to access localhost:
$ curl http://127.0.0.1/
Will print the default Apache2 index.html web page HTML on the screen:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <html> <head> <title>Index of /</title> </head> <body> <h1>Index of /</h1> <table> <tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr> <tr><th colspan="5"><hr></th></tr> <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="cgi-bin/">cgi-bin/</a></td><td align="right">2019-12-08 13:15 </td><td align="right"> - </td><td> </td></tr> <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="html/">html/</a></td><td align="right">2019-12-08 11:52 </td><td align="right"> - </td><td> </td></tr> <tr><th colspan="5"><hr></th></tr> </table> <address>Apache/2.4.29 (Ubuntu) Server at 127.0.0.1 Port 80</address> </body></html>$ curl http://127.0.0.1/
Enable Apache2 CGI module
By default Apache2 does not have its cgi module enabled.
The collection of available modules can be found in:
/etc/apache2/mods-available
Whilst the enabled modules are present as symbolic links to the available modules in:
/etc/apache2/mods-enabled
Enable the cgi module by adding a symbolic link from mods-enabled to mods-available:
$ cd /etc/apache2/mods-enabled $ sudo ln -s ../mods-available/cgi.load
Where to put CGI executables?
Examing the Apache2 configuration files reveals that, in a similar manner to modules, the available to enabled relationship is used:
/etc/apache2/conf-available/serve-cgi-bin.conf
file has a symbolic link from:
/etc/apache2/conf-enabled/serve-cgi-bin.conf
It has a section that maps the /cgi-bin path in the URLs to the /usr/lib/cgi-bin/ directory and enables CGI execution in this directory:
serve-cgi-bin.conf
<IfModule mod_alias.c>
<IfModule mod_cgi.c>
Define ENABLE_USR_LIB_CGI_BIN
</IfModule>
<IfModule mod_cgid.c>
Define ENABLE_USR_LIB_CGI_BIN
</IfModule>
<IfDefine ENABLE_USR_LIB_CGI_BIN>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
</IfDefine>
</IfModule>
Reload Apache configuration
As the configuration of Apache has changed tell Apache to reload its configuration files:
$ sudo /etc/init.d/apache2 restart
Create the first CGI script
With CGI enabled and Apache2 restarted test with a simple Bash scrip:
helloworld.sh
- #!/bin/bash
- printf "Content-type: text/html\n\n"
- printf "Hello World!\n"
Render it executable:
$ sudo chmod +x /usr/lib/cgi-bin/helloworld.sh
Then access it:
$ curl http://127.0.0.1/cgi-bin/hw.sh Hello World!
No comments:
Post a Comment