• su to become root
  • Create the following directory structure
    • /var/www/mysite
      • cgi-bin
        • index.py
      • html
        • index.html
  • Sample content for index.py
    #!/usr/bin/python

    print(“Content-type: text/html\n”)
    print(“””
    <html>
    <body>
    Hello Python CGI <br/><a href=’../’>Home</a>
    </body>
    </html>
    “””)

  • Sample content for index.html
    <html>
    <body>
    Home <br/>
    <a href=’cgi-bin/index.py’>CGI</a>
    </body>
    </html>
  • If you use SQLite, create a sample database file.
  • edit /etc/httpd/conf/httpd.conf, add the following code to the end of the file
    ScriptAlias /mysite/cgi-bin/ /var/www/mysite/cgi-bin/
    Alias /mysite/html /var/www/mysite/

    <Directory “/var/www/mysite/cgi-bin/”>
    AddHandler cgi-script .py
    AllowOverride None
    Options +Indexes +ExecCGI
    Order allow,deny
    Allow from all
    </Directory>

    <Directory “/var/www/mysite/”>
    AllowOverride None
    # Allow open access:
    Require all granted
    </Directory>

  • Run the following code in the terminal
    chcon -h -t httpd_sys_content_t /var/www/mysite/cgi-bin/
    # You need this line if you want to write to a database file
    chcon -h -t httpd_sys_content_rw_t /var/www/mysite/cgi-bin/database.db
    chcon -h -t httpd_sys_script_exec_t /var/www/mysite/cgi-bin/*.py
    chgrp apache /var/www/mysite/html -R
    chmod uo+r /var/www/mysite/html -R
    chmod uo+x /var/www/mysite/cgi-bin -R
  • Now try to start server by typing
     service httpd start
  • Go to your website from your browser at: http://localhost/mysite/
Advertisements