Connecting a Django project that is hosted on a Virtual Machine with Nginx and Gunicorn

24 Views Asked by At

I'm new to deploying applications. Currently, I'm hosting my Django project on an Ubuntu virtual machine with IP address 192.168.xxx.xxx. It is hosted on a physical service with public IP address of 202.xxx.xxx.xxx (using port 8080).

I have gunicorn and nginx installed and incorporated, and they're running properly as sudo systemctl status gunicorn and sudo systemctl status nginx show that they're running with no errors. However, when I try to access the public IP address 202.xxx.xxx.xxx:8080, I'm getting the default Nginx page.

As for how I setup my gunicorn and nginx, I followed the guide posted here:

https://medium.com/@muhammadali_12976/deploying-django-on-a-local-virtual-machine-virtualbox-with-nginx-and-gunicorn-369f70937913

Here's the gunicorn.socket file:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/home/myProject/myProject.sock

[Install]
WantedBy=sockets.target

Here's the gunicorn.service file:

[Unit]

Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]

User=myUserName

Group=www-data

WorkingDirectory=/home/myProject

ExecStart=/home/myProject/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/myProject/myProject.sock core.wsgi:application

[Install]

WantedBy=multi-user.target

Here's the contents of my /etc/nginx/sites-available/myProject file

server {
    listen 80;
    server_name 192.168.xxx.xxx;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/myProject;
    }
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/myProject/myProject.sock;
    }
}

Should I have used the public address in server_name or the IP address of the virtual machine where my Django project is hosted? In addition, I also tried changing the port in nginx file to 8080, but somehow it still does not work.

0

There are 0 best solutions below