How to install Python, WSGI, Flask & Apache on Centos 8 and get your first Python Web App up and running.

Installing Python 3 on Centos 8

sudo dnf update
sudo def install python3

You can now run Python, with the Python3 command, check the version information and then use exit() to quit.

Create a Python Virtual Environment for your Flask project

Keep in mind that the user that Apache runs your code as will need to be able to access the Python virtual environment. On some Linux distributions, the home directory of a user account is not accessible to other users. Rather than change the permissions on your home directory, it might be better to consider locating your WSGI application code and any Python virtual environment outside of your home directory.

Centos 8 and most Linux versions default to serving web documents from /var/www, so creating /var/www/Python-Projects as a home may be a good starting point, rather than using your home directory.

It’s important to create a virtual environment for each of your Python projects so that you don’t mix up dependancies and versions between projects. I have a details blog which talks you through setting up a Python virtual environment, please follow this to create a project called Demo and then return here.

Make a note of the directory path to your new virtual environment

After activating you environment you can run this command to print the fully qualified path to that environment.

python -c 'import sys; print(sys.prefix)'  

In my case this was “/var/www/Python-Projects/Demo’

Make a note of this as we will need it later when configuring the Apache WSGI Module, so that we can tell it where to find your environment.

Note that this should be the root directory of the Python virtual environment, which in turn contains the bin and lib directories for the Python virtual environment. It is a common mistake when setting up a Python virtual environment with mod_wsgi to use the full path to the python executable instead of the root directory. That will not work, so do not use the path for the python executable as the location of the Python virtual environment, it has to be the root directory.

Grant full access to your new directory

sudo setfacl -m u:<your_user_id>:rwx -R /var/www/

Set-up a demo WSGI Python Application

In the directory we created above, create a file called “demo_wsgi.py”

import datetime
def application(environ, start_response):
    status = '200 OK'
    
    runTimeStamp = datetime.datetime.now().strftime('%Y-%b-%d-%H-%M-%S')  # Todays date
    
    output = b'The time by Python & mod_wsgi is {runTimeStamp}'
 
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
 
    return [output]

Installing Apache

In these instructions we don’t talk about setting up a firewall – if you intend to use this in production, you need to understand the security implications and set-up a suitable firewall.

sudo dnf install httpd

Check your Apache web server starts up

sudo systemctl start httpd
sudo systemctl status httpd

In your web browser. go to http://your ip address on my laptop http://localhost displayed the HTTP SERVER TEST PAGE successfully.

Assuming this worked, you can now stop the server

sudo systemctl stop httpd

Installing the mod-wsgi Apache Module

sudo dnf install python3-mod_wsgi httpd

Configure Python WWSGI

sudo nano /etc/httpd/conf.d/python-wsgi.conf

Search


Recent Posts



Popular Posts




You May Also Like…

What is the best IDE for Python?

What is the best IDE for Python?

So what is the best Python IDE and what makes a good IDE anyway? Do you need an IDE to program in Python? No you...

Find similar blogs in these categories: Python
4 Comments
  1. Tom C

    You left me hanging man! do you plan to finish this?

    Reply
    • admin

      Hi Tom,

      Yes, sorry I was interrupted while working on this. Will try and get back to it asap.

      Reply
  2. Jefferson

    And… So….
    Are you going to complete this manual or you dimitt this blog..??

    Reply
    • admin

      Yes, I’ll be completing this but probably not for a few months. Busy on other projects.

      Reply
Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.