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 !
Related
I have a Python + FastAPI website, I use Gunicorn as the server. The website has been running successfully in local environment.
I have created a VM instance with Ubuntu 20.04 on Oracle Cloud and installed all needed Python packages.
I also followed this article to setup/enable Internet Access with port 5000 in Step 3 and Updated firewall settings in Step 4
I am not using virtual env.
Now I ssh into the VM and cd into the project root folder where the main.py is and ran this command:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app --timeout 1200 --bind 0.0.0.0:5000
Here is how it looks like in the command line:
Here is how the subnet details look like:
Then when I tried to access the website by this link:
subnet05182336.vcn05182336.oraclevcn.com
My website is never showing up.
Server not Found
is displaying on the Tab.
Update 1
I found that I should use ip address( http://168.138.12.192/) to access my website.
I followed a new article to setup Nginx and now when accessing the IP, I can see the default Nginx website has been set up.
Here is my Ingress Rules:
How could I somehow make Nginx works with Gunicorn to see my Python website? Or maybe I don't even need Gunicorn any more after installed Nginx?
Could you please confirm from where are you trying to access the website? The DNS domain name that you have mentioned is only for internal within the OCI use, this cannot be accessed from outside. For that you need to configure Custom DNS zone.
Please try to access the website from a new terminal, connect to your Ubuntu VM with your SSH keys, and then in the command line enter curl localhost:5000. Or, you can connect your browser to the public IP address assigned to your VM: http://<x.x.x.x>:5000.
i am trying to deploy a flask app onto aliyun ecs following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04 sorry if any of these questions are stupid, i am an absolute new comer to nginx and gunicorn.
i have questions about a few steps of the tutorial.
the tutorial creates a sample flask project for demonstration, but i have one from a git repository. after i git clone that repository onto my server, i have an extra directory inside the "myproject" directory, which differs from the tutorial. is that ok? or will that cause problems.
the tutorial saids to configure flask to run with host = 0.0.0.0 so that it listens to all ips of that network, however, after i write app.run(host="0.0.0.0"), my flask app still runs with a specified host as the following image.
running at the server's ip instead of 0.0.0.0
the tutorial instructs me to cd into the directory where app.py is located, and bind to gunicorn. and then use a browser into my server ip at port 5000. howver, i cannot load that page and it saids this site cannot be reached, [server ip] took too long to respond.
then i continued with the tutorial, and the after the step that told me to sudo systemctl status myproject, instead of what the expected output is, i get the expected output plus the following message tagged to the end.
active status plug error message in the end
finally, after completing the tutorial, i receive 502 bad gateway instead of my site.
If you want to extract the contents of your GitHub repo into the current directory use - git clone <YOUR REPO URL> . Look at the (.) at the end it instructs git to clone in the same directory instead of creating a new folder.
You can try this - app.run(host='0.0.0.0', port='5000')
Site cannot be reached because your port is blocked and you will have to enable the incoming and outgoing connections and enable the port as well (A quick google search will give you commands) (Also try to ping your server with port number and check if the port is blocked or not).
The systemctl thing is there to run your app every time you restart you server. Even if you don't do this and don't restart your server then the above three steps are sufficient.
I only have ssh access to a server. It is ubuntu from amazon aws. I have uploaded my django project to it. When I run the command python manage.py runserver or any variants of it, the server starts.
But as you know this will not allow me to access the web application
from my browser which resides on a different network.
My question is
What would be the way that after I give my command to runserver either via django's development server or gunicorn. I may access it from the outside world by typing IP:8000 in my browser.
Secondly I would like to turn the access to IP:8000 on/off as needed. Like I may allow access for 5 minutes and then run a command to stop the access from outside world?
For accessing the django project else every where you have use the command
python manage.py runserver 0.0.0.0:8000
then You will be access it outside by giving the IP:8000
Note : the server provided by the Django is only for the development purpose only . If you want a permanent hosting you may like to use Apache or any server , where You can host your project. Hope This helps . :)
You need assign a elastic IP address (EIP) on the aws ec2 instance, and allow the inbound traffic 0.0.0.0/0 to port 8000 from its security group.
Then you should be fine to access it from every where with EIP, such as http://54.12.23.34:8000
EIP may generate cost, if you stop the ec2 instance.
You may also want to try the command:
python manage.py runserver [::]:8000
This will bind runserver to all available interfaces on your AWS VM. Please also note: runserver is really just meant for development. Running it in production is a bad idea.
You can use http://localtunnel.me/, it's very very easy but you should use it only for development purposes.
Just in case, you want to run it on 80 port then you need root permissions
sudo python manage.py runserver 0.0.0.0:80
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
I have an Ubuntu 12.04 server setup that currently runs a Ruby on Rails application on a Passenger / Nginx install. I decided to play around with some Python and wrote a small application using Bottle. I would like to deploy this application to my server.
I followed this guide on setting up my server to run Python applications. When I run sudo service uwsgi restart I get the following error message:
Restarting app server(s) uwsgi
[uWSGI] getting INI configuration from
/usr/share/uwsgi/conf/default.ini [uWSGI] parsing config file /etc/uwsgi/apps-enabled/example.net.xml
open("./python_plugin.so"): No such file or directory [core/utils.cline 4700]
!!! UNABLE to load uWSGI plugin: ./python_plugin.so: cannot open shared object file: No such file or directory !!!
Sat Dec 8 18:29:14 2012 - [WARNING] option "app" is deprecated: use the more advanced "mount" option
I really don't know a ton about Python, I have installed the plugins I need via easy_install
Which are:
pymongo
beautifulsoup
bottle
My question is: how do I deploy this simple application to my server?
Thank you
I found out that Passenger will run WSGI apps. I followed the instructions on this post http://kbeezie.com/using-python-nginx-passenger/ and had no trouble getting it working.
It was actually pretty easy in the end.
Here is my adaptor in case anybody else has trouble:
https://github.com/nick-desteffen/astronomy-pics/blob/master/passenger_wsgi.py