2018年4月29日 星期日

How to set up cgi module for apache2

At this moment, i am studying CGI(common gateway interface) script with python. Need to setup a temporary local website for test. Some notes here.

Environment
1. ubuntu 14 server
2. apache2 install

I am not sure any python copy should be installed or not necessary.

First need to edit /etc/apache2/conf-enabled/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>
 
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
substitute the directory after "ScriptAlias" with the directory you want to put your cgi 
script files. 
Note. this directory must fall in access range of apache2. You can edit apache2.conf under 
/etc/apache2/ directory to add your directory 
<Directory "/var/www/cgi-bin">
   AllowOverride None
   Options ExecCGI
   Order allow,deny
   Allow from all
</Directory>

<Directory "/var/www/cgi-bin">
   Options All
</Directory>
cgi module is not enabled by default while apache2 installed. You have to manually add it by make a link to /etc/apache2/mods-enabled directory with below command

$ cd /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/cgi.load
then reload apache2

$ sudo service apache2 reload
It's all set. You can now put your first cgi script to your cgi-bin directory and test it with your favorite browser under http://localhost/cgi-bin/xxx.py
oh! one more thing! Remember to change access mode to your script file.
sudo chmod +x /usr/lib/cgi-bin/xxx.py

沒有留言:

張貼留言