{"id":2611,"date":"2023-10-21T02:53:45","date_gmt":"2023-11-17T04:22:29","guid":{"rendered":""},"modified":"2025-02-02T00:46:18","modified_gmt":"2025-02-01T23:46:18","slug":"how-to-run-python-scripts-with-apache-and-mod-wsgi-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/netcloud24.com\/knowledgebase\/how-to-run-python-scripts-with-apache-and-mod-wsgi-on-ubuntu-18-04\/","title":{"rendered":"How to Run Python Scripts with Apache and mod_wsgi on Ubuntu 18.04"},"content":{"rendered":"<p>\u00a0<\/p>\n<\/p>\n<h1>\u00a0<\/h1>\n<p>mod_wsgi is an Apache HTTP Server module that provides a WSGI-compliant interface for hosting Python web applications. It is commonly used to deploy Python-based web applications with Apache. In this guide, we will walk you through how to run Python scripts using Apache and mod_wsgi on Ubuntu 18.04 LTS. Whether you&#8217;re using a  or another hosting provider, this setup will allow you to efficiently deploy Python applications on your server.<\/p>\n<h2>Prerequisites<\/h2>\n<p>Before starting, ensure you have the following:<\/p>\n<ul>\n<li>An Ubuntu 18.04 LTS server, which can be hosted on a <a href=\"https:\/\/ie.netcloud24.com\" target=\"_blank\" rel=\"follow\">UK Windows VPS<\/a>, <a href=\"https:\/\/ie.netcloud24.com\" target=\"_blank\" rel=\"follow\">Windows VPSVirtual Private Servers<\/a>, or another <a href=\"https:\/\/ie.netcloud24.com\" target=\"_blank\" rel=\"follow\">Windows VPS Hosting UK<\/a> solution.<\/li>\n<li>Root or sudo privileges on your server.<\/li>\n<li>Python 3 installed on your server.<\/li>\n<\/ul>\n<h2>Step 1: Install Apache and mod_wsgi<\/h2>\n<p>First, update your system\u2019s package index and install Apache with mod_wsgi:<\/p>\n<pre><code>sudo apt update\r\nsudo apt install apache2 libapache2-mod-wsgi-py3 -y<\/code><\/pre>\n<p>After installation, start and enable Apache:<\/p>\n<pre><code>sudo systemctl start apache2\r\nsudo systemctl enable apache2<\/code><\/pre>\n<h2>Step 2: Install Python 3<\/h2>\n<p>Ubuntu 18.04 comes with Python 3 pre-installed. To ensure Python 3 is available, verify the installation with the following command:<\/p>\n<pre><code>python3 --version<\/code><\/pre>\n<p>If Python 3 is not installed, you can install it using:<\/p>\n<pre><code>sudo apt install python3 -y<\/code><\/pre>\n<h2>Step 3: Create a Python Application<\/h2>\n<p>In this step, you will create a basic Python WSGI application to test mod_wsgi. First, create a directory to hold your Python app:<\/p>\n<pre><code>sudo mkdir \/var\/www\/html\/myapp<\/code><\/pre>\n<p>Navigate to the newly created directory and create a Python WSGI application file:<\/p>\n<pre><code>cd \/var\/www\/html\/myapp\r\nsudo nano myapp.wsgi<\/code><\/pre>\n<p>Add the following content to the <code>myapp.wsgi<\/code> file:<\/p>\n<pre><code>def application(environ, start_response):\r\n    status = '200 OK'\r\n    output = b'Hello from WSGI!'\r\n\r\n    response_headers = [('Content-type', 'text\/plain'), ('Content-Length', str(len(output)))]\r\n    start_response(status, response_headers)\r\n\r\n    return [output]<\/code><\/pre>\n<p>This is a basic WSGI application that returns a &#8220;Hello from WSGI!&#8221; message when accessed via a web browser.<\/p>\n<h2>Step 4: Configure Apache to Serve Python Application<\/h2>\n<p>Now you need to configure Apache to serve your Python WSGI application. Create a new virtual host configuration file for your app:<\/p>\n<pre><code>sudo nano \/etc\/apache2\/sites-available\/myapp.conf<\/code><\/pre>\n<p>Add the following configuration:<\/p>\n<pre><code>&lt;VirtualHost *:80&gt;\r\n    ServerName your-domain.com\r\n    DocumentRoot \/var\/www\/html\/myapp\r\n\r\n    WSGIDaemonProcess myapp python-path=\/var\/www\/html\/myapp\r\n    WSGIProcessGroup myapp\r\n    WSGIScriptAlias \/ \/var\/www\/html\/myapp\/myapp.wsgi\r\n\r\n    &lt;Directory \/var\/www\/html\/myapp&gt;\r\n        Require all granted\r\n    &lt;\/Directory&gt;\r\n&lt;\/VirtualHost&gt;<\/code><\/pre>\n<p>Replace <code>your-domain.com<\/code> with your actual domain. Save the file and exit.<\/p>\n<p>Enable the new virtual host and disable the default one:<\/p>\n<pre><code>sudo a2ensite myapp.conf\r\nsudo a2dissite 000-default.conf\r\nsudo systemctl reload apache2<\/code><\/pre>\n<h2>Step 5: Test the Python Application<\/h2>\n<p>To test if everything is working, open your web browser and navigate to <code>http:\/\/your-domain.com<\/code>. You should see the message &#8220;Hello from WSGI!&#8221; displayed in the browser.<\/p>\n<h2>Step 6: Secure Your Application with Let&#8217;s Encrypt SSL (Optional)<\/h2>\n<p>If you want to secure your Python web application with SSL, you can use Let&#8217;s Encrypt. Install Certbot by running:<\/p>\n<pre><code>sudo apt install certbot python3-certbot-apache -y<\/code><\/pre>\n<p>Obtain an SSL certificate for your domain:<\/p>\n<pre><code>sudo certbot --apache -d your-domain.com -d www.your-domain.com<\/code><\/pre>\n<p>Follow the prompts to install and configure SSL for your Apache server. Certbot will automatically configure Apache to redirect HTTP traffic to HTTPS.<\/p>\n<h2>Conclusion<\/h2>\n<p>By following these steps, you have successfully set up Apache with mod_wsgi to run Python scripts on Ubuntu 18.04 LTS. This allows you to deploy Python web applications efficiently, whether you are using a , <a href=\"https:\/\/ie.netcloud24.com\" target=\"_blank\" rel=\"follow\">Microsoft SQL VPS Windows<\/a>, or another <a href=\"https:\/\/ie.netcloud24.com\" target=\"_blank\" rel=\"follow\">Windows VPSVirtual Private Server Hosting<\/a> solution. With mod_wsgi and Apache, you can ensure high performance and scalability for your Python applications.<\/p>\n<footer>\n<p>For more VPS hosting options, visit . They offer a variety of <a href=\"https:\/\/ie.netcloud24.com\" target=\"_blank\" rel=\"follow\">Windows VPSVirtual Private Servers<\/a> and <a href=\"https:\/\/ie.netcloud24.com\" target=\"_blank\" rel=\"follow\">Windows VPS Hosting UK<\/a> solutions tailored to your needs.<\/p>\n<\/footer>\n<div class=\"post-author-box\" style=\"border-top:1px solid #ddd;margin-top:20px;padding-top:15px;\">\n<p><strong>Author:<\/strong> \u0141ukasz Bodziony<\/p>\n<p><strong>Website:<\/strong> <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a><\/p>\n<p><em>\u0141ukasz Bodziony is the CEO and founder of <a href=\"https:\/\/netcloud24.com\" target=\"_blank\" rel=\"dofollow\">NETCLOUD24<\/a>, a global VPS hosting brand proudly originating from Poland. With extensive experience in cloud computing, virtualization, and server management, he delivers high-performance <strong>Windows VPS<\/strong> and <strong>Remote Desktop Services (RDS)<\/strong> solutions to clients across Europe, North America, and beyond.<\/em><\/p>\n<p><em>His expertise covers a wide range of technologies, including <strong>Microsoft Azure<\/strong>, <strong>Proxmox VE<\/strong>, <strong>Amazon Web Services (AWS)<\/strong>, and numerous other virtualization and cloud platforms.<\/em><\/p>\n<p><em>Beyond running his hosting business, \u0141ukasz also provides <strong>professional paid server configuration and optimization services<\/strong> for companies and individuals. Outside of work, he is dedicated to caring for his children and building a secure future for them.<\/em><\/p>\n<p><em>If you are interested in working with him or need expert assistance with your hosting, cloud environment, or server setup, feel free to reach out via <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a>.<\/em><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 mod_wsgi is an Apache HTTP Server module that provides a WSGI-compliant interface for hosting Python web applications. It is commonly used to deploy Python-based web applications\u2026<\/p>\n","protected":false},"author":1,"featured_media":3421,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[],"tags":[14,12,11,23,20,21,22,17,7,8,6,10,18,19,15,24,16,5,13,9],"class_list":["post-2611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","tag-cheapvps","tag-cloudvps","tag-hostingvps","tag-rds","tag-rdscal","tag-remotedesktop","tag-remotedesktopvps","tag-servervps","tag-ukvps","tag-virtualserver","tag-vpshosting","tag-vpsserver","tag-vpssolutions","tag-vpswindows","tag-vpswithwindows","tag-windowsrds","tag-windowsserver","tag-windowsvps","tag-windowsvpshosting","tag-windowsvpsuk"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=2611"}],"version-history":[{"count":0,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2611\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media\/3421"}],"wp:attachment":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=2611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=2611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=2611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}