I'm working on a Flask app where I need to have a socket connection to all files in a specific directory. Right now I'm using a Python3 http.server (https://docs.python.org/3/library/http.server.html) which does what I need, but it's not recommended for production. Is there a way to configure Nginx to function in a similar way?
When configuring your server block include this.
`location /{
include uwsgi_params;
uwsgi_pass unix:///path/to/your/socket.sock;
}
It will automatically create a socket file when you run the uwsgi server.
The functioning of uwsgi is beyond the scope of this question if you want to check it out look at:
detailed uwsgi guide
Related
I am currently working on a second api on an already running server which serves
port 80,
and other virtualhosts connected to flasks for each api port
via apache/httpd on Centos 7.
api1 is running with wsgi
api2 currently on plain flask webserver for testing
Problem is now that I can reach api2 on localhost:api2_port, but not on its external ip adress
I tried mtr from another network which actually logs returning packages from api2 external_ip:api2_port.
There is a hardware firewall between them, which is not under my control, however I was told, that the ports were opened as I asked. I think the mtr output also confirms this.
Questions
Does anyone have an idea how I can debug this?
Is there maybe a problem with multiple flask instances running on the server?
=> This however should've been covered by the localhost:api2_port test
The Virtualhost config is plain basic and works for api1
<VirtualHost *:api2_port>
ServerName external_ip:api2_port
ServerAlias Projectname
DocumentRoot /var/www/projectname
</VirtualHost>
Thanks in advance for any suggestion
Edit 1:
Also
the project files and folders are owned by a non-root user who also runs api1
iptables has rules for tcp input and output for the api ports
selinux is disabled
I am currently out of ideas
Thanks for reading and responses.
The Problem was a missing firewall rule after all, so it is solved.
This is my first time posting and its a pretty long question.
I'm a beginner and I've decided to learn and use Python and DjangoREST to build my first web application.
Unfortunately, I have a lot of issues that I'm not sure how to get answers to. Querying Google one at a time for each component is giving me answers that sometimes conflicts with another component.
First things first, the following is my project setup:-
OS->Ubuntu 16.04.1
Backend->Python3 and DjangoREST
Webserver->uWSGI and nginx
Database->PostgreSQL
Client Side software->AngularJS(which requires the setup of Node.js and NPM)
Now I installed pip and went through the process of creating a virtualenv and ran a test Django app. I was able to get that done.
When I went through the documentation of uWSGI and Nginx over here http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html, I came across the following line
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
This has me slightly confused. Is there a specific order in which I need to install the software I need? And if yes, could you all tell me the correct order?
Thanks in advance!
This is not actually the order you should install software. This is the communication flow in which you(your browser) connect to the django application on your server.
To make work your application in your server you should do the followings:
First install nginx (reverse proxy server and a web server as well)
Install Gunicorn (wsgi server implementation)
Clone your django project in any directory from your remote git (or simply upload it from local pc)
now run the below command inside the project directory
gunicorn YOUR_PROJECT_NAME.wsgi:application \
--name GIVE_A_NAME_AS_YOUR_WISH \
--worker-connections=1000 \
--threads=3 \
--workers NUM_WORKERS \
--user=USER --group=GROUP \
--bind=SERVER_HOST:ANY_PORT_AS_YOUR_CHOICE (TYPICALLY IT IS 8000)
Point number 4 is enough to run your project in your server. check it in the below link
http://YOUR_HOST_ADDRESS:8000/
Now you can redirect all of your traffic to this application by nginx web server configuration. To do so make a file inside /etc/nginx/conf.d/ naming my_app.conf. inside this file copy paste the below code:
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
Hopefully it is okay !
I'm trying to deploy a flask application on my droplet, which is running ubuntu, but every time I change my virtual host file to the domain, it just serves the index of /var/www/html and not the wsgi which I specified in the virtual host file. However, if I use my droplet's IP for "ServerName", it works fine.
Any ideas?
Thanks
I had the same problem. Not sure what causes it, but if it's the same one I have you should be able to fix it by disabling the default virtualhost cofiguration.
a2dissite 000-default
service apache2 restart
This should leave just the .conf file necessary for your flask application.
Also you mention a droplet, so you might be following the DigitalOcean Flask tutorial. If this is the case, don't forget to add the .conf extension to the configuration file in /etc/apache2/sites-available
In the server terminal, enter:
sudo nano /etc/apache2/sites-available/FlaskApp.conf
Then replace the raw IP with the domain name.
I use nginx + uwsgi to deploy my wsgi app. And I have to use the unix sock file for the communication of nginx and uwsgi.
The configuration as bellow:
location / {
include uwsgi_params;
uwsgi_pass unix:/path/to/uwsgi.sock;
}
But the parent path of my project is mutable, So I can not use a fixed absolute path.
And I also can not use a relative path. Because the pwd is mutable too.
So I wander is there any variables in nginx configuration file such as $hostname that can point to the absolute path of the derectory containing the nginx configuration file. But I found no such a variable.
Any good ideas?
PS: I alse don't want use the socket such as 0.0.0.0:8000 for commucation of nginx and uwsgi. And the user run server is not a root user. I wanna put the socket file in the project directory.
I'm not expert with nginx, However;
My recommendation is to use a standard UNIX approach and store your UNIX sockets in either one of two standard locations:
/var/run
/tmp
If storing UNIX Sockets for users I would store them in:
$HOME/var/run -- And document this to your users.
This is similar to how Apache's userdir option works by serving up files from /home/prologic/public_html for requests to GET ~/prologic for example.
I've written a simple CMS in Python. It currently runs on Apache, and it consists of a bunch of python scripts in /usr/lib/cgi-bin. I'm trying to get the CMS working in Nginx. I've never used Nginx before, so I'd appreciate some input from anyone who's familiar with it.
I'd like to run the CMS without having to modify it - ideally I'd like to be able use the CMS with Apache and Nginx, and I don't want to maintain two separate versions.
The main script is /usr/lib/cgi-bin/pyindex.py. I've set up two locations in /etc/nginx/sites-available/default:
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/dir.html /cgi-bin/pyindex.py?q=$uri;
}
location /cgi-bin/ {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8080;
}
I installed uwsgi and uwsgi-plugin-python and set up /etc/uwsgi/apps-available/mydomainname.com.xml as follows:
<uwsgi>
<plugin>python</plugin>
<plugin>cgi</plugin>
<socket>127.0.0.1:8080</socket>
<chdir>/usr/lib/cgi-bin/</chdir>
<pythonpath>/usr/lib/cgi-bin/</pythonpath>
<module>pyindex.py</module>
<cgi>/cgi-bin=/usr/lib/cgi-bin/</cgi>
<cgi-allowed-ext>.py</cgi-allowed-ext>
<master/>
<processes>4</processes>
<harakiri>60</harakiri>
<reload-mercy>8</reload-mercy>
<cpu-affinity>1</cpu-affinity>
<stats>/tmp/stats.socket</stats>
<max-requests>2000</max-requests>
<limit-as>512</limit-as>
<reload-on-as>256</reload-on-as>
<reload-on-rss>192</reload-on-rss>
<no-orphans/>
<vacuum/>
</uwsgi>
When I point my browser at any dynamic page, I see this error message:
uWSGI Error
Python application not found
So it looks like nginx is successfully passing requests to uwsgi, but uwsgi isn't set up correctly.
The log in /var/log/uwsgi/app/mydomainname.com.log shows that pyindex.py runs when uwsgi starts up, and falls over because the QUERY_STRING environment variable isn't available. The script doesn't get executed when a request is passed from nginx to uwsgi.
Is there a way to set this up so that pyindex.py isn't executed on start up, but executes when uwsgi receives a request from nginx?
I'm using Nginx v 1.2.1, uwsgi 1.2.3, Python 2.7 on Raspbian.
Thanks in advance
Forget about the uWSGI python plugin, you do not need it.
You need the CGI plugin:
http://uwsgi-docs.readthedocs.org/en/latest/CGI.html