Nginx and gunicorn are working fine, but site cannot be connected. How can I solve this problem?

Viewed 667

I am not good in English. I hope you understand.

I want to deploy the django project using AWS EC2. (instance settings: Ubuntu Server 18.04 LTS ... , t2 micro) + RDS + Gunicorn + Nginx

I finished all the setup of ec2. Python manage.py runserver 0.0.0.0:8000 works fine.

gunicorn --bind 0.0.0.0:8000 Backend.wsgi:application works fine too.

But when sudo systemctl gunicorn start, when I connect to http://{mypublic IPv4}, the site cannot be connected.

my /etc/systemd/system/gunicorn.service

(venv) ubuntu@ip-172-31-32-142:~/diary-with-pet/Backend$ cat /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/diary-with-pet/Backend/Backend
ExecStart=/home/ubuntu/diary-with-pet/Backend/venv/bin/gunicorn \
        --workers 3 \
        --bind unix:/home/ubuntu/diary-with-pet/Backend/run/gunicorn.sock \
        --workers 3 --bind 0.0.0.0:8000  Backend.wsgi:application

[Install]
WantedBy=multi-user.target

when I start gunicorn,

(venv) ubuntu@ip-172-31-32-142:~/diary-with-pet/Backend$ sudo systemctl start gunicorn
(venv) ubuntu@ip-172-31-32-142:~/diary-with-pet/Backend$ sudo systemctl enable gunicorn
(venv) ubuntu@ip-172-31-32-142:~/diary-with-pet/Backend$ sudo systemctl status gunicorn
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset:
   Active: active (running) since Mon 2021-04-19 05:15:22 UTC; 1 day 5h ago
 Main PID: 9679 (gunicorn)
    Tasks: 4 (limit: 1140)
   CGroup: /system.slice/gunicorn.service
           ├─9679 /home/ubuntu/diary-with-pet/Backend/venv/bin/python3 /home/ubu
           ├─9700 /home/ubuntu/diary-with-pet/Backend/venv/bin/python3 /home/ubu
           ├─9702 /home/ubuntu/diary-with-pet/Backend/venv/bin/python3 /home/ubu
           └─9703 /home/ubuntu/diary-with-pet/Backend/venv/bin/python3 /home/ubu

Apr 19 05:15:22 ip-172-31-32-142 systemd[1]: Started gunicorn daemon.
Apr 19 05:15:22 ip-172-31-32-142 gunicorn[9679]: [2021-04-19 05:15:22 +0000] [96
Apr 19 05:15:22 ip-172-31-32-142 gunicorn[9679]: [2021-04-19 05:15:22 +0000] [96
Apr 19 05:15:22 ip-172-31-32-142 gunicorn[9679]: [2021-04-19 05:15:22 +0000] [96
Apr 19 05:15:22 ip-172-31-32-142 gunicorn[9679]: [2021-04-19 05:15:22 +0000] [97
Apr 19 05:15:22 ip-172-31-32-142 gunicorn[9679]: [2021-04-19 05:15:22 +0000] [97
Apr 19 05:15:23 ip-172-31-32-142 gunicorn[9679]: [2021-04-19 05:15:23 +0000] [97
lines 1-18/18 (END)
  • nginx work fine too
(venv) ubuntu@ip-172-31-32-142:~/diary-with-pet/Backend$ sudo rm -f /etc/nginx/sites-enabled/default
(venv) ubuntu@ip-172-31-32-142:~/diary-with-pet/Backend$ sudo rm -f /etc/nginx/sites-available/default
(venv) ubuntu@ip-172-31-32-142:~/diary-with-pet/Backend$ sudo service nginx restart
(venv) ubuntu@ip-172-31-32-142:~/diary-with-pet/Backend$ sudo service nginx status

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en
   Active: active (running) since Tue 2021-04-20 10:41:41 UTC; 6s ago
     Docs: man:nginx(8)
  Process: 19581 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 
  Process: 19596 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (cod
  Process: 19584 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process
 Main PID: 19597 (nginx)
    Tasks: 2 (limit: 1140)
   CGroup: /system.slice/nginx.service
           ├─19597 nginx: master process /usr/sbin/nginx -g daemon on; master_pr
           └─19598 nginx: worker process

Apr 20 10:41:41 ip-172-31-32-142 systemd[1]: Stopped A high performance web serv
Apr 20 10:41:41 ip-172-31-32-142 systemd[1]: Starting A high performance web ser
Apr 20 10:41:41 ip-172-31-32-142 systemd[1]: nginx.service: Failed to parse PID 
Apr 20 10:41:41 ip-172-31-32-142 systemd[1]: Started A high performance web serv
  • my /etc/nginx/sites-available/Backend
server {
        listen 80;
        server_name 15.164.212.81;

        location = /favicon.ico { access_log off; log_not_found off; }

        location /static/ {
                root /home/ubuntu/diary-with-pet/Backend;
        }

    location /media/ {
        root /home/ubuntu/diary-with-pet/Backend;
    }

        location / {
                include proxy_params;
        proxy_pass http://15.164.212.81:8000;
        }
}
server {
        listen 80;
        server_name {my public IPv4};

        location = /favicon.ico { access_log off; log_not_found off; }

        location /static/ {
                root /home/ubuntu/diary-with-pet/Backend;
        }

    location /media/ {
        root /home/ubuntu/diary-with-pet/Backend;
    }

        location / {
                include proxy_params;
        proxy_pass http://{public IPv4}:8000;
        }
}

and I do this too.

$ sudo ln -s /etc/nginx/sites-available/Backend /etc/nginx/sites-enabled

How can I solve this?

0 Answers
Related