How to Install Python 3.7 on Arch Linux with Apache or Nginx

System Requirements

  • A server with the latest Arch Linux release (refer to the official setup guide).
  • A functional web server — either Apache or Nginx installed and configured.
  • Root-level access (sudo):
    Commands that require root privileges are marked with a #. The best practice is to use sudo with each root-level command as a non-root user.
  • An installed and familiar text editor such as vi, vim, nano, emacs, or any equivalent tool.

Python 3.7 Installation on Web Server

Using Apache

It is not feasible to run both Python 2.x and 3.x Apache modules simultaneously on Arch Linux. However, this is generally not an issue.

To activate Python 3.x support:

Next, modify the Apache configuration file at /etc/httpd/conf/httpd.conf. Scroll to the end of the LoadModule entries and append:

LoadModule wsgi_module modules/mod_wsgi.so

Using Nginx

To enable Python 3.x with Nginx, execute:

# pacman -S uwsgi-plugin-python

Validating Python Setup

In your desired directory, create a test.py file containing this script:

#-*- 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

Apache Configuration

To integrate the test application with Apache, append the following to /etc/httpd/conf/httpd.conf or insert it inside the appropriate <VirtualHost> block if using multiple hosts:

WSGIScriptAlias /wsgi_app /srv/http/test.py

Restart Apache for changes to apply:

Now, open a web browser and navigate to http://YOUR-SERVER-WEB-ADDRESS-OR-IP/wsgi_app. A test page displaying your Python and GCC versions should appear.

Finally, clean up by removing the test.py file and deleting the related WSGIScriptAlias directive from the Apache configuration.

Then restart Apache again:


Configure Python 3.7 with uWSGI and Nginx on Arch Linux

Create the uWSGI Configuration

Begin by creating a new file at /etc/uwsgi/wsgi_app.ini and populate it with the configuration below:

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

To begin serving wsgi_app with uWSGI, execute:

# systemctl start uwsgi@wsgi_app

Update Nginx Configuration

Edit /etc/nginx/nginx.conf. In every server block where testing is desired, add the following configuration. If you are using separate virtual host files, apply the change to each one individually:

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

Restart Nginx to apply the changes:

Open your browser and go to http://YOUR-SERVER-WEB-ADDRESS-OR-IP/wsgi_app. You should see a test page displaying the current Python and GCC versions.

Cleanup

After confirming it works, remove the test.py file and the location block for wsgi_app from /etc/nginx/nginx.conf.

Restart Nginx once again:

Then, stop the uWSGI service for wsgi_app:

# systemctl stop uwsgi@wsgi_app

Lastly, delete both the /etc/uwsgi/wsgi_app.ini and test.py files to complete the cleanup.

Conclusion

By following these steps, you’ve successfully configured Python 3.7 with uWSGI and Nginx on an Arch Linux server. This setup enables robust WSGI application serving while maintaining flexibility and performance. Don’t forget to clean up temporary test files and configurations to keep your server tidy and secure.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: