Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»Django»Development and Deployment of Django on Fedora
    Django

    Development and Deployment of Django on Fedora

    March 15, 2023No Comments12 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    A Django project was created using Cookiecutter, the application environment was controlled with Docker, and finally the app was deployed to Digital Ocean. Instead of using Docker, this guide will walk you through the steps of creating a Fedora 24 server and running a Cookiecutter-Django project.

    Updates:

    • 11/15/2016: Refactored Nginx config and updated to the latest version of Django (v1.10.3).
    • 10/06/2016: Updated to the latest versions of Fedora (v24), cookiecutter (v1.4.0), cookiecutter-django, and Django (v1.10.1).

    Development

    Create a new bootstrapped Django project by installing cookiecutter globally.

    $ pip install cookiecutter==1.4.0
    $ cookiecutter https://github.com/pydanny/cookiecutter-django.git
    

    Using this command, we can run cookiecutter with the cookiecutter-django repository and customise its settings for our current project. (The initiative and repository are known as django cookiecutter fedora.)

    For further details on this command and the resulting project structure, please refer to the Local Setup portion of the preceding article.

    There are only a few things left to do before we can launch our Django project…

    The Creation of a Database

    Given that cookiecutter-django expects its database to be Postgres (for more information, see django cookiecutter fedora/config/settings/common.py), we need get it up and running first. You may use any reliable online resource to learn how to install a Postgres database server, or you can skip down to the “Deployment” section below to learn how to install Postgres on Fedora.

    Please note that Postgres.app is available for Mac users.

    After the Postgres server is up and running, use psql to establish a new database with the same name as your project.

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    NOTE: The above command to create a database may differ somewhat depending on the version of Postgres you are using. This is where you can get the most up-to-date version of Postgres’ documentation, where you can double-check that you have the right command.

    The Dependency Setup

    Next, to set your Django Project in a condition suitable for development, go to the root directory, create/activate a virtual environment, and install the dependencies:

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    Sanity Check

    When the migrations have been applied, launch the local development server.

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    Make check everything is working by opening the Project quick start page at http://localhost:8000/ in your browser.

    Shut down the development server, create a new Git repository, make any necessary changes, and then send your changes to Github using git push.

    Deployment

    Now that we have the Project up and running locally, we can go on to deployment, where we will use the following tools:

    • Fedora 24
    • Postgres
    • Nginx
    • gunicorn

    Installing Fedora 24

    Create a new droplet on Digital Ocean, and be sure to use a Fedora 24 image. Please refer to this guide if you need assistance. Establishing an SSH key is recommended for safe login.

    Let’s go ahead and upgrade the server. Launch the update procedure by logging onto the server using SSH as root:

    $ ssh root@SERVER_IP_ADDRESS
    # dnf upgrade
    

    Regular User Who Is Not Root

    Next, we’ll create a user who isn’t root so that programmes don’t have access to system-wide rights.

    To create a user who isn’t root, you must enter the following instructions as root.

    # adduser <name-of-user>
    # passwd <name-of-user>
    Changing password for user <name-of-user>.
    New password:
    Retype new password:
    passwd: all authentication tokens updated successfully.
    

    In the above code snippet, we created a new non-root user named user and gave them a password. To make this user eligible to execute operations that normally need root access through sudo, we must now add them to the administrative group.

    # usermod <name-of-user> -a -G wheel
    

    Logout of the server and then back in as the regular user (not root). The shell prompt has morphed from a # (pound symbol) to a $ (dollar sign), have you noticed? This means our current login is not as the system’s root user.

    Packages Needed

    Download and install the following packages while signed in as a user other than root:

    WARNING: Here, we are granting our non-root user root permissions (recommended!). If you don’t want to provide the non-root user root access implicitly, you may avoid using the sudo prefix by appending the keyword to the beginning of each command you run in the terminal.

    $ sudo su
    # dnf install postgresql-server postgresql-contrib postgresql-devel
    # dnf install python3-devel python-devel gcc nginx git
    

    Prepare PostgreSQL

    Now that everything has been downloaded and set up, we can begin configuring our Postgres server and making our database.

    You must first manually start the server once Postgres has been initialised.

    # sudo postgresql-setup initdb
    # sudo systemctl start postgresql
    

    After that, su into the postgres account to access the Postgres server:

    # sudo su - postgres
    $ psql
    postgres=#
    

    Next, we’ll create a Postgres user and database for our project, with the username corresponding to the non-root user’s name:

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    0

    In case you get stuck, check out the official instructions to installing Postgres on Fedora.

    Close psql and log in as a non-root user again:

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    1

    You will be returned to your regular non-root user prompt after you log out of postgres. Cookie-cutter deployment in Django, through [username@django-cookie-deploy]#.

    Please take note of the pound symbol (#) in the prompt. We granted our non-root user root privileges before we even began setting up our server, and now we’re seeing this issue. If this does not appear, try running sudo su again, or prepend sudo to your commands.

    Set up Postgres to automatically launch on server reboot:

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    2

    Organizing a New Project

    The /opt directory should now contain a clone of your project’s structure as it appears on GitHub.

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    3

    IMPORTANT: If you want to utilise the repository for this lesson, go here. Just take off:

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    4

    The Dependency Setup

    Next, activate a virtualenv within the root directory of your project to make your Django app ready for deployment:

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    5

    Give the current non-root user administrative privileges (if not already provided) before starting the virtualenv:

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    6

    In contrast to the setup of the development environment described above, all of Pillow’s external libraries must be installed prior to installing the dependencies. If you’re looking for more reading, go here.

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    7

    To prevent dependency installation conflicts inside our virtualenv, we must now install one additional package.

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    8

    This code must then be executed:

    $ create database django_cookiecutter_fedora;
    CREATE DATABASE
    

    9

    All prerequisites for development, localization, and production will be set up in the same step. This is only a precautionary measure to make sure everything is operational. We’ll be switching environments soon, but for all intents and purposes, you’re looking at the production setting right now.

    Please turn off the virtual environment. The non-root user will no longer have administrative privileges if an exit command (such as exit) is issued. Take note of the new prompt ($, rather than #). Even so, the user has the option to launch the virtualenv. Try it!

    Maintains sanity (take 2)

    Do all necessary migrations:

    Start the server now:

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    0

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    1

    Just type the server’s IP address, such as :8000, into your browser to check on its status. After you’re finished, please shut off the server.

    Installing a Gunicorn

    We need to modify the production settings in the /config/settings/production.py module before we can proceed with configuring Gunicorn. Please review the production settings; these are the very minimum for transferring our Django Project to a live server.

    Just edit the file in VI to reflect the changes.

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    2

    To begin, make a blanket deletion of:

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    3

    The modified settings may be copied and then pasted into the newly empty file by switching to INSERT mode and then pasting. Adding your server’s IP address or hostname to the ALLOWED HOSTS environment option is also crucial. Turn off INSERT, save, and quit:

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    4

    As most of the production.py file’s config parameters are derived from environment variables, we’ll need to add them later.

    Using VI to make changes to this file once more:

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    5

    The following should be added in INSERT mode:

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    6

    There are two points worth emphasising:

    1. Notice how we updated the DJANGO_SETTINGS_MODULE variable to use the production settings.
    2. It’s a good practice to change your DJANGO_SECRET_KEY to a more complex string. Do this now if you want.

    Once again, after leaving INSERT mode, save your work and close VI.

    The.bashrc file must now be loaded again.

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    7

    Is the exam in sight? Invoke the gunicorn server within the root directory while using virtualenv:

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    8

    Hence, our online app will once again be accessible at the address:8000.

    Remember that if we log off of the server, this command will terminate and we will no longer be able to provide our web application. It is necessary to run the gunicorn server in the form of a service so that it can be easily started, stopped, and monitored.

    Setting Up Nginx

    Please follow these instructions to create a configuration file for our Django Project so that it may be served by Nginx.

    $ cd django_cookiecutter_fedora
    $ pyvenv-3.5 .venv
    $ source .venv/bin/activate
    $ ./utility/install_python_dependencies.sh
    

    9

    Remember to change the server, server name, and location in the following:

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    0

    After you’re done, close VI and restart the Nginx server:

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    1

    There you have it!

    Launching the Gunicorn Script

    Let’s get our bootstrapped Django web app up and running on the Gunicorn server, directed by Nginx, by writing a start script for it that can be executed as an executable.

    Please refer to Real Python’s guide or video course if you want to learn more about deploying a Django project using Gunicorn and Nginx.

    Execute inside the root of the project:

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    2

    This page contains the code for the gunicorn start script. There are three main sections, all of which are quite self-explanatory. Leave a question or remark below.

    WARNING: Be sure that the non-root user’s USER and GROUP variables are the same as theirs.

    Copy the text, paste it into VI, and then save and close the programme.

    At last, we’ll turn it into something that can be used:

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    3

    Launch the host:

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    4

    Now that you’ve restarted your server, you may access your Django web app by visiting its IP address in a browser.

    Specifically, did you see a 502 Bad gateway error? In most cases, if you only follow these instructions, your application will be successful.

    • Modifying SELinux Policy Rules
    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    5

    After you’re finished, restart your server using the tool on Digital Ocean’s control panel.

    Systemd

    We need to set up a systemd service to make our gunicorn start script run continuously, even if we aren’t logged into the server, so that our Django web application may continue to be served.

    Just create a service file by changing to the /etc/systemd/system directory:

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    6

    This should be added:

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    7

    Next, after saving and closing the file, start the service and turn it on:

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    8

    To get further information about an error, use the journalctl -xe command.

    The last step is to activate the service so that it operates indefinitely and resumes after unexpected system shutdowns:

    $ python manage.py makemigrations
    $ python manage.py migrate
    $ python manage.py runserver
    

    9

    Sanity Verify (absolute!)

    Identify the current condition of the service by:

    $ ssh root@SERVER_IP_ADDRESS
    # dnf upgrade
    

    0

    You may now access a Django error page by visiting your server’s IP address (or hostname). You may resolve this by executing the following command in the root directory of your project when your virtualenv is active:

    $ ssh root@SERVER_IP_ADDRESS
    # dnf upgrade
    

    1

    You can now see the Django web app running in the browser, complete with all of the static files (HTML, CSS, and JavaScript) functioning as intended.

    Get the source code from the repository if you need it as a reference. Don’t hesitate to share your thoughts and feelings by leaving a comment below. Cheers!

    Verify as Finished

    Development and Deployment of Django on Fedora Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleFraction Module in Python
    Next Article Python News Whats New From April 2022

    Related Posts

    python

    Plot With Pandas Python Data Visualization Basics

    March 27, 2023
    python

    Defining and Calling Python Functions

    March 27, 2023
    python

    BreadthFirst Search in Python

    March 27, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.