How To Install Python 3.7 On An Arch Linux Webserver

Last modified: October 7, 2020
You are here:
Estimated reading time: 1 min

Prerequisites

  • A FastyCloud  server running up to date Arch Linux (see this article.)
  • A running webserver, either Apache or Nginx
  • Sudo access:
    • Commands required to be ran as root are prefixed by #. The recommended way to run commands as root is to, as a regular user, prefix each of them with sudo
  • Have a text editor installed, and be familiar with it, such as vi, vim, nano, emacs or a similar editor

Install Python 3.7 On Your Webserver

On Apache

Unfortunately, it is not supported to run both versions of Apache modules (for Python 2.x and 3.x) at the same time on the same Arch system, but this is rarely a problem.

To use Python 3.x:

# pacman -S mod_wsgi

Enable the Apache mod_wsgi module by editing /etc/httpd/conf/httpd.conf, and at the end of the list of LoadModule commands, add the following:

LoadModule wsgi_module modules/mod_wsgi.so

On Nginx

To use Python 3.x:

# pacman -S uwsgi-plugin-python

Test Python

Within the appropriate directory, create test.py with the following contents:

#-*- coding: utf-8 -*-
def wsgi_app(environment, start_response):
    import sys
    output = sys.version.encode('utf8')
    status = '200 OK'
    headers = [('Content-type', 'text/plain'),
               ('Content-Length', str(len(output)))]
    start_response(status, headers)
    yield output

application = wsgi_app

On Apache

Add to the end of /etc/httpd/conf/httpd.conf, or if you are running multiple hosts, edit the appropriate configuration file, and add in the appropriate <VirtualHost> block:

WSGIScriptAlias /wsgi_app /srv/http/test.py

Restart Apache:

# systemctl restart httpd

In a web browser, visit http://YOUR-SERVER-WEB-ADDRESS-OR-IP/wsgi_app, and you will see a test page with the python and GCC versions.

Delete the test.py test file you just created, and the WSGIScriptAlias in your Apache configuration.

Restart Apache:

# systemctl restart httpd

On Nginx

Create the file /etc/uwsgi/wsgi_app.ini with the following contents:

[uwsgi]
socket = /run/uwsgi/wsgi_app.sock
uid = http
gid = http
plugins = python
chdir = /usr/share/nginx/html/
wsgi-file=test.py
callable = application

Start uWSGI serving wsqi_app:

# systemctl start uwsgi@wsgi_app

Allow Nginx to use uWSGI by editing /etc/nginx/nginx.conf, and for every server block you want to test, add the following. Alternatively, if you are using virtual hosts, edit each host’s configuration file:

location ~ \wsgi_app {
    root /usr/share/nginx/html/;
    include uwsgi_params;
    uwsgi_pass unix:/run/uwsgi/wsgi_app.sock;
}

Restart Nginx:

# systemctl restart nginx

In a web browser, visit http://YOUR-SERVER-WEB-ADDRESS-OR-IP/wsgi_app, and you will see a test page with the python and GCC versions.

Delete the test.py file you just created, and the location block you just added to /etc/nginx/nginx.conf for wsgi_app.

Restart Nginx:

# systemctl restart nginx

Stop uWSGI serving wsgi_app:

# systemctl stop uwsgi@wsgi_app

Delete the /etc/uwsgi/wsgi_app.ini and test.py test files you just created.

Was this article helpful?
Dislike 0
Views: 38