Posted on 2.2.2025

How to install and run Django on UpCloud Developer Cloud Servers

Django is a robust framework for developers to build strong, scalable web applications. It makes building better web apps easier and faster, and with less code.

In this step-by-step tutorial, we will show how to install Django on an UpCloud Developer Cloud Server, set up the development environment, create and configure a basic project, and run the Django application on your Developer Cloud Server with minimal fuss.

Why to run Django on UpCloud

Creating engaging and exciting apps within Django’s flexible framework is easy. These apps can be hosted on cloud servers, such as UpCloud’s Developer plans, offering users a wide range of benefits:

  • Speed—Django’s flexible and powerful modular framework allows developers to create applications quickly and efficiently while being able to reuse code where required.
  • Security—Security is a critical driver in Django’s value offer with its built-in mitigations protecting against common vulnerabilities such as SQL injection, clickjacking, and cross-site scripting.
  • Scalability—Django allows you to build as you grow by scaling up your app to handle more traffic over time.
  • Bundled extras—Django simplifies many common web development tasks, such as authentication, content administration, site maps, and RSS feeds, with dozens of extras included out of the box.

In short, Django offers developers everything they need to confidently host their apps. This guide to running Django on UpCloud Cloud Servers aims to help you solve your application hosting needs.

1.     Setting up your UpCloud server

To get started, you need to create a server to host your Django app. But before then, if you haven’t registered yet, sign up with UpCloud for a free trial to test things out.

Once logged in, you can set up a new cloud server.

  • 1.    On the Dashboard, click on Servers in the top left-hand corner.
  • 2.    Choose your server location. Look for the nearest data centre location to you to minimise latency for remote connections.
  • 3.    Select the right server plan for you. The €3/mo Developer plan with 1GB RAM and 10GB storage is perfect for a small project. You can always scale up later when needed.
  • 4.    Choose your operating system. UpCloud offers several templates. This guide is based on using Ubuntu, but much of the installation process works the same regardless of the distribution.
  • 5.    Add your SSH key as your chosen login method and click Deploy to launch your new server.

2.     Installing dependencies

Once you’ve established your server configuration, it’s time to install the dependencies so that Django can run smoothly.

Log Into your Cloud Server

Open your terminal, or you can use PuTTY on Windows. Run the following command replacing “your_server_public_ip” with the IP address from UpCloud.

ssh root@your_server_public_ip

If you included your SSH keys, you should get straight in. Alternatively, if you chose to deploy your Cloud Server with a one-time password, enter it when prompted. You should have received the one-time password either by email or text message depending on what you selected at deployment.

Update the server software

To ensure your server runs smoothly, run updates so you have the current versions to run Django on.

sudo apt update && sudo apt upgrade -y

Install Python, pip, Nginx, and virtualenv

Django is a Python web hosting app, meaning you’ll need to install Python to run Django. In addition, you can install pip and Nginx (in this example), the web server, at the same time.

sudo apt install python3-pip python3-dev libpq-dev nginx

Then, install virtualenv, the virtual environment, to separate the project’s dependencies.

sudo apt install virtualenv

Set up a database

With Nginx or Apache already established as the web server, you must create the SQL database. This example uses PostgreSQL, but Django supports a whole host of database types.

sudo apt install postgresql postgresql-contrib

Note that installing the database onto the same Cloud Server as Django works fine for testing and small development purposes. However, if you are planning to use Django for anything more substantial, we recommend deploying our Managed Databases for PostgreSQL.

3.     Installing Django

Once the server dependencies are installed, install Django and set up a project by following these steps.

Create a virtual environment

First, set up a project folder and a virtual environment within it.

mkdir ~/django
cd ~/django
virtualenv myenv

Then, activate the virtual environment.

source myenv/bin/activate

Install Django

Now, it’s time to install Django in your virtual environment.

pip install django

And create a new Django project.

django-admin startproject myproject .

Note: The period at the end of the line is an important part of the command, indicating that the project should be started in the current folder.

Configure the database

Before you can begin developing, you’ll need to set up a database. Start by installing the PostgreSQL adapter.

pip install psycopg2

Then, open your myproject/settings.py file to update the database configuration and ALLOWED_HOSTS parameter.

sudo nano ~/django/myproject/settings.py
ALLOWED_HOSTS = ['your_server_public_ip']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

The next step is to create the database and user details. Open the database client using the following command.

sudo -u postgres psql

Then create the database and your user credentials.

CREATE DATABASE myproject;
CREATE USER myprojectuser WITH PASSWORD 'password';
ALTER ROLE myprojectuser Set client_encoding TO 'utf8';
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;
ALTER DATABASE myproject OWNER TO myprojectuser;

When done, exit the database client to return to your user account.

exit;

Finally, run Django’s database migrations to complete the Django installation and test the configuration.

python manage.py migrate

4.     Configuring Nginx and Gunicorn for Django

Before deploying the app, you’ll need to configure your web server, in this case, Nginx and Gunicorn, a Python server that implements the Web Server Gateway Interface (WSGI).

Nginx works as a reverse proxy, while Gunicorn is Django’s application server. Nginx has already been installed as one of the project dependencies, so you only need to install Gunicorn at this stage.

Install Gunicorn

Install Gunicorn with the following command.

pip install gunicorn 

Then, test it by running Gunicorn alongside your Django project.

gunicorn --workers 3 myproject.wsgi:application

You should see an output similar to the example below.

[2025-02-02 13:17:07 +0000] [115894] [INFO] Starting gunicorn 23.0.0
[2025-02-02 13:17:07 +0000] [115894] [INFO] Listening at: http://127.0.0.1:8000 (115894)
[2025-02-02 13:17:07 +0000] [115894] [INFO] Using worker: sync
[2025-02-02 13:17:07 +0000] [115895] [INFO] Booting worker with pid: 115895
[2025-02-02 13:17:07 +0000] [115896] [INFO] Booting worker with pid: 115896
[2025-02-02 13:17:07 +0000] [115897] [INFO] Booting worker with pid: 115897

Once you can see that the workers have started successfully, you can shut down the workers by pressing Ctrl + C.

Configure Nginx

With the above test done, it’s time to configure Nginx to act as the reverse proxy. This enables it to forward requests to Guincorn from the internet.

Create a new configuration file.

sudo nano /etc/nginx/sites-available/myproject

Then, add this code to configure it as the reverse proxy.

server {
        listen 80;
        server_name your.domain.com your_server_public_ip;
        location / {
                proxy_pass http://localhost:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
        }
}

 To save and enable the configuration, add this code.

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo systemctl restart nginx

5.     Deploying and testing the app

The final step in running Django on UpCloud is to deploy your app and ensure everything works properly.

Firstly, run Django migrations. Since we ran this command already once, there shouldn’t be any changes to migrate.

python manage.py migrate 

Next, add a static root directory to your settings.py file.

STATIC_ROOT = BASE_DIR/'static'

Then, collect static files.

python manage.py collectstatic

Then, launch Gunicorn workers again.

gunicorn --workers 3 myproject.wsgi:application

You should then see an output similar to the example below.

[2025-02-02 13:25:05 +0000] [116075] [INFO] Starting gunicorn 23.0.0
[2025-02-02 13:25:05 +0000] [116075] [INFO] Listening at: http://127.0.0.1:8000 (116075)
[2025-02-02 13:25:05 +0000] [116075] [INFO] Using worker: sync
[2025-02-02 13:25:05 +0000] [116076] [INFO] Booting worker with pid: 116076
[2025-02-02 13:25:05 +0000] [116077] [INFO] Booting worker with pid: 116077
[2025-02-02 13:25:05 +0000] [116078] [INFO] Booting worker with pid: 116078

By this stage, your web-based application should be working.

To test it, open a browser and go to “http://your_server_public_ip” and your Django app should appear.

Common issues and troubleshooting

If you can’t access your web, here are some common issues to check and how to resolve them.

  • Firewall: Your server’s firewall may be blocking access. Use this command to allow all HTTP traffic through your firewall
sudo ufw allow 'Nginx Full'
  • Incorrect Nginx configuration: It’s possible that Nginx isn’t serving your app correctly. Run this command to double-check the Nginx configuration file and verify it is valid.
sudo nginx -t
  • Nginx and Gunicorn service status: Check the service status to ensure they are both running. Run these commands to test.
sudo systemctl status nginx
ps aux | grep gunicorn

If Nginx and Gunicorn are running and you still can’t access your app via a web browser, check the server logs for any reported issues.

Further resources

This comprehensive guide is designed to help you run Django on the Developer Cloud Servers hosted by UpCloud. For further information and reading, several excellent resources are available via UpCloud and Django to help you maximise the Django web hosting experience.

UpCloud advanced user setup guides

UpCloud API for developers

Security on UpCloud

Django “How-to” user guides

Conclusion

This tutorial offers a comprehensive guide to setting up a Django web app using UpCloud’s superb, no-fuss Developer Cloud Servers. The unparalleled value and reliability make it an excellent choice for developers wanting to host web applications remotely. It’s the ideal hosting space for Django’s highly flexible development environment.

In addition to offering a fast and robust platform to host web apps, the flexibility of our Cloud Servers makes it easy to scale to cope with more traffic as your audience grows. In addition, with an outstanding API designed to add efficiency and ease the development process, UpCloud and Django’s versatile web development environment is perfect for developers of all expertise.

Start building your Django apps on UpCloud today and enjoy the fastest Cloud Servers optimised for performance. Sign up for a free trial now!

Janne Ruostemaa

Editor-in-Chief

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top