Deploying in Ubuntu Machine Part 1
Resources
Django Setup
- Create an Virtual Machine with Ubuntu Image.
-
Allow TCP traffic to port 8000 in the security group.
-
Check for python version
python3 --version
- Install pip
sudo apt update
sudo apt install python3-pip
-
Clone the repository (https://github.com/Prabin-Kumar-Baniya-NP/Vaccination-Scheduling-Application.git)
-
Move to the repository and create a virtual environment
sudo apt install python3.10-venv
python3 -m venv env
- Then, activate the virtual environment
source env/bin/activate
- Install the dependencies
pip3 install -r requirements.txt
-
Move to the project root (where manage.py file is located) and Create .env files and set the dubug to True and add a secret key.
-
Move to the project root directory and migrate the changes to SQlite Database.
python3 manage.py migrate
- Collect the static files.
python3 manage.py collectstatic
- Check whether gunicorn is installed. If not, then install gunicorn.
pip3 install gunicorn
- Start the gunicorn workers.
gunicorn mysite.wsgi --bind 0.0.0.0:8000 --workers=2
Setting up Gunicorn Web Server
Socket File
Now, we will setup gunicorn to run on socket that will allow to communicate to gunicorn from different process of our linux instance.
- Create a gunicorn socket file
sudo nano /etc/systemd/system/gunicorn.socket
I have attached gunicorn-socket.txt file in the resources section. Plz download it and paste its content in this file.
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
-
This description is a human-readable description of the unit. In this case, it describes the purpose of the unit as a Gunicorn socket.
-
ListenStream specifies the socket that Gunicorn should listen on for incoming connections. In this case, it's set to /run/gunicorn.sock. Sockets are communication endpoints that enable processes to exchange data. The .sock extension in the path indicates that it's a Unix domain socket, which allows processes on the same machine to communicate.
-
WantedBy indicates the target that depends on this unit. In this case, it's set to sockets.target, which means that when the sockets.target is activated during system startup, systemd will activate this unit.
Service File
- Now, lets create a gunicorn service file. using this service file, we will run django with gunicorn.
sudo nano /etc/systemd/system/gunicorn.service
I have attached gunicorn-service.txt file. Plz download it and paste its content.
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/Vaccination-Scheduling-App/mysite/
ExecStart=/home/ubuntu/Vaccination-Scheduling-App/env/bin/gunicorn mysite.wsgi:application --bind unix:/run/gunicorn.sock --workers 2
[Install]
WantedBy=multi-user.target
-
Now, This servic file defines how a Gunicorn server should be managed and started as a systemd service.
-
In the Description we have provided a human-readable description of the unit, which in this case is a Gunicorn daemon.
-
Requires value Specifies that this unit requires the gunicorn.socket unit to be active before starting.
-
After value defines that this unit should start after the network.target unit has been activated, that means network services should be ready before starting this service.
-
User value Specifies the user account under which the Gunicorn service will run. In this case, it's set to "ubuntu."
- The Group value Specifies the group under which the Gunicorn service will run. In this case, it's also set to "ubuntu."
- WorkingDirectory Sets the working directory for the Gunicorn process. This is where Gunicorn will start looking for your application.
-
ExecStart defines the command to start the Gunicorn server. It activates a virtual environment located at /home/ubuntu/Vaccination-Scheduling-App/env, and then runs the Gunicorn server with the given configuration:
-
The mysite.wsgi:application specifies the WSGI application to be run.
-
--bind unix:/run/gunicorn.sock: Instructs Gunicorn to bind to a Unix socket at /run/gunicorn.sock. This allows communication between the server and applications.
-
--workers 2: Specifies that Gunicorn should spawn 2 worker processes to handle incoming requests.
-
WantedBy: Specifies the target that should be reached for this service to be considered "wanted." In this case, the service is wanted in the multi-user.target target, which is a common target for multi-user systems, indicating that this service should be started when the system enters the multi-user mode.
-
In summary, this configuration file defines a systemd service for running a Gunicorn server. When the service is started, it will activate the Gunicorn server with the specified settings, user, group, and working directory. The server will bind to a Unix socket for communication and use two worker processes to handle incoming requests.
Now, you don't need to remember this code. You can get this code easily over the internet. Just understand the process of deployment in this video.
- Start the socket
sudo systemctl start gunicorn.socket
- Check the status of socket
sudo systemctl status gunicorn.socket
- Start the gunicorn service
sudo systemctl start gunicorn.service
- Check the status of gunicorn service
sudo systemctl status gunicorn.service
You will get to see something like this
Jun 19 03:22:00 ip-172-31-6-82.ap-south-1.compute.internal gunicorn[27584]: [2023-06-19 03:22:00 +0000] [27584] [INFO] Starting gunicorn 20.1.0
Jun 19 03:22:00 ip-172-31-6-82.ap-south-1.compute.internal gunicorn[27584]: [2023-06-19 03:22:00 +0000] [27584] [INFO] Listening at: unix:/run/gunicorn.sock (27584)
Jun 19 03:22:00 ip-172-31-6-82.ap-south-1.compute.internal gunicorn[27584]: [2023-06-19 03:22:00 +0000] [27584] [INFO] Using worker: sync
Jun 19 03:22:00 ip-172-31-6-82.ap-south-1.compute.internal gunicorn[27585]: [2023-06-19 03:22:00 +0000] [27585] [INFO] Booting worker with pid: 27585
Jun 19 03:22:00 ip-172-31-6-82.ap-south-1.compute.internal gunicorn[27586]: [2023-06-19 03:22:00 +0000] [27586] [INFO] Booting worker with pid: 27586