Tuesday, 3 October 2017

How To Configure Nginx as a Web Server and Reverse Proxy for Apache on Ubuntu For Django


Introduction:


Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It's free and open source.

Apache and Nginx are two popular open source web servers often used with Django. It can be useful to run both of them on the same virtual machine when hosting multiple websites which have varied requirements. The general solution for running two web servers on a single system is to either use multiple IP addresses or different port numbers.

Prerequisites

 To follow along with this tutorial you will need:
    • An Ubuntu 16.04 server with a non-root user with sudo privileges:
    • Nginx and Apache2 installed.

    Step 1 — Installing Apache and Enabling Mod_wsgi



    sudo apt-get install apache2 libapache2-mod-wsgi 

    Step 2 — Configuring Apache  

    In this step we will change Apache's port number to 8080 and configure it to work with Django using the mod_wsgi module. Edit the Apache configuration file and change the port number of Apache.
    sudo nano /etc/apache2/ports.conf
    
    Find the following line:

    Listen 80
    Change it to:
    Listen 8080
    
    Save and exit ports.conf

    Next we'll edit the default virtual host file of Apache. The <VirtualHost> directive in this file is set to serve sites only on port 80.


    sudo nano /etc/apache2/sites-available/000-default.conf
    
    The first line should be:
    <VirtualHost *:80>
    
    Change it to:
    <VirtualHost *:8080>
    
    Save the file and reload Apache.
    sudo service apache2 reload
    
    Verify that Apache is now listening on 8080.
    sudo netstat -tlpn
    
    The output should look like below, with apache2 listening on :::8080.

    Step 3 — Configuring Apache to Use mod_wsgi


    sudo a2enmod wsgi
    <VirtualHost *:8080>
     
     ServerName www.example.com
            WSGIPassAuthorization On
     ServerAdmin webmaster@localhost
     Alias /static /path/to/static/folder
    
    <Directory /path/to/static/folder>
            Require all granted
        </Directory>
    <Directory /path/to/wsgi.py>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    WSGIDaemonProcess project_name python-path=/path/to/main/folder/  python-home=/path/to/your/virtual/enviornment's/folder/
        WSGIProcessGroup project_name
        WSGIScriptAlias / /path/to/wsgi.py
    
    
     
     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
    
     
    </VirtualHost>
    

    Note: Python-path should be the path to the folder where your manage.py is situated.

    Check Apache for configuration errors again.

    sudo apachectl -t
    

    Reload it if Syntax OK is displayed.

    sudo service apache2 reload
    
    To confirm the sites are working, open http://www.example.com:8080 in your browser and verify they're displaying your project's Homepage.

    Step 4 — Installing and Configuring Nginx


    Install Nginx.
    sudo apt-get install nginx
    Now let's edit the default config of nginx .

    sudo vim /etc/nginx/sites-enabled/default

    Paste the following into the file:


    server {
    
    listen 80;
    server_name www.example.com ;
    
    
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    
    
    location /static/ {
    # absolute path to your STATIC root folder
    root /path/to/static/folder;
    autoindex on;
    }
    # this will check if request is for media content
    location media/ {
    # absolute path to your MEDIA root folder
    root /path/to/media/folder;
    autoindex on;
    }
     
    
    root /path/to/your/home/directory;
    
    
    # this will execute if request is neither for static nor media. Here we are passing request to Apache to handle dynamic content
    location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Protocol $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Scheme $scheme;
    proxy_pass http://127.0.0.1:8080;
    # this is your URL for Apache site to which Nginx will pass requests for dynamic content to handle. We are configuring this URL in Apache configurations file in next step
    }
    }
     Save and close the file. 


    Do an Nginx configuration test:
    sudo nginx -t
    

    Then reload Nginx if OK is displayed.

    sudo nginx -s reload
    Open the browser and access the http://www.example.com/


    It will be displaying the homepage of your project.

    1 comment:




    1. Your complete Facial Attendance & access Control Solution at one place
      Scalable solution for your projects with reliability
      access control system

      ReplyDelete