Below are the detailed steps to deploy a Django project on an EC2 instance using Nginx and Gunicorn.
- In the EC2 security group, allow inbound rules for HTTP (80), HTTPS (443), and SSH (22) to enable external access.
2. Connect to the EC2 Instance via SSH
- Use the .pem key file downloaded when launching the instance to connect to the EC2 instance from your local machine.
1
| ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
|
3. Access EC2 Instance via SSH
- Connect to the instance using the public IP address of the EC2 instance.
1
| ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
|
4. Update System and Install Required Packages
- Update the system and install the necessary packages.
1
2
| sudo apt update
sudo apt install -y python3-pip python3-venv nginx postgresql postgresql-contrib libpq-dev git
|
5. Clone the Project Repository from GitHub
- Use a Personal Access Token to clone the repository from GitHub. Generate this token in Developer Settings under Personal access tokens in your GitHub account.
1
| git clone https://<your-github-token>@github.com/your-repo-url.git
|
6. Move to the Project Folder
7. Create a Python Virtual Environment
- Create a virtual environment to keep project dependencies isolated.
8. Activate the Virtual Environment
1
| source venv/bin/activate
|
9. Navigate to the Project Directory
- Go to the
workwise-01/
directory within the project folder.
10. Install Python Dependencies
- Install the required packages listed in
requirements.txt
.
1
| pip install -r requirements.txt
|
11. Set Up PostgreSQL
12. Update Django Project Settings
- Add the EC2 public IP to
ALLOWED_HOSTS
in Django’s settings.py
.
1
| ALLOWED_HOSTS = ['your-ec2-public-ip']
|
13. Migrate Database and Create Superuser
- Apply migrations to set up the database and create an admin account.
1
2
| python manage.py migrate
python manage.py createsuperuser
|
14. Run Application with Gunicorn
- Use Gunicorn to run the Django application.
1
| gunicorn --bind 0.0.0.0:8000 backend.wsgi:application
|
- If there’s an error, use the full path to Gunicorn:
1
| /home/ubuntu/workwise/venv/bin/python -m gunicorn --bind 0.0.0.0:8000 backend.wsgi:application
|
- Edit the
/etc/nginx/sites-available/default
file to configure Nginx to serve the Django application through Gunicorn.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| server {
listen 80;
server_name your-ec2-public-ip;
location / {
proxy_pass http://127.0.0.1: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;
}
location /static/ {
alias /home/ubuntu/workwise/workwise-01/backend/static/;
}
location /media/ {
alias /home/ubuntu/workwise/workwise-01/backend/media/;
}
}
|
16. Enable and Restart Nginx (** this is important when the nginx server is not working properly **)
- Test the Nginx configuration:
- Enable the Nginx configuration by creating a symbolic link and restart Nginx.
1
2
| sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled
sudo systemctl restart nginx
|
- Set up Gunicorn as a systemd service for better process management and to start the application automatically on server reboot.
Create a Gunicorn service file:
1
| sudo nano /etc/systemd/system/gunicorn.service
|
Add the following content:
1
2
3
4
5
6
7
8
9
10
11
12
| [Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/workwise/workwise-01/backend
ExecStart=/home/ubuntu/workwise/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/workwise/workwise-01/backend/backend.sock backend.wsgi:application
[Install]
WantedBy=multi-user.target
|
Enable and start the Gunicorn service:
1
2
| sudo systemctl start gunicorn
sudo systemctl enable gunicorn
|
18. Update Nginx to Use Socket (Optional)