The Ops Community ⚙️

Tikam Alma
Tikam Alma

Posted on

Deploy Backend Server Using Apache Webserver

How do the backend servers work?

What is a backend server?

The backend servers are the main component of any functional website which uses API, databases, and other features.
What the user sees on the browser is the frontend website and the processing of the login, signup, storing credentials, and data are done by backend servers on cloud services.
Frontend servers are rendered on the client-side, and backend servers are rendered on the server-side, and on the server, it processes all the complex functions and receives data from the database so that the frontend server can use it.

Hosting backend servers in any cloud system requires different configurations for each of the, there are different programming languages and the most popular programming languages which are used for writing backend servers are Python, Go, Javascript, PHP, RUBY Java.
Each language has its own framework to write backend servers, for python language Django framework is used widely, for javascript Node is used, for Go, Gin framework is used.

Dependency and required configurations for hosting backend servers

Different backend servers are built on different languages and different languages have their own frameworks and each framework has its own required dependency and tools to work on web servers.

Python - WSGI (Web Server Gateway Interface)

Building projects in Django creates auto-generated files by default which are the skeleton or the structure of the Django framework, wsgi.py is one such important file that is used to communicate with web servers. WSGI is Web Server Gateway Interface, it describes the communication between web servers and Python web application servers or frameworks.
WSGI (Web Server Gateway Interface) is a standard interface between web server software and web applications written in Python.
It covers how a web server connects with Python web applications/frameworks, as well as how web applications/frameworks may be chained together to execute a request.

The apache webservers are popular for their customization and multi-platform support, it has a specific module for Django servers, called mod_wsgi. The mod WSGI module provides a WSGI compliant interface for executing and running a python based web application server within apache.

Node - Proxy Module

In an apache web server, the node or javascript servers are hosted or run using the proxypass and reverse proxy method and its modules.

A reverse proxy serves as a link between the client and the server, forwarding requests to the main web server or application webservers. It manages the requests and responses between clients and servers and offers security and a high level of abstraction.

The ProxyPass directive defines how inbound requests are routed to the backend server (or a cluster of servers known as a Balancer group).

And another directive used is the ProxyPass directive, which defines how the inbound requests are sent to the Node/Javascript backend servers, it manages any requests or responses under the root URL (/) that should be sent or routes to the Node/Javascript backed servers localhost address or the root default address.

.

Hosting backend servers in the cloud

Django server configurations

Setup and install the Django dependencies and packages.

The basic requirements for hosting the Django server are its requiremnets.txt file in which all of the dependency packages are written and connecting it to the database and running its migrations.

For installing the dependency and packages, check the python version which the Django project application needs to be set and make a virtual environment upon which the Django server will run.

In any cloud service, Linux servers are mostly used, use these commands to check the python version and virtual environment.

  • Install all Python 3 packages. These will ensure we have everything for Python - Django framework application server.

Update the Linux/ubuntu system

$ sudo apt-get update && sudo apt-get dist-upgrade
Enter fullscreen mode Exit fullscreen mode
$ sudo apt install -y python3-pip python3 python3-setuptools build-essential libssl-dev libffi-dev python3-dev python3-venv
Enter fullscreen mode Exit fullscreen mode
  • Make a virtual environment
$ mkdir project_env
## Directory where environment files can be stored

$ cd project_env
$ python3 -m venv penv
## penv can be named anything
Enter fullscreen mode Exit fullscreen mode
  • Activate virtulenv and it should look like this :
$ cd project_env
$ source penv/bin/activate
(penv)[user]$ / directory /
Enter fullscreen mode Exit fullscreen mode
  • Install the packages inside the virtualenv
$ pip install package_name
OR
$ pip install -r requiremenrs.txt
Enter fullscreen mode Exit fullscreen mode

Configuring WSGI.py

Django autogenerates the wsgi.py file inside the main directory of django alonside settings.py and other files.

Open the wsgi.py and configure the project directory path.


"""
WSGI config for lgraph project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""

import os
import sys
from django.core.wsgi import get_wsgi_application

## Add this line to add project directory.
sys.path.append('/home/your_project')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

application = get_wsgi_application()
Enter fullscreen mode Exit fullscreen mode

Installing and enabling "Mod WSGI"

sudo apt-get update
sudo apt-get install libapache2-mod-wsgi-py3
Enter fullscreen mode Exit fullscreen mode

Enabling the mod_wsgi module server wide.

$ a2enmod wsgi
Enter fullscreen mode Exit fullscreen mode

The Django doundation has offical docmentation about how to used mod_wsgi with django - https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/modwsgi/

Running and testing Django server in localhost

After setting up the django application, now start the server, check if everything is working, start the django server with these commands:

To migrate the database, get static files ready and to test run the Django application server use these commands:

$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py collectstatic
$ python manage.py runserver 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode

Node server configuration

Setup Setup and install the Node dependencies and packages

Running and testing node server in localhost

Install and enable proxy_modules

Configuring apache virtualhost to serve the backend servers.

Configuring Virtualhost for Django Backend Server

After enabling the mod_wsgi, now it's time to configure the virtualhost to deploy the application server to expose it to internet

  • enable mod wsgi
$ a2enmod wsgi
Enter fullscreen mode Exit fullscreen mode

Open the /etc/apache2/site-available directory and edit the project's configuration file.

$ cd /etc/apache2/sites-available
$ /etc/apache2/sites-available / ~ ls
$ ls

$ nano django-project.conf
OR 
$ vim django-project.conf
Enter fullscreen mode Exit fullscreen mode

Configure and add the following line:

  • WSGIScriptAlias
  • WSGIDaemonProcess
  • WSGIProcessGroup
<VirtualHost *:80> 
 ServerName mysite.example.com 
 DocumentRoot /var/www/vhosts/mysite 
 WSGIScriptAlias / /var/www/vhosts/mysite/myproject/wsgi.py 

 # adjust the following line to match your Python path 
 WSGIDaemonProcess mysite.example.com processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/vhosts/mysite/venv/lib/python3.5 
 WSGIProcessGroup mysite.example.com 

 <directory /var/www/vhosts/mysite> 
   AllowOverride all 
   Require all granted 
   Options FollowSymlinks 
 </directory> 

 Alias /static/ /var/www/vhosts/mysite/static/ 

 <Directory /var/www/vhosts/mysite/static> 
  Require all granted 
 </Directory> 
</VirtualHost> 

Enter fullscreen mode Exit fullscreen mode

Configuring Virtualhost for Node Backend Server

Dry run and testing the live servers

To check the application server is running or not

  1. Check the apache webserver status

  2. Check the application server status

  3. Curl localhost:80

  4. If in the cloud get the public IP and open the web application using the IP or domain set in the configuration file.

Top comments (0)