I'm trying to get nginx/django/uwsgi set up, and I'm stuck at the first step. I'm just trying to get uwsgi to serve a test application directly to my browser, as described here as a basic test.
So, I create the test.py file:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
And then I start it up with $ uwsgi --plugins python --http :8001 --wsgi-file test.py
The output appears to be OK. I get the message WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x3a14c8 pid: 8295 (default app). I would expect that I would then be able to aim my browser at 127.0.0.1:8001, and see a hello world message. However, chrome just tells me that the site can't be reached.
I've been trying various permutations of tests from all over the internet for a solid day, but this is the simplest test I've found, and no one mentions what to do if it fails.
For reference, I'm using uwsgi 2.0.15 and python 3.6.1 on Arch.
EDIT: Evidently someone thought my question "did not show any research effort." So, to summarize what I've tried:
I checked that iptables wasn't blocking anything
I tried it on various ports
I tried moving on with the tutorial to see if it works with a real django project
I tried doing this all in a virtualenv
I tried running from a uwsgi ini file instead of the command line
I tried starting over any using various other tutorials.
Read this post and tried the answer
Output of $ curl -v 127.0.0.1:8001:
* Rebuilt URL to: 127.0.0.1:8001/
* Trying 127.0.0.1...
* TCP_NODELAY set
* connect to 127.0.0.1 port 8001 failed: Connection refused
* Failed to connect to 127.0.0.1 port 8001: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 127.0.0.1 port 8001: Connection refused
Please let me know if you can think of anything else.
Related
I'm setting up a test Python App on a Ubuntu server. I've installed Nginx, Python, and uWSGI. Nginx is able to host static files, but I get the error "uWSGI Error Python application not found" when I try to access a Python app.
I have created a file named "app.py", which is a simple "Hello World" app. This file is added to the same directory as the static HTML file. The static file comes up, so Nginx appears to be working.
When I tried to access the Python app, I got the error "uWSGI Error Python application not found". I searched for this error and one recommendation is from https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html. It mentions I had to run the following command:
uwsgi --http :8000 --wsgi-file test.py
I changed the port number and file name to what I have on my server. But I get the error:
uwsgi: unrecognized option '--wsgi-file'
I googled this error and a link mentioned I would have to install the python plugin: uwsgi options --wsgi-file and --module not recognized
But when I did the commands in this URL, it says I already have the newest version.
If I just type uwsgi on the command prompt, it would give me the message:
* WARNING: you are running uWSGI without its master process manager *
your memory page size is 4096 bytes
The -s/--socket option is missing and stdin is not a socket.
I was wondering if there are any items (e.g. programs, configuration settings) I need to look at to get uwsgi running and the web server able to host Python apps?
You can try running uwsgi by explicitly stating the plugin you want to load:
uwsgi --http-socket :8000 --plugin python --wsgi-file test.py
I am pretty new to Django. I think I cannot run django application as sudo since all pip related modules are installed for the user and not for the sudo user. So, it's a kind of basic question like how do I run django app that can listen for port 80 as well as port 443.
So, far I have tried following option - i.e pre-routing - NAT
I run my app using the following command -
$python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 13, 2018 - 03:04:41
Django version 2.1.1, using settings 'WebBlogger.settings'
Starting development server at http://127.0.0.1:8000/
Next, here is my iptables settings nothing worked for me though
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
$sudo iptables -t nat --line-numbers -n -L
Chain PREROUTING (policy ACCEPT)
num target prot opt source destination
1 DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
2 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 8000
Chain INPUT (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
1 DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination
1 MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0
2 RETURN all -- 192.168.122.0/24 224.0.0.0/24
3 RETURN all -- 192.168.122.0/24 255.255.255.255
4 MASQUERADE tcp -- 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
5 MASQUERADE udp -- 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
6 MASQUERADE all -- 192.168.122.0/24 !192.168.122.0/24
Chain DOCKER (2 references)
num target prot opt source destination
1 RETURN all -- 0.0.0.0/0 0.0.0.0/0
I did http://
and I see connection refused.
I have no idea how to debug the NAT stuff whether it is actually hitting NAT or not. How can I debug and also what is the correct solution to it?
You do not talk to a Django app directly. While Django has a simple development server available via the runserver command, it is not meant for anything but development work.
What you want is to setup a WSGI server to run your app and a web server to accept actual user requests and proxy them to the WSGI. The commonly used WSGI servers (don't worry about what WSGI is) are gunicorn and uWSGI. Both can be installed using PIP and you don't have to install them as the same user as your app. Gunicorn is a bit easier to use, so I would recommend that one. The most common web server nowadays is Nginx.
Also, you should pack your application into a virtual environment, so you can pack it together with all the dependencies without relying on a specific system having everything installed.
Here is a somewhat dated guide on how to do this. It should be mostly accurate though and is a good place to start
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04
In ideal world you will need a web serve to talk to your Django.
Web server (port 80/443) -> gunicorn (wigs) -> Django (port 8000)
And if you just want Django development server to run on 80 then try
python manage.py runserver 0.0.0.0:80
And make sure no other process is using port 80.
I have a GAE application and a bunch of people working on it so to save people the trouble of setting up all the dependencies and whatnot I was hoping to allow them to run the gae development server in a docker container.
My dockerfile ends with:
CMD dev_appserver.py app_localhost.yaml
And my docker-compose is like:
version: '3'
services:
my_image:
build: ./my_image
image: my_image
ports:
- "8080:8080"
- "8000:8000"
volumes:
- ./my_image:/usr/src/
building this works fine. And running it with docker-compute up also seems to work fine. I mean, it has friendly output saying that the default module is accessible at 8080 and all that good stuff.
But if I access localhose:8080 via chrome I get ERR_SOCKET_NOT_CONNECTED. If I try curl it I get curl: (56) Recv failure: Connection reset by peer.
It all runs fine and is accessable when I run it outside the container.
docker ps 56 ↵
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a2ae48f1f66 waxed_backend_image "/bin/sh -c 'dev_a..." 9 hours ago Up 8 hours 0.0.0.0:8000->8000/tcp, 0.0.0.0:8080->8080/tcp dockerpygae_waxed_backend_1
Here's a possibly related problem I have: making requests to localhost from inside docker container It seems that every time I try to communicate with the gae development server in any dockery way things start to go horribly wrong
I changed this:
CMD dev_appserver.py app_localhost.yaml
To this:
CMD dev_appserver.py --host 0.0.0.0 app_localhost.yaml
And now it works fine
Although I dont know why it worked. I'll still appreciate an answer tht is more correct than this one
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.
I found a MediaCrush open source from here
https://github.com/MediaCrush/MediaCrush
But stuck in last steps.
I started the Redis server, use command
$redis-cli
that received the "PONG" response.
Then used the command
$celery worker -A mediacrush -Q celery,priority
and after
python app.py
But it seem that nothing works. I just installed nginx, run it on my IP ok, but after edit the nginx.conf like a Mediacrush script, then accessing my IP, nothing happens.
So what am I missing here? and how to config nginx server and start redis server to run this script on CentOS (i can change it to Arch like them if required)
Thanks!
I just wanted to run it for fun.. so this may be wrong but what I did after running the celery daemon was edit the app.py script and manually set the host, port, and set debug to false. Then I just executed it like any other python script.
EDIT: This may work
PORT=8000 gunicorn -w 4 app:app
it switches your port to 8000 and runs the gunicron daemon with 4 workers. both approaches worked for me.
Edit File: ./app.py
from mediacrush.app import app
from mediacrush.config import _cfg, _cfgi
import os
app.static_folder = os.path.join(os.getcwd(), "static")
if __name__ == '__main__':
# CHANGE THIS LINE TO REFLECT YOUR DATA
app.run(host=_cfg("debug-host"), port=_cfgi('debug-port'), debug=True)
# FOR EXAMPLE I CHANGED IT TO THIS
# app.run(host="92.222.25.245", port=8000, debug=0)
Also to start redis i believe you should do redis-server& I use the cli to manually tinker with it.
Btw I did this on linux mint / ubuntu 14.04 / debian