๐Ÿš€ Running Python Scripts on Apache HTTP Server (httpd) in CentOS 7

Shrikant DandgeShrikant Dandge
3 min read

So, you've set up your CentOS 7 server with Apache HTTP Server (httpd), and now you're eager to run Python scripts on it. Great news! It's totally doable. In this guide, we'll walk you through the process step by step. Let's get started! ๐Ÿ’ก

Introduction

๐Ÿ” Understanding the Challenge: Apache HTTP Server is primarily designed for serving web pages, but you can configure it to execute Python scripts in a few ways. We'll use the Common Gateway Interface (CGI) approach for this tutorial.

Setting the Stage ๐ŸŽญ

  1. Server Setup: Ensure you have a CentOS 7 server running. If not, you can launch an EC2 instance on AWS with the CentOS 7 image.

  2. Python 3 and httpd Installation: Make sure Python 3 and Apache (httpd) is installed on your server. If not, install it using yum.

     sudo yum install python3
     sudo yum install httpd
    

The Script ๐Ÿ“

For this demonstration, let's use a simple "Hello, World!" Python script. Ensure to specify the location of the Python interpreter on top of script.

#!/usr/bin/python3
print("Content-Type: text/html")
print()
print("<html><body>")
print("<h1>Hello, World!</h1>")
print("</body></html>")

Configuration ๐Ÿ’ป

Now, let's configure your Apache server to run Python scripts.

  1. Edit Apache Configuration: Open the Apache configuration file for editing.

     sudo vim /etc/httpd/conf/httpd.conf
    
  2. Update Options: Locate the line with Options Indexes FollowSymLinks and add ExecCGI.

     Options Indexes FollowSymLinks -> original line
     Options Indexes FollowSymLinks ExecCGI -> modified line
    
  3. Add CGI Handler: Find the line for AddHandler cgi-script .cgi (uncomment it by removing #) and add .py to the end.

     #AddHandler cgi-script .cgi -> original line
     AddHandler cgi-script .cgi .py -> modified line
    
  4. Example Configuration: If you want to configure a specific script, you can add a section like this to your Apache configuration. Replace <your_script_path> with the actual path to your script.

     WSGIScriptAlias /your_script /var/www/python/your_script.py
    
     <Directory /var/www/python/>
       Options Indexes FollowSymLinks ExecCGI MultiViews
       AddHandler cgi-script .py
       AllowOverride None
       Order allow,deny
       Allow from all
     </Directory>
    
  5. Restart Apache: Now, restart Apache to apply the changes.

     sudo systemctl restart httpd
    

SELinux Consideration ๐Ÿ›ก๏ธ

SELinux (Security-Enhanced Linux) may affect script execution. Check its status with:

getenforce

If it's 'Enforcing,' add a rule for your script directory:

sudo semanage fcontext -a -t httpd_sys_script_exec_t "/var/www/your_script_directory(/.*)?"

Note: Remember to replace <your_script_directory> with your actual script directory.

Permission Matters ๐Ÿšฆ

Ensure your Python script has execute permissions:

chmod +x /var/www/python/your_script.py

Troubleshooting ๐Ÿ› ๏ธ

If you encounter issues, you can check the Apache error logs for more information:

tail -f /var/log/httpd/error_log

Final Words ๐Ÿค

I hope this guide helps you get your Python scripts up and running on your CentOS 7 Apache server. Remember that disabling SELinux is not recommended for security reasons.

Have a fantastic day! ๐ŸŒž

0
Subscribe to my newsletter

Read articles from Shrikant Dandge directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shrikant Dandge
Shrikant Dandge