I'm using Ubuntu 16.04 server, Python 2.7.12, and Apache 2.4.18 in a DigitalOcean Droplet.
I uploaded an index.py
to web root. The contents of the script are as follow:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Turn on debug mode.
import cgitb
import platform
cgitb.enable()
# Print necessary headers.
print "Content-Type: text/html\n"
print "<html><body>Python Version: %s</body></html>" % (platform.python_version())
If I set the index.py
to 644 (without execution rights), the script will return 500 Internal Server Error. The log said:
[Fri Dec 28 04:05:18.035946 2018] [cgi:error] [pid 29045] [client 202.75.86.173:54912] End of script output before headers: index.py
From the another answer, it is suggested that I should add permission rights via chmod +x index.py
:
-rwxr-xr-x 1 root www-data 254 Dec 28 04:05 index.py
After adding execution rights, the Python script can be run without problem.
- Am I doing this right?
- Will this lead to security problem?
Here is the Apache site config:
<VirtualHost *:80>
ServerName abc.example.com
DocumentRoot /var/www/vhosts/abc.example.com
<Directory /var/www/vhosts/abc.example.com/>
Options -Indexes
Options +ExecCGI
DirectoryIndex index.py
AllowOverride All
Order allow,deny
allow from all
</Directory>
AddHandler cgi-script .py
ErrorLog ${APACHE_LOG_DIR}/abc-apache2.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access-logfile.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =abc.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
UPDATE:
This question is not about the error I encountered, as I mentioned in the question, I already get my script working. I just worry about the security. In short, not a duplicate.