Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set up uwsgi and nginx?
#1
(sorry for the Google translator)

os Ubuntu 20.04 lts server x64

What is done:
mkdir django & cd django
. env/bin/activate
test project django created
~/django/test_site/test_site

created ~/django/test.py
	def application(env, start_response):
		start_response('200 OK', [('Content-Type','text/html')])
		return [b"Hello World"] # python3
nginx:
upstream django {
server 127.0.0.1:8000;
}

server {
......
location = / {
proxy_pass http://127.0.0.1:8000;
}

execute
:~/django$ uwsgi --http :8000 --wsgi-file test.py
OK. i seе "Hello World"

Further, any attempts to connect the test project and uwsgi were unsuccessful Sad

1. everywhere indicated for testing: uwsgi --http :8000 --module mysite.wsgi
but I don't have such file mysite.wsgi, I only have ~/django/test_site/test_site/wsgi.py !!!

2. It seems to me that this file (wsgi.py) is not correct by default. What else should be there?

3. Accordingly, if i use the ini file for uwsgi, what is there to indicate? example from here from here
does not clarify this question.

4. And I haven't tried sockets yet!

And most importantly: run for tests from a virtual environment or not? Is the virtual environment necessary only for development apparently?
Reply
#2
(Apr-19-2023, 11:10 AM)Stas43 Wrote: (sorry for the Google translator)

os Ubuntu 20.04 lts server x64

What is done:
mkdir django & cd django
. env/bin/activate
test project django created
~/django/test_site/test_site

created ~/django/test.py
	def application(env, start_response):
		start_response('200 OK', [('Content-Type','text/html')])
		return [b"Hello World"] # python3
nginx:
upstream django {
server 127.0.0.1:8000;
}

server {
......
location = / {
proxy_pass http://127.0.0.1:8000;
}

execute
:~/django$ uwsgi --http :8000 --wsgi-file test.py
OK. i seе "Hello World"

Further, any attempts to connect the test project and uwsgi were unsuccessful Sad

1. everywhere indicated for testing: uwsgi --http :8000 --module mysite.wsgi
but I don't have such file mysite.wsgi, I only have ~/django/test_site/test_site/wsgi.py !!!

2. It seems to me that this file (wsgi.py) is not correct by default. What else should be there?

3. Accordingly, if i use the ini file for uwsgi, what is there to indicate? example from here from here
does not clarify this question.

4. And I haven't tried sockets yet!

And most importantly: run for tests from a virtual environment or not? Is the virtual environment necessary only for development apparently?



1. If your project is named test_site and your wsgi.py file is located at ~/django/test_site/test_site/wsgi.py, then the command to start uWSGI should be:

`
uwsgi --http :8000 --wsgi-file ~/django/test_site/test_site/wsgi.py
`

The --module option is used when your project is a Python package and you have a wsgi.py file at the root of your package.

2. The default wsgi.py file generated by Django should be sufficient for most cases. However, you may need to modify it if you have specific requirements. For example, you may need to add middleware, change the static files configuration, or modify the allowed hosts settings.

3. If you want to use an ini file for uWSGI, you can specify the wsgi-file option in the ini file, like this:

`
[uwsgi]
http = :8000
wsgi-file = /path/to/wsgi.py
`

4. Using sockets instead of HTTP is a good idea for production environments because it provides better performance and security. To use sockets, you can specify the socket option instead of the http option:

`
uwsgi --socket /tmp/uwsgi.sock --wsgi-file ~/django/test_site/test_site/wsgi.py
`

5. It is recommended to run uWSGI from within a virtual environment, especially if you have dependencies that are not installed system-wide. The virtual environment isolates your project's dependencies from the system's dependencies, which makes it easier to manage and deploy your application. However, it is not strictly necessary, and you can run uWSGI from the system's Python interpreter if you prefer.
Stas43 likes this post
Reply
#3
I understood. Thank you, everything works! Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python flask uwsgi nginx docker mfaridi 2 1,879 Sep-14-2022, 11:07 AM
Last Post: mfaridi
  run watchdog in flask app with uwsgi experimental 0 2,912 Apr-25-2020, 09:44 PM
Last Post: experimental
  flask to uwsgi threading issue batchenr 1 4,074 Jun-17-2019, 02:12 PM
Last Post: batchenr
  Multi-tier Web application File Storage (NginX, File System or FTP) ? draems 0 1,640 Apr-22-2019, 07:47 AM
Last Post: draems
  flask nginx full stack web app develepoment pascale 5 4,711 Jul-03-2018, 08:20 PM
Last Post: pascale
  location settings for Django Uwsgi Nginx Configuration for production sandyman 0 2,979 Sep-15-2017, 09:08 AM
Last Post: sandyman
  Django+uWsgi unable to find "application" callable rosettas 3 11,961 Aug-24-2017, 01:41 PM
Last Post: nilamo
  Problems configuring and running uWSGI easy_c0mpany80 3 11,802 Dec-06-2016, 09:52 AM
Last Post: easy_c0mpany80

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020