Deploy Django project with Nginx, and Gunicorn on Ubuntu 22.04 |
HTML-код
- Опубликовано: 12 сен 2024
- Howdy Programers,
-------------------------------------------------------------
Creating systemd Socket and Service Files for Gunicorn
sudo nano /etc/systemd/system/gunicorn.socket
paste below code:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
-------------------------------------------------------------
sudo nano /etc/systemd/system/gunicorn.service
paste below code::
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myprojectdir
ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
myproject.wsgi:application
[Install]
WantedBy=multi-user.target
-------------------------------------------------------------
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
sudo systemctl status gunicorn.socket
-------------------------------------------------------------
Configure Nginx to Proxy Pass to Gunicorn
sudo nano /etc/nginx/sites-available/myproject
paste below code:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sammy/myprojectdir;
}
location / {
include proxy_params;
proxy_pass unix:/run/gunicorn.sock;
}
}
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
-------------------------------------------------------------
Thank you
Regards
Official Tech
--------------------------------------------------------------
Code: github.com/off...
Video: / officialtechjb
Home: hindbay.com
Services: fourthX.com
I spent almost 7 hours on this, and i finally did it in 30 minutes with your video. Thanks you very very very much!
After searching and watching millions of videos with Hindus you solved my problem which was not solved for the 3rd day. Respect!
Really good explanation, works very well! ;)
Thank you... 🤩🤩🤩
Thank you, it saved my hours of time.
Thank you a lot!
Thx for tutorial. One question, are you copying from the clipboard default nginx configuration text? Or some assistant do this? Because the most important part that connects the gunicorn socket with nginx is not explained. THX!
You can copy paste default configuration for gunicorn from internet
thanks the video is very helpful
Thank you Sujal... 🤩🤩🤩
So what are the steps if I have my finished project on local disk. Should I copy and paste all files to server, what about requirments.txt and so on ...
you need to put all the files there on server as well as requirements.txt and you have create the environment and install all the dependencies and so on...
Thank you, it helped a lot, do you have another video showing it and putting it on https?
No, not yet...
Thank you
hello great content but how can I configure the nginx to serve two Django apps in my ec2 instance on different ports using the public IP of the instance as server name
sudo apt update
sudo apt install nginx
sudo nano /etc/nginx/sites-available/app1
paste this code
server {
listen 80;
server_name YOUR_PUBLIC_IP;
location / {
proxy_pass localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
sudo nano /etc/nginx/sites-available/app2
paste this
server {
listen 80;
server_name YOUR_PUBLIC_IP;
location / {
proxy_pass localhost:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Enable Server Blocks
sudo ln -s /etc/nginx/sites-available/app1 /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/app2 /etc/nginx/sites-enabled/
sudo systemctl restart nginx
@@otJB Thanks for the reply though it says the server name for app1 will be ignored since it's the same public IP of my ec2 instance I was thinking of a way in which I can use the same public IP of my e2 instance to serve both apps running in different ports based on the location params in the nginx configuration like /home to serve app1 and /app1 to server app2
But if you created an apiview, could I consume it? implemented this way of deploying
Yes
please can you make a video on how we can deployit with docker?
Hey there! Thanks for reaching out. Right now, I'm a bit tied up with some other projects, but I hope to dive into creating that video in the future. Deploying with Docker sounds like a great topic to cover, so stay tuned, and hopefully, I'll have something for you soon! Thank you!
great video! my static files are not still loaded not sure why
nevermind i linked the wrong conf file. works thank you
🤪 Promo-SM
should i develop every django project in /var/www/html/... my django project?
you can deploy anywhere on the server but need to specify the path on nginx/apache configuration