My favorite way to run Python3 is a Nginx and Apache2 combo of the two servers, it’s great.

This post will describe how to use Nginx to do the work of the great two server option.

This simplifies things a little using Nginx to server Python3 and to do the https ssl also.

As with the Nginx and Apache2 combo.

First start off by configuring Nginx to serve on port 80 of a domain name.

Something simple, for example.

server {

    server_name topcoders.cloud;

    root /path/to/somelocation;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
	}

    # to RUN PYTHON3 on Apache2
    location ~ / {
        proxy_buffering off;
		
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Real-IP       $remote_addr;  
        proxy_set_header        Host            $host;  

        proxy_pass   http://127.0.0.1:8234$request_uri;
	}

    client_max_body_size 256M;	

    listen 80;
    listen [::]:80;
}

Next, if you have correctly installed python3-certbot-nginx from synaptic or cli apt.

Run the cli statement of

sudo certbot

This will change your /etc/nginx/sites-enabled/default

configuration to something like

...

    listen 443 ssl; # managed by Certbot
    listen [::]:443 ssl; # managed by Certbot
    ssl_certificate /path/to/location/fullchain.pem; # managed by Certbot
    ssl_certificate_key /path/to/location/privkey.pem; # managed by Certbot
    include /path/to/location/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /path/to/location/ssl-dhparams.pem; # managed by Certbot

Next, I like to keep the https server { } in such a way to utilize the proxy_pass http://127.0.0.1:8234$request_uri; statement

Ok, Now if you want, you may make Nginx serve the Python3 also.

Create a new server { } within the Nginx config. Something like.

server {

	listen 8234 default_server;
	listen [::]:8234 default_server;
	
	server_name topcoders.cloud;

	root /path/to/location/topcoders.cloud;


	location ~ \.js {
		
		default_type  application/octet-stream;
		include       /etc/nginx/mime.types;		
		
	}


	location ~ \.css {
		
		default_type  application/octet-stream;
		include /etc/nginx/mime.types;
		
	}


	location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
	
		# Serve static files directly
		sendfile on;
		tcp_nopush on;
		
		#expires 30d; # Set browser cache expiry for performance
		# Optional: Log access to static files if needed, or disable for performance
		#access_log off;
		
	}
	
	
	location / {

		default_type  application/octet-stream;
		include       /etc/nginx/mime.types;

	    	proxy_set_header    X-Real-IP        $remote_addr;
    		proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
	
		gzip off;
		
		fastcgi_pass unix:/var/run/fcgiwrap.socket;
		include /etc/nginx/fastcgi_params;
		
		fastcgi_param SCRIPT_FILENAME $document_root/index_websiteDomainName.py;
	
	}
}

And that would do it. However, the program fcgiwrap needs to be installed so that Nginx can server Python3 cgi. And you probably want to make sure that the owner and group that runs the program fcgiwrap is the same as your website files and folders owner / group, otherwise you may get write permission errors, etc.

This is the last post of the year 2025. Happy New Year to all. God bless America!

And as always, Happy Coding!

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *