M
M
Matvey Uvarov2020-07-05 10:34:45
Apache HTTP Server
Matvey Uvarov, 2020-07-05 10:34:45

How to fix "vncserver: The HOME environment variable is not set" error - run python script from PHP as user?

I run the Python script from PHP . The script starts normally, but when it reaches the vncserver start lines , it cannot start it:

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-

import time
import requests
from datetime import datetime
from pymongo import MongoClient
from multiprocessing import Pool
import json
import math 
import subprocess
import os
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
.
.
.
    ## VNC Display checks
    # Configure DISPLAY variable to VNC
    os.environ["DISPLAY"] = cfg["vnc_display"]
    if os.system("xhost >& /dev/null"):
        print("WARNING: No VNC display opened under %s. Running VNC Server and trying again" % (os.environ["DISPLAY"]))
        os.system("vncserver %s" % (os.environ["DISPLAY"]))
        time.sleep(5)
        if os.system("xhost >& /dev/null"):
            print("ERROR: No VNC display opened under %s. Cannot continue" % (os.environ["DISPLAY"]))
            return
.
.
.
if __name__ == "__main__":
    command_line_runner()

..throws an error:
vncserver: The HOME environment variable is not set
No VNC display opened under :1.
Running VNC Server and trying again.
ERROR: No VNC display opened under :1.
Cannot continue.


As I understand it , vncserver does not want to run with Apache user rights , but only as a user - the error is related to execution rights. Because if you run the script directly, on the command line, on behalf of the user, there are no errors and everything works fine.
QUESTION : how to run python script from PHP as user and get vncserver to start ?
I think that all the same, you need to somehow teach the script to work correctly with Apache rights , but I don’t know how to do it. Please suggest solutions. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Matvey Uvarov, 2020-07-10
@zoddak

Did it like this:

.
.
.
from os.path import expanduser
HOME = expanduser("~")
.
.
.
os.environ["HOME"] = HOME

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question