How To Install Apache Django Postgresql on Ubuntu 16.04 Part1

In this post we are going to install Django2.2 (current version), Apache 2 and Postgresql 9.5. Installing them was pretty easy on Windows OS but I had some difficulties on Ubuntu and I had to read various forum posts, articles and videos. So,I decided to share how we can configure Apache Django and Postgresql on Ubuntu 16.04. 

 Before we start I want to show the folder structure I am going to create.

-CRM Folder (Main Folder)
	-v_env(Virtual enviroment)
	-PrjCrm(Project folder)
		-PrjCrm(Main App created with the Project)
		-CrmApp(Custom App)
		-media
		-static

 

 

 Lets install pip, apache and mod-wsgi first.

sudo apt-get update
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3

 

 Ubuntu Kernel uses Python 2.7 by default. Therefore we need to add alias in bash:

Note: When I was writing this article, the current version was 3.5. Therefore just check what version is installed on your server and add alias according to the version you installed.

nano ~/.bashrc
#Add the following line to the top of your bashrc file.  Save and Exit.
alias python=python3.5

 

 

To make this change permanent run this command on your terminal

source ~/.bashrc

 

Now check python version on your terminal, it should return python 3.5

python --version

 

 

 

Install Virtual Environment on Ubuntu System with this command

sudo pip3 install virtualenv

 

I will create a main Folder under /home/djangoadm/ and I will name the main folder as CRM. Then I will create virtual environment for this project named v_env in CRM folder and activate the virtual environment. Then I will create a project named PrjCrm and PrjCrm folder also will be in CRM folder.

mkdir CRM
cd CRM/
virtualenv v_env
source v_env/bin/activate

 

Now we can install Django

pip3 install django

 

 Now I will create a project named PrjCrm. This will create a project folder and a main app folder. Both will have the same name(PrjCrm).

django-admin startproject PrjCrm

 

 

Open up project's settings.py file and specify static and media URL and ROOT:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

STATIC_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, "media/")

 

Change working directory to where your manage.py file resides

cd PrjCrm

Run the server with the following command to make sure everything is fine so far. Check on your browser if it works

python3 manage.py runserver

 

Use Ctrl + C to stop Django server. Then use deactivate command on Terminal to deactivate the virtual environment for now. 

deactivate

 

 

On your browser, type 127.0.0.1 and it should display default apache page

 

Now I am going to copy the existing apache default config file and edit it accordingly.

cd /etc/apache2/sites-available/
sudo cp 000-default.conf apache_crm.conf
sudo nano apache_crm.conf

 

 Apache config should be similar to this. Modify this according to your folder structure and make sure these line are between the <VirtualHost> tags

ServerName prjcrm.local
ServerAlias www.prjcrm.local
DocumentRoot /home/djangoadm/CRM/PrjCrm

Alias /static /home/djangoadm/CRM/PrjCrm/static
<Directory /home/djangoadm/CRM/PrjCrm/static>
    Require all granted
</Directory>

Alias /media /home/djangoadm/CRM/PrjCrm/media 
<Directory /home/djangoadm/CRM/PrjCrm/media>
    Require all granted
</Directory>

<Directory /home/djangoadm/CRM/PrjCrm/PrjCrm>
                <Files wsgi.py>
    Require all granted
                </Files>
</Directory>

 
WSGIDaemonProcess crm_project python-path=/home/djangoadm/CRM/PrjCrm python-home=/home/djangoadm/CRM/v_env
WSGIProcessGroup crm_project
WSGIScriptAlias / /home/djangoadm/CRM/PrjCrm/PrjCrm/wsgi.py

 

Save and Exit.

 

Enable Apache Virtual Host

cd /etc/apache2/sites-available
sudo a2ensite apache_crm.conf

 

Edit hosts file

sudo nano /etc/hosts

#Add this entry
127.0.0.1 prjcrm.local

 

 

Set Permissions:

Allow access to Apache on both port 80 and 443 on Firewall

sudo ufw allow 'Apache Full'

Set CRM folder's owner as Apache

sudo chown :www-data /home/djangoadm/CRM

 

We will test our Apache Config but before testing it we need to add ServerName to apache.conf file otherwise it will give warnings:

sudo nano /etc/apache2/apache2.conf

Add ServerName (mine is Django) and save it.

 

Now we can run configtest. It should returm Syntax OK

sudo apache2ctl configtest

 

cd /home/djangoadm/CRM/

#Reactivate the virtual environment
source v_env/bin/activate

 

Edit settings.py file for Allowed Hosts and save.

ALLOWED_HOSTS = ['prjcrm.local']

 

Change working directory to where your manage.py file resides

cd PrjCrm

I want to run and test my server to check everything is fine on Django side before running Apache.

python3 manage.py runserver

 

Now I can stop the Server (Ctrl +C)

Now we can restart Apache

sudo service apache2 restart

 

Apache is working fine.

 

You may wanna see Apache logs for issues, If so you can use the command below.

tail -f /var/log/apache2/error.log

 

 In Part2, We will install and configure Postgresql for our environment.How To Install Apache Django Postgresql on Ubuntu 18.04 Part2

© selimatmaca.com. All Rights Reserved.