How to Host a Python Flask App with SSL on WAMP Server in Windows: A Step-by-Step Guide for Beginners

Cyberac1d
0



Setting up a Python Flask application with SSL on a WAMP server in Windows can be challenging for beginners, but it's a valuable skill for deploying secure web applications. This step-by-step tutorial will guide you through the process, from configuring virtual hosts to enabling SSL on your WAMP server.

Prerequisites

Before starting, ensure you have the following:

  • WAMP server installed on your Windows machine
  • Python and Flask installed
  • Basic understanding of Apache, virtual hosts, and SSL certificates

Step 1: Setting Up the Project Directory

First, make sure your project directory is properly structured. For this example, we'll use the following paths:

  • Project directory: C:/wamp64/www/projectdirectory
  • Flask app entry point: C:/wamp64/www/projectdirectory/flaskapp.wsgi
  • Static files directory: C:/wamp64/www/projectdirectory/static

Step 2: Configuring Virtual Hosts in Apache

Next, you need to configure virtual hosts in Apache to handle different domains and SSL. Open your httpd-vhosts.conf file located at C:/wamp64/bin/apache/apache2.4.59/conf/extra/httpd-vhosts.conf and update it with the following configuration:

<VirtualHost _default_:80>

    ServerName localhost

    ServerAlias localhost

    DocumentRoot "${INSTALL_DIR}/www"

    <Directory "${INSTALL_DIR}/www/">

        Options +Indexes +Includes+FollowSymLinks +MultiViews

        AllowOverride All

        Require local

    </Directory>

</VirtualHost>

<VirtualHost *:80>

    ServerName example.test

    DocumentRoot "c:/wamp64/www/projectdirectory"

    WSGIScriptAlias / "c:/wamp64/www/projectdirectory/flaskapp.wsgi"

    <Directory "c:/wamp64/www/projectdirectory/">

        Require all granted

    </Directory>

    Alias /static "c:/wamp64/www/projectdirectory/static"

    <Directory "c:/wamp64/www/projectdirectory/static/">

        Require all granted

    </Directory>

</VirtualHost>

<VirtualHost *:80>

    ServerName exampledomain.com

    Redirect permanent / https://exampledomain.com/

</VirtualHost>

<VirtualHost *:443>

    ServerName exampledomain.com

    DocumentRoot "c:/wamp64/www/projectdirectory"

    WSGIScriptAlias / "c:/wamp64/www/projectdirectory/flaskapp.wsgi"

    <Directory "c:/wamp64/www/projectdirectory/">

        Require all granted

    </Directory>

    Alias /static "c:/wamp64/www/projectdirectory/static"

    <Directory "c:/wamp64/www/projectdirectory/static/">

        Require all granted

    </Directory>

    SSLEngine on

    SSLCertificateFile "c:/wamp64/ssl/certificate.pem"

    SSLCertificateKeyFile "c:/wamp64/ssl/key.key"

    ErrorLog "c:/wamp64/logs/myapp-error.log"

    CustomLog "c:/wamp64/logs/ myapp-access.log" common

</VirtualHost>

Step 3: Configuring Python and WSGI in Apache

To host a Python Flask application, you need to configure Python and mod_wsgi in Apache. Add the following lines to your httpd.conf file, located at C:/wamp64/bin/apache/apache2.4.59/conf/httpd.conf:

LoadFile "C:/Users/Administrator/AppData/Local/Programs/Python/Python312/python312.dll"
LoadModule wsgi_module "C:/projectdirectory/myenv/Lib/site-packages/mod_wsgi/server/mod_wsgi.so"
WSGIPythonHome "C:/projectdirectory/myenv"

Step 4: Enabling SSL in Apache

To enable SSL, ensure that the SSL module is loaded and the httpd-ssl.conf file is included in the Apache configuration. Uncomment the following line in your httpd.conf file:

Include conf/extra/httpd-ssl.conf

Step 5: Disabling Global SSL Settings

Since we're using virtual host-specific SSL settings, it's crucial to disable global SSL settings in httpd-ssl.conf. Comment out the SSL configuration in httpd-ssl.conf by placing # before the SSL directives.

Step 6: Restarting Apache

After completing all the configurations, restart Apache to apply the changes. You can do this via the WAMP control panel or by running the following command in the command prompt:

httpd.exe -k restart

Step 7: Testing Your Setup

Now, test your setup by accessing http://exampledomain.com/ and https://exampledomain.com/. Ensure that the site is accessible over both HTTP and HTTPS, and that SSL is properly configured.

Conclusion

By following this step-by-step guide, you should now have a working Python Flask application hosted on a WAMP server in Windows with SSL enabled. This setup ensures that your web application is secure and accessible to users over HTTPS. Happy hosting!

Final Thoughts

This tutorial aimed to simplify the process of hosting a Flask app with SSL on WAMP for beginners. The configurations provided are tailored to a typical deployment scenario, but you can customize them based on your project's specific needs. Always remember to keep your SSL certificates secure and up-to-date to maintain a safe web environment.

Post a Comment

0Comments

Thanks for sharing your feedback! It helps us grow.

Post a Comment (0)

Contact form