{"id":4426,"date":"2024-03-21T02:15:00","date_gmt":"2024-03-21T06:15:00","guid":{"rendered":"https:\/\/www.both.org\/?p=4426"},"modified":"2024-03-14T15:21:21","modified_gmt":"2024-03-14T19:21:21","slug":"how-to-generate-webpages-using-cgi-scripts","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=4426","title":{"rendered":"How to generate webpages using CGI scripts"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"4426\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p class=\"has-small-font-size\">Image by: Opensource.com<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Common Gateway Interface provides an easy way to build dynamic websites using any language of your choice.<\/h2>\n\n\n\n<p>Back in the stone age of the Internet when I first created my first business website, life was simple.<\/p>\n\n\n\n<p>I installed Apache and created a few simple HTML pages that stated a few important things about my business and gave important information like an overview of my product and how to contact me. It was a static website because the content seldom changed. Maintenance was simple because of the unchanging nature of my site.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Static content<\/h2>\n\n\n\n<p>Static content is easy and still common. Let&#8217;s take a quick look at a couple sample static web pages. You don&#8217;t need a working website to perform these little experiments. Just place the files in your home directory and open them with your browser. You will see exactly what you would if the file were served to your browser via a web server.<\/p>\n\n\n\n<p>The first thing you need on a static website is the <code>index.html<\/code> file, which is usually located in the <code>\/var\/www\/html <\/code>directory. This file can be as simple as a text phrase such as \u201cHello world\u201d without any HTML markup at all. This would simply display the text string. Create <code>index.html<\/code> in your home directory and add \u201cHello world\u201d (without the quotes) as its only content. Open <code>index.html<\/code> in your browser with the following URL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file:\/\/\/home\/&lt;yourhomedirectory&gt;\/index.html<\/code><\/pre>\n\n\n\n<p>HTML is not required, but if you had a large amount of text that needed formatting, the results of a web page with no HTML coding would be incomprehensible with everything running together.<\/p>\n\n\n\n<p>The next step is to make the content more readable by using a bit of HTML coding to provide some formatting. The following command creates a page with the absolute minimum markup required for a static web page with HTML. You could also use your favorite editor to create the content.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"&lt;h1&gt;Hello World&lt;\/h1&gt;\" &gt; test1.html<\/code><\/pre>\n\n\n\n<p>Now view index.html and see the difference.<\/p>\n\n\n\n<p>Of course you can put a lot of additional HTML around the actual content line to make a more complete and standard web page. That more complete version as shown below will still display the same results in the browser, but it also forms the basis for more standardized web site. Go ahead and use this content for your index.html file and display it in your browser.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE HTML PUBLIC \"-\/\/w3c\/\/DD HTML 4.0\/\/EN\"&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;My Web Page&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;Hello World&lt;\/h1&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>I built a couple static websites using these techniques back in those early days of the Net, but my life was about to change.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dynamic web pages for a new job<\/h2>\n\n\n\n<p>I took a new job in which my primary task was to create and maintain the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Common_Gateway_Interface\" target=\"_blank\" rel=\"noreferrer noopener\">Common Gateway Interface<\/a> (CGI) code for a very dynamic website. In this context, dynamic means that the HTML needed to produce the web page on a browser was generated from data that could be different every time the page was accessed. This includes input from the user on a web form that is used to look up data in a database. The resulting data is surrounded by appropriate HTML and displayed on the requesting browser. But it does not need to be that complex.<\/p>\n\n\n\n<p>Using CGI scripts for a website allows you to create simple or complex interactive programs that can be run to provide a dynamic web page that can change based on input, calculations, current conditions in the server, and so on. There are many languages that can be used for CGI scripts. We will look at two of them: Perl and Bash. Other popular CGI languages include PHP and Python.<\/p>\n\n\n\n<p>This article does not cover installation and setup of Apache or any other web server. If you have access to a web server that you can experiment with, you can directly view the results as they would appear in a browser. Otherwise, you can still run the programs from the command line and view the HTML that would be created. You can also redirect that HTML output to a file and then display the resulting file in your browser.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Perl<\/h2>\n\n\n\n<p>Perl is a very popular language for CGI scripts. Its strength is that it is a very powerful language for the manipulation of text.<\/p>\n\n\n\n<p>To get CGI scripts to execute, you need the following line in the in <code>httpd.conf<\/code> for the website you are using. This tells the web server where your executable CGI files are located. For this experiment, let&#8217;s not worry about that.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ScriptAlias \/cgi-bin\/ \"\/var\/www\/cgi-bin\/\"<\/code><\/pre>\n\n\n\n<p>Add the following Perl code to the file <code>index.cgi<\/code>, and save it in your home directory. Set the ownership of the file to <code>apache.apache<\/code> when you use a web server, and set the permissions to 755 because it must be executable no matter where it is located.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/perl\nprint \"Content-type: text\/html\\n\\n\";\nprint \"&lt;html&gt;&lt;body&gt;\\n\";\nprint \"&lt;h1&gt;Hello World&lt;\/h1&gt;\\n\";\nprint \"Using Perl&lt;p&gt;\\n\";\nprint \"&lt;\/body&gt;&lt;\/html&gt;\\n\";<\/code><\/pre>\n\n\n\n<p>Run this program from the command line and view the results. It displays the HTML code it generates.<\/p>\n\n\n\n<p>Now view the <code>index.cgi<\/code> in your browser. Well, all you get is the contents of the file. Browsers really need to have this delivered as CGI content. Apache does not really know that it needs to run the file as a CGI program unless the Apache configuration for the web site includes the <code>ScriptAlias<\/code> definition as shown above. Without that bit of configuration Apache just sends the data in the file to the browser. If you have access to a web server, you could try this out with your executable index files in the <code>\/var\/www\/cgi-bin<\/code> directory.<\/p>\n\n\n\n<p>To see what this would look like in your browser, run the program again and redirect the output to a new file. Name it whatever you want. Then use your browser to view the file that contains the generated content.<\/p>\n\n\n\n<p>The above CGI program is still generating static content because it always displays the same output. Add the following line to your CGI program immediately after the \u201cHello World\u201d line. The Perl <code>system<\/code> command executes the commands following it in a system shell, and returns the result to the program. In this case, we simply grep the current RAM usage out of the results from the free command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>system \"free | grep Mem\\n\";<\/code><\/pre>\n\n\n\n<p>Now run the program again and redirect the output to the results file. Reload the file in the browser. You see an additional line so that displays the system memory statistics. Run the program and refresh the browser a couple more times and notice that the memory usage should change occasionally.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Bash<\/h2>\n\n\n\n<p>Bash is probably the simplest language of all for use in CGI scripts. Its primary strength for CGI programming is that it has direct access to all of the standard GNU utilities and system programs.<\/p>\n\n\n\n<p>Rename the existing <code>index.cgi<\/code> to <code>Perl.index.cgi<\/code> and create a new <code>index.cgi<\/code> with this content: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\necho \"Content-type: text\/html\"\necho \"\" \necho '&lt;html&gt;'\necho '&lt;head&gt;'\necho '&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\"&gt;'\necho '&lt;title&gt;Hello World&lt;\/title&gt;'\necho '&lt;\/head&gt;'\necho '&lt;body&gt;'\necho '&lt;h1&gt;Hello World&lt;\/h1&gt;&lt;p&gt;'\necho 'Using Bash&lt;p&gt;'\nfree | grep Mem\necho '&lt;\/body&gt;'\necho '&lt;\/html&gt;'\nexit 0<\/code><\/pre>\n\n\n\n<p>Remember to set the permissions correctly to executable.<\/p>\n\n\n\n<p>Execute this program from the command-line and view the output, then run it and redirect the output to the temporary results file you created previously. Refresh the browser to view what it looks like displayed as a web page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>It&#8217;s easy to create CGI programs that can be used to generate a wide range of dynamic web pages. This is a trivial example, but you now see some of the possibilities.&nbsp;&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Common Gateway Interface provides an easy way to build dynamic websites using any language of your choice.<\/p>\n","protected":false},"author":2,"featured_media":2811,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5,315],"tags":[311,313,314],"class_list":["post-4426","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-web-server","tag-apache","tag-cgi","tag-web-server"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/4426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4426"}],"version-history":[{"count":4,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/4426\/revisions"}],"predecessor-version":[{"id":4434,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/4426\/revisions\/4434"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2811"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}