Python – Running a Python script which saves files to server with cgi and apache2 version 2.4

apache-2.4cgipermissionspython

I have a python script (python.py) located in the /var/www/html/Python folder.

The script saves some images to another directory (/var/www/html/Data)

I have the -x file permission on the python.py script and -rw permission for all on the files where the output is saved.

I am running apache2 version 2.4

This is what my apache2.conf file looks like:

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

</Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FileMatch>

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

Since I am running version 2.4 my cgi conf is at : /ect/apache2/conf-available/serve-cgi-bin.conf

It looks like this:

<IfModule mod_alias.c>
    <IfModule mod_cgi.c>
        Define ENABLE_USR_LIB_CGI_BIN
    </IfModule>

    ScriptAlias /cgi-bin/ /var/www/html/Python
    <IfDefine ENABLE_USR_LIB_CGI_BIN>
        Define ENABLE_USR_LIB_CGI_BIN
    </IfDefine>

    <Directory "var/www/html/Python">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
        AddHandler cgi-script .py
        AddHandler default-handler .html.htm
    </Directory>
</IfModule>

I have enabled the cgi module by running the command

sudo a2enmod cgi

and restarted the server using

service apache2 restart

However despite all this, my Python script is still not executed and displays as a text file.

Can anybody help me? I can't figure out what I'm doing wrong.

Best Answer

A stupid question: do you have a hardlink to /etc/apache2/conf-available/serve-cgi-bin.conf in the directory /etc/apache2/conf-enabled ?

Edit: In order to create a link, what you do is, in a command prompt, change to the /etc/apache2/conf-enabled directory, and type

ln /etc/apache2/conf-available/serve-cgi-bin.conf serve-cgi-bin.conf

You may have to sudo that command if you don't have file create permissions in that directory. You will have to restart apache once you've created that link, of course.

Related Topic