In this tutorial we will show you how to enable and connect the Django admin interface on a CentOS 7 VPS. Django is one of the most popular open source web application frameworks. It is written in Python by experienced developers. Django was originally designed for news websites because it can be easily scaled to handle very heavy traffic and it allows developers to make web application development faster and create complex, database driven websites without having to start coding from scratch. Django is a cross-platform application and supports all popular operating systems, but this tutorial was written especially for CentOS 7. At the time of writing, the latest Django version is 1.11.12.
Table of Contents
1. Log in to the server via SSH
First, you need to log in to your server via SSH as root user:
ssh root@IP_ADDRESS -p PORT_NUMBER
make sure that you replace “IP_ADDRESS” and “PORT_NUMBER” with your actual server IP address and the SSH port number.
2. Update your server OS packages
After you are logged in, make sure that your CentOS 7 server is up-to-date by running the following commands:
yum clean all yum update
3. Install Pip
Pip is a powerful package management tool, and it is used to install and manage software packages written in the Python programming language. Since Pip is available in the community repository of non-standard packages known as EPEL (Extra Packages for Enterprise Linux), we need to install the epel release for CentOS 7.
Download the epel release for CentOS 7.x using the wget command:
cd /opt wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
and install the epel-release-latest-7.noarch.rpm RPM package using the following command:
rpm -Uvh epel-release-latest-7.noarch.rpm
Install the required Python dependencies:
yum install python-devel python-setuptools
Then, install pip and upgrade it to the latest version available using the following commands:
yum install python-pip pip install --upgrade pip
4. Verify the Pip installation on CentOS 7
At this point you should have the pip package manager system installed and ready to use on your CentOS 7 VPS. If you want to verify that the installation was successful you can use the following command:
pip -V
The output should be something like this:
# pip -V pip 9.0.3 from /usr/lib/python2.7/site-packages (python 2.7)
If you get output similar to the one listed above you are ready to use pip on your CentOS server and use it to install Django.
5. Install Django
There are many different ways to install Django on a CentOS 7 VPS. It can be installed using a rpm package available in the EPEL repository, through pip so it is available on a server globally, through pip on a virtual environment, or we can install it using git. For the purposes of this tutorial, we will install Django through pip on a virtual environment. Using this method is recommended as it provides the most flexibility in building new projects. This way, installed Django version will be more up-to-date than if it was installed from the EPEL repository.
Prior installing Django, install the virtualenv package and create the Python virtual environment:
pip install virtualenv
Create a new project directory for Django using the following command:
mkdir /opt/django
Create a Python virtual environment for Django framework using virtualenv:
cd /opt/django virtualenv djangoenv
This way, you can install Django framework in a single project directory or virtual environment without affecting the entire server.
Enable the virtual environment:
source djangoenv/bin/activate
Install the Django package into the virtual environment:
pip install django
In order to verify that the installation was successful you can use the following command:
django-admin --version
The output should look similar to this:
1.11.12
6. Create a Django project
Create a new Django project using the following command:
django-admin.py startproject newproject .
Edit the settings.py file and add the following lines at the end:
vi newproject/settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
Run the following command to transfer the new project database to the SQLite database:
./manage.py migrate
Create a new Django administrator user account for the Django web based admin panel:
./manage.py createsuperuser
Enter the Django admin username, email address and password when prompted.
7. Enable the Django Admin interface
Edit the request.py file and add your server IP address:
vi djangoenv/lib64/python2.7/site-packages/django/http/request.py allowed_hosts = ['localhost', 'your_server_ip']
Run the following command to run your new Django project:
./manage.py runserver 0.0.0.0:9090
This will start a server which is listening on port number 9090.
8. Connect the Django Admin interface
Open your favorite web browser and type this URL http://your_server_ip:9090/admin (change your_server_ip with the actual server IP address), and enter your Django superuser username and password which you have created earlier.
In order to leave the virtual environment, run the following command:
deactivate
Of course you don’t have to install Django and enable Django admin functionality on CentOS 7, if you use one of our Managed CentOS VPS hosting plans in which case you can simply ask our expert Linux admins to install Django and enable Django admin functionality for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post, on how to enable and connect the Django admin interface on CentOS 7, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.