a python web application on an Unbuntu server using apache2
a python web application on an Unbuntu server using apache2
Author
Discussion

CasioPasio

Original Poster:

227 posts

111 months

Tuesday 31st July 2018
quotequote all
Hi,

We are using Digital Ocean to deploy our software which is connected to our GitHub. However, when we shut the terminal after we have deployed it the server shuts and doesn't allow for users to sign up or login - strange because other parts work.

We have tried using daemon to keep the server running in the background when the terminal has been shut, however, this causes a problem with the login and messes with our username and password system.

Does anyone have experience with this sort of thing?

Regards

EDIT: Ubuntu not Unbuntu.

Taita

8,021 posts

234 months

Tuesday 31st July 2018
quotequote all
Apache2 should be running as a service and being SSH'd in shouldn't affect it.

SSH in and run systemctl status apache2. Is it running OK?

Have for some reason you're devs have set the Apache2 application to run upon a user login or something mental?

ETA: If everything else works it is unlikely apache2, what is your DB running as?

Run ps aux| grep mysql / postgres / mongodb or whatever your DB engine is. Then check out the conf files for it - is it registered as a service / daemon?

tribbles

4,178 posts

253 months

Tuesday 31st July 2018
quotequote all
No idea about your explicit case, but ISTR that on unixy systems, in order to get a program to stay running after you log off, the program needs to close down stdin and stdout.

Perl has a "daemonize" function to do this.

What you could try doing is redirecting input and output to /dev/null. Something like:

./program > /dev/null < /del/null &

Then you can close the terminal, and it shpuld run.

Not done this for a while though!

CasioPasio

Original Poster:

227 posts

111 months

Tuesday 31st July 2018
quotequote all
Taita said:
Apache2 should be running as a service and being SSH'd in shouldn't affect it.

SSH in and run systemctl status apache2. Is it running OK?

Have for some reason you're devs have set the Apache2 application to run upon a user login or something mental?

ETA: If everything else works it is unlikely apache2, what is your DB running as?

Run ps aux| grep mysql / postgres / mongodb or whatever your DB engine is. Then check out the conf files for it - is it registered as a service / daemon?


Good point about the databases, but the thing is, sometimes the user can log in and sometimes they can't. Its not a constant issue, it goes and comes when it likes - which makes it a pain to track.

But we use SQLite for our DBs and I can't find it when I do ps aux.

TonyRPH

13,554 posts

199 months

Tuesday 31st July 2018
quotequote all
If I understand correctly - you have a Python app that you start, and then when logging out / disconnecting from the server, the app stops?

If so, any foreground app will simply exit as soon as you terminate the shell it was started in.

Try starting your app with 'nohup'

e.g. nohup python_app.py > /dev/null 2>&1 &

You could also try: python_app.py > /dev/null 2>&1 & (this should put it in the background)

Alternately, install screen (screen will provide you with a shell process that doesn't terminate when logging out / disconnecting form the server).




Taita

8,021 posts

234 months

Tuesday 31st July 2018
quotequote all
Yup nohup would be next recommendation if it _always_ stops on exit. But that would kill all aspects of the app (assuming it is one big .py).


CasioPasio

Original Poster:

227 posts

111 months

Tuesday 31st July 2018
quotequote all
TonyRPH said:
If I understand correctly - you have a Python app that you start, and then when logging out / disconnecting from the server, the app stops?

If so, any foreground app will simply exit as soon as you terminate the shell it was started in.

Try starting your app with 'nohup'

e.g. nohup python_app.py > /dev/null 2>&1 &

You could also try: python_app.py > /dev/null 2>&1 & (this should put it in the background)

Alternately, install screen (screen will provide you with a shell process that doesn't terminate when logging out / disconnecting form the server).
Ohhhh mate, where can we meet? I want to buy you a pint biggrinbeer

python app.py > /dev/null 2>&1 &

That worked!

Massive thanks to everyone for the responses.

TonyRPH

13,554 posts

199 months

Tuesday 31st July 2018
quotequote all
Happy to help.

If this Python app is web facing, you might want to consider running it as a non privileged user, if not doing so already.

According to this page on Stackoverflow, you can also launch Python apps as daemons using systemd.

Stackoverflow said:
[Unit]
Description=Python daemon
After=syslog.target
After=network.target

[Service]
Type=simple
User=<run as user>
Group=<run as group group>
ExecStart=/usr/bin/python <python script home>/script.py

# Give the script some time to startup
TimeoutSec=300

[Install]
WantedBy=multi-user.target

GrumpyTwig

3,354 posts

188 months

Tuesday 31st July 2018
quotequote all
As Tony says, do yourself a favour and set it up as a service. At least then you're doing it correctly and can set up so your app restarts if it crashes.

Ynox

1,755 posts

210 months

Wednesday 1st August 2018
quotequote all
+1 for running it as a service. Way better than messing about with redirects to /dev/null or nohup.

Also I'd strongly recommend ensuring it's running with the least amount of privileges required.

CasioPasio

Original Poster:

227 posts

111 months

Wednesday 1st August 2018
quotequote all
Thanks for the replies.

I am definitely looking into this, we're just working on a few features then I'll sit down to make it a service.

I am running the Linux through Git Bash right now, so going forward I should probably install Ubuntu before I go ahead further. The amount of Linux I still need to learn is huge though.

I've learned all this in 1 day just to deploy our software, but I am looking into this more and more in my spare time.


GrumpyTwig

3,354 posts

188 months

Wednesday 1st August 2018
quotequote all
CasioPasio said:
Thanks for the replies.

I am definitely looking into this, we're just working on a few features then I'll sit down to make it a service.

I am running the Linux through Git Bash right now, so going forward I should probably install Ubuntu before I go ahead further. The amount of Linux I still need to learn is huge though.

I've learned all this in 1 day just to deploy our software, but I am looking into this more and more in my spare time.
Sounds like an odd position whereby you've got people adding features to something but the deployment mechanism is down to you.
Aren't your developers involved in how their software is deployed?