Deployment on DigitalOcean
Introduction
Deploying a Django application on DigitalOcean involves several steps, from creating a droplet to setting up your server and deploying your code. This tutorial will guide you through the entire process, ensuring your application runs smoothly on a DigitalOcean server.
Creating a DigitalOcean Account
Before you can deploy your application, you need to create a DigitalOcean account.
- Go to DigitalOcean.
- Sign up for an account or log in if you already have one.
- Once logged in, you can create your first droplet.
Creating a Droplet
A droplet is a virtual private server that can host your Django application.
- Click on the "Create" button and select "Droplets".
- Choose an image. For this tutorial, we'll use Ubuntu 20.04.
- Select a plan. The standard plan is sufficient for most small to medium-sized applications.
- Choose a datacenter region close to your target audience.
- Add SSH keys for secure access to your server.
- Click on "Create Droplet" to finalize the creation process.
Accessing Your Droplet
Once your droplet is created, you can access it using SSH. To do this, you need the IP address of your droplet, which can be found on your DigitalOcean dashboard.
Open your terminal and run the following command:
Replace your_droplet_ip
with the actual IP address of your droplet.
Setting Up the Server
After accessing your droplet, you need to set up the server for your Django application.
- Update the package list and upgrade the installed packages:
- Install necessary packages:
sudo apt update
sudo apt upgrade
sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl
Setting Up PostgreSQL
Django works well with PostgreSQL. Let's set it up.
- Log in to the PostgreSQL database server:
- Create a database and a user:
- Install and configure the virtual environment:
- Install Django and Gunicorn:
CREATE DATABASE mydb;
CREATE USER myuser WITH PASSWORD 'password';
ALTER ROLE myuser SET client_encoding TO 'utf8';
ALTER ROLE myuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
\q
sudo apt install python3-venv
python3 -m venv myenv
source myenv/bin/activate
pip install django gunicorn psycopg2-binary
Deploying Your Django Application
Let's deploy your Django application on the server.
- Clone your Django project from a version control system like Git:
- Navigate to your project directory:
- Install the project dependencies:
- Apply the migrations:
- Collect static files:
- Test your application:
Configuring Gunicorn
Gunicorn is a Python WSGI HTTP Server for UNIX. It will serve your Django application.
- Create a systemd service file for Gunicorn:
- Add the following content to the file:
- Replace the placeholders with your actual paths and project names.
- Start and enable the Gunicorn service:
sudo nano /etc/systemd/system/gunicorn.service
[Unit] Description=gunicorn daemon After=network.target [Service] User=root Group=www-data WorkingDirectory=/path/to/your/project ExecStart=/path/to/your/venv/bin/gunicorn --workers 3 --bind unix:/path/to/your/project.sock your_project.wsgi:application [Install] WantedBy=multi-user.target
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
Configuring Nginx
Nginx will act as a reverse proxy for Gunicorn.
- Create a new server block configuration file:
- Add the following content to the file:
- Replace the placeholders with your actual paths and project names.
- Enable the file by creating a symbolic link:
- Test the configuration and restart Nginx:
sudo nano /etc/nginx/sites-available/your_project
server { listen 80; server_name your_domain_or_IP; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /path/to/your/project; } location / { include proxy_params; proxy_pass http://unix:/path/to/your/project.sock; } }
sudo ln -s /etc/nginx/sites-available/your_project /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
Conclusion
Congratulations! You have successfully deployed your Django application on DigitalOcean. Your application should now be running and accessible via your domain or IP address.