Introduction:
Django is a ground-breaking web structure that can assist you with getting your Python application or site going. Django incorporates an improved on advancement worker for testing your code locally, yet for anything even somewhat creation-related, a safer and ground-breaking web worker is required.
In this guide, we will show how to introduce and design a few segments on Linux to help and serve Django applications. We will create the uWSGI application worker to interface with our applications. We will at that point set up Nginx to invert intermediary to uWSGI, giving us admittance to its security and execution highlights to serve our applications.
Prerequisites:
To finish this guide, you should have complete the below points on a new Ubuntu/Linux.
* A non-root user with Sudo advantages.
* Install Python with Pip.
* Install & Create a python virtual environment for the backend(Django).
* Install Nginx web server.
* Install Database server & configure it for authentication with username & password.
Download & Setup Your Django Project :
* Create & Activate the environment & go to the backend source code directory.
* Download your project using GIT & Install the requirements using PIP.
* Configured the database login credentials in Django source code & Do migrate the database for Django.
* Start & Test the Django server
This Python command will start the development server on 8000
port.
You can test your server using your server's domain name or IP address followed by 8000
in your browser:
After testing To stop the development server type Ctrl+c in the termianl.
* Deactivate the Virtual Environment
We can now move forward and configure the application server.
Setting Up The uWSGI Application Server:
Now we have the Django application installed & ready to go, So now we're going to configure uWSGI. uWSGI is a software service that could communicate with application programs over an interface known as WSGI.
* Installing uWSGI
- For Django with Python 3
We can quickly check this utility server by passing it the facts for one in all our sites. Run the application using uWSGI .
Here we've told uWSGI our environment & source code location using --home and --chdir arg...
Running uWSGI from the command line is beneficial for testing, however isn’t mainly useful for a real deployment. here we will create a configuration .ini file that will run uWSGI in “Emperor mode”, which allows a master process to manage applications automatically.
The configuration will look like this:
Here, our project’s uWSGI configuration is complete.
Here. we will create a systemd unit file to manage the uWSGI and automatically start uWSGI at startup. This will make the full process automatic.
The configuration will look like this:
So now we have configured the Uwsgi & systemd file but we can't able to start the service successfully because the www-data user relies on Nginx web server.
- Install and Configure Nginx as a Reverse Proxy forBackend app. we will create basic configurations including the uwsgi parameters found in the /etc/nginx/uwsgi_params and pass the traffic to the uwsgi server's socket.
* The configuration of these items will look like this:
* Check the configuration:
Now, Restart the Nginx & uWSGI services.
* Allow the UFW rules for Nginx
* Enable both services to start automatically at boot:
* If you made any changes in your Django application, you should restart the uWSGI service & if you made any changes in systemd file, you have to reload the daemon.
* After any Nginx changes::
So, now we have set up our Django application using a virtual environment and configured uWSGI, and configured the Nginx web server as reverse proxy you can check your application using your server IP/Domain name in the browser.
In the production environment using uWSGI is the best way to serve your Django application. here we've deployed our Django application using uWSGI and Nginx reverse proxy which will serve our project at client's request.