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.
How to get Python3 + WSGI + Apache HTTPD running on Linux.
The following instructions should work for any Ubuntu-based Linux distro. It has been tested on Linux Mint.
-
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 -
Check out the repository to your chosen location on your server. In these instructions, we'll put it in
/srv/basicwsgi/ -
Edit the Apache vhost configuration file, for example,
/etc/apache2/sites-enabled/000-default.confor/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>-
On a developement server, you may wish to change the loglevel to
infoin order to get more detailed information (such as WSGI process restarts) in/var/log/apache2/error.log. -
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.sqland run it. -
Update the MySQL configuration in
config.py. -
Update the paths and base URL in
application.wsgi. -
Open the appropriate firewall port (usually TCP port 80) if necessary.
-
Browse to http://localhost/basicwsgi/, or whatever your url is based on your configuration.
The following instructions have been tested on CentOS 7, starting with the minimal ISO install.
-
Some extremely useful tools that every command-line guru knows (substitute nano with your favorite text editor):
sudo yum install nano net-tools screen -
Dependencies for code compilation:
sudo yum -y groupinstall development -
Add repo:
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm -
Search for the latest available version of Python to use in the next command:
sudo yum search python3 -
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 -
To start Apache without rebooting the server
sudo systemctl enable httpd sudo systemctl start httpd -
To open the firewall port (very important!)
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent sudo firewall-cmd --reload -
So you can use /usr/bin/python3 in the shebang line
sudo ln -s /usr/bin/python3.6 /usr/bin/python3 -
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 -
Start MariaDB/MySQL.
sudo systemctl enable mariadb sudo systemctl start mariadb -
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 -
Edit
application.wsgiand update the appropriate paths and URL. Be sure to change it from localhost to whatever hostname you're using.sudo nano application.wsgi -
Create a Apache configuration file in
/etc/httpd/conf.d/.sudo nano /etc/httpd/conf.d/basicwsgi.confFill 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>-
On a developement server, you may wish to change the loglevel in
/etc/httpd/conf/httpd.conftoinfoin order to get more detailed information (such as WSGI process restarts) in/etc/httpd/logs/error_log -
Reload Apache configuration files to get it going:
sudo systemctl reload httpd -
Get SELinux to stop raining on your parade:
chcon -R -t httpd_sys_content_t /srv/basicwsgi -
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 -urootThen in the MariaDB command line:
source /srv/basicwsgi/install/dbsetup.sql quit; -
Change the MySQL password in
/srv/basicwsgi/config.py.sudo nano /srv/basicwsgi/config.py -
Browse to http://localhost/basicwsgi/, or whatever your url is based on your configuration.
-
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 -fmonitoring 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.logFor 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.
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