<\/span><\/h2>\n\n\n\nFirst, log in to your Ubuntu 20.04 server through SSH as the root user:<\/p>\n\n\n\n
ssh root@IP_Address -p Port_number<\/pre>\n\n\n\nYou will need to replace \u2018IP_Address\u2018 and \u2018Port_number\u2018 with your server\u2019s respective IP address and SSH port number. Additionally, replace \u2018root\u2019 with the username of the admin account if necessary.<\/p>\n\n\n\n
Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:<\/p>\n\n\n\n
# apt update -y\n# apt upgrade -y<\/pre>\n\n\n\n<\/span>Step 2. Create a system user<\/span><\/h2>\n\n\n\nOnce logged in to Ubuntu 20.04 as root, we will create a new system user and grant it with sudo privileges. We will also use this user to complete this installation. In this tutorial, we will create a new system user called ‘master’, you can choose any username you like.<\/p>\n\n\n\n
# adduser master<\/pre>\n\n\n\nOnce created, let’s run this command to add the new user to sudo group. In Ubuntu, users who are members of sudo group are allowed to run sudo commands.<\/p>\n\n\n\n
# usermod -aG sudo master<\/pre>\n\n\n\nWe will also add user master to our www-data group<\/p>\n\n\n\n
# usermod -aG www-data master<\/pre>\n\n\n\nNow, we can log in as the new user ‘master’<\/p>\n\n\n\n
# su - master<\/pre>\n\n\n\n<\/span>Step 3. Install Packages<\/span><\/h2>\n\n\n\nTo start this, we will install every required package from the repository. To install Nginx, PostgreSQL, Python from the repository, we can run this command:<\/p>\n\n\n\n
$ sudo apt install postgresql postgresql-contrib python3-pip python3-dev libpq-dev nginx -y<\/pre>\n\n\n\n<\/span>Step 4. Add Database and Database User<\/span><\/h2>\n\n\n\nDuring the PostgreSQL installation, a system user named postgres was created as the default administrative user. We need to use this user to log in to the PostgreSQL shell and perform administrative tasks.<\/p>\n\n\n\n
$ sudo -u postgres psql<\/pre>\n\n\n\nYou will be asked for your ‘master’ password and the shell would be like this:<\/p>\n\n\n\n
postgres=#<\/pre>\n\n\n\nWhile in the postgreSQL shell, run the following commands:<\/p>\n\n\n\n
postgres=# CREATE USER djangouser WITH PASSWORD 'm0d1fyth15'; postgres=# CREATE DATABASE djangodb;<\/pre>\n\n\n\nNext, we also need to change the encoding to UTF-8, the timezone and default_transaction_isolation database scheme.<\/p>\n\n\n\n
postgres=# ALTER ROLE djangouser SET client_encoding TO 'utf8'; postgres=# ALTER ROLE djangouser SET default_transaction_isolation TO 'read committed'; postgres=# ALTER ROLE djangouser SET timezone TO 'UTC';<\/pre>\n\n\n\nThe last part, we grant the privileges to the new database then exit PostgreSQL shell.<\/p>\n\n\n\n
postgres=# GRANT ALL PRIVILEGES ON DATABASE djangodb TO djangouser; postgres=# \\q<\/pre>\n\n\n\n<\/span>Step 5. Create Python Virtual Environment<\/span><\/h2>\n\n\n\nDjango can be installed in several ways, in this article we will show you how to install it in a python virtual environment.<\/p>\n\n\n\n
$ sudo -H pip3 install --upgrade pip $ sudo -H pip3 install virtualenv<\/pre>\n\n\n\nOnce completed, let’s create a new directory for django installation then enter to the new directory<\/p>\n\n\n\n
$ mkdir django && cd $_<\/pre>\n\n\n\nAfter entering the ‘django’ directory, we can run this command to create a new virtual environment.<\/p>\n\n\n\n
$ virtualenv djangoenv<\/pre>\n\n\n\n <\/figure>\n\n\n\nAs seen in the picture, the new virtual environment is created in directory ‘djangoenv’. To install Django, Gunicorn and Psycopg2 in the virtual environment, we have to activate it first.<\/p>\n\n\n\n
$ source djangoenv\/bin\/activate<\/pre>\n\n\n\nBy running the command above, your shell will look like this:<\/p>\n\n\n\n
(djangoenv) master@ubuntu20:~\/django$<\/pre>\n\n\n\nWhile in the shell, run this command to install django.<\/p>\n\n\n\n
(djangoenv) master@ubuntu20:~\/django$ pip install django gunicorn psycopg2<\/pre>\n\n\n\nOnce completed, you should see an output like this:<\/p>\n\n\n\n
Installing collected packages: sqlparse, backports.zoneinfo, asgiref, psycopg2, gunicorn, django Successfully installed asgiref-3.4.1 backports.zoneinfo-0.2.1 django-4.0 gunicorn-20.1.0 psycopg2-2.9.2 sqlparse-0.4.2<\/pre>\n\n\n\n <\/figure>\n\n\n\nAll required components to start a django project should be installed in the virtual environment.<\/p>\n\n\n\n
<\/span>Step 6. Create Django Project<\/span><\/h2>\n\n\n\nIn the previous step, we created a directory ~\/django<\/code> or in this case \/home\/master\/django<\/code>. We will create a django project in this directory, we can run this command to install django project in ~\/django<\/code>. The command should be ran while we are in the virtual environment.<\/p>\n\n\n\n(djangoenv) master@ubuntu20:~\/django$ django-admin startproject djangoproject ~\/django<\/pre>\n\n\n\nThe command above will install django project in to ~\/django\/djangoproject<\/code>. If you list the directory, you will see manage.py<\/code>, djangoenv<\/code>, and djangoproject<\/code> in your ~\/django<\/code>.<\/p>\n\n\n\nNow, since we want to use PostgreSQL as the database storage, we need to modify the configuration.<\/p>\n\n\n\n
$ nano ~\/django\/djangoproject\/settings.py<\/pre>\n\n\n\nAdd this line at the top of your settings.py<\/p>\n\n\n\n
import os<\/pre>\n\n\n\nAnd replace the existing database information with the following.<\/p>\n\n\n\n
DATABASES = {\n 'default': {\n 'ENGINE': 'django.db.backends.postgresql_psycopg2',\n 'NAME': 'djangodb',\n 'USER': 'djangouser',\n 'PASSWORD': 'm0d1fyth15',\n 'HOST': 'localhost',\n 'PORT': '',\n }\n}<\/pre>\n\n\n\nMake sure the database username and password match with the one we created in the previous step. Still in the same settings.py file, find ALLOWED_HOSTS then you can add your server IP adress, domain names, sub domain names. The entries should be seperated by a comma and listed in quotations.<\/p>\n\n\n\n
ALLOWED_HOSTS = ['123.123.123.123', 'domain.com', 'sub.domain.com']<\/pre>\n\n\n\nNext, find the Django’s static files configuration. The changes is required to make your Django static files accessible through nginx, and to prevent nginx from returning error 404 messages. Add this line after STATIC_URL = ‘\/static\/’<\/p>\n\n\n\n
STATIC_ROOT = os.path.join(BASE_DIR, 'static\/')<\/pre>\n\n\n\nDo not forget to save the changes then exit.<\/p>\n\n\n\n
Next, it is time to migrate the initial database schema to our PostgreSQL database, let’s run these commands.<\/p>\n\n\n\n
(djangoenv) master@ubuntu20:~\/django$ ~\/django\/manage.py makemigrations (djangoenv) master@ubuntu20:~\/django$ ~\/django\/manage.py migrate<\/pre>\n\n\n\n <\/figure>\n\n\n\nThen, create a superuser.<\/p>\n\n\n\n
(djangoenv) master@ubuntu20:~\/django$ ~\/django\/manage.py createsuperuser<\/pre>\n\n\n\nYou will be asked to create a username, provide an email address and the username’s password. You would want to save the information about this administrative user and password because you will use them to log in to Django backend.<\/p>\n\n\n\n
For the last one, run the command below to collect static files.<\/p>\n\n\n\n
(djangoenv) master@ubuntu20:~\/django$ ~\/django\/manage.py collectstatic<\/pre>\n\n\n\nYou will need to confirm the action by answering it with yes then you will see this output after confirming:<\/p>\n\n\n\n
128 static files copied to '\/home\/master\/django\/static'<\/pre>\n\n\n\nDeactivate the virtual environment to exit.<\/p>\n\n\n\n
(djangoenv) master@ubuntu20:~\/django$ deactivate<\/pre>\n\n\n\n<\/span>Step 7. Create Gunicorn Systemd File<\/span><\/h2>\n\n\n\nTo manage Gunicorn service, we will create a systemd unit file at \/etc\/systemd\/system\/gunicorn.service.<\/p>\n\n\n\n
$ sudo nano \/etc\/systemd\/system\/gunicorn.service<\/pre>\n\n\n\nPaste these lines<\/p>\n\n\n\n
[Unit]\nDescription=gunicorn daemon\nAfter=network.target\n\n[Service]\nUser=master\nGroup=www-data\nWorkingDirectory=\/home\/master\/django\nExecStart=\/home\/master\/django\/djangoenv\/bin\/gunicorn --access-logfile - --workers 3 --bind unix:\/home\/master\/django\/djangoproject.sock djangoproject.wsgi:application\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n\n\n\nSave the file then exit. Then reload systemd and start gunicorn.<\/p>\n\n\n\n
$ sudo systemctl daemon-reload $ sudo systemctl start gunicorn<\/pre>\n\n\n\nTo check whether gunicorn is running or not, run this command:<\/p>\n\n\n\n
$ sudo systemctl status gunicorn<\/pre>\n\n\n\n <\/figure>\n\n\n\n