Deploying Django using Ngnix and Gunicorn on Ubuntu
Deploying Django using Ngnix and Gunicorn on Ubuntu
First of all,Ā I am going to install MySQL database because we are using it in our project
Installing MySQLĀ
UpdateĀ
sudo apt update
Then install the mysql-server package:
sudo apt install mysql-server
Ensure that the server is running using the systemctl start command:
sudo systemctl start mysql.service
Configure Mysql
mysql_secure_installation
We areĀ not going to use root user , so we will create new userĀ
Creating New MySQL User
mysql -u root -p
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO āuserā@'hostā;
FLUSH PRIVILEGES;
exit
Now you can login with your username and password using command:
mysql -u username -p
Ā
Deploying Django with Nginx and Guicorn
Install python, pip and NgnixĀ
sudo apt install python3-pip python3-dev nginx
Creating a python virtual environmentĀ
sudo apt install virtualenv
Create project folderĀ
cd /
mkdir projects
cd projects
I have created projects folder at root.
Create and activate the virtual env
virtual env
source env/bin/activate
Upload the django projectĀ Ā
I am going to clone the project from github as i have project on github.
git clone https://github.com/DurgeshCoder/lcwd-back.git
cd lcwd-back
pip3 install -r requirements.txt
Ā
If you have any migrations then migrate and also put the correct database details in settings.py file
python3 manage.py migrate
Now test application is running on development server or not
python3 manage.py runserver 0.0.0.0:8000
Check using IP and Port 8000.
Now use GunicornĀ
Let's create a system socket file for gunicorn now:
sudo vim /etc/systemd/system/gunicorn.socket
Paste the contents below and save the file
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target```
Next, we will create a service file for gunicorn
sudo vim /etc/systemd/system/gunicorn.serviceĀ
Paste the contents below inside this file:
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/projects/lcwd-back
ExecStart=/projecgs/env/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
textutils.wsgi:application
[Install]
WantedBy=multi-user.target```
Lets now start and enable the gunicorn socket
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
Configurations of Ngnix
sudo vim /etc/nginx/sites-available/lcwd
Paste the below contents inside the file created
listen 80;
server_name www.learncodewithdurgesh.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /projects/lcwd-back;
}
location /media/ {
root /projects/lcwd-back;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Activate the configuration using the following command:
sudo ln -s /etc/nginx/sites-available/lcwd /etc/nginx/sites-enabled/Ā
Restart nginx and allow the changes to take place.
sudo systemctl restart nginx
Now check on port 80 your site will run.