Python Forum
How to set up uwsgi and nginx? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: How to set up uwsgi and nginx? (/thread-39830.html)



How to set up uwsgi and nginx? - Stas43 - Apr-19-2023

(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?


RE: How to set up uwsgi and nginx? - farshid - Apr-20-2023

(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.


RE: How to set up uwsgi and nginx? - Stas43 - Apr-21-2023

I understood. Thank you, everything works! Smile