2

I am a IIS - Internet Information Services (Microsoft Windows) user and I would like to migrate to Nginx.

How can I publish multiple web applications in Python using virtual directories (IIS style) and access them via IP and directory name (Ex: http://HOST_IP/APP_DIRECTORY) in Nginx?

The server operating system I use is Ubuntu Server 12.04.1 LTS AMD64.

[]'s

1 Answer 1

2

The purpose of this topic asked and answered by myself is to establish a short, easy and accurate path for users coming from IIS - Internet Information Services (Microsoft Windows) that want to migrate and/or use Nginx with Python. So I ask everyone to help to reach this goal!

Aspects considered:

  1. Use the Ubuntu Server 12.04.1 LTS AMD64;
  2. Use Python Web applications;
  3. Access multiple applications using the model http://HOST_IP/APP_DIRECTORY;

I know little about Nginx. I emphasize that the goal here is to create web Python applications for testing and development in a simple and easy way, without the presence of a domain (eg www.domain.com) and accessing them via IP and virtual directory.

I do not know if this is the best way to publish applications using virtual directories. Comments on this aspect are very welcome.

HERE IS MY ANSWER!

  • Install Nginx and uWSGI that will run Python applications;

    apt-get install nginx-full uwsgi uwsgi-plugin-python

  • Create the configuration file for Nginx. This will contain the list of virtual directories   that will be available in Nginx (folder "sites-available"):

    nano /etc/nginx/sites-available/applications

Content of archive:

server
{
        listen          80;
        server_name     $hostname;
        access_log /srv/www/applications/logs/access.log;
        error_log /srv/www/applications/logs/error.log;

        location /pytest0
        {
            #uwsgi_pass      127.0.0.1:9001;
            uwsgi_pass      unix:///run/uwsgi/app/pytest0/pytest0.socket;
            include         uwsgi_params;
            uwsgi_param     UWSGI_SCHEME $scheme;
            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
        }

        location /pytest1
        {
            #uwsgi_pass      127.0.0.1:9001;
            uwsgi_pass      unix:///run/uwsgi/app/pytest1/pytest1.socket;
            include         uwsgi_params;
            uwsgi_param     UWSGI_SCHEME $scheme;
            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
        }
}
  • Create directory where Nginx will save the logs:

    mkdir -p /srv/www/applications/logs

  • Place the list of applications that you created in the set of enabled applications (folder "applications-enabled"):

    ln -s /etc/nginx/sites-available/applications /etc/nginx/sites-enabled/applications

  • Remove the link to the default virtual host by entering the following command:

    rm /etc/nginx/sites-enabled/default

    Note: With this action is easier to diagnose possible errors, because in case of failure the NGINX returns the error instead of a standard page.

  • Create the folders for the 1st Python test application:

    mkdir -p /srv/www/pytest0/public_html/static

    mkdir /srv/www/pytest0/application

  • Set the 1st test application and makes it available in uWSGI (folder "apps-available"):

    mkdir -p /etc/uwsgi/apps-available/

    nano /etc/uwsgi/apps-available/pytest0.xml

Content of archive:

<uwsgi>
    <plugin>python</plugin>
    <socket>/run/uwsgi/app/pytest0/pytest0.socket</socket>
    <pythonpath>/srv/www/pytest0/application/</pythonpath>
    <app mountpoint="/">

        <script>wsgi_configuration_module</script>

    </app>
    <master/>
    <processes>4</processes>
    <harakiri>60</harakiri>
    <reload-mercy>8</reload-mercy>
    <cpu-affinity>1</cpu-affinity>
    <stats>/tmp/stats.socket</stats>
    <max-requests>2000</max-requests>
    <limit-as>512</limit-as>
    <reload-on-as>256</reload-on-as>
    <reload-on-rss>192</reload-on-rss>
    <no-orphans/>
    <vacuum/>
</uwsgi>
  • Place the 1st test application as enabled in uWSGI (folder "apps-enabled"):

    ln -s /etc/uwsgi/apps-available/pytest0.xml /etc/uwsgi/apps-enabled/pytest0.xml

  • "Create" the 1st test application in uWSGI:

    nano /srv/www/pytest0/application/wsgi_configuration_module.py

Content of archive:

import os
import sys

sys.path.append('/srv/www/pytest0/application')

os.environ['PYTHON_EGG_CACHE'] = '/srv/www/pytest0/.python-egg'

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello pytest0!'

    response_headers = [('Content-type', 'text/plain'),
                    ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]
  • Create the folders for the 2nd Python test application:

    mkdir -p /srv/www/pytest1/public_html/static

    mkdir /srv/www/pytest1/application

  • Set the 2nd test application and makes it available in uWSGI (folder "apps-available"):

    nano /etc/uwsgi/apps-available/pytest1.xml

Content of archive:

<uwsgi>
    <plugin>python</plugin>
    <socket>/run/uwsgi/app/pytest1/pytest1.socket</socket>
    <pythonpath>/srv/www/pytest1/application/</pythonpath>
    <app mountpoint="/">

        <script>wsgi_configuration_module</script>

    </app>
    <master/>
    <processes>4</processes>
    <harakiri>60</harakiri>
    <reload-mercy>8</reload-mercy>
    <cpu-affinity>1</cpu-affinity>
    <stats>/tmp/stats.socket</stats>
    <max-requests>2000</max-requests>
    <limit-as>512</limit-as>
    <reload-on-as>256</reload-on-as>
    <reload-on-rss>192</reload-on-rss>
    <no-orphans/>
    <vacuum/>
</uwsgi>
  • Place the 2nd test application as enabled in uWSGI (folder "apps-enabled"):

    ln -s /etc/uwsgi/apps-available/pytest1.xml /etc/uwsgi/apps-enabled/pytest1.xml

  • "Create" the 2nd test application in uWSGI:

    nano /srv/www/pytest1/application/wsgi_configuration_module.py

Content of archive:

import os
import sys

sys.path.append('/srv/www/pytest1/application')

os.environ['PYTHON_EGG_CACHE'] = '/srv/www/pytest1/.python-egg'

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello pytest1!'

    response_headers = [('Content-type', 'text/plain'),
                    ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]
  • Restart the services:

    service uwsgi restart

    service nginx restart

PLEASE COMMENT!

  • References:

https://library.linode.com/web-servers/nginx/python-uwsgi/ubuntu-12.04-precise-pangolin

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .