How to Install and deploy a Django application on AlmaLinux (Linux Server)

Updated: June 16th, 2023, 07:33:58 IST
Published: June 16th, 2023
How to Install and deploy a Django application on AlmaLinux (Linux Server)
Title: How to Install and deploy a Django application on AlmaLinux (Linux Server)

AlmaLinux is a well-known Linux distribution that is recognized for its reliability and security. In this article, we'll go through how to setup and deploy a Django application on AlmaLinux. We'll go over installing dependencies, creating a virtual environment, configuring the Django project, and deploying it to a web server.

Prerequisites:

  • AlmaLinux installed on your server
  • Basic knowledge of the Linux command line
  • Python 3 installed on your system

1. Update the Linux System

Before starting, it's recommended to update your AlmaLinux system to ensure you have the latest packages and security updates. Open a terminal and run the following commands:

sudo yum update

2. Install Python and pip

AlmaLinux comes with Python pre-installed. However, we need to install pip, the package installer for Python. Run the following command to install pip:

sudo yum install python3-pip

 

You can check the Python version and pip version in AlmaLinux, using the following commands:

1) To check the Python version:

python3 --version

2) To check the pip version:

pip3 --version

 

Note: In AlmaLinux, the Python executable is typically referred to as python3, and the corresponding pip executable is referred to as pip3. This is to distinguish Python 3 from Python 2, which may also be installed on the system.

Running these commands will provide you with the Python version and pip version installed on your AlmaLinux system.

3. Create a virtual environment in Python

To create a virtual environment in Python, you can use the built-in venv module. Here are the steps to create a virtual environment:

1. Open your terminal or command prompt.

2. Navigate to the directory where you want to create the virtual environment.

3. Use this command to create the virtual environment:

python3 -m venv <env_name>

Replace with the name you want to give to your virtual environment.

example:

python3 -m venv env

4. Once the command executes successfully, the virtual environment will be created in the current directory.

5. Command to activate the virtual environment

For Unix/Linux:

source <env_name>/bin/activate

For Windows:

<env_name>\Scripts\activate

After activating the virtual environment, your terminal prompt should change to indicate that you are now working inside the virtual environment.

Now you can install packages and work on your Python project within the isolated virtual environment. When you're done, you can deactivate the virtual environment by running the deactivate command in the terminal.

Note: It's recommended to use a unique name for each virtual environment to avoid conflicts.

 

4. Get code from a GitHub repository to your Linux server

To get code from a GitHub repository to your Linux server, you can follow these steps:

1. Check that your Linux server has Git installed. If it isn't already installed, use your Linux distribution's package manager to install it. On Ubuntu, for example, you can type:

sudo apt-get install git

2. Open the terminal on your Linux server.

3. Navigate to the directory where you want to clone the repository.

4. Clone the repository by running the following command:

git clone <repository_url>

Replace with the URL of the GitHub repository you want to clone.

Enter your GitHub credentials if prompted.

Wait for the repository to be cloned. Once the command finishes, you will have a local copy of the repository on your Linux server.

Now you can proceed with the next step, which is to install Django and other project dependencies using pip within your virtual environment.

 

5. Install Django and other project dependencies

With the virtual environment activated, we can now install Django and other project dependencies using pip:

python3 -m pip install Django

 

Installing Dependencies for a Django Application using requirements.txt

a) Activate the virtual environment:

source <virtual_environment_name>/bin/activate

 

b) Navigate to the root directory of your Django project.

If you have already installed packages using pip, then use this following cmd:

c) Run the following command to generate the requirements.txt file using pip freeze within the activated environment:

pip freeze > requirements.txt

This command will generate a requirements.txt file containing a list of installed packages and their versions.

d) Verify that the requirements.txt file has been created successfully.

cat requirements.txt

This command will display the contents of the requirements.txt file.

Django==3.2.4
requests==2.25.1

 

If you have already installed packages and you have generated requirements.txt file.

e) Install the dependencies from the requirements.txt file:

pip install -r requirements.txt

This command will install the listed packages with their specified versions.

 

f) Wait for the installation to complete.

Once the installation is finished, all the required dependencies will be installed in your virtual environment. You can now proceed with running your Django application.

6. Create a new Django Project

If you are not using a git project, then you create a new one straight way.

Create a new Django project using the following command:

django-admin startproject myproject

7. To Run Database Migrations

To initialize the database for your Django project, run the following command:

python manage.py makemigrations
python manage.py migrate

8. Run the Development Server

Start the Django development server to test your application locally:

python manage.py runserver 0.0.0.0:8000

Once you are able to setup and run a Django app, then you need to configure a web server to make this work properly on IP address or site url.

9. Configure a Web Server (e.g., Apache)

To deploy your Django application in a production environment, it's recommended to use a web server like Apache. Install Apache using the following command:

sudo yum install httpd

Configure Apache to serve your Django application by creating a virtual host configuration file:

sudo vim /etc/httpd/conf.d/myapp.conf

Add the following content to the file:

<VirtualHost *:80>
    ServerName myapp.example.com
    DocumentRoot /path/to/mysite

    <Directory /path/to/mysite>
        Require all granted
        Options FollowSymlinks
    </Directory>

    Alias /static /path/to/mysite/static
    <Directory /path/to/mysite/static>
        Require all granted
    </Directory>

    <Directory /path/to/mysite/mysite>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess myapp python-path=/path/to/mysite python-home=/path/to/env
    WSGIProcessGroup myapp
    WSGIScriptAlias / /path/to/mysite/mysite/wsgi.py

    ErrorLog /path/to/mysite/error.log
    CustomLog /path/to/mysite/access.log combined
</VirtualHost>

Replace /path/to/mysite with the actual path to your Django project directory, and myapp.example.com with your domain or server IP.

Save the file and exit the text editor.

10. Enable and Start Apache

Enable the Apache service to start on boot:

sudo systemctl enable httpd

Start the Apache service:

sudo systemctl start httpd

 

11. Access Your Django Application

You can now access your Django application by visiting your domain or server IP in a web browser.

Conclusion: In this article, we went through how to install and deploy a Django application on AlmaLinux. We installed the required dependencies, set up a virtual environment, set up the Django project, and deployed it using Apache as the web server. By following these instructions, you may successfully deploy and make your Django application available to users on AlmaLinux.

Remember to safeguard your deployment by enabling HTTPS, maintaining server permissions, and implementing other security best practices as needed.