DEV Community

Tonny Kirwa
Tonny Kirwa

Posted on • Updated on

Deploying Django Project on an Ubuntu Server

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:

   ssh -i ~/.ssh/id_rsa ubuntu@ip_address
Enter fullscreen mode Exit fullscreen mode

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:

   sudo apt update
Enter fullscreen mode Exit fullscreen mode

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:

   sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

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

   sudo systemctl start nginx
   sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

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

   sudo ufw allow 'Nginx Full'
Enter fullscreen mode Exit fullscreen mode

Install PostgreSQL & Allow it via Firewall
If you intend to use PostgreSQL as your database, you can install it using the following command:

   sudo apt install postgresql postgresql-contrib
Enter fullscreen mode Exit fullscreen mode

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

   sudo ufw allow 'PostgreSQL'
Enter fullscreen mode Exit fullscreen mode

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:

   sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

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:

   CREATE DATABASE your_db_name;
Enter fullscreen mode Exit fullscreen mode

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:

   CREATE USER your_db_user WITH PASSWORD 'your_password';
Enter fullscreen mode Exit fullscreen mode

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

   GRANT ALL PRIVILEGES ON DATABASE your_db_name TO your_db_user;
Enter fullscreen mode Exit fullscreen mode

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:

   \q
Enter fullscreen mode Exit fullscreen mode

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

   sudo apt install python3 python3-venv
Enter fullscreen mode Exit fullscreen mode

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

   cd /path/to/your/django/project
   python3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

Activate the virtual environment:

   source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Install project dependencies:

   pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

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

    python manage.py collectstatic
Enter fullscreen mode Exit fullscreen mode

Migrate Database
Apply initial database migrations

    python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Test Your Application
Start your Django development server to test your application

    python manage.py runserver 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
chatelo profile image
Benard Ronoh

I missed out on how to copy your project dorectory to the server