I am very new to Apache in Ubuntu. I recently installed Apache2 on Ubuntu and I am trying to configure CGI. I added the following lines in apache2.conf file:

ScriptAlias  /cgi-bin/ /home/router/cgi-bin/

<Directory /home/router/cgi-bin/>
        Options ExecCGI
        AddHandler cgi-script cgi pl

And I saved and restart apache2, but when I type the following in browser: 

I get the following error:

You don't have permission to access /cgi-bin/file1.cgi on this server.

2 Answers 2


Firstly, I would not run CGI from your /home/ directory. Permissions are not set up for that, for web servers, and can lead to some headaches at times, both with permissions configurations and other issues.

Secondly, I would suggest *creating a dedicated directory within /var/www/ (such as cgi-bin) where your CGI scripts get put, and use that in the Apache configuration.

sudo mkdir -p /var/www/cgi-bin/
sudo chown $USER:www-data /var/www/cgi-bin/

The above commands will create the directory and set up permissions so that you can write to the directory, and the webserver can read the directory and have access to do things inside the directory.

From there, drop your CGI there, make sure it's owned in a similar way (sudo chown $USER:www-data /var/www/cgi-bin/*) and then set your Apache configuration to use /var/www/cgi-bin instead of /home/router/cgi-bin.

If for some reason you must use /home/router/cgi-bin, then you need to do several things: permit www-data and all users to traverse up to the cgi-bin directory, and give www-data access to the directory itself.

sudo chgrp www-data /home/router/cgi-bin
sudo chmod +rx /home/
sudo chmod +rx /home/router/
sudo chmod 750 /home/router/cgi-bin/

The problem with this is you have to open your home directory just enough so that the 'other' category of users can reach into the directory structure and traverse it. This is a security hole, because any other user shouldn't be permitted to traverse the file system into your home directory in this way, as it could lead to data leakage or theft of personal data. (This is why most guides for web servers suggest NOT using home directories, and instead dedicated directories in /srv/ or /var/www/ for web server document directories)


I've just set up a new LAMP server using Ubuntu 18.04.01, which installs or upgrades (I'm not sure which) to Apache2 v 2.4.29. This is a bit new to me as the Apache configuration files have changed again, but I think the procedure below is right.

The cgi load module was not enabled so I had to do that first by running a2enmod. (Check the available modules for the correct name.) This sets up a symlink from the enabled folder to the file in the available folder.

The cgi configuration module was already enabled - I thought this a bit odd, but that's what it was. The configuration module has a conditional statement that checks whether the load module is available and if so sets the Scriptalias to /usr/bin/cgi-bin. I have read that this is not a good idea, as in the previous answer, and it is better to make a /var/www/cgi-bin directory for your scripts, so I did that.

Next, I put my php script into the new directory I had created at /var/www/cgi-bin. It only had to be read only - I chmod'ed it to 444, and it worked.

Over the years the apache2.conf file has got smaller and, as explained in the docs, the developers have tried to make it more easily configurable by getting it to call in the other bits.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .