Host flask api on server locally [duplicate] - flask

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 2 years ago.
I need to host my API on the server. How is it possible? I have done by virtualenv on my computer but I need to host it now on my company server I need to host exactly like virtualenv ? Or there is some other way as in PHP we use xamp?

You simply install flask in a virtualenv or not, and do flask run app.py to run the server, just like the documentation says.
Alternatively, if you want a feature set similar to Apache HTTPd, then you can look into uWSGI

Related

Serve Flask application with GUnicorn on Localhost

I am a noob in this area so please bear with my dumb questions.
I have a Flask application and I want to run that with GUnicorn on my localhost. I looked on Google but almost every tutorial requires a domain name and there isn't much documentation for running it on a mac.
Please tell how can I run the app with GUnicorn on my mac?
I want to use https for the secure communication so how can I change the configuration of Gunicorn to do so?
Any help will be great.
Cheers
Since you want to run it on your localhost you don't need any domain name. You just have to run Python file where you have configured your flask app on your terminal and then you will get a port number where application will be running on your local machine.
Steps to run app
Change your current directory of your terminal to directory where your project is present, using
cd <your directory address>
Now run your app using
python ./<file_name>.Py
Note: If you have both Python 2 and Python 3 installed (Your machine comes with a version of Python 2 but you can install Python 3 as well), you should run
python3 <file_name>.py
Even if you want to deploy on web on a server without your domain name for free you can do it using heroku or any other service as you like.
Running a Flask application on gunicorn is quite simple:
gunicorn <file_name>:app
Gunicorn provides many command-line options – see gunicorn -h. For example, to run a Flask application with 4 worker processes (-w 4) binding to localhost on port 4000 (-b 127.0.0.1:4000):
gunicorn -w 4 -b 127.0.0.1:4000 <file_name>:app

Is it a good practice use Django integrated web server behind a proxy?

I implement a Django website behind a NGINX configured as reverse proxy which serve also the static for Django when it is out of debug mode, all this is in a docker-compose app.
I know by read the Django docs they do not recommend to use integrated web server in a production environment (and it is not at the moment), put it behind a reverse proxy is acceptable to avoid security issue or it is the same as expose it directly?
If it is not an acceptable solution, could you suggest any implementation to not use the Django integrated web server?
Consider that the container structure like the following:
Proxy is the NGINX Official image
www1 & www2 are Python3 Official image with Django installed as described here.
database is the Postgres Official image.
Only the Proxy container is exposed to the external world.
Thanks.
I get my answer, I gonna use gunicorn instead the integrated Django web browser.
I had use documentation present here That describe how to configure gunicorn and nginx on the same host, but using http instead linux socket, instead the command to run the Django integrate web server I have just to run gunicorn like this:
gunicorn --workers=4 --bind=0.0.0.0:9000 --access-logfile - --error-logfile - --log-level debug myapp.wsgi:application
with the previous command I also get logs managed by container.
Off course I also add gunicorn in the requirement file.
I hope this question will help also some one else.

How can I setup my Django server on LAN

I have made a Django employee portal which will be accessed by LAN only.
It works when another employee opens it by typing the IP address of the server on their web browser.
However I don't have much experience with Django and I think that this is not the proper way to do so. I run my server using python manage.py runserver and use sqlite3 as database.
Is this the correct way to do so? How should I deploy my portal.
I am very new to Django and would appreciate some help.
I am using a windows machine and I used pycharm to make my project.
And also I need to know how can I have the server running even when I close pycharm, as ctrl-C or closing pycharm breaks the server
The simplest way to allow everyone on your network to access your Django webserver is to do python manage.py runserver 0.0.0.0:8000
This allows anyone on the network to access it by using your IP address (or computer name) and the port 8000. (so something like 192.168.1.2:8000)
This of course isn't really nice specially if you intent to use this as a production environment. panchicore's answer should help you setup a good production environment.
Setting up Django and your web server with uWSGI and nginx
There is not an official way to do it, what I do effectively, intranet solutions as well, is with nginx and uWSGI (on ubuntu).
Serving with Windows? perhaps: https://www.toptal.com/django/installing-django-on-iis-a-step-by-step-tutorial is a proper way to do so.
I think for ip address issue you can use host names
https://wesbos.com/localhost-mobile-device-testing/
and for running server in background you can use gunicorn with supervisor check this out https://www.agiliq.com/blog/2014/05/supervisor-with-django-and-gunicorn/

How to start Django server that is located in another machine

For business matters i've moved my local server to enterprise's server for doing some tests but i don't know how to start/stop server via local, i guess it's launching via python runserver... but i don't know how to do it with the files allocated in another machine.
Regards!
You should connect to a remote machine, activate virtualenv , python manage.py runserver serverip:port . That's all. Also check that your port is not closed by firewall.

Django + Apache: make project accessible from WWW [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I've developed a Django project running on dev server. Now I am struggling in using Apache as the web server to run this project.
I've changed the port (say, 8088) at Router, and also edited http.conf in Apache dir to listen this port 8088.
Now I can view apache page at http://XX.XX.XX.XX:8088 (using ip) which shows 'It works'..
however, when I tried to run Django project using 'python manage.py runserver 0.0.0.0:8088', it says 'ERROR 10013: An attempt was made to access a socket in a way forbidden by its access permissons'.
It seems that the connection between Django and Apache doesn't work out, since I can view Apache working page but I can't get the project page...
I did configurate mod_wsgi.
maybe there are other silly stuff I've mistakenly done.
if anybody has some advice, that would be much appreciated!
If you've configured mod_wsgi properly, you don't need to run python manage.py runserver. Simply running the Apache daemon will make it listen on the port configured and will make it use the mod_wsgi file you've defined in the site-available.
Re-read this Django - how to use Django with Apache and mod_wsgi to verify you have it set up appropriately. You cannot run python manage.py runserver on a port that is already being listened to by Apache. Hence, the error attempting to access a socket (on port 8088) in a forbidden way.