This repository contains the codebase for the Attendance Application. Below are the steps to set it up on your local machine.
To get started, clone this repository using the following command:
git clone https://github.com/vaaditya320/attendance-ledger.gitChange into the project directory:
cd attendance-ledgerSet up a Python virtual environment for the project:
python3 -m venv venvActivate the virtual environment:
- On Linux/Mac:
source venv/bin/activate - On Windows:
.\venv\Scripts\activate
Install the necessary Python dependencies:
pip install -r requirements.txtCreate a .env file in the root directory of the project and add the following environment variables:
MAILJET_API_KEY=your-mailjet-api-key
MAILJET_SECRET_KEY=your-mailjet-secret-keyReplace the placeholder values with your actual keys and secrets.
Run the following command to apply database migrations:
python manage.py migrateStart the development server:
python manage.py runserverThe application will be available at http://127.0.0.1:8000/.
To access the Django admin panel, create a superuser:
python manage.py createsuperuserFollow the prompts to set up an admin account. Then navigate to http://127.0.0.1:8000/admin to log in.
- Launch an EC2 instance (Ubuntu).
- Connect via SSH:
ssh -i "your-key.pem" ubuntu@your-ec2-public-ip
- Update packages:
sudo apt update && sudo apt upgrade -y - Install Python and Nginx:
sudo apt install python3-pip python3-venv nginx -y
-
Clone your project:
cd /home/ubuntu/projects git clone https://github.com/vaaditya320/attendance-ledger.git attendance cd attendance
-
Create a virtual environment:
python3 -m venv venv source venv/bin/activate pip install -r requirements.txt -
Collect static files:
python manage.py collectstatic
-
Update
.envfile with Mailjet credentials.MAILJET_API_KEY = "your-mailjey-api-key" MAILJET_API_SECRET = "your-mailjey-api-secret"
-
Modify
settings.py:DEBUG = FalseALLOWED_HOSTS = ["attendance.aadityavinayak.in.net"]STATIC_ROOT = "/home/ubuntu/projects/attendance/staticfiles"
- Start Gunicorn manually to serve your app:
gunicorn --workers 3 --bind unix:/home/ubuntu/projects/attendance/attendance.sock attendance.wsgi:application
-
Create an Nginx configuration:
sudo nano /etc/nginx/sites-available/attendance
Add the following configuration:
server { listen 80; server_name attendance.aadityavinayak.in.net; location / { proxy_pass http://unix:/home/ubuntu/projects/attendance/attendance.sock; 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; } location /static/ { alias /home/ubuntu/projects/attendance/staticfiles/; } }
-
Enable the site:
sudo ln -s /etc/nginx/sites-available/attendance /etc/nginx/sites-enabled
-
Test and restart Nginx:
sudo nginx -t sudo systemctl restart nginx
-
Make sure the static files are accessible:
sudo chmod -R 755 /home/ubuntu/projects/attendance/staticfiles
-
Ensure the Nginx user (
www-data) has permission to read the static files and Gunicorn socket:sudo chown -R ubuntu:www-data /home/ubuntu/projects/attendance
- Point your domain (
attendance.aadityavinayak.in.net) to your EC2 instance's public IP.
Since you are running Gunicorn manually:
- Open a new screen session:
screen -S attendance
- Run Gunicorn:
gunicorn --workers 3 --bind unix:/home/ubuntu/projects/attendance/attendance.sock attendance.wsgi:application
- Detach the screen (
Ctrl+A, then D), so it continues running in the background.
-
Static Files Directory:
/home/ubuntu/projects/attendance/staticfiles -
Nginx Config File:
/etc/nginx/sites-available/attendance -
Gunicorn Socket:
/home/ubuntu/projects/attendance/attendance.sock
You can also add authentication to the static files directory by including the auth_basic configuration in the /static/ location block in Nginx.
location /static/ {
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;
alias /home/ubuntu/projects/attendance/staticfiles/;
}To add more users to your .htpasswd file, run the following command (without the -c flag):
sudo htpasswd /etc/nginx/.htpasswd anotheruserThis will prompt you for the password for the new user.
To remove a user, you can simply delete the corresponding line from the .htpasswd file or use htpasswd -D to delete a user:
sudo htpasswd -D /etc/nginx/.htpasswd usernameNow your application is live! You can always reconnect to your screen session to manage the running Gunicorn process.