#Media files not working in ubuntu/nginx

569 messages · Page 1 of 1 (latest)

midnight umbra
#

Hi, so some time ago i had a problem with static files and one of the Moderators/Helpers told me how to fix it by doing this:
"
In settings.py
STATIC_ROOT = "/srv/http/static/"
MEDIA_ROOT = "/srv/http/media/"
In nginx.conf:

  location /static {
    autoindex on;
    root /srv/http/;
  }

  # ATTENTION: /home/ubuntu/jogo/project/media 
  location /media {
    autoindex on;
    root /srv/http/;
  }

After that, restart your wsgi server (gunicorn?) and nginx, run collectstatic, and if you have media files, move them from /home/ubuntu/jogo/project/media/ to /srv/http/media/...
"

well it worked perfectly for my static files, but media files aren't working and i get a "Server Error (500)" when i try to upload something in my admin area.

Just to say, it was working perfectly in my localhost before deployment

acoustic phoenix
#

Sounds like me 😂
What does ls -lh /srv/http/ print?

midnight umbra
#

let me see

#

ubuntu@ip-172-31-45-192:~$ ls -lh /srv/http/
total 8.0K
drwxr-xr-x 4 ubuntu ubuntu 4.0K Apr 11 16:52 media
drwxr-xr-x 5 ubuntu ubuntu 4.0K Apr 11 16:33 static
ubuntu@ip-172-31-45-192:~$

#

it shows this @acoustic phoenix

#

so i have all permissions

acoustic phoenix
midnight umbra
#

sorry it might take a little more because i stopped my instance(few free hours left in my account)

#

and it changed the ip omg

acoustic phoenix
#

No worries

midnight umbra
#

so i have to reconfigure things

midnight umbra
#

i updated the .env and restarted nginx so it applied the DEBUG = True i guess

#

but nothing changed

acoustic phoenix
#

Did you restart the gunicorn service?

midnight umbra
#

i will do that now

acoustic phoenix
#

That's what applies changes to Django code.

midnight umbra
#

i am getting a bad gateway now for some reason damn

#

ubuntu@ip-172-31-45-192:~$ sudo systemctl daemon-reload
ubuntu@ip-172-31-45-192:~$ sudo systemctl restart blog.service
ubuntu@ip-172-31-45-192:~$ sudo systemctl restart blog.socket
ubuntu@ip-172-31-45-192:~$ sudo systemctl restart blog
ubuntu@ip-172-31-45-192:~$ sudo systemctl restart nginx
ubuntu@ip-172-31-45-192:~$
after i did these things

#

blog.socket: Failed with result 'service-start-limit-hit'. lol

#

fuck my life

#

it just doesnt work anymore

midnight umbra
#

by tomorrow it will be fixed

#

since i will do everything again

#

@acoustic phoenix up to which time can you help me ?
because i have few hours left in my free acount this month, so if you cant stay for long today i will stop working in this today, since i have to make a new instance etc.

#

i will do that tomorrow then

#

could my original problem have any relation with having to change this MEDIA_URL = '/media/' ?

midnight umbra
#

Alright see you tomorrow!

acoustic phoenix
#

Hey, sorry, I'm in European time 😅

#

Ping me when you're back at it

midnight umbra
#

@acoustic phoenix My bad, my limit got 100%, so i will ask for help again next month 😅

#

i cant do anything for now

#

do you have experience with AWS EC2? if i interrupt my instances it shouldnt ask for payment right?

worldly bone
#

Anyway you need to grab the log why you get the server error. Could setup LOGGING too

acoustic phoenix
#

If you have enough free space and RAM, you can always try running Ubuntu server on VirtualBox.

#

That's free, and very close to the setup you get in a VPS.

midnight umbra
midnight umbra
#

probably my pc will start slowing down

#

😅

midnight umbra
#

there is one of them that i want just to interrupt : (

acoustic phoenix
#

No idea. I have not used AWS in years.

midnight umbra
#

ah no problem

midnight umbra
#

hii

#

i am back at it, an important thing that i should note is that i was using docker

midnight umbra
#

i am trying to put my site on again and this is happening:

#

File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 330, in cursor return self._cursor() ^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 306, in _cursor self.ensure_connection() File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection with self.wrap_database_errors: File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection self.connect() File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 270, in connect self.connection = self.get_new_connection(conn_params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#

File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection connection = self.Database.connect(**conn_params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/psycopg/connection.py", line 728, in connect attempts = conninfo_attempts(params) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/psycopg/_conninfo_attempts.py", line 45, in conninfo_attempts raise e.OperationalError(str(last_exc)) django.db.utils.OperationalError: [Errno -3] Temporary failure in name resolution

#

please help : (

worldly bone
#

It can't connect to your DB apparently, but can't say I ever seen this error

#

something in network or docker I suppose

acoustic phoenix
#

If you use docker compose you need a healthcheck.

midnight umbra
acoustic phoenix
#

This is mine. It ensures that Postgres is running before Django starts.

services:
  backend:
    build:
      target: backend
    depends_on:
      postgres:
        condition: service_healthy
    ...
  postgres:
    container_name: postgres
    image: postgres:16
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 6
midnight umbra
#

damn i feel so bad its the second instance that just doesnt work, i always use the same configurations but again it goes wrong

acoustic phoenix
#

Comose file. As I said, that's only if you use compose 😅

#

If you don't, share your DB settings (with private info censored)

midnight umbra
#

SECRET_KEY = '0idKvc9BfAoJlpzRlqtS<Gf2qufk5$RIVNfnM0'

#0 false, 1 true

DEBUG= True

ALLOWED_HOSTS="127.0.0.1, localhost, 51.44.19.194"

DB_ENGINE="django.db.backends.postgresql"
POSTGRES_DB="blog_base_de_dados"
POSTGRES_USER="blog_user"
POSTGRES_PASSWORD="blog_user_password"
POSTGRES_HOST="psql"
POSTGRES_PORT="5432"

thats my .env

sudo -u postgres psql

postgres=# CREATE ROLE blog_user WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'blog_user_password';
postgres=# CREATE DATABASE blog_base_de_dados WITH OWNER blog_user;
postgres=# GRANT ALL PRIVILEGES ON DATABASE blog_base_de_dados TO blog_user;
postgres=# \q

sudo systemctl restart postgresql

thats the commands i used

#

: (

#

i kinda have no hopes anymore

#

there is always an error

acoustic phoenix
#

POSTGRES_HOST="psql" what does psql refer to?

midnight umbra
#

damn

#

it should be postgree_sql or something like that right

#

idk

acoustic phoenix
#

Depends. Is it a separate host?

#

Did you assign it a host name as part of a VPC or something?

midnight umbra
#

hm it should be localhost

acoustic phoenix
midnight umbra
#

● blog.socket - gunicorn blog socket
Loaded: loaded (/etc/systemd/system/blog.socket; enabled; preset: enabled)
Active: active (listening) since Thu 2024-05-02 17:08:37 UTC; 20s ago
Triggers: ● blog.service
Listen: /run/blog.socket (Stream)
CGroup: /system.slice/blog.socket

#

the blog.service isnt working
but when i created them it worked
after i configured nginx it didnt work anymore

#

my nginx

server {
listen 80;
listen [::]:80;
server_name 51.44.19.194;

Add index.php to the list if you are using PHP

index index.html index.htm index.nginx-debian.html index.php;

location /static {
autoindex on;
root /srv/http/;
}

location /media {
autoindex on;
root /srv/http/;
}

ATTENTION: blog

location / {
proxy_pass http://unix:/run/blog.socket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
image/svg+xml;

access_log off;
#access_log /var/log/nginx/51.44.19.194-access.log;
error_log /var/log/nginx/51.44.19.194-error.log;
}

acoustic phoenix
#

What does ls -lh /run/blog.socket print?

midnight umbra
#

sorry i just deleted the instance and i will create everything again

#

: (

#

is there any problem if i config nginx first then gunicorn?

acoustic phoenix
#

It might fail to start if it doesn't find the socket.

#

Might I suggest you write a script to automate the deployment?

#

It's much better than doing everything manually and possibly missing a step.

#

You could just create a new repository for it and clone it. A simple script like

set -ex
sudo apt install nginx # and other dependencies
# copy your files, e.g.
sudo cp nginx.conf /etc/nginx/nginx.conf
# refresh systemd
sudo systemctl daemon-reload

etc.

I normally use Ansible to do server config, but just bash is enough to start.

#

Especially if you have limited compute time, doing it right once and getting a fast and repeatable process should help a lot. Why waste 4 hours when you can do it in 20 minutes 🙂

midnight umbra
#

i need to learn that after this project 😅

acoustic phoenix
#

Well, a bash script is just putting your commands in a file instead of the terminal, making sure it's executable, and running ./init-server.sh instead of writing each command one at a time 🙂

#

Ansible has a bit more of a learning curve, but is definitely worth it IMO.

midnight umbra
#

but calm i am already finishing the new ubuntu server
Just for the record when i access my server ip in this moment i get the Welcome to nginx!

#

so for now i did nothing wrong right

acoustic phoenix
#

Yup. Sounds good so far.

midnight umbra
#

why runserver doesnt work

#

it actually works but i cant see my page

acoustic phoenix
#

Do you have debug off?

#

Is it just a blank page?

midnight umbra
#

my debug is true

midnight umbra
#

in this case its just nginx page here

#

127.0.0.1:8000 also doesnt work
same for the private ip

#

i tried all of them

acoustic phoenix
#

http://publicip:8000 doesn't work?

midnight umbra
#

doesnt work

#

It's not possible to access this site
35.180.252.141 took too long to respond.

Try:
Checking the connection
Checking the proxy and the firewall
Running the Windows Network Diagnostics
ERR_CONNECTION_TIMED_OUT
Check your Internet connection.
Check cables and restart any routers, modems, or other network devices you are using.

Allow Opera to access the network in your firewall and antivirus settings.
If it's already listed as a program allowed to access the network, try removing it from the list and adding it again.

If you're using a proxy server…
Check your proxy settings or contact your network administrator to ensure the proxy server is functioning. If you believe you shouldn't be using a proxy server: Go to the main Menu > Settings > Change Proxy Settings... > LAN Settings and uncheck "Use a proxy server for your LAN".

acoustic phoenix
#

Do you have a firewall?

#

sudo ufw status

midnight umbra
#

^C(venv) ubuntu@ip-172-31-39-247:~/projetoblog/djangoapp$ sudo ufw status
Status: inactive

#

i didnt config gunicorn or nginx yet also

#

i was trying from 0 step by step

acoustic phoenix
midnight umbra
#

well

#

maybe i should put everything from .env and put on the settings.py

#

just to make sure it should work?

acoustic phoenix
#

How do you connect to the server?

midnight umbra
#

GNU nano 7.2 .env
SECRET_KEY = '0idKvc9BfAoJlpzRlqtS<Gf2qufk5$RIVNfnM0'

#0 false, 1 true
DEBUG='1'

ALLOWED_HOSTS="127.0.0.1, localhost, 35.180.252.141"

DB_ENGINE="django.db.backends.postgresql"
POSTGRES_DB="blog_base_de_dados"
POSTGRES_USER="blog_user"
POSTGRES_PASSWORD="blog_user_password"
POSTGRES_HOST="localhost"
POSTGRES_PORT="5432"

midnight umbra
acoustic phoenix
#

Do you use the ssh command directly?

midnight umbra
#

no

acoustic phoenix
#

Or some web console?

midnight umbra
#

i didnt get it 🧐

acoustic phoenix
#

What do you use to run commands on the server?

midnight umbra
#

ubuntu

acoustic phoenix
#

But you have to connect to the server somehow to run commands on it 😅

midnight umbra
#

i just run the commands like this:
python3.11 -m venv venv
. venv/bin/activate
pip install --upgrade pip
pip install django
pip install pillow
pip install gunicorn
pip install psycopg
pip install faker

python manage.py runserver 0.0.0.0:8000
python manage.py migrate

midnight umbra
acoustic phoenix
#

Your dev machine is running Ubuntu? Windows?

midnight umbra
#

aws

#

i use amazon web service

acoustic phoenix
#

That's for production.

#

You do have a home computer?

midnight umbra
#

yes

#

for dev i use my windows

#

and the program works

#

if you want i can also stream everything here lol

#

idk if that helps

acoustic phoenix
#

Sure. We can go into VC in a moment...

#

Ok, I'm in Voice A... unless you prefer a private call.

midnight umbra
#

@acoustic phoenix i think my phone isnt working lol my bad

#

but i actually found the soluction here

#

the problem was my security group

#

from aws

#

ok the runserver is working now...

#

sorry for all this

#

alright back to gunicorn and nginx

acoustic phoenix
#

No worries 🙂

#

Though I will have to go out in a bit 😅

midnight umbra
#

ok

#

: (

#

np

acoustic phoenix
#

Have you installed gunicorn, and created a socket for it yet?

acoustic phoenix
midnight umbra
#

sudo nano /etc/systemd/system/blog.socket

[Unit]
Description=gunicorn blog socket

[Socket]
ListenStream=/run/blog.socket

[Install]
WantedBy=sockets.target

acoustic phoenix
#

And the gunicorn service unit?

midnight umbra
#

[Unit]
Description=Gunicorn daemon (You can change if you want)
Requires=blog.socket
After=network.target

[Service]
User=ubuntu
Group=www-data
Restart=on-failure

EnvironmentFile=/home/ubuntu/projetoblog/.env

WorkingDirectory=/home/ubuntu/projetoblog/djangoapp

--error-logfile --enable-stdio-inheritance --log-level and --capture-output

are all for debugging purposes.

ExecStart=/home/ubuntu/projetoblog/venv/bin/gunicorn
--error-logfile /home/ubuntu/projetoblog/gunicorn-error-log
--enable-stdio-inheritance
--log-level "debug"
--capture-output
--access-logfile -
--workers 6
--bind unix:/run/blog.socket
project.wsgi:application

[Install]
WantedBy=multi-user.target

#

hm

#

(venv) ubuntu@ip-172-31-39-247:~/projetoblog/djangoapp$ sudo systemctl status blog.socket
● blog.socket - gunicorn blog socket
Loaded: loaded (/etc/systemd/system/blog.socket; enabled; preset: enabled)
Active: active (listening) since Thu 2024-05-02 18:16:57 UTC; 14s ago
Triggers: ● blog.service
Listen: /run/blog.socket (Stream)
CGroup: /system.slice/blog.socket

#

seems fine right

acoustic phoenix
#

Does sudo systemctl enable --now gunicorn.service (or whatever you named the file) work?

#

Yeah.. so apply the nginx config, and restart nginx?

midnight umbra
#

(venv) ubuntu@ip-172-31-39-247:~/projetoblog/djangoapp$ sudo systemctl enable --now blog.service
Created symlink /etc/systemd/system/multi-user.target.wants/blog.service → /etc/systemd/system/blog.service.
(venv) ubuntu@ip-172-31-39-247:~/projetoblog/djangoapp$

midnight umbra
#

but

#

everytime the things go right until this point

#

but when i put the nginx, my gunicorn will stop working...

#

here we go

#

cd /etc/nginx/sites-available/

sudo nano blog

cd ..

cd sites-enabled/

sudo ln -s /etc/nginx/sites-available/blog /etc/nginx/sites-enabled/blog

#sudo systemctl restart nginx

i will apply these commands

and i will put this code on sudo nano blog:
server {
listen 80;
listen [::]:80;
server_name 35.180.252.141;

Add index.php to the list if you are using PHP

index index.html index.htm index.nginx-debian.html index.php;

location /static {
autoindex on;
root /srv/http/;
}

location /media {
autoindex on;
root /srv/http/;
}

ATTENTION: blog

location / {
proxy_pass http://unix:/run/blog.socket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
image/svg+xml;

access_log off;
#access_log /var/log/nginx/51.44.19.194-access.log;
error_log /var/log/nginx/51.44.19.194-error.log;
}

#

just for the record, before anything here, nginx is still saying Welcome to nginx!

#

now i did all that and restarted nginx

#

502 Bad Gateway
nginx/1.24.0 (Ubuntu) : )

acoustic phoenix
#

ls -lh /run/blog.socket ?

midnight umbra
#

srw-rw-rw- 1 root root 0 May 2 18:16 /run/blog.socket

acoustic phoenix
#

oh... you're missing a colon

#
  # ATTENTION: blog
  location / {
-   proxy_pass http://unix/run/blog.socket;
+   proxy_pass http://unix:/run/blog.socket;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
midnight umbra
#

Nani

#

ok i will test that

acoustic phoenix
#

It's unix:, not unix

midnight umbra
#

location / {
proxy_pass http://unix:/run/blog.socket; #old
proxy_pass http://unix:/run/blog.socket; #new
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;

#

but it was correct i think?

#

for some reason it wasnt in the message i sent u

#

but it is lol

acoustic phoenix
#

You have a colon (:) here?:

proxy_pass http://unix:/run/blog.socket; #new
                      ^
midnight umbra
#

yes

#

anwait

#

ah

#

yeah i have it

acoustic phoenix
#

You still get a bad gateway after restarting nginx?

midnight umbra
#

yes

#

that was probably just a discord bug

acoustic phoenix
#

Can you check: less /var/log/nginx/51.44.19.194-error.log?

midnight umbra
#

ops

#

that ip for error log is wrong i have to change it

acoustic phoenix
#

You can delete it altogether... it really doesn't matter 🙂

#

It's just a file name.

midnight umbra
#

2024/05/02 18:38:24 [error] 8228#8228: *1 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"
/var/log/nginx/35.180.252.141-error.log (END)

#

how i leave it

#

lol

#

nvm

#

2024/05/02 18:24:05 [error] 8058#8058: *1 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"
2024/05/02 18:32:16 [error] 8174#8174: *2 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"
2024/05/02 18:32:18 [error] 8174#8174: *2 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"
2024/05/02 18:33:58 [error] 8174#8174: *7 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"
2024/05/02 18:34:21 [error] 8174#8174: *7 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"
2024/05/02 18:34:38 [error] 8174#8174: *7 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"
2024/05/02 18:37:53 [error] 8174#8174: *11 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"

acoustic phoenix
#

Is "http://unix/run/blog.socket:/ another discord bug? 😅

midnight umbra
#

i dont know why its happening

acoustic phoenix
#
- http://unix/run/blog.socket:/
+ http://unix:/run/blog.socket/
             ^here            ^not here
midnight umbra
#

but it is correct on my nginx file 🧐

#

`server {
listen 80;
listen [::]:80;
server_name 35.180.252.141;

Add index.php to the list if you are using PHP

index index.html index.htm index.nginx-debian.html index.php;

location /static {
autoindex on;
root /srv/http/;
}

location /media {
autoindex on;
root /srv/http/;
}

ATTENTION: blog

location / {proxy_pass http://unix:/run/blog.socket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 4 32k;`

acoustic phoenix
#

You can use triple backticks to create a code block.
```nginx
server {
...
}
```

#

But yes, it does look correct. It's been a while since I've used unix sockets 😅

#

Try this:

upstream gunicorn {
  server unix:/run/blog.socket;
}

server {
  listen 80;
  listen [::]:80;
  server_name 35.180.252.141;

  # Add index.php to the list if you are using PHP
  index index.html index.htm index.nginx-debian.html index.php;

  location /static {
    autoindex on;
    root /srv/http/;
  }

  location /media {
    autoindex on;
    root /srv/http/;
  }

  # ATTENTION: blog
  location / {
    proxy_pass http://gunicorn;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }

  gzip on;
  gzip_disable "msie6";

  gzip_comp_level 6;
  gzip_min_length 1100;
  gzip_buffers 4 32k;
midnight umbra
#

hmm

#

(venv) ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ sudo systemctl restart nginx
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.
(venv) ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$

#

this happened

acoustic phoenix
#

sudo nginx -t?

midnight umbra
#

why god : (

#

(venv) ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ sudo nginx -t
2024/05/02 18:57:06 [emerg] 8356#8356: unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/blog:39
nginx: configuration file /etc/nginx/nginx.conf test failed
(venv) ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$

#

alright

#

ok now i restarted

#

still 502 bad gateway

acoustic phoenix
#

Oops, sorry. I copied your file 😅

midnight umbra
#

Here is the translation:

"Hey, gabigolgoat10!

Let's analyze the results of the commands you ran:

  • gunicorn --version: You're using version 22.0.0 of gunicorn, which is a recent version.
  • nginx -t: The configuration test indicates that the syntax of the /etc/nginx/nginx.conf file is correct. However, there are two issues:
    • The first warning line indicates that the user directive in /etc/nginx/nginx.conf line 1 is being ignored because the master process is not being run with superuser privileges. This is not a major issue, but it's important to verify that the user configuration is correct.
    • The second error line indicates that nginx does not have permission to open the /run/nginx.pid file because the process does not have write permission in the /run directory. This is a serious issue, as nginx needs permission to write the PID file to function correctly.

To resolve these issues, you can try the following solutions:

  • Verify that the user configuration in /etc/nginx/nginx.conf line 1 is correct and that the user has permission to run nginx.
  • Verify that the /run directory has write permission for the user running nginx. You can try running the command sudo chown -R nginx:nginx /run/nginx.pid to give the nginx user permission to write to the PID file.
  • Verify that the nginx configuration file is correct and that there are no other permission or configuration issues.

If you continue to have problems, it may be helpful to check the nginx and system logs to identify errors more precisely.

Remember, I'm here to help you debug the issue. If you need more help, just ask!"

gpt said that

acoustic phoenix
#

Need to add a } on a line to close the server tag.

midnight umbra
#

but still got the bad gateway

acoustic phoenix
#

Sorry, I do need to run for the a few hours. I may be back later this evening...

midnight umbra
#

ok : (

midnight umbra
#

can u ping me when u come back ? 😅

acoustic phoenix
midnight umbra
#

so u have a plan?

#

a next step?

acoustic phoenix
#

Check the (nginx) error log again?

midnight umbra
#

ok

#

2024/05/02 18:38:24 [error] 8228#8228: *1 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"
2024/05/02 18:42:05 [error] 8228#8228: *5 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"
/var/log/nginx/35.180.252.141-error.log (END)

acoustic phoenix
#

And sudo nginx -t?

midnight umbra
#

ubuntu@ip-172-31-39-247:~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
ubuntu@ip-172-31-39-247:~$

#

Pat sensei i have another django/ubuntu project that works, is there a way to copy all and then i just change the files idk

acoustic phoenix
#

Depends 😅

#

Is it also Django and gunicorn?

midnight umbra
acoustic phoenix
#

You can try... though the better option may be copy its config to /tmp/working.conf and run diff /etc/nginx/nginx.conf /tmp/working.conf 🙂

midnight umbra
#

hm

acoustic phoenix
#

See what the difference is between the broken config, and the working config.

#

You can also bind gunicorn to a port instead of a unix socket, and give up on those 😅

midnight umbra
#

i have no idea how to do these things

#

i will try but i might be offline for a few moments

acoustic phoenix
#

No worries. You can always paste the two files somewhere and share them, if they don't contain sensitive information.

midnight umbra
#

`server {
listen 80;
listen [::]:80;
server_name 35.180.100.66;

Add index.php to the list if you are using PHP

index index.html index.htm index.nginx-debian.html index.php;

ATTENTION: /home/ubuntu/jogo/project/static

location /static {
autoindex on;
root /srv/http/;
}

ATTENTION: /home/ubuntu/jogo/project/media

location /media {
autoindex on;
root /srv/http/;

ATTENTION: agenda

location / {
proxy_pass http://unix:/run/agenda.socket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

}`

#

this is the one that works

#

i dont even see the difference

midnight umbra
#

i think the problem is something else

#

once i deleted my blog from sites enabled i get the Welcome to nginx

#

but my blog.service doesnt work anyway

acoustic phoenix
#

If the problem is something like "error connecting upstream" it's because nginx fails to connect to gunicorn...

#

Oh, wait...

#

Why do you have

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
#

Isn't that for websockets?

midnight umbra
#

i have no idea

#

lol

#

i am trying to do things here, but no success

#

i deleted my gunicorn configs

#

and my nginx config

#

i will try configure nginx first then gunicorn

acoustic phoenix
#

Ok, new plan. Try this and restart nginx.

upstream gunicorn {
  server unix:/run/blog.socket;
}

server {
  listen 80;
  listen [::]:80;
  server_name 35.180.252.141;

  location /static {
    autoindex on;
    root /srv/http/;
  }

  location /media {
    autoindex on;
    root /srv/http/;
  }

  # ATTENTION: blog
  location / {
    proxy_pass http://gunicorn;
    proxy_set_header Host $http_host;
  }

  gzip on;
  gzip_disable "msie6";

  gzip_comp_level 6;
  gzip_min_length 1100;
  gzip_buffers 4 32k;
}
midnight umbra
#

ok

acoustic phoenix
#

Actually, wait...

midnight umbra
#

hm

#

there is no problem is doing nginx first right

#

it should work right

#

after configure the gunicorn

acoustic phoenix
#

Now is good. You don't need index files with Django.

#

Hmmm... You should normally configure gunicorn first, as the unreachable upstream may prevent nginx from starting...

midnight umbra
#

ok then

#

i will do gunicorn first

#

i doubt its going to work

#

it isnt triggering the blog.service

#

for some reason

#

: (

acoustic phoenix
#

Which tutorial are you following? 😅

midnight umbra
#

502 Bad Gateway
nginx/1.24.0 (Ubuntu)

midnight umbra
acoustic phoenix
#

Oh 🥲

midnight umbra
#

ok i will take a look in my other socket and service

#

from my other project

acoustic phoenix
midnight umbra
#

ok

acoustic phoenix
#

You don't have to use Digital Ocean for it to be useful, as long as it's Ubuntu (hopefully 22.04)

midnight umbra
#

i did it all

#

: (

#

its pretty much the same i was doing

#

and it happens again:
the gunicorn was perfect fine until i put the nginx : (

#

actually now its still working

#

🧐

#

User
2024/05/02 22:58:20 [error] 11510#11510: *1 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141"

but still

#

well

#

ok i have to delete these first (venv) ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ dir
blog default myproject

#

2024/05/02 23:09:00 [error] 11603#11603: *11 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "35.180.252.141"

current error

midnight umbra
acoustic phoenix
#

Have you tried curl --unix-socket /run/gunicorn.sock localhost ?

midnight umbra
#

(venv) ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ curl --unix-socket /run/gunicorn.sock localhost
curl: (7) Failed to connect to localhost port 80 after 0 ms: Couldn't connect to server
(venv) ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$

#

also when he said to do this:
cd ~/myprojectdir
gunicorn --bind 0.0.0.0:8000 myproject.wsgi

it worked

#

but my site was different( i dont remeber what) when i run in a different browser

acoustic phoenix
#

Ok... I need to go to bed 😩

midnight umbra
#

: (

acoustic phoenix
#

I think the best would be for you do try again from scratch, list every step and command you ran, as well as the content of each file you generate.

#

Then we can step through it in details and see where you may go wrong 🙂

midnight umbra
#

well anyway thank you for everything : )

midnight umbra
#

until i put the nginx

#

my socket and service works etc.
then i run the nginx and it stops

#

anyway gn

#

@worldly bone can you help me ?😅

#

i think the problem might be related to this:

(venv) ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ ls -l /etc/systemd/system/blog.socket
-rw-r--r-- 1 root root 115 May 2 22:28 /etc/systemd/system/blog.socket

it says root root instead www-data

#

but i dont know how to fix it

midnight umbra
#

probably not the problem since the program that works also sayy root root : (

#

maybe www data isnt getting the correct permissions idk

midnight umbra
#

i think i might be offline some part o fthe day tomorrow : (

midnight umbra
midnight umbra
midnight umbra
#

i dont know which or why but it must be some permission problem

worldly bone
#

Can you repast or link to messages with current nginx and wsgi setups?

midnight umbra
#

hi

#

sorry for the late response

#

i was off the entire day

midnight umbra
# worldly bone Can you repast or link to messages with current nginx and wsgi setups?

`gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

gunicorn.service

GNU nano 7.2 /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/projetoblog/djangoapp
ExecStart=/home/ubuntu/projetoblog/venv/bin/gunicorn
--access-logfile -
--workers 3
--bind unix:/run/gunicorn.sock
project.wsgi:application
[Install]
WantedBy=multi-user.target
`

worldly bone
#

ListenStream=/run/gunicorn.sock doesn't seem to match what you have in nginx

midnight umbra
#

nginx

`server {
listen 80;
server_name 35.180.252.141;

location = /favicon.ico { access_log off; log_not_found off; }
 location /static {
autoindex on;
root /srv/http/;

}

ATTENTION: /home/ubuntu/jogo/project/media

location /media {
autoindex on;
root /srv/http/;
}

location / {
    include proxy_params;
    proxy_pass http://unix:/run/gunicorn.sock;
}`
worldly bone
#

ah, with this one it matches

midnight umbra
worldly bone
#

yes. Also check socket permissins

midnight umbra
#

2024/05/02 22:58:20 [error] 11510#11510: *1 connect() to unix:/run/blog.socket failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, server: 35.180.252.141, request: "GET / HTTP/1.1", upstream: "http://unix:/run/blog.socket:/", host: "35.180.252.141

#

this is the error

midnight umbra
worldly bone
#

ls -la /run/

midnight umbra
#

after run i should add what?

#

the path ?

worldly bone
#

nothing, just find gunicorn socket in output

midnight umbra
#

ah i see

#

srw-rw-rw- 1 root root 0 May 2 23:11 gunicorn.sock

#

-rw-r--r-- 1 root root 6 May 2 23:11 nginx.pid

worldly bone
#

hm, so it's set rw for everyone

midnight umbra
#

do u think the problem its something related to www data idk

worldly bone
#

are you sure your nginx is www-data first of all?

#

I don't have good ideas right now, can only suggest using non-unix but tcp socket, they are quite easier

midnight umbra
worldly bone
#

just run gunicorn on 8000 or something and point nginx ther

midnight umbra
#

hm how i do that

worldly bone
midnight umbra
#

but if i remove that line do u think it can work?

worldly bone
midnight umbra
#

in the nginx config right?

worldly bone
midnight umbra
#

`server {
listen 80;
server_name 35.180.252.141;

location = /favicon.ico { access_log off; log_not_found off; }
 location /static {
autoindex on;
root /srv/http/;

}

ATTENTION: /home/ubuntu/jogo/project/media

location /media {
autoindex on;
root /srv/http/;
}

location / {
    include proxy_params;
    proxy_pass 127.0.0.1:8000
}

}`

#

ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ sudo nano myproject
ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ sudo systemctl restart nginx
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.
ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$

worldly bone
#

in gunicorn too

#

or just manually start gunicorn for test

midnight umbra
worldly bone
#

here:

ExecStart=/home/ubuntu/projetoblog/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          project.wsgi:application
midnight umbra
#

ok

#

in bind unix: i replace with 127.0.0.1:8000 right

worldly bone
#

yes --bind 127.0.0.1:8000

midnight umbra
#

ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ sudo systemctl restart nginx
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.
ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$

#

its still saying that

#

May 04 01:24:02 ip-172-31-39-247 nginx[22020]: nginx: configuration file /etc/nginx/nginx.conf test failed
May 04 01:24:02 ip-172-31-39-247 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ An ExecStartPre= process belonging to unit nginx.service has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 1.
May 04 01:24:02 ip-172-31-39-247 systemd[1]: nginx.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit nginx.service has entered the 'failed' state with result 'exit-code'.
May 04 01:24:02 ip-172-31-39-247 systemd[1]: Failed to start nginx.service - A high performance web server and a reverse proxy server.
░░ Subject: A start job for unit nginx.service has failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A start job for unit nginx.service has finished with a failure.
░░
░░ The job identifier is 45284 and the job result is failed.
lines 628-650/650 (END)

worldly bone
#

May 04 01:24:02 ip-172-31-39-247 nginx[22020]: nginx: configuration file /etc/nginx/nginx.conf test failed

#

That's different error, you have mistake in nginx.conf

#

run sudo nginx -t

midnight umbra
#

ah

#

GNU nano 7.2 myproject
server {
listen 80;
server_name 35.180.252.141;

location = /favicon.ico { access_log off; log_not_found off; }
 location /static {
autoindex on;
root /srv/http/;

}

ATTENTION: /home/ubuntu/jogo/project/media

location /media {
autoindex on;
root /srv/http/;
}

location / {
    include proxy_params;
    proxy_pass 127.0.0.1:8000
}
#

it says it have an unexpected } in the end

#

but i dont see it

#

ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ sudo nginx -t
2024/05/04 01:27:53 [emerg] 22082#22082: unexpected "}" in /etc/nginx/sites-enabled/myproject:20

#

i removed the last "}"

#

but it didnt change the error message

#

AH MY BAD

#

i should have added ;

#

still have an error 1 sec

#

2024/05/04 01:36:24 [emerg] 22559#22559: invalid URL prefix in /etc/nginx/sites-enabled/myproject:19
nginx: configuration file /etc/nginx/nginx.conf test failed
ubuntu@ip-172-31-39-247:~$

#

i didnt get it

worldly bone
#

let me check

midnight umbra
#

`server {
listen 80;
server_name 35.180.252.141;

location = /favicon.ico { access_log off; log_not_found off; }
 location /static {
autoindex on;
root /srv/http/;

}

ATTENTION: /home/ubuntu/jogo/project/media

location /media {
autoindex on;
root /srv/http/;
}

location / {
    include proxy_params;
    proxy_pass 127.0.0.1:8000;
}

}`

#

this is the "myproject"

worldly bone
#

try proxy_pass http://127.0.0.1:8000;

#

It's not exactly how I've used to config it so don't remember exactly

midnight umbra
#

ok

#

restarted nginx

#

still getting a bad gateway

#

well time to check the error message

worldly bone
#

restarted gunicorn?

midnight umbra
#

restarted now

#

still bad gateway

worldly bone
#

it's important that both are running listening and proxying to same port

midnight umbra
#

i dont remember how to check the error log 😅

#

alright

#

GNU nano 7.2 /var/log/nginx/error.log
2024/05/04 00:11:28 [error] 11722#11722: *6101 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upst>
2024/05/04 00:35:14 [error] 11722#11722: *6104 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upst>
2024/05/04 01:09:12 [error] 11722#11722: *6106 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upst>
2024/05/04 01:41:22 [error] 22593#22593: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, >
2024/05/04 01:43:42 [error] 22593#22593: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, >
2024/05/04 01:43:43 [error] 22593#22593: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, >
2024/05/04 01:44:10 [error] 22593#22593: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, >
2024/05/04 01:44:11 [alert] 22593#22593: *7 open socket #11 left in connection 4
2024/05/04 01:44:11 [alert] 22593#22593: aborting
2024/05/04 01:44:14 [error] 23032#23032: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 38.159.186.151, >

#

: (

worldly bone
#

2024/05/04 00:35:14 [error] 11722#11722:6104 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upst>

#

It's still trying unix socket

#

ah, it's old one I guess

#

Do you have a firewall? Try connecting directly to port 8000

midnight umbra
#

i already connected with port 8000 using runserver

worldly bone
#

wait

#

You have runserver running already?

#

two servers can't run on same port

#

nginx should be able to proxy to runserver though

midnight umbra
#

no

#

i just put runserver now

#

just to test again

#

just quitted it

#

so whats the plan now? : (

worldly bone
#

So what happens with runserver?

midnight umbra
#

runserver run normally

worldly bone
#

You can access it on :8000 ?

midnight umbra
worldly bone
#

And when you go without it you get a badgateway still?

midnight umbra
#

without runserver?

worldly bone
#

without :8000

#

which would mean default port

midnight umbra
#

no i dont get a badgateway when i use runserver without :8000

worldly bone
#

So it works with runserver?

midnight umbra
#

yes

#

also 1 thing to note

#

maybe you didnt me saying it

worldly bone
#

hm, you were changing gunicorn.service file?

midnight umbra
#

when i start configuring gunicorn my socket triggers the gunicorn.service but when i configure my nginx, it wont trigger gunicorn.service anymore

midnight umbra
worldly bone
#

do following: sudo systemctl daemon-reload

#

then sudo systemctl restart gunicorn

midnight umbra
#

still bad gateway

worldly bone
#

show me current gunicorn.service

#

actually do also systemctl status gunicorn maybe it fails?

midnight umbra
#

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/projetoblog/djangoapp
ExecStart=/home/ubuntu/projetoblog/venv/bin/gunicorn
--access-logfile -
--workers 3
--bind 127.0.0.1:8000
project.wsgi:application
[Install]
WantedBy=multi-user.target

i actually should have added http here too i guess

#

i changed it now but still get bad gateway

worldly bone
#

Yeah I wouldn't think it's the case here

#

what status says?

midnight umbra
#

(venv) ubuntu@ip-172-31-39-247:/etc/nginx/sites-enabled$ systemctl status gunicorn
× gunicorn.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; preset: enabled)
Active: failed (Result: exit-code) since Sat 2024-05-04 02:02:24 UTC; 41s ago
Duration: 9ms
TriggeredBy: ● gunicorn.socket
Process: 23374 ExecStart=/home/ubuntu/projetoblog/venv/bin/gunicorn --access-logfile - --workers 3 --bind http://127.0.0.1:8000 project.w>
Main PID: 23374 (code=exited, status=203/EXEC)
CPU: 2ms

May 04 02:02:24 ip-172-31-39-247 systemd[1]: Started gunicorn.service - gunicorn daemon.
May 04 02:02:24 ip-172-31-39-247 systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC
May 04 02:02:24 ip-172-31-39-247 systemd[1]: gunicorn.service: Failed with result 'exit-code'.

worldly bone
#

Ok, it fails

#

try running gunicorn manually for now

midnight umbra
#

how

#

ah i think it worked yesterday

#

gunicorn --bind 0.0.0.0:8000 myproject.wsgi

#

ah

#

sec

worldly bone
#

cd /home/ubuntu/projetoblog/djangoapp
/home/ubuntu/projetoblog/venv/bin/gunicorn --bind 127.0.0.1:8000 project.wsgi:application

#

Now when I look at it is project.wsgi is correct

#

project is your module that typically has settings.py (and so wsgi.py)

midnight umbra
#

yes wsgi is there

worldly bone
#

dir named project/ ?

midnight umbra
#

i used this command gunicorn --bind 0.0.0.0:8000 project.wsgi

#

its working

#

except

#

no static files

#

(venv) ubuntu@ip-172-31-39-247:~/projetoblog/djangoapp$ gunicorn --bind 0.0.0.0:8000 project.wsgi
[2024-05-04 02:09:36 +0000] [23423] [INFO] Starting gunicorn 22.0.0
[2024-05-04 02:09:36 +0000] [23423] [INFO] Listening at: http://0.0.0.0:8000 (23423)
[2024-05-04 02:09:36 +0000] [23423] [INFO] Using worker: sync
[2024-05-04 02:09:36 +0000] [23424] [INFO] Booting worker with pid: 23424

worldly bone
#

try command exactly like this now /home/ubuntu/projetoblog/venv/bin/gunicorn --access-logfile - --workers 3 --bind 0.0.0.0:8000 project.wsgi:application

midnight umbra
#

(venv) ubuntu@ip-172-31-39-247:~/projetoblog/djangoapp$ gunicorn --access-logfile - --workers 3 --bind 0.0.0.0:8000 project.wsgi:application
[2024-05-04 02:13:42 +0000] [23445] [INFO] Starting gunicorn 22.0.0
[2024-05-04 02:13:42 +0000] [23445] [INFO] Listening at: http://0.0.0.0:8000 (23445)
[2024-05-04 02:13:42 +0000] [23445] [INFO] Using worker: sync
[2024-05-04 02:13:42 +0000] [23446] [INFO] Booting worker with pid: 23446
[2024-05-04 02:13:42 +0000] [23447] [INFO] Booting worker with pid: 23447
[2024-05-04 02:13:43 +0000] [23448] [INFO] Booting worker with pid: 23448

#

its working, just no static files

worldly bone
#

=\

#

Why it doesn't start in systemd then

#

Change service part like this:

#
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/projetoblog/djangoapp
ExecStart=/home/ubuntu/projetoblog/venv/bin/gunicorn \
          --workers 3 \
          --bind 0.0.0.0:8000 \
          project.wsgi:application
midnight umbra
#

ok

#

how i stop that

worldly bone
#

then again both systemctl daemon-reload systemctl restart gunicorn

worldly bone
midnight umbra
#

small problem, the terminal froze

#

i had to restart it

#

and i cant ctrl c anymore

worldly bone
#

ps aux | grep gunicorn

#

see first number you see in the line and do kill 1234

midnight umbra
#

should i change now the nginx to 0.0.0.0:8000 now too right

worldly bone
#

Nope, we leave nginx alone now, issue is with gunicorn

midnight umbra
#

ubuntu@ip-172-31-39-247:~$ ps aux | grep gunicorn
ubuntu 23445 0.0 2.4 33988 23552 ? S 02:13 0:00 /home/ubuntu/projetoblog/djangoapp/venv/bin/python3.11 /home/ubuntu/projetoblog/djangoapp/venv/bin/gunicorn --access-logfile - --workers 3 --bind 0.0.0.0:8000 project.wsgi:application
ubuntu 23469 0.1 5.7 75364 56212 ? S 02:16 0:00 /home/ubuntu/projetoblog/djangoapp/venv/bin/python3.11 /home/ubuntu/projetoblog/djangoapp/venv/bin/gunicorn --access-logfile - --workers 3 --bind 0.0.0.0:8000 project.wsgi:application
ubuntu 23470 0.1 5.2 73560 51736 ? S 02:16 0:00 /home/ubuntu/projetoblog/djangoapp/venv/bin/python3.11 /home/ubuntu/projetoblog/djangoapp/venv/bin/gunicorn --access-logfile - --workers 3 --bind 0.0.0.0:8000 project.wsgi:application
ubuntu 23471 0.1 5.7 75368 56216 ? S 02:16 0:00 /home/ubuntu/projetoblog/djangoapp/venv/bin/python3.11 /home/ubuntu/projetoblog/djangoapp/venv/bin/gunicorn --access-logfile - --workers 3 --bind 0.0.0.0:8000 project.wsgi:application
ubuntu 24026 0.0 0.2 7080 2048 pts/3 S+ 02:20 0:00 grep --color=auto gunicorn
ubuntu@ip-172-31-39-247:~$

worldly bone
#

ah, dam right we started workers too

midnight umbra
worldly bone
#

try kill 23445 and check ps aux command again

midnight umbra
#

ok

#

back at bad gateway

worldly bone
#

wait, we are not intersted in it

#

Does systemctl start gunicorn?

midnight umbra
worldly bone
#

and then systemctl status gunicorn

midnight umbra
#

it said (venv) ubuntu@ip-172-31-39-247:~/projetoblog/djangoapp$ systemctl daemon-reload
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ====
Authentication is required to reload the systemd state.
Multiple identities can be used for authentication:

  1. Ubuntu (ubuntu)
  2. www-data
    Choose identity to authenticate as (1-2):
#

ah

#

just sudo

#

ok

#

i restarted etc.

#

i think i should change nginx now?

worldly bone
#

yeah sudo all systemctl commands (except status doesn't need it probably)

#

No, show me what status says

#

Until we see gunicorn is running no point doing anything in nginx

midnight umbra
#

(venv) ubuntu@ip-172-31-39-247:~/projetoblog/djangoapp$ sudo systemctl status gunicorn.socket
● gunicorn.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; preset: enabled)
Active: active (listening) since Sat 2024-05-04 01:43:40 UTC; 42min ago
Triggers: ● gunicorn.service
Listen: /run/gunicorn.sock (Stream)
CGroup: /system.slice/gunicorn.socket

May 04 01:43:40 ip-172-31-39-247 systemd[1]: Listening on gunicorn.socket - gunicorn socket.
(venv) ubuntu@ip-172-31-39-247:~/projetoblog/djangoapp$ sudo systemctl status gunicorn.service
× gunicorn.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; preset: enabled)
Active: failed (Result: exit-code) since Sat 2024-05-04 02:25:13 UTC; 1min 23s ago
Duration: 12ms
TriggeredBy: ● gunicorn.socket
Process: 24135 ExecStart=/home/ubuntu/projetoblog/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 project.wsgi:application (code=exi>
Main PID: 24135 (code=exited, status=203/EXEC)
CPU: 3ms

May 04 02:25:13 ip-172-31-39-247 systemd[1]: Started gunicorn.service - gunicorn daemon.
May 04 02:25:13 ip-172-31-39-247 systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC
May 04 02:25:13 ip-172-31-39-247 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
lines 1-12/12 (END)

#

1 thing

#

if i do another socket and another service, it will work

#

its always like that

#

i can trigger the gunicorn.service

#

making a new socket and making a new service

worldly bone
#

maube issue is the socket

#

I don't know really

#

Well in worst case for time being you can start server manually by using command and & at the end that will send it to background

midnight umbra
#

GNU nano 7.2 /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

this is the socket btw

worldly bone
#

you can run /home/ubuntu/projetoblog/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 project.wsgi:application & it will start process and keep terminal free. It's just unhandy to control

midnight umbra
#

ah

#

and then what i do

#

like how i use nginx then etc

worldly bone
#

Well, it works like that, right ? just without static

midnight umbra
#

yeah

#

but how i will use static : (

worldly bone
#

Can just try to fix static

#

Have you run collectstatic?

midnight umbra
#

yes

#

but in this case i am not using nginx or am i?

worldly bone
#

If you can connect without :8000 port in url - you are using nginx

midnight umbra
#

ah i see

#

ok time to try fix static right

#

i will try

#

but

#

1 question

#

am i losing something doing this?

worldly bone
#

without systemctl? Mainly ease of control

#

But I don't have a good idea why it doesn't work, everything seems ok

#

Maybe indeed make a new service file and use it

#

without socket for now

#

unix socket vs :8000 is no big deal

midnight umbra
#

but wait

#

i am confused

#

why in runserver my static works

#

but here it dont

worldly bone
#

runserver serves static on it's own. But its very inefficient so it's not fit for deployment

midnight umbra
#

but it indicates that they are in correct places right

worldly bone
#

no

midnight umbra
#

what is wrong with my current static then 🧐

worldly bone
#

It doesn't need collectstaic at all, it takes it directly from it's source

midnight umbra
#

sudo mkdir -p /srv/http/{media,static}
sudo chown -R ubuntu:ubuntu /srv/http

#

sec

#
STATIC_ROOT = "/srv/http/static/"
MEDIA_ROOT = "/srv/http/media/"
#

In nginx.conf:

  location /static {
    autoindex on;
    root /srv/http/;
  }

  # ATTENTION: /home/ubuntu/jogo/project/media 
  location /media {
    autoindex on;
    root /srv/http/;
  }
#

After that, restart your wsgi server (gunicorn?) and nginx, run collectstatic, and if you have media files, move them from /home/ubuntu/jogo/project/media/ to /srv/http/media/...

#

these are my saved steps to configure it

#

that Pat told me in my previous project

#

it should work right?

#

i am pretty sure i already did this here too

#

but i will do again to confirm

worldly bone
#

Looks OK-ish

midnight umbra
#

alright

#

i fixed the static files

#

now i have to look if the media files are working

#

(the original problem in this thread lol)

#

my media files arent working correctly : (

#

i have the said configurations

#

i uploaded it in my admin

#

but it isnt showing in my page

#

1st: it uploaded the image but didnt show it
2nd:
Traceback (most recent call last): File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/gunicorn/workers/sync.py", line 134, in handle req = next(parser) ^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/gunicorn/http/parser.py", line 42, in __next__ self.mesg = self.mesg_class(self.cfg, self.unreader, self.source_addr, self.req_count)

#

`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/gunicorn/http/message.py", line 257, in init
super().init(cfg, unreader, peer_addr)
File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/gunicorn/http/message.py", line 60, in init
unused = self.parse(self.unreader)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/gunicorn/http/message.py", line 269, in parse
self.get_data(unreader, buf, stop=True)
File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/gunicorn/http/message.py", line 260, in get_data
data = unreader.read()
^^^^^^^^^^^^^^^
`

#

File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/gunicorn/http/unreader.py", line 37, in read d = self.chunk() ^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/gunicorn/http/unreader.py", line 64, in chunk return self.sock.recv(self.mxchunk) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/gunicorn/workers/base.py", line 203, in handle_abort sys.exit(1)

#

SystemExit: 1 [2024-05-04 00:10:59 -0300] [26674] [INFO] Worker exiting (pid: 26674) [2024-05-04 03:10:59 +0000] [26689] [INFO] Booting worker with pid: 26689 Internal Server Error: /admin/blog/post/1/change/ Traceback (most recent call last): File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request)

#

^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/contrib/admin/options.py", line 688, in wrapper return self.admin_site.admin_view(view)(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/utils/decorators.py", line 134, in _wrapper_view response = view_func(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/views/decorators/cache.py", line 62, in _wrapper_view_func response = view_func(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/contrib/admin/sites.py", line 242, in inner return view(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/contrib/admin/options.py", line 1889, in change_view return self.changeform_view(request, object_id, form_url, extra_context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/projetoblog/djangoapp/venv/lib/python3.11/site-packages/django/utils/decorators.py", line 46, in _wrapper return bound_method(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

midnight umbra
midnight umbra
#

ok i fixed