Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
NGINX HTTP Server

You're reading from   NGINX HTTP Server Harness the power of NGINX with a series of detailed tutorials and real-life examples

Arrow left icon
Product type Paperback
Published in May 2024
Publisher Packt
ISBN-13 9781835469873
Length 262 pages
Edition 5th Edition
Languages
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Gabriel Ouiran Gabriel Ouiran
Author Profile Icon Gabriel Ouiran
Gabriel Ouiran
 Nedelcu Nedelcu
Author Profile Icon Nedelcu
Nedelcu
 Fjordvald Fjordvald
Author Profile Icon Fjordvald
Fjordvald
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Part 1:Begin with NGINX FREE CHAPTER
2. Chapter 1: Downloading and Installing NGINX 3. Chapter 2: Basic NGINX Configuration 4. Part 2: Dive into NGINX
5. Chapter 3: Exploring the HTTP Configuration 6. Chapter 4: Exploring Module Configuration in NGINX 7. Chapter 5: PHP and Python with NGINX 8. Chapter 6: NGINX as a Reverse Proxy 9. Part 3: NGINX in Action
10. Chapter 7: Introduction to Load Balancing and Optimization 11. Chapter 8: NGINX within a Cloud Infrastructure 12. Chapter 9: Fully Deploy, Manage, and Auto-Update NGINX with Ansible 13. Chapter 10: Case Studies 14. Chapter 11: Troubleshooting 15. Index 16. Other Books You May Enjoy

Python and NGINX

Python is a popular object-oriented programming (OOP) language available on many platforms, from Unix-based systems to Windows. It is also available for Java and the Microsoft .NET platform. If you are interested in configuring Python to work with NGINX, it’s likely that you already have a clear idea of what Python does. We are going to use Python as a server-side web programming language, with the help of the Django framework.

Django

Django is an open source web development framework for Python that aims at making web development simple and easy, as its slogan states:

The web framework for perfectionists with deadlines.

More information is available on the project website at https://www.djangoproject.com/.

Among other interesting features, such as a dynamic administrative interface, a caching framework, and unit tests, Django comes with a FastCGI manager. Although the usual way to deploy Django is through WSGI, we’re going to use Django’s FastCGI manager as it’s going to make things much simpler for us from the perspective of running Python scripts through NGINX.

Setting up Python and Django

We will now install Python and Django on your Linux operating system, along with its prerequisites. The process is relatively smooth and mostly consists of running a couple of commands that rarely cause trouble.

Python

Python should be available on your package manager repositories. To install it, run the following commands. For Red Hat-based systems and other systems using dnf as the package manager, use this command:

[root@local ~]# dnf install python python-devel

For Ubuntu, Debian, and other systems that use apt, use this command:

[root@local ~]# apt install python python-dev

The package manager will resolve dependencies by itself.

Django

In order to install Django, we will use a different approach (although you could skip this entirely and just install it from your usual repositories). We will be downloading the framework with pip, a tool that simplifies the installation of Python packages. Therefore, the first step is to install pip; for Red Hat-based systems and other systems using dnf as the package manager, execute the following command:

[root@local ~]# dnf install python-pip

For Ubuntu, Debian, and other systems that use apt, use this command:

[root@local ~]# apt install python-pip

The package manager will resolve dependencies by itself. Once pip is installed, run the following command to download and install Django 1.8.2, the latest stable version to date:

[root@website.com ~]# pip install Django==5.0.3
[...]
[root@website.com ~]# pip install -e django-trunk/

Finally, there is one last component required to run the Python FastCGI manager: the flup library. This provides the actual FastCGI protocol implementation. For Red Hat-based systems and other systems using dnf as the package manager (Extra Packages for Enterprise Linux (EPEL) repositories must be enabled; otherwise, you will need to build from source), use the following command:

[root@local ~]# dnf install python-flup

For Ubuntu, Debian, and other systems that use apt, use this command:

[root@local ~]# apt install python-flup

Starting the FastCGI process manager

The process of beginning to build a website with the Django framework is as simple as running the following command:

[root@website.com ~]# django-admin startproject mysite

Once that part is done, you will find a manage.py Python script that comes with the default project template. Open the newly created mysite directory containing manage.py, and run the following command:

[root@website.com mysite]# python manage.py runfcgi method=prefork host=127.0.0.1 port=9000 pidfile=/var/run/django.pid

If everything is correctly configured and the dependencies are properly installed, running this command should produce no output, which is often a good sign. The FastCGI process manager is now running in the background, waiting for connections. You can verify that the application is running with the ps command (for example, by executing ps aux | grep python). If you don’t see any running process, try changing the previous command slightly by selecting a different port. All we need to do now is to set up the virtual host in the NGINX configuration file.

NGINX configuration

The NGINX configuration is similar to the PHP one:

server {
    server_name .website.com;
listen 80;
# Insert the path of your Python project public files below
    root /home/website/www;
    index index.html;
    location / {
       fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        include fastcgi_params;
    }
}

We’ve now completed covering running NGINX with Python via FastCGI.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon