‚Č° Menu

How to create cgi-bin directory per virtual host?

The question – I have several virtual hosts on my Apache config on my Linux CentOS box. How do I create a cgi-bin directory per virtual host? I want each virtual host to have a separate cgi-bin directory. Is there an Apache directive to do this?

Apache httpd has a directive called ScriptAlias which points to the cgi-bin directory.

First locate your httpd.conf file:

httpd -V

This will output the following:

[root@server ~]# httpd -V
Server version: Apache/2.2.15 (Unix)
Server built: Aug 13 2013 17:27:11
Server's Module Magic Number: 20051115:25
Server loaded: APR 1.3.9, APR-Util 1.3.9
Compiled using: APR 1.3.9, APR-Util 1.3.9
Architecture: 32-bit
Server MPM: Prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=128
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="run/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="logs/accept.lock"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
[root@server ~]#

This will give you the location of your httpd.conf file. Now you must find your vhost.conf file. Usually, this is located in HTTPD_ROOT/conf.d/vhost.conf. Note that you can also directly add virtual hosts to your httpd.conf file and if this is what you have done,  continue to add virtual hosts to your httpd.conf file.

Open your vhost.conf file:

vi /etc/httpd/conf.d/vhost.conf

In each of your virtual hosts, you must add the ScriptAlias directive:

ScriptAlias /cgi-bin/ /home/ewhathow/public_html/cgi-bin

The above statement says that http://www.ewhathow.com/cgi-bin/ is located at /home/ewhathow/public_html/cgi-bin/.

Here is an example virtual host configuration:

NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin info@ewhathow.com
ServerName ewhathow.com
ScriptAlias /cgi-bin/ /home/ewhathow/public_html/cgi-bin/
ServerAlias www.ewhathow.com
DocumentRoot /srv/www/ewhathow.com/public_html/
ErrorLog /srv/www/ewhathow.com/logs/error.log
CustomLog /srv/www/ewhathow.com/logs/access.log combined
</VirtualHost>

The bolded line is what you must add to each of your virtual hosts to create a cgi-bin directory specific to each virtual host.

After adding the cgi-bin lines to your virtual hosts file, remember to restart your httpd server:

service httpd restart

Incoming search terms:

  • /cgi-bin/ for each vhost (1)

Comments on this entry are closed.