Running HTTPS server in background using

Hello,

I am trying to publish a directory in Linux over HTTPs. I am using the below code.

from http.server import HTTPServer, BaseHTTPRequestHandler,SimpleHTTPRequestHandler
import ssl,os
web_dir = os.path.join(os.path.dirname(__file__), 'as4-web')
cert_path=os.getcwd()
os.chdir(web_dir)

def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler):
    server_address = ('',8444)
    httpd = server_class(server_address, handler_class)
    httpd.socket = ssl.wrap_socket(httpd.socket,
                                   server_side=True,
                                   certfile=os.path.join(cert_path,'cert.pem'),
                                   keyfile=os.path.join(cert_path,'key.pem'),
                                   ssl_version=ssl.PROTOCOL_TLS)
    print(f'Starting httpsd server on port 8444')
    httpd.serve_forever()

run()

After running the code, a prompt comes up to provide the PEM pass phrase like below.

Enter PEM pass phrase:

After entering the passphrase, I want to run this program in background. Can someone please suggest how to achieve it? I looked for insights over internet but could not make it work, also referred below post but in my use case there is also an user input use case.

Why not use a web server like apache httpd to server the folder?
It can be done as a service in your OS.

How you put a program into that background is OS specific.
Which OS are you using?
Do you want the program to stop when you logout or keep running?

At the moment requirement is for Ubuntu, but you are right with this approach in Windows it may be difficult in future. The program has to keep running. The problem with this approach I am facing is … as soon as the terminal is closed the server stops serving. Earlier I was using the http variant and I could run that in background like below.

python3 -m http.server 9876 &> /dev/null

Let me explore the apache httpd option, thank you!