How do I configure Apache for Python CGI programming?

For your server to run CGI scripts correctly, you must configure your web server. We will discuss how to configure your Apache web server to run CGI scripts.

Using ScriptAlias ​​

You can set the directory as a ScriptAlias ​​directive (options for configuring Apache). Thus, Apache understands that all files in this directory are CGI scripts. This may be the easiest way to run CGI scripts on Apache. A typical ScriptAlias ​​line looks like this in the httpd.conf file of your Apache web server.

 ScriptAlias ​​/ cgi-bin / / usr / local / apache2 / cgi-bin / 

So, look for ScriptAlias ​​in your httpd.conf file and uncomment the line (remove the previous #) if you want to keep all your CGI files in the default directory specified by Apache. But this may not always satisfy your requirements. So we`ll look at some other options for running Python as a CGI.

Launch CGI from a specific a directory other than the default directory.

You can use the following to prepare a specific directory to run CGI files.

 & lt; Directory / usr / local / apache2 / htdocs / somedir & gt; Options + ExecCGI & lt; / Directory & gt; 

Where "somedir" &this is the directory of your preferences. 

If you are using the above configuration. then you must also specify the following to tell the server-side extensions of the CGI files you are running.

 AddHandler cgi-script .cgi .pl 

So the above tells Apache to run .cgi and .pl files as CGI

< p> Custom Directories

If you want to run CGI files from a user directory, you can use the following:

 & lt; Directory / home / * / public_html & gt; Options + ExecCGI AddHandler cgi-script .cgi & lt; / Directory & gt; 

The above says that Apache runs all .cgi files presented as CGI in the user`s directory.

Again if you want to run all files located in the user directory as CGI, then you can use the following:

 & lt; Directory / home / * / public_html / cg-bin & gt; Options + ExecCGI SetHandler cgi-script & lt; / Directory & gt; Using .htaccess 

If you don`t have access to the httpd.conf file, you can use the .htaccess file to run CGI scripts. To use files with specific extensions as CGIs, configure your .htaccess file as follows:

 Options + ExecCGI AddHandler cgi-script cgi pl  

If you want to run all files in a directory as CGI, you can use the following:

 Options + ExecCGI SetHandler cgi -script 


Get Solution for free from DataCamp guru