Deploying Django Project on an Ubuntu Server
Tags: devops, django, deploy, tutorial
Published: 2023-10-10 10:35:37

Setting up a Django project on an Ubuntu server involves several steps. Here's a step-by-step guide to help you get started:

**Connect to the Remote Server**
   Open your terminal and use the `ssh` command to connect to your remote server using the provided IP address and your SSH key. Replace `~/.ssh/id_rsa` with the path to your private SSH key:

   ```bash
   ssh -i ~/.ssh/id_rsa ubuntu@ip_address
   ```

   You should now be logged into your Ubuntu server.

**Update Package List**
   It's always a good practice to update the package list on your server before installing new software. Run the following command to update the package list:

   ```bash
   sudo apt update
   ```
------------
**Install Nginx**
   Nginx is a popular web server that can serve as a reverse proxy for your Django application. Install Nginx using the following command:

   ```bash
   sudo apt install nginx
   ```

   After the installation is complete, start the Nginx service and enable it to start at boot:

   ```bash
   sudo systemctl start nginx
   sudo systemctl enable nginx
   ```

   By default, Nginx should be allowed through the firewall, but you can verify that it's enabled by running:

   ```bash
   sudo ufw allow 'Nginx Full'
   ```
------------
**Install PostgreSQL & Allow it via Firewall**
   If you intend to use PostgreSQL as your database, you can install it using the following command:

   ```bash
   sudo apt install postgresql postgresql-contrib
   ```

   PostgreSQL should also be allowed through the firewall, but you can explicitly enable it:

   ```bash
   sudo ufw allow 'PostgreSQL'
   ```

To create a PostgreSQL database, assign a user to it, and grant all privileges, you can follow these steps:

**Log in as the PostgreSQL Superuser**

   First, switch to the PostgreSQL user and open the PostgreSQL command line utility:

   ```bash
   sudo -u postgres psql
   ```

**Create the Database**

   Once you are in the PostgreSQL shell, you can create a new database. Replace `your_db_name` with your desired database name and `your_db_user` with your desired username:

   ```sql
   CREATE DATABASE your_db_name;
   ```

**Create a User and Assign to the Database**

   Create a user and set a password for it. Replace `your_db_user` and `your_password` with your desired username and password:

   ```sql
   CREATE USER your_db_user WITH PASSWORD 'your_password';
   ```

   Next, grant the user privileges on the database. Replace `your_db_name` and `your_db_user` with your database name and username:

   ```sql
   GRANT ALL PRIVILEGES ON DATABASE your_db_name TO your_db_user;
   ```

   This will grant the user full access to the specified database.

**Exit the PostgreSQL Shell**

   To exit the PostgreSQL shell and return to the regular command line, type:

   ```sql
   \q
   ```

   You should now have created a PostgreSQL database, assigned a user to it, and granted that user all privileges on the database. Make sure to update your Django project's settings to use the correct database name, username, and password as configured in these steps.

------------
**Install Python and Virtual Environment**
   You'll need Python and a virtual environment for your Django project. Install Python and `venv`

   ```bash
   sudo apt install python3 python3-venv
   ```

**Clone Your Django Project**
   You can clone your Django project repository from a Git repository or transfer it to the server using another method.

**Set Up Your Django Project**
   Navigate to your Django project directory and create a virtual environment

   ```bash
   cd /path/to/your/django/project
   python3 -m venv venv
   ```

   Activate the virtual environment:

   ```bash
   source venv/bin/activate
   ```

   Install project dependencies:

   ```bash
   pip install -r requirements.txt
   ```


**Configure Django Settings**
   Configure your Django settings to use the PostgreSQL database and other necessary configurations.

**Collect Static Files**
    If your project serves static files using Django, collect them using the following command

   ```bash
    python manage.py collectstatic
   ```

**Migrate Database**
    Apply initial database migrations

   ```bash
    python manage.py migrate
   ```

**Test Your Application**
    Start your Django development server to test your application
   ```bash
    python manage.py runserver 0.0.0.0:8000
   ```

**Configure Nginx for Django**
    Configure Nginx to serve your Django application. Create an Nginx server block (virtual host) configuration file for your site. You can create a new configuration file in `/etc/nginx/sites-available/`, or modify the default configuration. Make sure to configure Nginx to proxy requests to your Django application using the Gunicorn or uWSGI server.

**Test Your Website**
    Test your website by accessing it through your domain or the server's IP address in a web browser.

**Secure Your Site with SSL** (Optional)
    Consider securing your site with SSL using Let's Encrypt or another SSL certificate provider.

That's a general overview of the steps required to set up a Django project on an Ubuntu server. The specific details may vary depending on your project's requirements and configurations.