Skip to content

smidgen/basicwsgi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

basicwsgi

A basic, framework-less Python WSGI web application, with sample Environment dump and MySQL access pages. This is meant as a base for future projects.

This is a Python 3 app.

Setup

How to get Python3 + WSGI + Apache HTTPD running on Linux.

Ubuntu-based distros

The following instructions should work for any Ubuntu-based Linux distro. It has been tested on Linux Mint.

  1. Install Apache, WSGI, and Python-MySQLdb. The following command works for distros using Ubuntu repositories.

     sudo apt-get install apache2 libapache2-mod-wsgi-py3 python3-mysqldb
    
  2. Check out the repository to your chosen location on your server. In these instructions, we'll put it in /srv/basicwsgi/

  3. Edit the Apache vhost configuration file, for example, /etc/apache2/sites-enabled/000-default.conf or /etc/apache2/sites-enabled/default-ssl.conf. Add the following lines:

WSGIDaemonProcess basicwsgi processes=2 threads=5
WSGIProcessGroup basicwsgi

WSGIScriptAlias /basicwsgi /srv/basicwsgi/application.wsgi

<Directory /srv/basicwsgi>
        Options None
        AllowOverride None
        Require all granted
</Directory>

Alias "/basicwsgi/assets/" /srv/basicwsgi/assets/
<Directory /srv/basicwsgi/assets>
        Options None
        AllowOverride None
        Require all granted
</Directory>
  1. On a developement server, you may wish to change the loglevel to info in order to get more detailed information (such as WSGI process restarts) in /var/log/apache2/error.log.

  2. Set up a MySQL user and database, grant privileges for that database, create the test table, and insert some data. The simplest way to go about this is to update the password in install/dbsetup.sql and run it.

  3. Update the MySQL configuration in config.py.

  4. Update the paths and base URL in application.wsgi.

  5. Open the appropriate firewall port (usually TCP port 80) if necessary.

  6. Browse to http://localhost/basicwsgi/, or whatever your url is based on your configuration.

CentOS

The following instructions have been tested on CentOS 7, starting with the minimal ISO install.

  1. Some extremely useful tools that every command-line guru knows (substitute nano with your favorite text editor):

     sudo yum install nano net-tools screen
    
  2. Dependencies for code compilation:

     sudo yum -y groupinstall development
    
  3. Add repo:

     sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
    
  4. Search for the latest available version of Python to use in the next command:

     sudo yum search python3
    
  5. Install mod_wsgi and its dependencies, including Python3 and Apache (be sure to use the above yum search command to check if python36u-mod_wsgi is still the latest version)

     sudo yum -y install python36u-mod_wsgi
    
  6. To start Apache without rebooting the server

     sudo systemctl enable httpd
     sudo systemctl start httpd
    
  7. To open the firewall port (very important!)

     sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
     sudo firewall-cmd --reload
    
  8. So you can use /usr/bin/python3 in the shebang line

     sudo ln -s /usr/bin/python3.6 /usr/bin/python3
    
  9. Install Python3 MySQLdb driver. Again, do a yum search to get the latest available version of Python3.

     sudo yum -y install python36u-pip python36u-devel
     sudo yum -y install mariadb mariadb-server mariadb-devel
     sudo pip3.6 install mysqlclient
    
  10. Start MariaDB/MySQL.

     sudo systemctl enable mariadb
     sudo systemctl start mariadb
    
  11. Check out the project (modify the path and username in the following commands to suit).

    cd /srv
    sudo mkdir basicwsgi && sudo chown nolan:nolan basicwsgi
    git clone https://github.com/smidgen/basicwsgi.git basicwsgi
    
  12. Edit application.wsgi and update the appropriate paths and URL. Be sure to change it from localhost to whatever hostname you're using.

    sudo nano application.wsgi
    
  13. Create a Apache configuration file in /etc/httpd/conf.d/.

    sudo nano /etc/httpd/conf.d/basicwsgi.conf
    

    Fill it with the following:

WSGIDaemonProcess basicwsgi processes=2 threads=5
WSGIProcessGroup basicwsgi

WSGIScriptAlias /basicwsgi /srv/basicwsgi/application.wsgi

<Directory /srv/basicwsgi>
        Options None
        AllowOverride None
        Require all granted
</Directory>

Alias "/basicwsgi/assets/" /srv/basicwsgi/assets/
<Directory /srv/basicwsgi/assets>
        Options None
        AllowOverride None
        Require all granted
</Directory>
  1. On a developement server, you may wish to change the loglevel in /etc/httpd/conf/httpd.conf to info in order to get more detailed information (such as WSGI process restarts) in /etc/httpd/logs/error_log

  2. Reload Apache configuration files to get it going:

    sudo systemctl reload httpd
    
  3. Get SELinux to stop raining on your parade:

    chcon -R -t httpd_sys_content_t /srv/basicwsgi
    
  4. Set up a MySQL user and database, grant privileges for that database, create the test table, and insert some data. The simplest way to go about this is to update the password in install/dbsetup.sql and run it.

    sudo nano /srv/basicwsgi/install/dbsetup.sql
    mysql -uroot
    

    Then in the MariaDB command line:

    source /srv/basicwsgi/install/dbsetup.sql
    quit;
    
  5. Change the MySQL password in /srv/basicwsgi/config.py.

    sudo nano /srv/basicwsgi/config.py
    
  6. Browse to http://localhost/basicwsgi/, or whatever your url is based on your configuration.

Development Tips

  • Remember that whenever you make a code change to the WSGI application, you'll need to update the last modified date on application.wsgi.

      touch /srv/basicwsgi/application.wsgi
    
  • During development, you pretty much have to keep a terminal window open with tail -f monitoring the Apache error log in order to see parse errors and things that can cause the error system to fail.

    For Debian/Ubuntu-based systems:

      sudo tail -f /var/log/apache2/error.log
    

    For CentOS:

      sudo tail -f /etc/httpd/logs/error_log
    
    • Due to this, if you have a server on which multiple WSGI applications are running, it may be wise to create a separate Apache log file for each one, so you don't have error messages from all the applications mixed together.

Resources

The installation instructions for Python3 on CentOS were modified from the following article, which also explains some of the how's and why's, takes you through creating a virtualenv, and helps you create a basic Python (not WSGI) hello world. https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-centos-7

About

A basic, framework-less Python WSGI web application, with sample Environment dump and MySQL access pages. This is meant as a base for future projects.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors