Mrsreez
Mrsreez

Reputation: 79

Need to run django application in a domain name without specific port

I am newbie to Django recently I created a Django app and I uploaded to the server. I assigned a domain name to it. each time I run the server I need to type xyz.com:8000 to see my website. Is there any way to resolve this issue? Also, I have doubt. Do I need to type python manage.py runserver 0:8000 to launch the website or it's just run automatically like PHP.

Upvotes: 0

Views: 1786

Answers (1)

Dmitry Shevchenko
Dmitry Shevchenko

Reputation: 478

You should set up a web server! The web server is required for any site to work. Currently the most popular are Apache and NGINX. It is the web server that responds to user requests. We need to ensure the interaction of the web server and the python application. Most popular solutions:

Consider an example with Nginx and Gunicorn:

Let's start by installing the Gunicorn module in a virtual environment:

pip install gunicorn

Configure the gunicorn service settings for our project:

sudo nano /etc/systemd/system/gunicorn.service

/etc/systemd/system/gunicorn.service:

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

[Service]
User=my_user
Group=www-data
WorkingDirectory=/home/project_dir/project
ExecStart=/home/project_dir/project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/project_dir/project/project.sock project.wsgi

[Install]
WantedBy=multi-user.target

We enable and run the gunicorn service, check its status:

sudo systemctl enable gunicorn
sudo systemctl start gunicorn
sudo systemctl status gunicorn

If all is well, install the nginx web server:

sudo apt install nginx

Configure the project site parameters:

sudo nano /etc/nginx/sites-available/project

/etc/nginx/sites-available/project:

server {
    listen 80;
    server_name <server IP or domain name>;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/project_dir/project;
    }

    location /media/ {
        root /home/project_dir/project;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/project_dir/project/project.sock;
    }
}

We use Nginx as a proxy for the gunicorn Python server:

proxy_pass http://unix:/home/project_dir/project/project.sock;

Create a link in the allowed sites folder “/etc/nginx/sites-enabled”:

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

We restart the Nginx service and add permissions to the firewall:

sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'

Done! You can check the operation of our site by typing the IP address of the server in the browser. P.S. Sorry for my english! If you see an error in the text or code, please edit me.

Upvotes: 1

Related Questions