Linux is a versatile operating system that is used to power the majority of the websites and devices on the internet. Django is a popular web framework used to develop high-performance and scalable websites.
And when you’re a web developer that uses Linux and Django, you may want to use both to accomplish your tasks.
You can use Django in Linux to develop your websites locally and host them on a server running on Linux operating system such as Ubuntu. The majority of the web solutions developed with Django use Linux OS for hosting, mainly Ubuntu. So, for desktop and server, Linux and Django work well while achieving the best performance and speed.
There are a couple of things that you need to do before you can start using Django in Linux.
At first, you will need to have Python installed. The reason is that Django is a web framework developed using Python programming language. Thus, Django cannot run when you do not have Python installed on your Linux system.
Here’s a summary of how to install Python on a Linux system such as Ubuntu, Kali Linux, e.t.c:
Open the Terminal window by pressing CTRL + ALT + T or locating the Terminal app in your application list.
Type the following to install Python on your system:
sudo apt update && sudo apt upgrade sudo apt install python3
The above commands install or update Python to the latest version.
To confirm that you have installed Python successfully, type
and press Enter
You should see the latest Python version 3 has been installed.
Check this guide for a step-by-step guide to installing and troubleshooting Python installation on Linux.
After successfully installing or updating Python 3, you may now install Django on your system by creating a Python virtual environment that isolates your Django project from the system-wide environment.
How to install and run a Django project in Terminal
To install Django using the Terminal, you will need to:
1. open a Terminal window,
2. create a new directory using mkdir command,
3. create a new virtual environment and install Django,
4. create a new Django project using the django-admin command, and
5. serve your Django project locally on Django default web server, runserver.
To do that, create a new folder for your new Django project and navigate into the folder.
You may create the directory anywhere on your system. I will use the Desktop directory.
cd ~/Desktop mkdir newDjangoWebsite cd newDjangoWebsite
Create a new virtual environment and activate it:
python3 -m venv ProjectEnv source ProjectEnv/bin/activate
Install Django in the newly created virtual environment:
pip install django
You can now create a new Django project and serve the project locally using the Django development server,
python manage.py runserver.
django-admin startproject myDjangoWebsite .
Run local development server by executing
python manage.py runserver
Open the browser and type the following in the address bar 127.0.0.1:8000
You should see a similar window indicating that Django has been successfully installed on your system and working fine.
For a more step-by-step guide to installing and configuring Django on Linux, check this tutorial. If you want to deploy your Django web applications/websites on Linux servers, you can check out this article I have compiled.
Now go ahead and install or use your favorite IDE or code editor.
My favorite is Pycharm IDE and Visual Studio Code code editor. I use these tools to write effective Django code and collaborate with others. Moreover, they offer the best features not limited to debugging, code linting, error highlighting, e.t.c.
Check this article for more detailed information on how to install Pycharm, Visual Studio Code, and other helpful tools and applications you need as a Web developer who uses Linux operating system.
When it comes to deploying your Django website, there are other things that you must configure, such as setting up the database, web server, environment variables, and XML schemas needed for Search Engine Optimization.
First, you will need a database if you’re building a Django website that handles various data storage and manipulation, such as a blog.
Does Django use SQL?
Django is compatible with popular SQL databases that include MySQL, PostgreSQL, Oracle, and MariaDB. On top of that, you may find third-party database connectors that allow Django to support Microsoft SQL Server and Firebird databases. Thus, if you have a hosting service that provides these databases, Django can be hosted on their virtual private servers, dedicated servers, and shared hosting servers that run on Linux.
This article details how to deploy your Django application on cPanel using MySQL or PostgreSQL databases.
For dedicated or virtual private servers such as AWS EC2 instances, you may install Ubuntu operating system on the instances and use Ubuntu OS to deploy your Django web application.
In this article, you will find all the steps needed to host and serve your Django website on AWS EC2 or any other dedicated server.
Also, follow this link to check out the best hosting providers I have used for VPS and Shared hosting servers.
Does Django need a web server?
A live Django website needs a web server other than the local development server, which is not suitable for serving production-ready Django websites. You will need to use Gunicorn and NGINX when deploying your Django application on the internet. Gunicorn and NGINX use an interface such as WSGI and ASGI to communicate with Django.
WSGI and ASGI interfaces are, by default, supported by Django.
This article has all the steps you need to configure and connect Gunicorn and NGINX with Django on a Linux server setup.
Installing Django and using the framework in a Linux environment is very straightforward to set up on both desktop and server environments. Linux makes it easy to write Django code on your machine and deploy your Django applications on a server setup that still uses Linux.
Thus, you do not need to learn a new operating system as you will be familiar with using Django on a desktop. Thus, deploying Django websites on a Linux server should be a walk in the park. Besides, get free licensing and use Linux servers for free.
And that’s it basically for this tutorial. I hope that you can now use Django in your Linux system.
Happy Django-ing, Dev!