FreeBSD Enable Apache mod wsgi Python Module

The mod_wsgi module package implements an easy-to-use Apache module that can host Python-based web applications, with Python WSGI support. This module is suitable for use in hosting high-performance websites, and can also be used for self-managed personal sites or blogs running on a web hosting service.

mod_wsgi is an Apache HTTP Server module that embeds Python applications within a server and allows them to communicate via the Python WSGI interface as defined in Python PEP 333. WSGI is one of Python's ways of producing high-quality, high-performance web applications.

WSGI provides a standard way to connect various web applications without any hassle. Some well-known python applications or frameworks provide wsgi for easy deployment and embedding. This means that we can embed a Django-powered personal blog and a Trac project into a single Pylons application that wraps it to handle, say, authentication without changing the previous one.

WSGI (Web Server Gateway Interface) is a simple method for a web server to forward requests to a web application or framework written in Python. WSGI is a specification that describes how web servers communicate with web applications. Mod_wsgi is an Apache module used to serve Python scripts over HTTP.

mod_wsgi is an Apache HTTP Server module created by Graham Dumpleton that provides a WSGI-compliant interface for hosting Python-based web applications on Apache. As of version 4.5.3, mod_wsgi supports Python 2 and 3 (since 2.6 and 3.2). This is an alternative to mod_python, CGI and FastCGI solutions for Python and web integration. mod_wsgi First available in 2007.

This article helps you how to install Apache mod_wsgi module on FreeBSD system. In this article we will also explain how to create a python file and run it on the apache24 web server.



1. Installing mod_wsgi on FreeBSD

So that mod_wsgi can run on the Apache24 web server, we first install mod_wsgi. On FreeBSD, installing mod_wsgi can be done in 2 ways. In this article we will install mod_wsgi with the ports system on FreeBSD. Writing and implementing mod_wsgi in this article uses FreeBSD 13.2 which has Apache24, pyhton39, PHP8 installed along with mods and extensions. Another PHP module that you must install to run mod_wsgi is PHP_FPM.



The following is how to install mod_wsgi on a FreeBSD system.

root@ns1:~ # cd /usr/ports/www/mod_wsgi4
root@ns1:/usr/ports/www/mod_wsgi4 #
make install clean

After the installation process is complete, mod_wsgi is not immediately active. We must activate the wsgi module on the Apache24 web server.


2. Enable mod_wsgi on Apache24

On a web server, the wsgi module can be placed in the httpd.conf file or can be created separately in the /usr/local/etc/apache24/modules.d folder. In this article we will create a separate file for the wsgi module.

The first step is to create a wsgi file which we name "270_mod_wsgi.conf" and place it in the /usr/local/etc/apache24/modules.d folder. The file "270_mod_wsgi.conf" contains a module script to activate mod_wsgi on the Apache24 web server.

Below is how to create a file /usr/local/etc/apache24/modules.d/270_mod_wsgi.conf and enter a script in that file, as in the example below.

root@ns1:~ # touch /usr/local/etc/apache24/modules.d/270_mod_wsgi.conf
root@ns1:~ #
ee /usr/local/etc/apache24/modules.d/270_mod_wsgi.conf
LoadModule wsgi_module libexec/apache24/mod_wsgi.so

The first script in the command above is to create the file "270_mod_wsgi.conf", and the second script includes wsgi_modul in the file "270_mod_wsgi.conf".

Then we continue by creating the /usr/local/www/apache24/wsgi folder.

root@ns1:~ # mkdir -p /usr/local/www/apache24/wsgi

Give the user and group www:www to the folder.

root@ns1:~ # chown -R www:www /usr/local/www/apache24/wsgi

After that, we edit the file /usr/local/etc/apache24/httpd.conf and enter the script below.

root@ns1:~ # ee /usr/local/etc/apache24/httpd.conf
<Directory /usr/local/www/apache24/wsgi/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Options +ExecCGI
SetHandler wsgi-script
</Directory>
Alias /wsgi "/usr/local/www/apache24/wsgi/"


The script above can be placed at the very bottom of the /usr/local/etc/apache24/httpd.conf file.


3. Running mod_wsgi On Apache24

After everything has been configured, it's time for us to test or run the mod_wsgi, whether the module can run or not on the Apache24 web server. To make testing easier, we will create a python file with the name "/usr/local/www/apache24/wsgi/sample.py". Here's how to create the file.

root@ns1:~ # touch /usr/local/www/apache24/wsgi/sample.py
root@ns1:~ #
chown -R www:www /usr/local/www/apache24/wsgi/sample.py
root@ns1:~ #
chmod -R 777 /usr/local/www/apache24/wsgi/sample.py

In the python file /usr/local/www/apache24/wsgi/sample.py", we enter the script as follows.

root@ns1:~ # ee /usr/local/www/apache24/wsgi/sample.py
def application(environ, start_response):
status = '200 OK'
output = b'Selamat! Anda Telah Berhasil Menjalankan Modul wsgi Pada Web Server Apache24.'

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]

After the python file /usr/local/www/apache24/wsgi/sample.py" has been successfully created, we restart Apache24 so it can load the wsgi module.

root@ns1:~ # service apache24 restart

The next step, open the Google Chrome, Yandex or Firefox web browser. In the web browser's address bar menu, type http://192.168.5.2/wsgi/sample.py, and see the results.

If the Google Chrome browser says "Congratulations! You have successfully run the wsgi module on the Apache24 web server", that means you have successfully run the wsgi module on the Apache24 web server.

WSGI comes from CGI. In this article, we introduce a Python implementation of WSGI using Apache24 mod_wsgi. You can also apply the WSGI module together with Django in creating large or small scale blogs.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post