Apache & mod_wsgi settings for Django Python Project - python

I am trying to setup my django-python project on Google Compute Engine Debian8 VM. I made few config changes in /etc/apache2/sites-available/000-default.conf & /etc/apache2/sites-available/default-ssl.conf files
Then tried to restart the server. And I got following Errors. Uninstalling & reinstalling the apache2 too is not fixing this error. Any suggestions to fix this issue ?
Command:
sudo service apache2 restart
Error:
Job for apache2.service failed. See 'systemctl status apache2.service' and 'journalctl -xn' for details.
Command:
sudo systemctl status apache2.service -l
Error:
apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2)
Active: active (exited) since Wed 2016-08-31 05:08:43 UTC; 3min 37s ago
Process: 3539 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
Process: 3521 ExecReload=/etc/init.d/apache2 reload (code=exited, status=1/FAILURE)
Process: 3546 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
Unit apache2.service entered failed state.
Starting LSB: Apache2 web server...
Starting web server: apache2(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
.
Started LSB: Apache2 web server.
Thanks,

Have you read the error ? It's clearly stated why it fails:
Cannot load /usr/lib/apache2/modules/mod_wsgi.so into server: /usr/lib/apache2/modules/mod_wsgi.so: cannot open shared object file: No such file or directory
do you have libapache2-mod-wsgi or libapache2-mod-wsgi-python3 installed? if you have access to shell you can install it with
sudo apt-get install libapache2-mod-wsgi
# or for python 3
sudo apt-get install libapache2-mod-wsgi-python3

Ran the Command to see the process running on port 80:
sudo netstat -ltnp | grep ':80'
Output:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24482/nginx -g daem
tcp6 0 0 :::80 :::* LISTEN 24482/nginx -g daem
Running the next two commands then fixed the issue:
sudo kill -9 24482
sudo service apache2 restart
Hope it can be helpful.
Thanks,

Related

docker.errors.DockerException: Error while fetching server API version

I want to install this module but there is something wrong when I try the step docker-compose build ...
I tried to update the Docker version and restart Docker many times. But it didn't work.
git clone https://github.com/uhh-lt/158.git
cd 158
docker-compose build
File "/home/ming/.local/bin/docker-compose", line 8, in <module>
sys.exit(main())
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/main.py", line 67, in main
command()
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/main.py", line 123, in perform_command
project = project_from_options('.', options)
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/command.py", line 60, in project_from_options
return get_project(
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/command.py", line 131, in get_project
client = get_client(
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/docker_client.py", line 41, in get_client
client = docker_client(
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/docker_client.py", line 170, in docker_client
client = APIClient(**kwargs)
File "/home/ming/.local/lib/python3.8/site-packages/docker/api/client.py", line 188, in __init__
self._version = self._retrieve_server_version()
File "/home/ming/.local/lib/python3.8/site-packages/docker/api/client.py", line 212, in _retrieve_server_version
raise DockerException(
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
Update 2020-11-23
Thanks for two of you helping me with the error! I tried the commend but can't connect to my Docker:
ming#KITM-7664:~$ sudo /etc/init.d/docker start
[sudo] password for ming:
* Starting Docker: docker [ OK ]
ming#KITM-7664:~$ which docker
/usr/bin/docker
ming#KITM-7664:~$ docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:52 2020
OS/Arch: linux/amd64
Experimental: false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
ming#KITM-7664:~$ systemctl status docker
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
ming#KITM-7664:~$ systemctl start docker
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
ming#KITM-7664:~$ sudo /etc/init.d/docker start
* Starting Docker: docker [ OK ]
ming#KITM-7664:~$ docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:52 2020
OS/Arch: linux/amd64
Experimental: false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Are you sure docker is running on your system? You can get that error when compose is not able to connect to docker via docker socket (if any other way for connection is not defined).
If you are running on linux, usually you can run systemctl status docker to check if docker daemon is running and systemctl start docker to start it.
It would help to tell what OS and docker version are you using.
set the permission like this,
sudo chmod 666 /var/run/docker.sock
When used WSL (Windows Subsystem for Linux) need to enable 'WSL Integration' for required distro in Windows Docker Desktop (Settings -> Resources-> WSL Integration -> Enable integration with required distros).
By default, the docker command can only be run the root user or by a user in the docker group, which is automatically created during Docker’s installation process. If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:
sudo usermod -aG docker ${USER}
To apply the new group membership, log out of the server and back in, or type the following:
su - ${USER}
You will be prompted to enter your user’s password to continue.
sudo service docker start
or
sudo service docker restart
Just had the same issue after updating Windows Docker desktop to it's latest version (20.10.2, build 2291f61). It happened that this update disabled the WSL2 integration with my virtual Ubuntu 18.04, which I use to run most projects.
I solved it this way:
Open Docker Desktop
Go to Settings > Resources > WSL Integration
Make sure that your distribution is enabled
Restart Docker
Without the need of restarting WSL2, docker should work again
I had a similar issue and it turned out to be due to Docker server not running. I started the app and then ran docker-compose up and it started working fine. Hope it helps anyone who's caught in a similar situation. :-)
You don't have permission to use the docker socket, by default only the docker group can access it. You can verify this with ls -l /var/run/docker.sock, which will print something like:
srw-rw----. 1 root docker 0 Oct 4 18:04 /var/run/docker.sock
To be able to access the socket and use Docker, add yourself to the Docker group with the following command:
sudo usermod -a -G docker $(whoami)
Then logout and back in. Docker will now work.
Make sure you check the box for 'use Docker Compose V2'. It solved for me.
docker desktop
If you're using docker-compose inside podman try the below command to resolve this issue "docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))"
systemctl start podman.socket
I had the same problem but I managed to solve it when I ran docker server.
I'm also get this error when i try to run the docker-compose and docker desktop is turn off, i think that is bug in docker-compose that not notify the user it that the problem is becuse the docker service is down, same as the docker cli that throw correct error:
C:\Users\x\IdeaProjects\mongo-exmple>docker pull node
Using default tag: latest
error during connect: This error may indicate that the docker daemon is not running.: Post "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/images/create?fromImage=node&tag=latest": open //./pipe/docker_engine: The system cannot find
the file specified.
In Mac OS and Windows sometimes that's enough to start up docker application and this error is because of loss of docker daemon in system.
My issue was I was not signed into docker desktop.
At Linux (at least Ubuntu 18.04.x), you sometimes need to log out and re-login your X user session. Logging out of bash or out of the terminal window and re-logging in is not enough, see for example https://askubuntu.com/questions/1161020/groups-and-groups-user-show-different-groups-dialout-is-missing
I had this happen on a Mac after doing an OS update.
I was able to run docker on the command line but Docker Desktop wasn't running - starting Docker Desktop up fixed the issue.
In Windows exiting from Docker and running it again sometimes helps. Apparently not always docker starts up properly.
I had this problem while using ubuntu 22.04 wsl on windows and then I solve it like this
sudo update-alternatives --config iptables
it will show something like this
Selection Path Priority Status
------------------------------------------------------------
0 /usr/sbin/iptables-nft 20 auto mode
* 1 /usr/sbin/iptables-legacy 10 manual mode
2 /usr/sbin/iptables-nft 20 manual mode
then press 1
Press <enter> to keep the current choice[*], or type selection number: 1
Start docker using services
sudo service docker start
I had this error running docker compose in Linux Subsystem on Win10, the problem was that I have forgotten to start Docker Desktop application

Trying to configure gunicorn

I have been following this tutorial to deploy my django project on Digital Ocean. I am trying to configure gunicorn.
My project structure looks similar to this:
On my settings.py I use DEBUG=False
I create the gunicorn.socket and gunicorn.service.
/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=someuser
Group=www-data
WorkingDirectory=/home/someuser/myproject
ExecStart=/home/someuser/myproject/myprojectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
Myproject.wsgi:application
[Install]
WantedBy=multi-user.target
I start and enable the Gunicorn socket:
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
Check the status of the process to find out whether it was able to start:
sudo systemctl status gunicorn.socket
This is what I get and then it returns to the command line.
Failed to dump process list, ignoring: No such file or directory
β—� gunicorn.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
Active: active (listening) since Sat 2019-05-04 23:12:03 UTC; 13s ago
Listen: /run/gunicorn.sock (Stream)
CGroup: /system.slice/gunicorn.socket
May 04 23:12:03 myproject systemd[1]: Listening on gunicorn socket.
Next, I check for the existence of the gunicorn.sock file within the /run directory:
file /run/gunicorn.sock
/run/gunicorn.sock: socket
It seems that a file or directory doesn't exist, but it doesn't provide any more details. The gunicorn.sock seems to exist.
I am familiar with Python but not with servers and deploying so I am at a loss here.
I have successfully followed the similar tutorial for Ubuntu 16.04 a few months ago, but now I keep hitting issues.
I know, that's old question, but...
I haved some error. In my case wrong WorkingDirectory and application with modul wsgi. I think that's error in "myproject.wsgi:application"
This is actually an issue with your usage of systemd. You need to refer to the name of the service in your calls to systemctl, not the socket as you're doing. In your case the service will be called gunicorn because you've named your systemd unit file as gunicorn.service.
systemctl enable gunicorn
systemctl start gunicorn

Ubuntu Server 16.04 systemctl service with python script is running but not working

i have read a lot of posts on this site on how to implement a python script as a service.
After fiddling around i am at the point that the service is started via systemctl (and running) but the script is doing nothing...
My config file in /etc/systemd/system/:
[Unit]
Description=tg Bot
[Service]
Type=simple
User=user
WorkingDirectory=/home/user/tg_onduty/
ExecStart=/usr/bin/python3 /home/user/tg_onduty/on_duty.py
Restart=always
[Install]
WantedBy=multi-user.target
Output:
user#server:~$ sudo service tg_onduty status
● tg_onduty.service - Telegram OnDuty Bot
Loaded: loaded (/etc/systemd/system/tg_onduty.service; enabled; vendor preset
Active: active (running) since Thu 2018-02-15 11:28:20 CET; 2min 17s ago
Main PID: 1538 (python3)
Tasks: 9
Memory: 17.7M
CPU: 351ms
CGroup: /system.slice/tg_onduty.service
└─1538 /usr/bin/python3 /home/user/tg_onduty/on_duty.py
I have read https://unix.stackexchange.com/questions/339638/difference-between-systemd-and-terminal-starting-program/339645#339645 and understand that running the script via systemctl is different than running via CLI (via CLI /usr/bin/python3 /home/user/tg_onduty/on_duty.py is working).
My question is now:
How can i trace or see whats going wrong or why the script seems to do nothing?
Via Journalctl i only see: Feb 15 11:56:17 server systemd[1]: Started tg Bot.
Any help is appreciated.
Thanks,
David
Are you sure you script not running at all? try to put logs in your script to see if something inside the script doesn't work.
Add a simple action to the script. maybe echo something or create a dummy file. That is the easiest way to know if it's working

Getting "502 Bad Gateway" with nginx, uwsgi python-flask on ubuntu 16.04

I am following the this to deploy a flask app(simple hello world) on the Ubuntu 16-04. digital Ocean tutorial
Everything works fine till Testing uWSGI Serving. After that I followed the step as described and when I finally reach the bottom and check server IP address then I got:
502 Bad Gateway
Ok fine. I searched and checked my error log, I got this :-
2017/01/16 05:29:27 [crit] 20714#20714: *2 connect() to unix:/home/sajjan/project/project.sock failed (2: No such file or directory) while connecting to upstream, client: xx.9.xxx.xxx, server: 138.xxx.xx.xxx, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: "xx.xx.xx.xx"
So After taking a error log , I created the file project.sock manually. again Go to server ip address and then same error "502 Bad Gateway"
Again checked the error log and found this
2017/01/16 06:07:11 [crit] 20874#20874: *1 connect() to unix:/home/sajjan/project/project.sock failed (13: Permission denied) while connecting to upstream, client: 47.9.237.113, server: XX.XX.XX.XX, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: " XX.XX.XX.XX "
I figured out about the permission issue and change the permission using below command
sudo chmod 666 project.sock
Now I checked the permision( using ls -l filename)
-rw-rw-rw- 1 root root 0 Jan 16 05:31 project.sock
Now I go back to check the server's IP but found the same "502 Bad Gateway".
Again I checked the error log and found this :
017/01/16 06:13:31 [error] 20897#20897: *6 connect() to unix:/home/sajjan/project/project.sock failed (111: Connection refused) while connecting to upstream, client: 47.9.237.113, server: XX.XX.XX.XX, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: " XX.XX.XX.XX ", referrer: "http:// XX.XX.XX.XX /"
I googled for above error read a lot in last two days but nothing to seem working for me .
I have check these answers but no help stackanswer-1 stackanswer-2 and along with these I checked all the digital-ocean community thread but nothing seems to work.
I am total begineer to servers and don't know much about ubuntu. If you can help me to find out what wrong am I doing or suggest some better tutorial/ways to deploy my flask application, then I would be greatful.
These are my files
hello.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0')
project.ini
[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = /home/sajjan/project/project.sock
chmod-socket = 660
vacuum = true
die-on-term = true
wsgi.py
from hello import app
if __name__ == "__main__":
app.run()
Below is file : /etc/nginx/sites-available/project
server {
listen 80;
server_name 138.197.28.107;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/sajjan/project/project.sock;
}
}
When I run the command :
sudo service uwsgi restart
output:
Failed to restart wsgi.service: Unit wsgi.service not found.
while output of
sudo service nginx status/restart
then this show that nginx is running .
Help me, If anything else that you want to know then let me know.
Thanks
EDIT :
I have created a project.service file and its conetent is :
[Unit]
Description=uWSGI instance to serve project
After=network.target
[Service]
User=sajjan
Group=www-data
WorkingDirectory=/home/sajjan/project
Environment="PATH=/home/sajjan/project/venv/bin"
ExecStart=/home/sajjan/project/venv/bin/uwsgi --ini project.ini
[Install]
WantedBy=multi-user.target
I figured out I have to run below command :
sudo systemctl start project
Output is :
Warning: project.service changed on disk. Run 'systemctl daemon-reload' to reload units.
and when I run
sudo systemcl reload project
then output :
Failed to reload project.service: Job type reload is not applicable for unit project.service.
See system logs and 'systemctl status project.service' for details.
and when I check the "systemctl status project.service"
● project.service - uWSGI instance to serve project
Loaded: loaded (/etc/systemd/system/project.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2017-01-16 17:49:29 UTC; 6min ago
Main PID: 27157 (code=exited, status=203/EXEC)
Jan 16 17:49:29 learningwithpython systemd[1]: Started uWSGI instance to serve project.
Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Main process exited, code=exited, status=203/EXEC
Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Unit entered failed state.
Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Failed with result 'exit-code'.
I had the same problem using the guide. As far as I've read; 502 bad gateway is a symptom of Nginx not being able to properly connect to the uwsgi. Changing the permissions on the socket solved the issue for me.
sudo chmod 777 /home/sajjan/project/project.sock
sudo systemctl restart nginx
777 is offcourse a bit excessive, but its a quick and dirty way to verify if it is in fact a problem with permissions
Nginx has no permission to write to socket. Granting appropriate mode with below command, helped me.
chmod 0755 /to/project
Same problem.
But I give the 666 permission and restart all, and it works.
I think the error log only show the problem's one possible cause. While, the journalctl -u <yourproject>.service command help present another reason.
My error log also tells me that he can't find the "myproject.socket". But .ini has already help us build it. Then I get this error: myproject.service: Failed at step USER spawning ~/bin/uwsgi: No such process
So maybe it is the permission's problem.
Try running myapp/bin/uwsgi --ini myapp.ini to see the actual error thats preventing uwsgi to run.
In my case 5 processes in the .ini configuration file were to much to handle for my cpu this was my error output.
your processes number limit is 3900
your memory page size is 4096 bytes
detected max file descriptor number: 1024
If this is your case lowering the number of processes to 2 in your .ini file might work.
For me this problem was due to the users configuration. Basically, I set up all of my project using the root user. But then I redid everything from scratch using a non-root user with sudo privileges. It worked.
I saw your comment on https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04
try run sudo /etc/init.d/nginx start
Then try open http://server_domain_or_IP
If it works, type which uwsgi to find the right uwsgi path and change "/etc/systemd/system/myproject.service"
change
Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
ExecStart=/home/sammy/myproject/myprojectenv/bin/uwsgi --ini myproject.ini
into the real path instead of env path.
I figure out this problem through these command.
As many people already mentioned, the 502 error is about socket permissions (you may see Permission denied errors in /var/log/nginx/error.log). However, it all seems to have to work, since
socket's file ownership is your_user:www-data
socket's file permissions are 660 (which means that you and group can read/write socket)
nginx works with the user www-data:www-data (it's configured in nginx .conf file)
The real problem is that nginx can't connect to the socket located in your home directory!
So the solution is simple - just move the socket file somewhere else. For example, to /tmp/ or /var/www/... folders.
Solution:
Create directories
sudo mkdir /var/www/your_project
sudo chown your_user:www-data /var/www/your_project
Modify your_project.ini
socket = /var/www/your_project/your_project.sock
Modify nginx server block
uwsgi_pass unix:///var/www/your_project/your_project.sock;
Restart uWSGI and nginx
sudo systemctl restart your_project_service
sudo systemctl restart nginx
And it all has to work now.

How to reflect python changes in django, uwsgi and nginx setup

Hi I have deployed Django using UWSGI and Nginx using following tutorial http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
Everything is running fine. I face a challenge while updating python code. I don't know the efficient way to deploy new changes.
after hit and trial, I used following commands to deploy
git pull; sudo service uwsgi stop; sudo service nginx restart; sudo service uwsgi restart; /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals
this command works fine. But I face following problems
Usagi runs in the foreground. Every time I make changes, a new UWSGI instance start running.
Due to multiple UWSGI instances, My AWS server get crashed, due to memory exhaustion.
I want to know what commands should I run to reflect changes in python code.
PS: in my previous APACHE Django setup, I only used to restart apache, is it possible to reflect changes by only restarting nginx.
Try this:
git pull
python manage.py migrate # to run any migrations
sudo service uwsgi restart
Press Ctrl + Z and then bg + enter
This should run the process in the background.
Please let me know if this works.
Please have a look at this for running uwsgi in background. create an .ini file /etc/uwsgi/sites/projectname.ini. The script would look like this(for ubuntu 16.04):
[uwsgi]
project = projectname
base = projectpath
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 5
socket = %(base)/%(project)/%(project).sock
chmod-socket = 666
vacuum = true
(For ubuntu 16.04):
then create the following systemd script at /etc/systemd/system/uwsgi.service:
[Unit]
Description=uWSGI Emperor service
After=syslog.target
[Service]
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Refresh the state of the systemd init system with this new uWSGI service on board
sudo systemctl daemon-reload
In order to start the script you'll need to run the following:
sudo systemctl start uwsgi
In order to start uWSGI on reboot, you will also need:
sudo systemctl enable uwsgi
You can use the following to check its status:
systemctl status uwsgi
(For ubuntu 14.04):
Create an upstart script for uWSGI:
sudo nano /etc/init/uwsgi.conf
Then add following lines in the above created file:
description "uWSGI application server in Emperor mode"
start on runlevel [2345]
stop on runlevel [!2345]
setuid user
setgid www-data
exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/sites

Categories

Resources